本文整理汇总了TypeScript中@angular-devkit/schematics.Tree.read方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Tree.read方法的具体用法?TypeScript Tree.read怎么用?TypeScript Tree.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@angular-devkit/schematics.Tree
的用法示例。
在下文中一共展示了Tree.read方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getSourceFile
export function getSourceFile(host: Tree, path: string): ts.SourceFile {
const buffer = host.read(path);
if (!buffer) {
throw new SchematicsException(`Could not find file for path: ${path}`);
}
const content = buffer.toString();
const source = ts.createSourceFile(
path,
content,
ts.ScriptTarget.Latest,
true
);
return source;
}
示例2: _readPackageJson
function _readPackageJson(tree: Tree): JsonAstObject {
const buffer = tree.read(pkgJsonPath);
if (buffer === null) {
throw new SchematicsException('Could not read package.json.');
}
const content = buffer.toString();
const packageJson = parseJsonAst(content, JsonParseMode.Strict);
if (packageJson.kind != 'object') {
throw new SchematicsException('Invalid package.json. Was expecting an object');
}
return packageJson;
}
示例3: getWorkspaceConfigGracefully
/**
* Resolve the workspace configuration of the specified tree gracefully. We cannot use the utility
* functions from the default Angular schematics because those might not be present in older
* versions of the CLI. Also it's important to resolve the workspace gracefully because
* the CLI project could be still using `.angular-cli.json` instead of thew new config.
*/
function getWorkspaceConfigGracefully(tree: Tree): any {
const path = defaultWorkspaceConfigPaths.find(filePath => tree.exists(filePath));
const configBuffer = tree.read(path !);
if (!path || !configBuffer) {
return null;
}
try {
return JSON.parse(configBuffer.toString());
} catch {
return null;
}
}
示例4: return
return (tree: Tree) => {
const workspaceConfig = tree.read('/angular.json');
if (!workspaceConfig) {
throw new SchematicsException('Could not find Angular workspace configuration');
}
// convert workspace to string
const workspaceContent = workspaceConfig.toString();
// parse workspace string into JSON object
const workspace: experimental.workspace.WorkspaceSchema = JSON.parse(workspaceContent);
// #enddocregion workspace
// #docregion project-fallback
if (!options.project) {
options.project = workspace.defaultProject;
}
// #enddocregion project-fallback
// #docregion project-info
const projectName = options.project as string;
const project = workspace.projects[projectName];
const projectType = project.projectType === 'application' ? 'app' : 'lib';
// #enddocregion project-info
// #docregion path
if (options.path === undefined) {
options.path = `${project.sourceRoot}/${projectType}`;
}
// #enddocregion path
// #docregion template
const templateSource = apply(url('./files'), [
applyTemplates({
classify: strings.classify,
dasherize: strings.dasherize,
name: options.name
}),
move(normalize(options.path as string))
]);
// #enddocregion template
// #docregion chain
return chain([
mergeWith(templateSource)
]);
// #enddocregion chain
// #docregion workspace
};
示例5: return
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
const project = workspace.projects[options.project as string];
let path: string;
const projectTargets = project.targets || project.architect;
if (project && projectTargets && projectTargets.build && projectTargets.build.options.index) {
path = projectTargets.build.options.index;
} else {
throw new SchematicsException('Could not find index file for the project');
}
const buffer = host.read(path);
if (buffer === null) {
throw new SchematicsException(`Could not read index file: ${path}`);
}
const content = buffer.toString();
const lines = content.split('\n');
let closingHeadTagLineIndex = -1;
let closingBodyTagLineIndex = -1;
lines.forEach((line, index) => {
if (closingHeadTagLineIndex === -1 && /<\/head>/.test(line)) {
closingHeadTagLineIndex = index;
} else if (closingBodyTagLineIndex === -1 && /<\/body>/.test(line)) {
closingBodyTagLineIndex = index;
}
});
const headIndent = getIndent(lines[closingHeadTagLineIndex]) + ' ';
const itemsToAddToHead = [
'<link rel="manifest" href="manifest.json">',
'<meta name="theme-color" content="#1976d2">',
];
const bodyIndent = getIndent(lines[closingBodyTagLineIndex]) + ' ';
const itemsToAddToBody = [
'<noscript>Please enable JavaScript to continue using this application.</noscript>',
];
const updatedIndex = [
...lines.slice(0, closingHeadTagLineIndex),
...itemsToAddToHead.map(line => headIndent + line),
...lines.slice(closingHeadTagLineIndex, closingBodyTagLineIndex),
...itemsToAddToBody.map(line => bodyIndent + line),
...lines.slice(closingHeadTagLineIndex),
].join('\n');
host.overwrite(path, updatedIndex);
return host;
};
示例6: return
return (host: Tree) => {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const appHTMLFile = `${project.sourceRoot}/app/app.component.html`;
const buffer = host.read(appHTMLFile);
if (!buffer) {
console.log();
console.error(chalk.red(`Could not find the project ${chalk.blue(appHTMLFile)} file inside of the ` +
`workspace config`));
return;
}
host.overwrite(appHTMLFile, bootstrapHTML);
return host;
};
示例7: 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;
}
示例8: return
return (host: Tree) => {
const pkgPath = '/package.json';
const buffer = host.read(pkgPath);
if (buffer === null) {
throw new SchematicsException('Could not find package.json');
}
const pkg = JSON.parse(buffer.toString());
const ngCoreVersion = pkg.dependencies['@angular/core'];
pkg.dependencies['@angular/platform-server'] = ngCoreVersion;
host.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
return host;
};
示例9: addPackageToPackageJson
export function addPackageToPackageJson(host: Tree, type: string, pkg: string, version: string) {
if (host.exists('package.json')) {
const sourceText = host.read('package.json')!.toString('utf-8');
const json = JSON.parse(sourceText);
if (!json[type]) {
json[type] = {};
}
if (!json[type][pkg]) {
json[type][pkg] = version;
}
host.overwrite('package.json', JSON.stringify(json, null, 2));
}
return host;
}
示例10: return
return (host: Tree) => {
const gitignore = '/.gitignore';
if (!host.exists(gitignore)) {
return host;
}
const gitIgnoreContent = host.read(gitignore).toString();
if (gitIgnoreContent.includes('\n/bazel-out\n')) {
return host;
}
const compiledOutput = '# compiled output\n';
const index = gitIgnoreContent.indexOf(compiledOutput);
const insertionIndex = index >= 0 ? index + compiledOutput.length : gitIgnoreContent.length;
const recorder = host.beginUpdate(gitignore);
recorder.insertRight(insertionIndex, '/bazel-out\n');
host.commitUpdate(recorder);
return host;
};