@o3r/core
Version:
Core of the Otter Framework
138 lines • 6.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateOtterEnvironmentAdapter = updateOtterEnvironmentAdapter;
const node_path_1 = require("node:path");
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@o3r/schematics");
const index_1 = require("@schematics/angular/environments/index");
const ts = require("typescript");
const editTsConfigJson = (tree) => {
const tsConfigPath = '/tsconfig.json';
if (tree.exists(tsConfigPath)) {
const tsConfig = ts.parseConfigFileTextToJson(tsConfigPath, tree.readText(tsConfigPath)).config;
if (tsConfig.compilerOptions) {
if (tsConfig.compilerOptions.noPropertyAccessFromIndexSignature) {
delete tsConfig.compilerOptions.noPropertyAccessFromIndexSignature;
}
tsConfig.compilerOptions.moduleResolution = tsConfig.compilerOptions.module === 'CommonJS' ? 'node' : 'bundler';
tsConfig.compilerOptions.declaration = true;
}
tree.overwrite(tsConfigPath, JSON.stringify(tsConfig, null, 2));
}
return tree;
};
/**
* Update Otter environment variable for schematics
* @param options @see RuleFactory.options
* @param options.projectName
* @param options.enableStyling
* @param options.enableAnalytics
* @param options.workingDirectory
* @param _rootPath @see RuleFactory.rootPath
*/
function updateOtterEnvironmentAdapter(options, _rootPath) {
/**
* Add Configuration for schematics
* @param tree
* @param context
*/
const editAngularJson = (tree, context) => {
const workspace = (0, schematics_2.getWorkspaceConfig)(tree);
const workspaceProject = options.projectName ? workspace?.projects[options.projectName] : undefined;
if (!workspace || !workspaceProject) {
context.logger.error('No application detected, the Otter environment will not be setup');
return tree;
}
workspace.cli = workspace.cli || {};
workspaceProject.schematics ||= {};
if (workspaceProject.projectType === 'application') {
schematics_2.OTTER_ITEM_TYPES.forEach((item) => {
const path = schematics_2.TYPES_DEFAULT_FOLDER[item].app;
if (path) {
workspaceProject.schematics[item] = {
path: node_path_1.posix.join(workspaceProject.root, path),
...workspaceProject.schematics[item]
};
}
});
// override angular's dist/webapp output path with apps/webapp/dist or add default value if not provided
if (workspaceProject.architect?.build?.options) {
workspaceProject.architect.build.options.outputPath = node_path_1.posix.join(workspaceProject.root, 'dist');
}
workspace.projects[options.projectName] = workspaceProject;
}
else {
workspace.cli.packageManager ||= (0, schematics_2.getPackageManager)();
schematics_2.OTTER_ITEM_TYPES.forEach((item) => {
const path = schematics_2.TYPES_DEFAULT_FOLDER[item].lib;
if (path) {
workspaceProject.schematics[item] = {
path: node_path_1.posix.join(workspaceProject.root, path),
...workspaceProject.schematics[item]
};
}
});
}
(0, schematics_2.registerCollectionSchematics)(workspace, '@o3r/core');
workspace.cli.analytics = false;
tree.overwrite('/angular.json', JSON.stringify(workspace, null, 2));
return tree;
};
/**
* Use Angular CLI generator to create environment files
* @param tree
* @param context
*/
const generateEnvironmentFiles = (tree, context) => {
const workspace = (0, schematics_2.getWorkspaceConfig)(tree);
const workspaceProject = options.projectName ? workspace?.projects[options.projectName] : undefined;
if (!workspace || !workspaceProject) {
context.logger.error('No application detected, the environment can not be generated');
return tree;
}
if (tree.exists(node_path_1.posix.join(workspaceProject.root, 'src/environments/environment.ts'))) {
return tree;
}
const projectName = options.projectName || Object.keys(workspace.projects)[0];
const envBasePath = node_path_1.posix.join(workspaceProject.root, 'src', 'environments');
const envDevFilePath = node_path_1.posix.join(envBasePath, 'environment.development.ts');
if (!tree.exists(envDevFilePath)) {
return (0, index_1.default)({ project: projectName })(tree, context);
}
return tree;
};
/**
* Update environment files to add `production` boolean
* @param tree
* @param _context
*/
const editEnvironmentFiles = (tree, _context) => {
const workspaceProject = options.projectName ? (0, schematics_2.getWorkspaceConfig)(tree)?.projects[options.projectName] : undefined;
if (!workspaceProject) {
return tree;
}
const envBasePath = node_path_1.posix.join(workspaceProject.root, 'src', 'environments');
const envDevFilePath = node_path_1.posix.join(envBasePath, 'environment.development.ts');
if (!tree.exists(envDevFilePath)) {
return tree;
}
const envDefaultFilePath = node_path_1.posix.join(envBasePath, 'environment.ts');
const addProductionBoolean = (envFilePath, value) => {
let envContent = tree.readText(envFilePath);
if (!/production["']?:\s*(true|false)/.test(envContent)) {
envContent = envContent.replace(/(const environment = {)/, `$1\n production: ${String(value)},\n`);
tree.overwrite(envFilePath, envContent);
}
};
addProductionBoolean(envDefaultFilePath, true);
addProductionBoolean(envDevFilePath, false);
return tree;
};
return (0, schematics_1.chain)([
editAngularJson,
editTsConfigJson,
generateEnvironmentFiles,
editEnvironmentFiles
]);
}
//# sourceMappingURL=index.js.map