life-sciences-portal-cli
Version:
A CLI to generate and manage life sciences portals.
57 lines (56 loc) • 3.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.lifeSciencesPortalInternalCms = void 0;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
function updatePackageJson(_path) {
return (host) => {
const packageJsonFileName = `${_path}/package.json`;
const packageJsonBuffer = host.read(packageJsonFileName);
if (packageJsonBuffer === null) {
return host;
}
const packageJson = JSON.parse(packageJsonBuffer.toString());
packageJson.dependencies = Object.assign(Object.assign({}, packageJson.dependencies), { 'gray-matter': '^4.0.3', 'markdown-it': '^12.2.0' });
packageJson.devDependencies = Object.assign(Object.assign({}, packageJson.devDependencies), { 'chokidar': '^3.5.2', 'concurrently': '^6.3.0' });
packageJson.scripts = Object.assign(Object.assign({}, packageJson.scripts), { 'start': 'concurrently "npm run cms:watch" "ng serve"', 'cms:generate': 'node src/scripts/cms.js generate', 'cms:watch': 'node src/scripts/cms.js watch' });
host.overwrite(packageJsonFileName, JSON.stringify(packageJson, null, 2));
};
}
function updateAppRoutingModule(_path) {
return (host) => {
const appRoutingModulePath = (0, core_1.normalize)(`/${_path || ''}/src/app/app-routing.module.ts`);
let text = host.read(appRoutingModulePath);
if (!text) {
throw new schematics_1.SchematicsException(`File ${appRoutingModulePath} does not exist.`);
}
let sourceText = text.toString('utf-8');
const routesArrayRegex = /const routes: Routes = \[((?:.|\s)*?)\]/gm;
const routesArrayRegexMatches = routesArrayRegex.exec(sourceText);
if (routesArrayRegexMatches === null) {
throw new schematics_1.SchematicsException('Expected routes variable in app-routing.module.ts');
}
const routes = '\n {\n path: \'content\',\n loadChildren: () => import(\'./cms/cms.module\').then(m => m.CmsModule)\n },' + routesArrayRegexMatches[1];
sourceText = sourceText.replace(routesArrayRegex, `const routes: Routes = [${routes}]`);
host.overwrite(appRoutingModulePath, sourceText);
};
}
function lifeSciencesPortalInternalCms(_options) {
return (_tree, _context) => {
const cmsContentPath = (0, core_1.normalize)(`/${_options.path || ''}/src/app`);
const cmsContentTemplateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files/cms-template'), [
(0, schematics_1.move)(cmsContentPath)
]);
const cmsScriptsPath = (0, core_1.normalize)(`/${_options.path || ''}/src`);
const cmsScriptsTemplateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files/scripts-template'), [
(0, schematics_1.move)(cmsScriptsPath)
]);
return (0, schematics_1.chain)([
(0, schematics_1.mergeWith)(cmsContentTemplateSource),
(0, schematics_1.mergeWith)(cmsScriptsTemplateSource),
updatePackageJson(_options.path),
updateAppRoutingModule(_options.path)
]);
};
}
exports.lifeSciencesPortalInternalCms = lifeSciencesPortalInternalCms;