UNPKG

ng-afelio

Version:
154 lines (153 loc) 6.87 kB
"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 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"); function installNgxs() { return (host, context) => { const lib = { type: dependencies_1.NodeDependencyType.Default, name: '@ngxs/store', version: '^3.7.1', overwrite: false, }; const plugin = { type: dependencies_1.NodeDependencyType.Dev, name: '@ngxs/devtools-plugin', version: '^3.7.1', overwrite: false, }; (0, dependencies_1.addPackageJsonDependency)(host, lib); (0, dependencies_1.addPackageJsonDependency)(host, plugin); context.addTask(new tasks_1.NodePackageInstallTask(), []); }; } function applyModuleImports(projectAppPath, options) { 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); // Add ts imports changes.push((0, ast_util_1.insertImport)(source, modulePath, 'NgxsModule', '@ngxs/store')); changes.push((0, ast_util_1.insertImport)(source, modulePath, 'NgxsReduxDevtoolsPluginModule', '@ngxs/devtools-plugin')); changes.push((0, ast_util_1.insertImport)(source, modulePath, 'environment', '../environments/environment')); // Add ng imports const translateImport = `NgxsModule.forRoot([], { developmentMode: environment.ngxsDebugger })`; const pluginImport = `...(!environment.ngxsDebugger ? [] : [ NgxsReduxDevtoolsPluginModule.forRoot() ])`; changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, translateImport, null)); changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, pluginImport, null)); // Save changes (0, change_1.applyChangesToHost)(host, modulePath, changes); } return host; }; } // 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) { return (0, schematics_1.chain)([ (0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, `\n ngxsDebugger: true`, 'ngxsDebugger:', false), (0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, `\n ngxsDebugger: false`, 'ngxsDebugger:', true) ]); // 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) { // if (!node.getText().includes('ngxsDebugger:')) { // const lastRouteNode = node.getLastToken(); // const envToAdd = `\n ngxsDebugger: true`; // 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 default_1(options) { return async (host) => { if (!options.project) { throw new schematics_1.SchematicsException('Option (project) is required.'); } 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.`); } return (0, schematics_1.chain)([ (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([ installNgxs(), applyIntoEnvironment(projectAppPath, options.project), applyModuleImports(projectAppPath, options), ])), ]); }; } exports.default = default_1;