angular-auth-oidc-client
Version:
Angular Lib for OpenID Connect & OAuth2
53 lines • 2.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addModuleToImports = addModuleToImports;
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const angular_utils_1 = require("../../utils/angular-utils");
function addModuleToImports(options) {
return (host, context) => {
const [, project] = (0, angular_utils_1.getProject)(host);
const { moduleFileName, moduleName } = options.moduleInfo;
// Try to find the app module file with different naming conventions
const appModulePath = findAppModulePath(host, project.sourceRoot);
if (!appModulePath) {
throw new Error('Could not find app module file. Tried: app.module.ts, app-module.ts. ' +
'Please ensure your app module exists in the src/app directory.');
}
const modulesToImport = [
{
target: appModulePath,
moduleName,
modulePath: `./auth/${moduleFileName}`,
},
];
modulesToImport.forEach(({ target, moduleName, modulePath }) => {
addImport(host, context, moduleName, modulePath, target);
});
context.logger.info(`✅️ All imports done, please add the 'RouterModule' as well if you don't have it imported yet.`);
return host;
};
}
function findAppModulePath(host, sourceRoot) {
// Try common naming conventions for app module
const possiblePaths = [
`${sourceRoot}/app/app.module.ts`, // Traditional Angular CLI naming
`${sourceRoot}/app/app-module.ts`, // Newer Angular CLI naming convention
];
for (const path of possiblePaths) {
if (host.exists(path)) {
return path;
}
}
return null;
}
function addImport(host, context, moduleName, source, target) {
const sourcefile = (0, angular_utils_1.readIntoSourceFile)(host, target);
const importChanges = (0, ast_utils_1.addImportToModule)(sourcefile, source, moduleName, source);
importChanges.forEach((insertChange) => {
const exportRecorder = host.beginUpdate(target);
exportRecorder.insertLeft(insertChange.pos, insertChange.toAdd);
host.commitUpdate(exportRecorder);
});
context.logger.info(`✅️ '${moduleName}' is imported in '${target}'`);
}
//# sourceMappingURL=add-module-import.js.map