How to use the @angular-devkit/core.parseJsonAst function in @angular-devkit/core

To help you get started, we’ve selected a few @angular-devkit/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github angular / angular-cli / packages / schematics / angular / utility / json-utils_spec.ts View on Github external
function runTest(testFn: Function, obj: JsonValue, indent: number): string {
    const content = JSON.stringify(obj, null, indent);
    tree.create(filePath, content);
    const ast = parseJsonAst(content);
    const rec = tree.beginUpdate(filePath);
    testFn(rec, ast);
    tree.commitUpdate(rec);

    const result = tree.readContent(filePath);
    // Clean up the tree by deleting the file.
    tree.delete(filePath);

    return result;
  }
github angular / angular-cli / packages / schematics / angular / migrations / update-6 / index.ts View on Github external
apps.forEach((app: AppConfig, idx: number) => {
      const testTsConfig = app.testTsconfig || defaults.testTsConfig;
      const tsSpecConfigPath = join(normalize(app.root || ''), testTsConfig);
      const buffer = host.read(tsSpecConfigPath);

      if (!buffer) {
        return;
      }


      const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
      if (tsCfgAst.kind != 'object') {
        throw new SchematicsException('Invalid tsconfig. Was expecting an object');
      }

      const filesAstNode = findPropertyInAstObject(tsCfgAst, 'files');
      if (filesAstNode && filesAstNode.kind != 'array') {
        throw new SchematicsException('Invalid tsconfig "files" property; expected an array.');
      }

      const recorder = host.beginUpdate(tsSpecConfigPath);

      const polyfills = app.polyfills || defaults.polyfills;
      if (!filesAstNode) {
        // Do nothing if the files array does not exist. This means exclude or include are
        // set and we shouldn't mess with that.
      } else {
github caroso1222 / ng-lib-schematics / src / lib-standalone / index.ts View on Github external
return (tree: Tree, context: SchematicContext) => {
    const packageJsonPath = './package.json';

    let packageJsonContent = tree.read(packageJsonPath);
    if (!verifyPackageJson(tree, context, packageJsonContent)) {
      return;
    }

    const parsedDevDependencies = JSON.parse(packageJsonContent.toString('utf-8')).devDependencies;
    if (!parsedDevDependencies) {
      return;
    }

    const packageJsonAst = parseJsonAst(packageJsonContent.toString('utf-8'));

    if (packageJsonAst.kind !== 'object') {
      throw new Error('Invalid "package.json" file.');
    }

    const dependencyToken = getJsonToken(packageJsonAst, 'devDependencies');
    if (dependencyToken) {
      const currentDevDependencies = Object.keys(parsedDevDependencies);
      const recorder = tree.beginUpdate(packageJsonPath);
      const dependenciesToInsert = [];

      // analyze the devDependencies and add only the missing ones
      for (const dep of DEV_DEPENDENCIES) {
        if (currentDevDependencies.indexOf(dep.name) === -1) {
          dependenciesToInsert.push(dep);
        }
github angular / universal / modules / common / schematics / add / index.ts View on Github external
return;
    }

    const tsConfigPath = serverTarget.options.tsConfig;
    if (!tsConfigPath || typeof tsConfigPath !== 'string') {
      // No tsconfig path
      return;
    }

    const configBuffer = host.read(tsConfigPath);
    if (!configBuffer) {
      throw new SchematicsException(`Could not find (${tsConfigPath})`);
    }

    const content = configBuffer.toString();
    const tsConfigAst = parseJsonAst(content, JsonParseMode.Loose);
    if (!tsConfigAst || tsConfigAst.kind !== 'object') {
      throw new SchematicsException(`Invalid JSON AST Object (${tsConfigPath})`);
    }

    const filesAstNode = findPropertyInAstObject(tsConfigAst, 'files');

    const serverFilePath = stripTsExtension(options.serverFileName) + '.ts';
    if (
      filesAstNode &&
      filesAstNode.kind === 'array' &&
      !filesAstNode.elements.some(({ text }) => text === serverFilePath)) {
      const recorder = host.beginUpdate(tsConfigPath);

      appendValueInAstArray(
        recorder,
        filesAstNode,
github angular / angular-cli / packages / schematics / angular / application / index.ts View on Github external
function readTsLintConfig(host: Tree, path: string): JsonAstObject {
  const buffer = host.read(path);
  if (!buffer) {
    throw new SchematicsException(`Could not read ${path}.`);
  }

  const config = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
  if (config.kind !== 'object') {
    throw new SchematicsException(`Invalid ${path}. Was expecting an object.`);
  }

  return config;
}
github angular / angular-cli / packages / schematics / angular / migrations / update-6 / index.ts View on Github external
return (host: Tree, context: SchematicContext) => {
    const tsConfigPath = '/tsconfig.json';
    const buffer = host.read(tsConfigPath);
    if (!buffer) {
      return;
    }

    const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);
    if (tsCfgAst.kind !== 'object') {
      throw new SchematicsException('Invalid root tsconfig. Was expecting an object');
    }

    const compilerOptionsAstNode = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
    if (!compilerOptionsAstNode || compilerOptionsAstNode.kind != 'object') {
      throw new SchematicsException(
        'Invalid root tsconfig "compilerOptions" property; expected an object.',
      );
    }

    if (
      findPropertyInAstObject(compilerOptionsAstNode, 'baseUrl') &&
      findPropertyInAstObject(compilerOptionsAstNode, 'module')
    ) {
      return host;
github angular / angular / packages / bazel / src / schematics / ng-add / index.ts View on Github external
return (host: Tree, context: SchematicContext) => {
    const name = options.name !;
    const workspacePath = getWorkspacePath(host);
    if (!workspacePath) {
      throw new Error('Could not find angular.json');
    }
    const workspaceContent = host.read(workspacePath);
    if (!workspaceContent) {
      throw new Error('Failed to read angular.json content');
    }
    const workspaceJsonAst = parseJsonAst(workspaceContent.toString()) as JsonAstObject;
    const projects = findPropertyInAstObject(workspaceJsonAst, 'projects');
    if (!projects) {
      throw new SchematicsException('Expect projects in angular.json to be an Object');
    }
    const project = findPropertyInAstObject(projects as JsonAstObject, name);
    if (!project) {
      throw new SchematicsException(`Expected projects to contain ${name}`);
    }
    const recorder = host.beginUpdate(workspacePath);
    const indent = 8;
    const architect =
        findPropertyInAstObject(project as JsonAstObject, 'architect') as JsonAstObject;
    replacePropertyInAstObject(
        recorder, architect, 'build', {
          builder: '@angular/bazel:build',
          options: {