本文整理汇总了TypeScript中@schematics/angular/utility/json-utils.findPropertyInAstObject函数的典型用法代码示例。如果您正苦于以下问题:TypeScript findPropertyInAstObject函数的具体用法?TypeScript findPropertyInAstObject怎么用?TypeScript findPropertyInAstObject使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了findPropertyInAstObject函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: replacePropertyInAstObject
function replacePropertyInAstObject(
recorder: UpdateRecorder, node: JsonAstObject, propertyName: string, value: JsonValue,
indent: number) {
const property = findPropertyInAstObject(node, propertyName);
if (property === null) {
throw new Error(`Property ${propertyName} does not exist in JSON object`);
}
const {start, text} = property;
recorder.remove(start.offset, text.length);
const indentStr = '\n' +
' '.repeat(indent);
const content = JSON.stringify(value, null, ' ').replace(/\n/g, indentStr);
recorder.insertLeft(start.offset, content);
}
示例2: return
return (host: Tree, context: SchematicContext) => {
const packageJson = 'package.json';
if (!host.exists(packageJson)) {
throw new Error(`Could not find ${packageJson}`);
}
const content = host.read(packageJson);
if (!content) {
throw new Error('Failed to read package.json content');
}
const jsonAst = parseJsonAst(content.toString());
if (!isJsonAstObject(jsonAst)) {
throw new Error(`Failed to parse JSON for ${packageJson}`);
}
const deps = findPropertyInAstObject(jsonAst, 'dependencies');
if (!isJsonAstObject(deps)) {
throw new Error(`Failed to find dependencies in ${packageJson}`);
}
const rxjs = findPropertyInAstObject(deps, 'rxjs');
if (!rxjs) {
throw new Error(`Failed to find rxjs in dependencies of ${packageJson}`);
}
const value = rxjs.value as string; // value can be version or range
const match = value.match(/(\d)+\.(\d)+.(\d)+$/);
if (match) {
const [_, major, minor] = match;
if (major < '6' || (major === '6' && minor < '4')) {
const recorder = host.beginUpdate(packageJson);
replacePropertyInAstObject(recorder, deps, 'rxjs', '~6.4.0');
host.commitUpdate(recorder);
}
} else {
context.logger.info(
'Could not determine version of rxjs. \n' +
'Please make sure that version is at least 6.4.0.');
}
return host;
};
示例3: return
return (host: Tree) => {
const packageJson = 'package.json';
if (!host.exists(packageJson)) {
throw new Error(`Could not find ${packageJson}`);
}
const packageJsonContent = host.read(packageJson);
if (!packageJsonContent) {
throw new Error('Failed to read package.json content');
}
const jsonAst = parseJsonAst(packageJsonContent.toString()) as JsonAstObject;
const deps = findPropertyInAstObject(jsonAst, 'dependencies') as JsonAstObject;
const devDeps = findPropertyInAstObject(jsonAst, 'devDependencies') as JsonAstObject;
const angularCoreNode = findPropertyInAstObject(deps, '@angular/core');
if (!angularCoreNode) {
throw new Error('@angular/core dependency not found in package.json');
}
const angularCoreVersion = angularCoreNode.value as string;
const devDependencies: {[k: string]: string} = {
'@angular/bazel': angularCoreVersion,
// TODO(kyliau): Consider moving this to latest-versions.ts
'@bazel/bazel': '^0.22.1',
'@bazel/ibazel': '^0.9.0',
'@bazel/karma': '^0.23.2',
'@bazel/typescript': '^0.23.2',
};
const recorder = host.beginUpdate(packageJson);
for (const packageName of Object.keys(devDependencies)) {
const version = devDependencies[packageName];
const indent = 4;
insertPropertyInAstObjectInOrder(recorder, devDeps, packageName, version, indent);
}
host.commitUpdate(recorder);
return host;
};
示例4: return
return (host: Tree, context: SchematicContext) => {
const {name} = options;
const workspacePath = `${name}/angular.json`;
if (!host.exists(workspacePath)) {
throw new SchematicsException(`Workspace file ${workspacePath} not found.`);
}
const workspaceBuffer = host.read(workspacePath) !;
const workspaceJsonAst = parseJsonAst(workspaceBuffer.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 = 6;
replacePropertyInAstObject(
recorder, project as JsonAstObject, 'architect', {
'build': {
'builder': '@angular/bazel:build',
'options': {'targetLabel': '//src:bundle.js', 'bazelCommand': 'build'},
'configurations': {'production': {'targetLabel': '//src:bundle'}}
},
'serve': {
'builder': '@angular/bazel:build',
'options': {'targetLabel': '//src:devserver', 'bazelCommand': 'run'},
'configurations': {'production': {'targetLabel': '//src:prodserver'}}
},
'extract-i18n': {
'builder': '@angular-devkit/build-angular:extract-i18n',
'options': {'browserTarget': `${name}:build`}
},
'test': {
'builder': '@angular/bazel:build',
'options': {'bazelCommand': 'test', 'targetLabel': '//src/...'}
},
'lint': {
'builder': '@angular-devkit/build-angular:tslint',
'options': {
'tsConfig': ['src/tsconfig.app.json', 'src/tsconfig.spec.json'],
'exclude': ['**/node_modules/**']
}
}
},
indent);
const e2e = `${options.name}-e2e`;
const e2eNode = findPropertyInAstObject(projects as JsonAstObject, e2e);
if (e2eNode) {
replacePropertyInAstObject(
recorder, e2eNode as JsonAstObject, 'architect', {
'e2e': {
'builder': '@angular/bazel:build',
'options': {'bazelCommand': 'test', 'targetLabel': '//e2e:devserver_test'},
'configurations': {'production': {'targetLabel': '//e2e:prodserver_test'}}
},
'lint': {
'builder': '@angular-devkit/build-angular:tslint',
'options': {'tsConfig': 'e2e/tsconfig.e2e.json', 'exclude': ['**/node_modules/**']}
}
},
indent);
}
host.commitUpdate(recorder);
return host;
};
示例5:
const depsToInstall = Object.keys(devDependencies).filter((name) => {
return !findPropertyInAstObject(devDeps, name);
});