当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript core.parseJson函数代码示例

本文整理汇总了TypeScript中@angular-devkit/core.parseJson函数的典型用法代码示例。如果您正苦于以下问题:TypeScript parseJson函数的具体用法?TypeScript parseJson怎么用?TypeScript parseJson使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了parseJson函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: SchematicsException

    (tree: Tree) => {
      const packageJsonContent = tree.read('/package.json');
      if (!packageJsonContent) {
        throw new SchematicsException('Could not find package.json.');
      }
      const packageJson = parseJson(packageJsonContent.toString(), JsonParseMode.Strict);
      if (packageJson === null || typeof packageJson !== 'object' || Array.isArray(packageJson)) {
        throw new SchematicsException('Could not parse package.json.');
      }

      for (const field of kPackageJsonDependencyFields) {
        const deps = packageJson[field];
        if (!deps || typeof deps !== 'object' || Array.isArray(deps)) {
          continue;
        }

        for (const depName of Object.keys(deps)) {
          if (allVersions[depName]) {
            deps[depName] = allVersions[depName];
          }
        }
      }

      tree.overwrite('/package.json', JSON.stringify(packageJson, null, 2) + '\n');

      return tree;
    },
开发者ID:fmalcher,项目名称:angular-cli,代码行数:27,代码来源:npm.ts

示例2: return

  return (host: Tree, context: SchematicContext) => {
    if (host.exists('/.angular.json') || host.exists('/angular.json')) {
      context.logger.info('Found a modern configuration file. Nothing to be done.');

      return host;
    }

    const configPath = getConfigPath(host);
    const configBuffer = host.read(normalize(configPath));
    if (configBuffer == null) {
      throw new SchematicsException(`Could not find configuration file (${configPath})`);
    }
    const config = parseJson(configBuffer.toString(), JsonParseMode.Loose);

    if (typeof config != 'object' || Array.isArray(config) || config === null) {
      throw new SchematicsException('Invalid angular-cli.json configuration; expected an object.');
    }

    return chain([
      migrateKarmaConfiguration(config),
      migrateConfiguration(config, context.logger),
      updateSpecTsConfig(config),
      updatePackageJson(config),
      updateRootTsConfig(),
      updateTsLintConfig(),
      (host: Tree, context: SchematicContext) => {
        context.logger.warn(tags.oneLine`Some configuration options have been changed,
          please make sure to update any npm scripts which you may have modified.`);

        return host;
      },
    ]);
  };
开发者ID:baconwaffles,项目名称:angular-cli,代码行数:33,代码来源:index.ts

示例3: return

  return (tree) => {
    const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json');

    if (!angularConfigContent) {
      return;
    }

    const angularJson = parseJson(angularConfigContent.toString(), JsonParseMode.Loose);

    if (!isJsonObject(angularJson) || !isJsonObject(angularJson.projects)) {
      // If that field isn't there, no use...
      return;
    }

    // For all projects
    for (const [name, project] of Object.entries(angularJson.projects)) {
      if (!isJsonObject(project)) {
        continue;
      }
      if (typeof project.root != 'string' || project.projectType !== 'application') {
        continue;
      }
      if (name.endsWith('-e2e')) {
        // Skip existing separate E2E projects
        continue;
      }

      const browserslistPath = join(normalize(project.root), 'browserslist');
      if (typeof project.sourceRoot === 'string') {
        // Move the CLI 7 style browserlist to root if it's there.
        const srcBrowsersList = join(normalize(project.sourceRoot), 'browserslist');
        if (tree.exists(srcBrowsersList) && !tree.exists(browserslistPath)) {
          // TODO: use rename instead.
          // This is a hacky workaround. We should be able to just rename it.
          // On unit tests the rename works fine but on real projects it fails with
          //     ERROR! browserslist does not exist..
          // This seems to happen because we are both renaming and then commiting an update.
          // But it's fine if we read/create/delete. There's a bug somewhere.
          // tree.rename(srcBrowsersList, browserslistPath);
          const content = tree.read(srcBrowsersList);
          if (content) {
            tree.create(browserslistPath, content);
            tree.delete(srcBrowsersList);
          }
        }
      }

      const source = tree.read(browserslistPath);
      if (!source) {
        tree.create(browserslistPath, browserslistContent);
      } else if (!source.toString().toLowerCase().includes('chrome 41')) {
        const recorder = tree.beginUpdate(browserslistPath);
        recorder.insertRight(source.length, '\nChrome 41 # Googlebot');
        tree.commitUpdate(recorder);
      }
    }

    return tree;
  };
开发者ID:angular,项目名称:angular-cli,代码行数:59,代码来源:differential-loading.ts

示例4: parseJson

 response.on('end', () => {
   try {
     const json = parseJson(data, JsonParseMode.Strict);
     subject.next(json as JsonObject);
     subject.complete();
   } catch (err) {
     subject.error(err);
   }
 });
开发者ID:fmalcher,项目名称:angular-cli,代码行数:9,代码来源:npm.ts

示例5: getWorkspace

export function getWorkspace(host: Tree): WorkspaceSchema {
  const path = getWorkspacePath(host);
  const configBuffer = host.read(path);
  if (configBuffer === null) {
    throw new SchematicsException(`Could not find (${path})`);
  }
  const content = configBuffer.toString();

  return parseJson(content, JsonParseMode.Loose) as {} as WorkspaceSchema;
}
开发者ID:DevIntent,项目名称:angular-cli,代码行数:10,代码来源:config.ts

