ng-afelio
Version:
Extended Angular CLI
183 lines (182 loc) • 8.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
const find_module_1 = require("@schematics/angular/utility/find-module");
const workspace_1 = require("@schematics/angular/utility/workspace");
const ts = require("typescript");
const ast_util_1 = require("../util/ast-util");
const change_1 = require("../util/change");
const environment_1 = require("../util/environment");
const buildPath = './core';
function installNgxToastr() {
return (host, context) => {
const ngxToastr = {
type: dependencies_1.NodeDependencyType.Default,
name: 'ngx-toastr',
version: '^16.0.1',
overwrite: false,
};
(0, dependencies_1.addPackageJsonDependency)(host, ngxToastr);
context.addTask(new tasks_1.NodePackageInstallTask(), []);
};
}
// function getEnvironmentNode(source: ts.SourceFile): ts.Node | undefined {
// const keywords = findNodes(source, ts.SyntaxKind.VariableStatement);
// for (const keyword of keywords) {
// if (ts.isVariableStatement(keyword)) {
// const [declaration] = keyword.declarationList.declarations;
// if (
// ts.isVariableDeclaration(declaration) &&
// declaration.initializer &&
// declaration.name.getText() === 'environment'
// ) {
// return declaration.initializer.getChildAt(1);
// }
// }
// }
// }
function applyIntoEnvironment(projectAppPath, projectName) {
const envToAdd = `\n errorsHandler: {\n enable: true,\n codesToExclude: []\n }`;
return (0, schematics_1.chain)([
(0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, envToAdd, 'errorsHandler:', false),
(0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, envToAdd, 'errorsHandler:', true)
]);
// // const projectEnvPath = join(projectAppPath as Path, prodEnv ? '../environments/environment.prod.ts' : '../environments/environment.ts');
// let projectEnvPath = join(projectAppPath as Path, '../environments/environment.development.ts');
// return host => {
// let text = host.read(projectEnvPath);
// if (!text) { // Fallback for old project version
// projectEnvPath = join(projectAppPath as Path, '../environments/environment.ts');
// text = host.read(projectEnvPath);
// }
// if (!text) {
// throw new SchematicsException(`Environment file on ${projectName} project does not exist.`);
// }
// const sourceText = text.toString('utf8');
// const source = ts.createSourceFile(
// projectEnvPath,
// sourceText,
// ts.ScriptTarget.Latest,
// true
// );
// const node = getEnvironmentNode(source);
// const changes: Change[] = [];
// if (node) {
// const lastRouteNode = node.getLastToken();
// const envToAdd = `\n errorsHandler: {\n enable: true,\n codesToExclude: []\n }`;
// if (lastRouteNode) {
// changes.push(
// new InsertChange(
// projectEnvPath,
// lastRouteNode.getEnd(),
// `,${envToAdd}`
// )
// );
// } else {
// changes.push(
// new InsertChange(
// projectEnvPath,
// node.getEnd(),
// `${envToAdd}\n`
// )
// );
// }
// console.log(`${colors.green('Changes will be applied to dev environment.')} ${colors.yellow('Please apply it to others.')}`);
// } else {
// throw new SchematicsException(`No "export const environment" found`);
// }
// applyChangesToHost(host, projectEnvPath, changes);
// return host;
// };
}
function applyModuleImports(projectAppPath, options, useNgxToastr) {
return host => {
if (options.appModule) {
const changes = [];
const modulePath = (0, core_1.join)(projectAppPath, options.appModule);
const text = host.read(modulePath);
if (!text) {
throw new schematics_1.SchematicsException(`Module file at ${modulePath} does not exist.`);
}
const sourceText = text.toString('utf8');
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
const projectErrorHandlerPath = (0, core_1.join)(projectAppPath, buildPath, 'modules', 'http-error');
const relativeErrorHandlerPath = (0, find_module_1.buildRelativePath)(modulePath, projectErrorHandlerPath);
// Add ts imports
changes.push((0, ast_util_1.insertImport)(source, modulePath, 'HttpErrorModule', relativeErrorHandlerPath));
if (useNgxToastr) {
changes.push((0, ast_util_1.insertImport)(source, modulePath, 'BrowserAnimationsModule', '@angular/platform-browser/animations'));
changes.push((0, ast_util_1.insertImport)(source, modulePath, 'ToastrModule', 'ngx-toastr'));
}
// Add environments ts import
const projectEnvPath = (0, core_1.join)(projectAppPath, '../environments/environment');
const relativeEnvPath = (0, find_module_1.buildRelativePath)(modulePath, projectEnvPath);
changes.push((0, ast_util_1.insertImport)(source, modulePath, 'environment', relativeEnvPath));
// Add ng imports
changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, 'HttpErrorModule.forRoot(environment.errorsHandler)', null));
if (useNgxToastr) {
changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, 'BrowserAnimationsModule', null));
changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, 'ToastrModule.forRoot()', null));
}
// Save changes
(0, change_1.applyChangesToHost)(host, modulePath, changes);
}
return host;
};
}
function addNgxToastrStyle(projectAppPath) {
return host => {
const changes = [];
const stylePath = (0, core_1.join)(projectAppPath, '../styles.scss');
const text = host.read(stylePath);
if (!text) {
throw new schematics_1.SchematicsException(`Can not add NgxStoastr style, ${stylePath} does not exist.`);
}
if (!text.includes('ngx-toastr/toastr')) {
changes.push(new change_1.InsertChange(stylePath, text.length, `\n@import 'node_modules/ngx-toastr/toastr';\n`));
(0, change_1.applyChangesToHost)(host, stylePath, changes);
}
return host;
};
}
function default_1(options) {
return async (host) => {
const useNgxToastr = options.useNgxToastr;
const workspaceConfigBuffer = host.read('angular.json');
if (!workspaceConfigBuffer) {
throw new schematics_1.SchematicsException('Not an Angular CLI workspace');
}
const workspace = await (0, workspace_1.getWorkspace)(host);
const project = workspace.projects.get(options.project);
let projectAppPath;
if (project) {
projectAppPath = (0, workspace_1.buildDefaultPath)(project);
}
else {
throw new schematics_1.SchematicsException(`Project "${options.project}" not found.`);
}
const parsedPath = (0, core_1.join)(projectAppPath, buildPath);
const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
(0, schematics_1.template)({
...core_1.strings,
...options,
}),
(0, schematics_1.move)(parsedPath),
]);
const rules = [
(0, schematics_1.mergeWith)(templateSource),
applyIntoEnvironment(projectAppPath, options.project),
// applyIntoEnvironment(projectAppPath, options.project, true),
applyModuleImports(projectAppPath, options, useNgxToastr),
];
if (useNgxToastr) {
rules.push(installNgxToastr());
rules.push(addNgxToastrStyle(projectAppPath));
}
return (0, schematics_1.chain)([(0, schematics_1.branchAndMerge)((0, schematics_1.chain)(rules))]);
};
}
exports.default = default_1;