@o3r/application
Version:
This module provides development help to monitor your Otter Application
68 lines • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ngAdd = void 0;
const path = require("node:path");
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@o3r/schematics");
const utility_1 = require("@schematics/angular/utility");
const devtools_registration_1 = require("./helpers/devtools-registration");
/**
* List of external dependencies to be added to the project as peer dependencies
*/
const dependenciesToInstall = [
'@angular/common',
'@angular/core',
'rxjs'
];
/**
* List of external dependencies to be added to the project as dev dependencies
*/
const devDependenciesToInstall = [];
/**
* Add Otter application to an Angular Project
* @param options The options to pass to ng-add execution
*/
function ngAddFn(options) {
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const addAngularAnimationPreferences = async (tree, context) => {
const { isImported } = await Promise.resolve().then(() => require('@schematics/angular/utility/ast-utils')).catch(() => ({ isImported: undefined }));
const ts = await Promise.resolve().then(() => require('typescript')).catch(() => undefined);
const additionalRules = [];
const moduleFilePath = (0, schematics_2.getAppModuleFilePath)(tree, context, options.projectName);
if (!moduleFilePath || !options.projectName) {
return tree;
}
const sourceFileContent = tree.readText(moduleFilePath);
const sourceFile = ts?.createSourceFile(moduleFilePath, sourceFileContent, ts.ScriptTarget.ES2015, true);
if (!sourceFile) {
context.logger.warn('No Typescript executor detected, the ng-add process will be skipped.');
return tree;
}
if (!isImported) {
context.logger.warn('No @schematics/angular dependency detected, the ng-add process will be skipped.');
return tree;
}
if (isImported(sourceFile, 'prefersReducedMotion', '@o3r/application')) {
context.logger.info('prefersReducedMotion from @o3r/application is already imported.');
return tree;
}
const importInRootModule = (name, file, moduleFunction) => additionalRules.push((0, utility_1.addRootImport)(options.projectName, ({ code, external }) => code `\n${external(name, file)}${moduleFunction}`));
const recorder = tree.beginUpdate(moduleFilePath);
(0, schematics_2.insertImportToModuleFile)('prefersReducedMotion', '@o3r/application', sourceFile, recorder, moduleFilePath);
importInRootModule('BrowserAnimationsModule', '@angular/platform-browser/animations', '.withConfig({disableAnimations: prefersReducedMotion()})');
tree.commitUpdate(recorder);
return (0, schematics_1.chain)(additionalRules);
};
return (0, schematics_1.chain)([
(0, schematics_2.ngAddDependenciesRule)(options, packageJsonPath, { dependenciesToInstall, devDependenciesToInstall }),
addAngularAnimationPreferences,
(0, devtools_registration_1.registerDevtools)(options)
]);
}
/**
* Add Otter application to an Angular Project
* @param options The options to pass to ng-add execution
*/
const ngAdd = (options) => (0, schematics_2.createOtterSchematic)(ngAddFn)(options);
exports.ngAdd = ngAdd;
//# sourceMappingURL=index.js.map