示例6: getConfig

export function getConfig(host: Tree): CliConfig {
  const configBuffer = host.read(configPath);
  if (configBuffer === null) {
    throw new SchematicsException('Could not find .angular-cli.json');
  }

  const config = parseJson(configBuffer.toString(), JsonParseMode.Loose) as {} as CliConfig;

  return config;
}
开发者ID:DevIntent,项目名称:angular-cli,代码行数:10,代码来源:config.ts

示例7: parseJson

function updateJsonFile<T>(host: Tree, path: string, callback: UpdateJsonFn<T>): Tree {
  const source = host.read(path);
  if (source) {
    const sourceText = source.toString('utf-8');
    const json = parseJson(sourceText);
    callback(json as {} as T);
    host.overwrite(path, JSON.stringify(json, null, 2));
  }

  return host;
}
开发者ID:cexbrayat,项目名称:angular-cli,代码行数:11,代码来源:index.ts

示例8: migrateLegacyGlobalConfig

export function migrateLegacyGlobalConfig(): boolean {
  const homeDir = os.homedir();
  if (homeDir) {
    const legacyGlobalConfigPath = path.join(homeDir, '.angular-cli.json');
    if (existsSync(legacyGlobalConfigPath)) {
      const content = readFileSync(legacyGlobalConfigPath, 'utf-8');
      const legacy = parseJson(content, JsonParseMode.Loose);
      if (!legacy || typeof legacy != 'object' || Array.isArray(legacy)) {
        return false;
      }

      const cli: JsonObject = {};

      if (legacy.packageManager && typeof legacy.packageManager == 'string'
          && legacy.packageManager !== 'default') {
        cli['packageManager'] = legacy.packageManager;
      }

      if (legacy.defaults && typeof legacy.defaults == 'object' && !Array.isArray(legacy.defaults)
          && legacy.defaults.schematics && typeof legacy.defaults.schematics == 'object'
          && !Array.isArray(legacy.defaults.schematics)
          && typeof legacy.defaults.schematics.collection == 'string') {
        cli['defaultCollection'] = legacy.defaults.schematics.collection;
      }

      if (legacy.warnings && typeof legacy.warnings == 'object'
          && !Array.isArray(legacy.warnings)) {

        const warnings: JsonObject = {};
        if (typeof legacy.warnings.versionMismatch == 'boolean') {
          warnings['versionMismatch'] = legacy.warnings.versionMismatch;
        }
        if (typeof legacy.warnings.typescriptMismatch == 'boolean') {
          warnings['typescriptMismatch'] = legacy.warnings.typescriptMismatch;
        }

        if (Object.getOwnPropertyNames(warnings).length > 0) {
          cli['warnings'] = warnings;
        }
      }

      if (Object.getOwnPropertyNames(cli).length > 0) {
        const globalPath = path.join(homeDir, globalFileName);
        writeFileSync(globalPath, JSON.stringify({ version: 1, cli }, null, 2));

        return true;
      }
    }
  }

  return false;
}
开发者ID:rexebin,项目名称:angular-cli,代码行数:52,代码来源:config.ts

示例9: getTsConfigOutDir

function getTsConfigOutDir(host: Tree, tsConfigPath: string): string {
  const tsConfigBuffer = host.read(tsConfigPath);
  if (!tsConfigBuffer) {
    throw new SchematicsException(`Could not read ${tsConfigPath}`);
  }
  const tsConfigContent = tsConfigBuffer.toString();
  const tsConfig = parseJson(tsConfigContent);
  if (tsConfig === null || typeof tsConfig !== 'object' || Array.isArray(tsConfig) ||
    tsConfig.compilerOptions === null || typeof tsConfig.compilerOptions !== 'object' ||
    Array.isArray(tsConfig.compilerOptions)) {
    throw new SchematicsException(`Invalid tsconfig - ${tsConfigPath}`);
  }
  const outDir = tsConfig.compilerOptions.outDir;

  return outDir as string;
}
开发者ID:angular,项目名称:angular-cli,代码行数:16,代码来源:index.ts

示例10: return

  return (tree) => {
    // Simple. Take the ast of polyfills (if it exists) and find the import metadata. Remove it.
    const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json');
    const rules: Rule[] = [];

    if (!angularConfigContent) {
      // Is this even an angular project?
      return;
    }

    const angularJson = parseJson(angularConfigContent.toString(), JsonParseMode.Loose);

    if (!isJsonObject(angularJson) || !isJsonObject(angularJson.projects)) {
      // If that field isn't there, no use...
      return;
    }

    // For all projects, for all targets, read the polyfill field, and read the environment.
    for (const projectName of Object.keys(angularJson.projects)) {
      const project = angularJson.projects[projectName];
      if (!isJsonObject(project)) {
        continue;
      }
      if (typeof project.root != 'string') {
        continue;
      }

      const targets = project.targets || project.architect;
      if (!isJsonObject(targets)) {
        continue;
      }

      for (const targetName of Object.keys(targets)) {
        const target = targets[targetName];
        if (isJsonObject(target)) {
          rules.push(_updateProjectTarget(target));
        }
      }
    }

    // Remove null or undefined rules.
    return chain(rules);
  };
开发者ID:angular,项目名称:angular-cli,代码行数:43,代码来源:polyfill-metadata.ts


注:本文中的@angular-devkit/core.parseJson函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。