Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const sourceFile = ts.createSourceFile(
modulePath,
host.read(modulePath).toString('utf-8'),
ts.ScriptTarget.Latest,
true
);
let code = fs
.readFileSync(join(__dirname, './files/code.bs'), 'utf-8')
.toString();
const componentName = classify(schema.name.split('/').pop()) + 'Component';
code = code.replace('EmptyComponent', componentName);
const moduleImportChanges = addImportToModule(
sourceFile,
modulePath,
'SlidesModule',
'@codelab/slides'
);
const routingImportChanges = addImportToModule(
sourceFile,
modulePath,
'routes',
null
);
const changes = [...moduleImportChanges, ...routingImportChanges];
const recorder = host.beginUpdate(modulePath);
export function addModuleImportToModule(host: Tree, modulePath: string, moduleName: string, src: string) {
const moduleSource = getSourceFile(host, modulePath);
if (!moduleSource) {
throw new SchematicsException(`Module not found: ${modulePath}`);
}
const changes = addImportToModule(moduleSource, modulePath, moduleName, src);
const recorder = host.beginUpdate(modulePath);
changes.forEach((change) => {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
});
host.commitUpdate(recorder);
}
return (host: Tree) => {
const source = getSourceFile(host, routingPath);
if (!source) {
return host;
}
let changes;
if (isView) {
changes = addDeclarationToModule(source, routingPath, options.componentName, options.relativePath);
} else {
changes = addImportToModule(source, routingPath, options.componentName, options.relativePath);
}
return applyChanges(host, changes, routingPath);
};
}
return (host: Tree, context: SchematicContext) => {
const animImport = ['BrowserAnimationsModule', '@angular/platform-browser/animations'];
try {
const modulePath = getAppModulePath(host, getMainProjectPath(host, options));
const srcPath = getSourceFile(host, modulePath);
if (!isImported(srcPath, 'MetaConfig, MetaUIRulesModule',
'@ngx-metaui/rules')) {
let changes = [
...addImportToModule(srcPath, modulePath, animImport[0], animImport[1]),
...addSymbolToNgModuleMetadata(srcPath, modulePath, 'imports',
'MetaUIRulesModule.forRoot({})')];
if (options.uiLib === 'prime-ng') {
changes = [...changes, ...addSymbolToNgModuleMetadata(srcPath, modulePath,
'imports',
'PrimeNgRulesModule.forRoot()')];
}
const recorder = host.beginUpdate(modulePath);
changes.forEach((change) => {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
});
host.commitUpdate(recorder);
export function addModuleImportToModule(host: Tree, modulePath: string, moduleName: string,
src: string) {
const moduleSource = getSourceFile(host, modulePath);
if (!moduleSource) {
throw new SchematicsException(`Module not found: ${modulePath}`);
}
const changes = addImportToModule(moduleSource, modulePath, moduleName, src);
const recorder = host.beginUpdate(modulePath);
changes.forEach((change) => {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
});
host.commitUpdate(recorder);
}
export function addModuleImportToModule(
host: Tree,
modulePath: string,
moduleName: string,
src: string,
isModuleImport: boolean = true
) {
const moduleSource = getSourceFile(host, modulePath);
if (!moduleSource) {
throw new SchematicsException(`Module not found: ${modulePath}`);
}
let changes: Change[] = [];
if (isModuleImport) {
changes = addImportToModule(moduleSource, modulePath, moduleName, src);
} else {
if (!isImported(moduleSource, moduleName, src)) {
const changeItem = insertImport(
moduleSource,
modulePath,
moduleName,
src
);
changes.push(changeItem);
}
}
const recorder = host.beginUpdate(modulePath);
changes.forEach((change: Change) => {
if (change instanceof InsertChange) {
return (host: Tree) => {
const modulePath = `${path}/app/app.module.ts`;
const moduleSource = host.read(modulePath)!.toString('utf-8');
const sourceFile = ts.createSourceFile(modulePath, moduleSource, ts.ScriptTarget.Latest, true);
const importChanges = addImportToModule(sourceFile, modulePath, 'BrowserModule', '@angular/platform-browser');
const bootstrapChanges = addBootstrapToModule(sourceFile, modulePath, 'AppComponent', './app.component');
insert(host, modulePath, [...importChanges, ...bootstrapChanges]);
return host;
};
}
function processRoutingModule(tree: Tree, modulePath: Path) {
const moduleDeclarations = getClassWithDecorator(tree, modulePath, 'NgModule');
if (!moduleDeclarations.length) {
return;
}
const featureModulePath = findFeatureModule(tree, dirname(modulePath));
if (!featureModulePath) {
throw new SchematicsException(`Can't find module for routing module ${featureModulePath }.`);
}
const featureModuleSource = getSourceFile(tree, featureModulePath);
const importString = importPath(featureModulePath, modulePath);
for (const moduleDeclaration of moduleDeclarations) {
const className = (moduleDeclaration.name as ts.Identifier).getText();
const changes = addImportToModule(featureModuleSource, featureModulePath, className, importString);
applyInsertChange(tree, featureModulePath, ...changes);
}
}