Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// unnecessary path segments and windows backslash delimiters.
const customThemePath = normalize(join(project.sourceRoot, defaultCustomThemeFilename));
if (host.exists(customThemePath)) {
console.log();
console.warn(chalk.yellow(`Cannot create a custom NG-ZORRO theme because
${chalk.bold(customThemePath)} already exists. Skipping custom theme generation.`));
return;
}
host.create(customThemePath, themeContent);
addThemeStyleToTarget(project, 'build', host, customThemePath, workspace);
return;
}
const insertion = new InsertChange(stylesPath, 0, themeContent);
const recorder = host.beginUpdate(stylesPath);
recorder.insertLeft(insertion.pos, insertion.toAdd);
host.commitUpdate(recorder);
}
position = expr.getEnd() - 1;
toInsert = ` ${metadataField}: [${symbolName}],\n`;
} else {
node = expr.properties[expr.properties.length - 1];
position = node.getEnd();
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
const matches = text.match(/^\r?\n\s*/);
if (matches.length > 0) {
toInsert = `,${matches[0]}${metadataField}: ${symbolName},`;
} else {
toInsert = `, ${metadataField}: ${symbolName},`;
}
}
return [new InsertChange(componentPath, position, toInsert)];
}
const assignment = matchingProperties[0] as ts.PropertyAssignment;
// If it's not an array, keep the original value.
if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
return [];
}
const arrLiteral = assignment.initializer as ts.ArrayLiteralExpression;
if (arrLiteral.elements.length == 0) {
// Forward the property.
node = arrLiteral;
} else {
node = arrLiteral.elements;
}
}
}
} else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
// We found the field but it's empty. Insert it just before the `]`.
position--;
toInsert = `${expression}`;
} else {
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
if (text.match(/^\r?\n/)) {
toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${expression}`;
} else {
toInsert = `, ${expression}`;
}
}
const insert = new InsertChange(ngModulePath, position, toInsert);
return [insert];
}
}
} else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
// We found the field but it's empty. Insert it just before the `]`.
position--;
toInsert = `${symbolName}`;
} else {
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
if (text.match(/^\r?\n/)) {
toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbolName},`;
} else {
toInsert = `, ${symbolName},`;
}
}
return [new InsertChange(componentPath, position, toInsert)];
}
if (ts.isVariableDeclaration(declaration) && declaration.initializer && declaration.name.getText() === 'routes') {
const node = declaration.initializer.getChildAt(1);
const lastRouteNode = node.getLastToken();
if (!lastRouteNode) {
return [];
}
const changes: Change[] = [];
let trailingCommaFound = false;
if (lastRouteNode.kind === ts.SyntaxKind.CommaToken) {
trailingCommaFound = true;
} else {
changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd(), ','));
}
changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd() + 1, ` {\n path: '${routePath}',\n loadChildren: () => import('${routeLoadChildren}').then( m => m.${ngModuleName})\n }${trailingCommaFound ? ',' : ''}\n`));
return changes;
}
}
}
return [];
}
export function addValueToVariable(host: Tree, filePath: string, variableName: string, text: string, needWrap = true) {
const source = getSourceFile(host, filePath);
const node = findNode(source, ts.SyntaxKind.Identifier, variableName);
if (!node) {
throw new SchematicsException(`Could not find any [${variableName}] variable in path '${filePath}'.`);
}
const arr = (node.parent as any).initializer as ts.ArrayLiteralExpression;
const change = new InsertChange(
filePath,
arr.end - 1,
`${arr.elements && arr.elements.length > 0 ? ',' : ''}${needWrap ? '\n ' : ''}${text}`,
);
const declarationRecorder = host.beginUpdate(filePath);
declarationRecorder.insertLeft(change.pos, change.toAdd);
host.commitUpdate(declarationRecorder);
}
const lastRouteNode = node.getLastToken();
if (!lastRouteNode) {
return [];
}
const changes: Change[] = [];
let trailingCommaFound = false;
if (lastRouteNode.kind === ts.SyntaxKind.CommaToken) {
trailingCommaFound = true;
} else {
changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd(), ','));
}
changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd() + 1, ` {\n path: '${routePath}',\n loadChildren: () => import('${routeLoadChildren}').then( m => m.${ngModuleName})\n }${trailingCommaFound ? ',' : ''}\n`));
return changes;
}
}
}
return [];
}
try {
const pathToUserRules = normalize(options.path + '/rules/user-rules.ts');
const path = getSourceFile(host, pathToUserRules);
const tsClass = strings.classify(options.modelClass);
const exportList = getSourceNodes(path)
.find(n => n.kind === ts.SyntaxKind.SyntaxList);
if (exportList) {
const exports = exportList.getChildren();
const lastExport = exports[exports.length - 1];
const rec = `\n\n/** Auto generated export */\nexport * from './ts/${tsClass}OSS';`;
const change = new InsertChange(pathToUserRules, lastExport.getEnd(), rec);
const declarationRecorder = host.beginUpdate(pathToUserRules);
declarationRecorder.insertLeft(change.pos, change.toAdd);
host.commitUpdate(declarationRecorder);
}
return host;
} catch (e) {
context.logger.log('warn',
`✅️ Failed to add new export to the user-rules.ts ${e}`);
}
};
}
export function addHtmlToBody(host: Tree, project: Project, html: string) {
const { indexPath, src } = getIndexHtmlContent(host, project);
if (src.indexOf(html) === -1) {
const node = getTag(host, src, 'body');
const insertion = new InsertChange(indexPath, node.endOffset, html);
const recorder = host.beginUpdate(indexPath);
recorder.insertLeft(insertion.pos, insertion.toAdd);
host.commitUpdate(recorder);
}
}