@o3r/application
Version:
This module provides development help to monitor your Otter Application
97 lines • 5.1 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) {
/* ng add rules */
return 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 packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const depsInfo = (0, schematics_2.getO3rPeerDeps)(packageJsonPath);
const workspaceProject = options.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined;
const addAngularAnimationPreferences = () => {
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)(tree, context);
};
const dependencies = depsInfo.o3rPeerDeps.reduce((acc, dep) => {
acc[dep] = {
inManifest: [{
range: `${options.exactO3rVersion ? '' : '~'}${depsInfo.packageVersion}`,
types: (0, schematics_2.getProjectNewDependenciesTypes)(workspaceProject)
}],
ngAddOptions: { exactO3rVersion: options.exactO3rVersion }
};
return acc;
}, (0, schematics_2.getPackageInstallConfig)(packageJsonPath, tree, options.projectName, false, !!options.exactO3rVersion));
const projectDirectory = workspaceProject?.root || '.';
const projectPackageJson = tree.readJson(path.posix.join(projectDirectory, 'package.json'));
const externalDependenciesInfo = (0, schematics_2.getExternalDependenciesInfo)({
devDependenciesToInstall,
dependenciesToInstall,
projectType: workspaceProject?.projectType,
o3rPackageJsonPath: packageJsonPath,
projectPackageJson
}, context.logger);
const registerDevtoolRule = (0, devtools_registration_1.registerDevtools)(options);
return () => (0, schematics_1.chain)([
(0, schematics_2.setupDependencies)({
projectName: options.projectName,
dependencies: { ...dependencies, ...externalDependenciesInfo },
ngAddToRun: depsInfo.o3rPeerDeps
}),
addAngularAnimationPreferences,
registerDevtoolRule
])(tree, context);
};
}
/**
* 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