Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function addThemeStyleToTarget(project: WorkspaceProject, targetName: 'test' | 'build', host: Tree,
assetPath: string, workspace: WorkspaceSchema) {
// Do not update the builder options in case the target does not use the default CLI builder.
if (!validateDefaultTargetBuilder(project, targetName)) {
return;
}
const targetOptions = getProjectTargetOptions(project, targetName);
if (!targetOptions.styles) {
targetOptions.styles = [assetPath];
} else {
const existingStyles = targetOptions.styles.map(s => typeof s === 'string' ? s : s.input);
for (let [index, stylePath] of existingStyles.entries()) {
// If the given asset is already specified in the styles, we don't need to do anything.
if (stylePath === assetPath) {
return;
}
// In case a prebuilt theme is already set up, we can safely replace the theme with the new
// theme file. If a custom theme is set up, we are not able to safely replace the custom
// theme because these files can contain custom styles, while prebuilt themes are
// always packaged and considered replaceable.
function addThemeStyleToTarget(project: WorkspaceProject, targetName: 'test' | 'build', host: Tree,
assetPath: string, workspace: WorkspaceSchema): void {
// Do not update the builder options in case the target does not use the default CLI builder.
if (!validateDefaultTargetBuilder(project, targetName)) {
return;
}
const targetOptions = getProjectTargetOptions(project, targetName);
if (!targetOptions.styles) {
targetOptions.styles = [ assetPath ];
} else {
const existingStyles = targetOptions.styles.map(s => typeof s === 'string' ? s : s.input);
for (const [ index, stylePath ] of existingStyles.entries()) {
// If the given asset is already specified in the styles, we don't need to do anything.
if (stylePath === assetPath) {
return;
}
// In case a prebuilt theme is already set up, we can safely replace the theme with the new
// theme file. If a custom theme is set up, we are not able to safely replace the custom
// theme because these files can contain custom styles, while prebuilt themes are
// always packaged and considered replaceable.
function addThemeStyleToTarget(project: WorkspaceProject, targetName: 'test' | 'build', host: Tree,
assetPath: string, workspace: WorkspaceSchema) {
// Do not update the builder options in case the target does not use the default CLI builder.
if (!validateDefaultTargetBuilder(project, targetName)) {
return;
}
const targetOptions = getProjectTargetOptions(project, targetName);
if (!targetOptions.styles) {
targetOptions.styles = [assetPath];
} else {
const existingStyles = targetOptions.styles.map((s) => typeof s === 'string' ? s : s.input);
for (const [index, stylePath] of existingStyles.entries()) {
// If the given asset is already specified in the styles, we don't need to do anything.
if (stylePath === assetPath) {
return;
}
// In case a prebuilt theme is already set up, we can safely replace the theme with the new
// theme file. If a custom theme is set up, we are not able to safely replace the custom
// theme because these files can contain custom styles, while prebuilt themes are
// always packaged and considered replaceable.
it('should not add a theme file multiple times', () => {
writeStyleFileToWorkspace(appTree, defaultPrebuiltThemePath);
const tree = runner.runSchematic('ng-add-setup-project', {}, appTree);
const workspace = getWorkspace(tree);
const project = getProjectFromWorkspace(workspace);
const styles = getProjectTargetOptions(project, 'build').styles;
expect(styles).toEqual(['projects/material/src/styles.css', defaultPrebuiltThemePath],
'Expected the "styles.css" file and default prebuilt theme to be the only styles');
});
function writeStyleFileToWorkspace(tree: Tree, stylePath: string) {
const workspace = getWorkspace(tree);
const project = getProjectFromWorkspace(workspace);
const buildOptions = getProjectTargetOptions(project, 'build');
if (!buildOptions.styles) {
buildOptions.styles = [stylePath];
} else {
buildOptions.styles.push(stylePath);
}
tree.overwrite('/angular.json', JSON.stringify(workspace, null, 2));
}
function expectProjectStyleFile(project: WorkspaceProject, filePath: string) {
expect(getProjectTargetOptions(project, 'build').styles).toContain(filePath,
`Expected "${filePath}" to be added to the project styles in the workspace.`);
}