Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const modifyRoutingModule = (host: Tree, routingModulePath: string) => {
// TODO: Try to use the isolated host to generate the result string
let source = getSourceFile(host, routingModulePath)!;
const importChange = insertImport(source, routingModulePath, 'LoginFormComponent', './shared/components');
const providerChanges = addProviderToModule(source, routingModulePath, 'AuthGuardService', './shared/services');
applyChanges(host, [ importChange, ...providerChanges], routingModulePath);
source = getSourceFile(host, routingModulePath)!;
const routes = findRoutesInSource(source)!;
if (!hasComponentInRoutes(routes, 'login-form')) {
const loginFormRoute = getRoute('login-form');
insertItemToArray(host, routingModulePath, routes, loginFormRoute);
}
};
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const appModulePath = getAppModulePath(host, getProjectMainFile(project));
const moduleSource = getSourceFile(host, appModulePath);
const locale = getCompatibleLocal(options);
const localePrefix = locale.split('_')[ 0 ];
const recorder = host.beginUpdate(appModulePath);
const changes = [
insertImport(moduleSource, appModulePath, 'NZ_I18N',
'ng-zorro-antd/i18n'),
insertImport(moduleSource, appModulePath, locale,
'ng-zorro-antd/i18n'),
insertImport(moduleSource, appModulePath, 'registerLocaleData',
'@angular/common'),
insertImport(moduleSource, appModulePath, localePrefix,
`@angular/common/locales/${localePrefix}`, true),
registerLocaleData(moduleSource, appModulePath, localePrefix),
...insertI18nTokenProvide(moduleSource, appModulePath, locale)
];
changes.forEach((change) => {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
});
host.commitUpdate(recorder);
return host;
return (host: Tree) => {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
const appModulePath = getAppModulePath(host, getProjectMainFile(project));
const moduleSource = getSourceFile(host, appModulePath);
const locale = getCompatibleLocal(options);
const localePrefix = locale.split('_')[ 0 ];
const recorder = host.beginUpdate(appModulePath);
const changes = [
insertImport(moduleSource, appModulePath, 'NZ_I18N',
'ng-zorro-antd/i18n'),
insertImport(moduleSource, appModulePath, locale,
'ng-zorro-antd/i18n'),
insertImport(moduleSource, appModulePath, 'registerLocaleData',
'@angular/common'),
insertImport(moduleSource, appModulePath, localePrefix,
`@angular/common/locales/${localePrefix}`, true),
registerLocaleData(moduleSource, appModulePath, localePrefix),
...insertI18nTokenProvide(moduleSource, appModulePath, locale)
];
changes.forEach((change) => {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
});
host.commitUpdate(recorder);
function addImportDeclaration(host: Tree, modulePath: string, fileName: string, filePath: string) {
const source = readIntoSourceFile(host, modulePath);
const changes = insertImport(source as any, modulePath, fileName, filePath);
const declarationRecorder = host.beginUpdate(modulePath);
if (changes instanceof InsertChange) {
declarationRecorder.insertLeft(changes.pos, changes.toAdd);
}
host.commitUpdate(declarationRecorder);
}
indexFilePath,
indexSource,
ts.ScriptTarget.Latest,
true
);
const moduleSource = host.read(modulePath)!.toString('utf-8');
const moduleSourceFile = ts.createSourceFile(
modulePath,
moduleSource,
ts.ScriptTarget.Latest,
true
);
const constName = `${toPropertyName(schema.name)}Routes`;
insert(host, modulePath, [
insertImport(
moduleSourceFile,
modulePath,
'RouterModule, Route',
'@angular/router'
),
...addImportToModule(moduleSourceFile, modulePath, `RouterModule`),
...addGlobal(
moduleSourceFile,
modulePath,
`export const ${constName}: Route[] = [];`
)
]);
insert(host, indexFilePath, [
...addReexport(indexSourceFile, indexFilePath, moduleFileName, constName)
]);
return host;
function addImportToModule(host: Tree, filePath: string, symbolName: string, fileName: string) {
const source = getSourceFile(host, filePath);
const change = insertImport(source, filePath, symbolName, fileName) as InsertChange;
const declarationRecorder = host.beginUpdate(filePath);
declarationRecorder.insertLeft(change.pos, change.toAdd);
host.commitUpdate(declarationRecorder);
}
routingModulePath: Path,
routes: ts.ArrayLiteralExpression,
route: string,
componentClass?: string,
fileImportPath?: string,
): void {
const source = getSourceFile(tree, routingModulePath);
const alreadyInRoutes = routes.getFullText().includes(route);
if (alreadyInRoutes) {
return;
}
addArrayElement(tree, source, routes, route);
if (componentClass && fileImportPath) {
const importChange = insertImport(source, source.fileName, componentClass, fileImportPath);
applyInsertChange(tree, normalize(source.fileName), importChange);
}
}
const changes = newImports.map(([a, b]) => insertImport(codeSource, codeAction.path, a, b));
const recorder = tree.beginUpdate(codeAction.path);
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
);
insert(host, modulePath, [
insertImport(sourceFile, modulePath, 'NxModule', '@nrwl/nx'),
...addImportToModule(sourceFile, modulePath, 'NxModule.forRoot()')
]);
return host;
};
}