本文整理匯總了TypeScript中@angular-devkit/schematics.Tree類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Tree類的具體用法?TypeScript Tree怎麽用?TypeScript Tree使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Tree類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: createFile
function createFile(remoteFiles: Tree, { path, file }: { path: string, file: FileEntry }) {
if (!file) {
console.log(`missing: ${path}`)
}
remoteFiles.create(path, file.content);
}
示例2: hasNgModuleImport
export function hasNgModuleImport(tree: Tree, modulePath: string, className: string): boolean {
const moduleFileContent = tree.read(modulePath);
if (!moduleFileContent) {
throw new SchematicsException(`Could not read Angular module file: ${modulePath}`);
}
const parsedFile = ts.createSourceFile(modulePath, moduleFileContent.toString(),
ts.ScriptTarget.Latest, true);
const ngModuleMetadata = findNgModuleMetadata(parsedFile);
if (!ngModuleMetadata) {
throw new SchematicsException(`Could not find NgModule declaration inside: "${modulePath}"`);
}
for (let property of ngModuleMetadata!.properties) {
if (!ts.isPropertyAssignment(property) || property.name.getText() !== 'imports' ||
!ts.isArrayLiteralExpression(property.initializer)) {
continue;
}
if (property.initializer.elements.some(element => element.getText() === className)) {
return true;
}
}
return false;
}
示例3: createAppModuleWithEffects
export function createAppModuleWithEffects(
tree: Tree,
path: string,
effects?: string
): Tree {
tree.create(
path || '/src/app/app.module.ts',
`
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { EffectsModule } from '@ngrx/effects';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
${effects}
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
`
);
return tree;
}
示例4: createReducers
export function createReducers(tree: Tree, path?: string) {
tree.create(
path || '/src/app/reducers/index.ts',
`
import {
ActionReducer,
ActionReducerMap,
createFeatureSelector,
createSelector,
MetaReducer
} from '@ngrx/store';
import { environment } from '../../environments/environment';
export interface State {
}
export const reducers: ActionReducerMap<State> = {
};
export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];
`
);
return tree;
}
示例5: it
it('works', () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = runner.runSchematic('my-full-schematic', { name: 'str' }, Tree.empty());
// Listing files
expect(tree.files.sort()).toEqual(['/allo', '/hola', '/test1', '/test2']);
});
示例6: Readable
return new Observable<Tree>(obs => {
const input = new Readable({
encoding: 'utf8',
read(): void {
this.push(buffer);
this.push(null);
},
});
const chunks: Array<Buffer> = [];
const output = new Writable({
write(chunk: string | Buffer, encoding: string, callback: Function): void {
chunks.push(typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk);
callback();
},
final(callback: (error?: Error) => void): void {
const full = Buffer.concat(chunks);
host.overwrite(path, full.toString());
callback();
obs.next(host);
obs.complete();
},
});
input.pipe(rewriter).pipe(output);
});
示例7: it
it('should import the state path if provided', () => {
const options = { ...defaultOptions, state: 'reducers' };
appTree.create('/src/app/reducers', '');
const tree = schematicRunner.runSchematic('container', options, appTree);
const content = getFileContent(tree, '/src/app/foo/foo.component.ts');
expect(content).toMatch(/import \* as fromStore from '..\/reducers';/);
});
示例8: it
it('should not overwrite existing custom theme files', () => {
appTree.create('/projects/material/custom-theme.scss', 'custom-theme');
const tree = runner.runSchematic('ng-add-setup-project', {theme: 'custom'}, appTree);
expect(tree.readContent('/projects/material/custom-theme.scss')).toBe('custom-theme',
'Expected the old custom theme content to be unchanged.');
});
示例9: return
return (tree: Tree, _context: SchematicContext) => {
const localFiles = url("./files")(_context) as Tree;
const updateFiles = getUpdateFiles(localFiles);
const createFiles = getCreateFiles(localFiles);
if (updateFiles.length > 0) {
updateFiles.forEach(f => updateFile(tree, f));
}
if (createFiles.length > 0) {
createFiles.forEach(f => createFile(tree, f));
}
const config = tree.read(".angular-cli.json");
if (config) {
const asJson = JSON.parse(config.toString());
asJson.apps[0].assets.push("config.json");
tree.overwrite(".angular-cli.json", JSON.stringify(asJson, null, 2));
}
// angular 6
const configA6 = tree.read("angular.json");
if (configA6) {
const asJson = JSON.parse(configA6.toString());
Object.entries(asJson.projects).forEach(([,value] : any) => {
const options = value.architect &&
value.architect.build &&
value.architect.build.options;
const assets = options && options.assets;
if (assets) {
assets.push("src/config.json");
}
const styles = options && options.styles;
if (styles) {
styles.push("src/theme.css");
}
});
tree.overwrite("angular.json", JSON.stringify(asJson, null, 2));
}
return tree;
};
示例10: 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;
}
示例11: findModuleFromOptions
// Looks up and finds the path to the app module (or other module if specified)
function findModuleFromOptions(host: Tree, options: ComponentOptions): Path | undefined {
const modulePath = normalize(
'/' + options.sourceDir + '/' + (options.appRoot) + '/' + options.module);
const moduleBaseName = normalize(modulePath).split('/').pop();
if (host.exists(modulePath)) {
return normalize(modulePath);
} else if (host.exists(modulePath + '.ts')) {
return normalize(modulePath + '.ts');
} else if (host.exists(modulePath + '.module.ts')) {
return normalize(modulePath + '.module.ts');
} else if (host.exists(modulePath + '/' + moduleBaseName + '.module.ts')) {
return normalize(modulePath + '/' + moduleBaseName + '.module.ts');
} else {
throw new Error('Specified module does not exist');
}
}
示例12: it
it('works', () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = runner.runSchematic('add', {
module: 'app'
}, Tree.empty());
expect(tree.files).toEqual(['/hello']);
});
示例13: 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();
return ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
}
示例14: addImportToModule
function addImportToModule(
host: Tree,
path: string,
symbolName: string,
fileName: string,
) {
const source = getTsSource(host, path);
const change = insertImport(
source,
path,
symbolName,
fileName,
) as InsertChange;
const declarationRecorder = host.beginUpdate(path);
declarationRecorder.insertLeft(change.pos, change.toAdd);
host.commitUpdate(declarationRecorder);
}
示例15: return
return (tree: Tree, context: SchematicContext) => {
debugger;
// We pass information back to the test.
tree.create(
(context.schematic.description as any).extra, // tslint:disable-line:no-any
(context.schematic.collection.description as any).extra, // tslint:disable-line:no-any
);
};