typescript#createSourceFile TypeScript Examples

The following examples show how to use typescript#createSourceFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: sourceFilesHelper.ts    From elephize with MIT License 6 votes vote down vote up
// Try find d.ts source in typescript folder
function getDtsSourceFile(name: string, target?: ScriptTarget) {
  if (sourceFiles[name] === undefined) {
    let path;
    try {
      path = name.startsWith('/') ? name : require.resolve('typescript/lib/' + name);
    } catch (e) {
      path = require.resolve(name.replace(/^node_modules\//, ''));
    }
    if (existsSync(path)) {
      const input = readFileSync(path, { encoding: 'utf-8' });
      sourceFiles[name] = createSourceFile(name, input, target!);
    } else {
      sourceFiles[name] = null;
    }
  }

  return sourceFiles[name];
}
Example #2
Source File: sourceFilesHelper.ts    From elephize with MIT License 6 votes vote down vote up
function getSourceFile(path: string, target?: ScriptTarget) {
  if (sourceFiles[path] === undefined) {
    if (existsSync(path)) {
      const input = readFileSync(path, { encoding: 'utf-8' });
      sourceFiles[path] = createSourceFile(path, input, target!);
    } else {
      sourceFiles[path] = null;
    }
  }

  return sourceFiles[path];
}
Example #3
Source File: ng-module.ts    From cli with Apache License 2.0 6 votes vote down vote up
export function updateNgModule(
  _options: CoveoSchema,
  _project: ProjectDefinition
): Rule {
  return (tree: Tree) => {
    const appModulePath = getAppModulePath(tree, getProjectMainFile(_project));

    const appModuleContent =
      tree.get(appModulePath)?.content.toString() ||
      getDefaultAppModuleContent();

    const source = createSourceFile(
      appModulePath,
      appModuleContent,
      ScriptTarget.Latest,
      true
    );
    const updateRecorder = tree.beginUpdate(appModulePath);

    const changes = getAdditionalImports(source, appModulePath);

    changes.map((change) => {
      if (change instanceof InsertChange) {
        updateRecorder.insertLeft(change.pos, change.toAdd);
      }
    });

    tree.commitUpdate(updateRecorder);

    return tree;
  };
}
Example #4
Source File: index.ts    From edit-in-place with MIT License 6 votes vote down vote up
export function getModuleFile(host: Tree, options: SchemaOptions): SourceFile {
  const modulePath = options.module;

  if (!host.exists(modulePath)) {
    throw new SchematicsException(`File ${modulePath} does not exist.`);
  }

  const text = host.read(modulePath);
  if (text === null) {
    throw new SchematicsException(`File ${modulePath} does not exist.`);
  }
  const sourceText = text.toString('utf-8');

  return createSourceFile(modulePath, sourceText, ScriptTarget.Latest, true);
}
Example #5
Source File: dangerfile.ts    From autocomplete with MIT License 4 votes vote down vote up
schedule(async () => {
  const { owner, repo, number } = danger.github.thisPR;

  const { data: comments } = await danger.github.api.issues.listComments({
    issue_number: number,
    repo,
    owner,
  });

  const reviewCommentRef = comments.find((comment) =>
    comment.body.includes("id: review-bot")
  );

  // Get all changed and added files
  const updatedFiles = danger.git.modified_files
    .concat(danger.git.created_files)
    .filter((file) => file.includes("src/"));

  // console.log(fs.readdirSync("src/", { encoding: "utf-8" }));
  let message = "<!-- id: review-bot --> \n";
  let comment = "";

  if (updatedFiles.length > 0) {
    const promises = updatedFiles.map(async (fileName) => {
      const res = await danger.github.api.repos.getContents({
        owner: danger.github.pr.user.login,
        repo: danger.github.pr.head.repo.name,
        path: fileName,
        ref: danger.github.pr.head.sha,
      });
      if (!res || !(res.data as any)?.content) return;
      const content = Buffer.from((res.data as any).content, "base64").toString(
        "utf-8"
      );

      const sourceFile = createSourceFile("temp", content, ScriptTarget.Latest);
      const fileContent = getFileContent(sourceFile);
      // START MESSAGE
      message += `## ${fileName}:
### Info:
${fileContent.pairs
  .map(
    ([scriptName, [key, value]]) => `**Script:**
\`${scriptName}\`
**${key}(function):**
\`\`\`typescript
${value}
\`\`\`
`
  )
  .join("\n")}
${
  fileContent.scripts.length > 0
    ? `### Single Scripts:
${fileContent.scripts.map((s) => `- \`${s}\``).join("\n")}`
    : ""
}
${
  fileContent.functions.length > 0
    ? `### Single Functions:
${fileContent.functions
  .map(
    ([key, value]) => `**${key}:**
\`\`\`typescript
${value}
\`\`\`
`
  )
  .join("\n")}`
    : ""
}
${
  fileContent.urls.length > 0
    ? `### URLs:
${fileContent.urls.map((s) => `- \`${s}\``).join("\n")}`
    : ""
}
`;
      // END MESSAGE
      // END LOOP
    });
    await Promise.all(promises);
    comment = `# Overview
${message}`;
  } else {
    comment = `# No files changed ☑️ ${message}`;
  }

  if (reviewCommentRef != null) {
    await danger.github.api.issues.updateComment({
      body: comment,
      comment_id: reviewCommentRef.id,
      owner,
      repo,
    });
  } else {
    await danger.github.api.issues.createComment({
      body: comment,
      issue_number: number,
      owner,
      repo,
    });
  }
});