@domoinc/ryuu-angular
Version:
Angular Schematic to add @domoinc/ryuu-proxy support
155 lines • 7.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ngAdd = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const core_1 = require("@angular-devkit/core");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
const utils_1 = require("./utils");
/**
* Retrieves the contents of the tsconfig.json file from the given tree.
* @param {Tree} tree - The tree object representing the file system.
* @returns The contents of the tsconfig.json file as a JSON object, or an empty string if the file does not exist.
*/
const getTsConfig = (tree) => { var _a; return (_a = tree.readJson('tsconfig.json')) !== null && _a !== void 0 ? _a : ''; };
/**
* Retrieves the Angular configuration from the given tree.
* @param {Tree} tree - The tree object representing the file system.
* @returns The Angular configuration object if found, otherwise an empty string.
*/
const getAngularConfig = (tree) => { var _a; return (_a = tree.readJson('angular.json')) !== null && _a !== void 0 ? _a : ''; };
/**
* Updates the tsconfig.json file in the given tree by adding or modifying the compiler options.
* @param {Tree} tree - The tree representing the file system.
* @returns The updated tree.
*/
const updateTSConfig = () => {
return (tree) => {
const config = getTsConfig(tree);
config.compilerOptions = Object.assign(Object.assign({}, config.compilerOptions), { resolveJsonModule: true, allowSyntheticDefaultImports: true });
tree.overwrite('tsconfig.json', JSON.stringify(config, null, 2));
return tree;
};
};
/**
* Updates the angular.json file with the specified options.
* @param {Schema} options - The options to update the angular.json file with.
* @returns A function that takes in a Tree object and updates the angular.json file.
*/
const updateAngularJSON = (options) => {
return (tree) => {
var _a;
const workspace = (0, utils_1.getWorkspace)(tree);
const project = (0, utils_1.getProject)(workspace, options.project);
(project.architect.build
.builder) = '@angular-builders/custom-webpack:browser';
(project.architect.serve
.builder) = '@angular-builders/custom-webpack:dev-server';
(project.architect.build.options)['customWebpackConfig'] = {
path: './extra-webpack.config.ts',
};
// Options
const mainSrc = (_a = project.architect.build.options.browser) !== null && _a !== void 0 ? _a : 'src/main.ts';
(project.architect.build
.options.main) = mainSrc;
delete project.architect.build.options.browser; // Remove browser option for Custom Webpack
tree.overwrite('angular.json', JSON.stringify(workspace, null, 2));
return tree;
};
};
/**
* Retrieves the version number of the project from the package.json file in the given tree.
* @param {Tree} tree - The tree object representing the project file structure.
* @returns The version number of the project.
*/
const getProjectVersion = (tree) => {
var _a;
const packageBuffer = (_a = tree.read('package.json')) !== null && _a !== void 0 ? _a : '';
const pack = (0, utils_1.parseJson)(packageBuffer.toString());
return pack.version;
};
/**
* Copies Domo files to the specified tree directory.
* @param {Tree} tree - The tree directory to copy the files to.
* @param {Schema} options - The options for copying the files.
* @returns A merged tree with the copied files.
*/
const copyDomoFiles = (tree, options) => {
const version = getProjectVersion(tree);
return (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files'), [
(0, schematics_1.template)(Object.assign(Object.assign(Object.assign({}, options), { version }), core_1.strings)),
(0, schematics_1.forEach)((file) => {
if (tree.exists(file.path))
return null;
return file;
}),
]));
};
/**
* Adds the specified dependencies to the package.json file.
* @param {Tree} tree - The tree object representing the file system.
* @returns A function that takes in a tree object and adds the dependencies to the package.json file.
*/
const addDepsToPackage = () => {
return (tree) => {
const deps = {
'@angular-builders/custom-webpack': '^18.0.0',
'@domoinc/ryuu-proxy': '^4.3.4',
'copy-webpack-plugin': '^11.0.0',
};
Object.keys(deps).forEach((key) => {
(0, dependencies_1.addPackageJsonDependency)(tree, {
name: key,
type: dependencies_1.NodeDependencyType.Dev,
version: deps[key],
});
});
};
};
/**
* Adds helper scripts to the package.json file based on the provided options.
* @param {Schema} options - The options object containing project information.
* @returns {Tree} The updated tree with the modified package.json file.
*/
const addHelperScripts = (options) => {
return (tree) => {
var _a, _b;
const packageBuffer = (_a = tree.read('package.json')) !== null && _a !== void 0 ? _a : '';
const pack = (0, utils_1.parseJson)(packageBuffer.toString());
const distFolder = (_b = options.output) !== null && _b !== void 0 ? _b : `dist/${options.project}`;
pack.scripts = Object.assign(Object.assign({}, pack.scripts), { 'domo:upload': `npm run build && cd ${distFolder} && domo publish`, 'postdomo:upload': `cp ${distFolder}/manifest.json domo/` });
tree.overwrite('package.json', JSON.stringify(pack, null, 2));
return tree;
};
};
/**
* Angular schematic rule that adds dependencies and configuration to an Angular project.
* @param {any} _options - The options for the schematic.
* @returns {Rule} - A rule function that modifies the project tree.
*/
function ngAdd(options) {
return (tree, context) => {
context.addTask(new tasks_1.NodePackageInstallTask());
// Check for Project Name
if (!options.project) {
const config = getAngularConfig(tree);
if (config.projects) {
const projects = Object.keys(config.projects);
const defaultProjectName = projects.length > 0 ? projects[0] : '';
const defaultProject = config.projects[defaultProjectName];
const buildOutput = defaultProject.architect.build.options.outputPath;
options = Object.assign(Object.assign({}, options), { project: defaultProjectName, output: buildOutput });
}
}
console.log('[ngAdd] options', options);
return (0, schematics_1.chain)([
addDepsToPackage(),
copyDomoFiles(tree, options),
updateAngularJSON(options),
updateTSConfig(),
addHelperScripts(options),
]);
};
}
exports.ngAdd = ngAdd;
//# sourceMappingURL=index.js.map