Skip to content

Instantly share code, notes, and snippets.

@altbdoor
Created May 10, 2023 03:35
Show Gist options
  • Save altbdoor/f0ec2743afe0275f5c0a9458961dc5b8 to your computer and use it in GitHub Desktop.
Save altbdoor/f0ec2743afe0275f5c0a9458961dc5b8 to your computer and use it in GitHub Desktop.
NodeJS script to fix include paths in Angular JSON
#!/usr/bin/env node
/* eslint-env node */
const process = require("process");
const fs = require("fs");
const args = process.argv.slice(2);
if (args.length !== 1) {
console.error("Please pass in path to angular.json file");
process.exit(1);
}
const pathToAngularJson = args[0];
let content = fs.readFileSync(pathToAngularJson, { encoding: "utf8" });
content = JSON.parse(content);
Object.keys(content.projects).forEach((projectName) => {
const project = content.projects[projectName];
["build", "test"].forEach((buildType) => {
/** @type {string[]} */
let sassOptions = [];
try {
sassOptions =
project.architect[buildType].options.stylePreprocessorOptions
.includePaths;
} catch (err) {
console.warn(
`Cannot find "stylePreprocessorOptions" for ${projectName}.architect.${buildType}`
);
return;
}
if (sassOptions.length === 0) {
console.info(
`Nothing to update for ${projectName}.architect.${buildType}`
);
return;
}
console.info(`Updating ${projectName}.architect.${buildType}`);
/** @type {string[]} */
const addPathList = [project.root, `${project.root}/node_modules`];
addPathList.reverse().forEach((addPath) => {
if (!sassOptions.includes(addPath)) {
sassOptions.unshift(addPath);
}
});
project.architect[buildType].options.stylePreprocessorOptions.includePaths =
sassOptions;
});
});
fs.writeFileSync(pathToAngularJson, JSON.stringify(content, null, 4), {
encoding: "utf8",
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment