ng-afelio
Version:
Extended Angular CLI
138 lines (137 loc) • 6.77 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 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");
function applyModuleImports(projectAppPath, mockPath, 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 MockInterceptor ts import
const relativeMockPath = (0, find_module_1.buildRelativePath)(modulePath, mockPath);
const interceptorPath = (0, core_1.join)(relativeMockPath, 'mockHttpInterceptor');
changes.push((0, ast_util_1.insertImport)(source, modulePath, 'mockInterceptorProvider', interceptorPath));
// 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 provider
changes.push(...(0, ast_util_1.addProviderToModule)(source, modulePath, '...(environment.mock.enable ? [mockInterceptorProvider] : [])', 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) {
const envToAdd = `\n mock: {\n enable: true,\n all: false,\n services: {\n getPets: true\n }\n }`;
const envToAddProd = `\n mock: {\n enable: false,\n all: false,\n services: {\n getPets: false\n }\n }`;
return (0, schematics_1.chain)([
(0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, envToAdd, 'mock:', false),
(0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, envToAddProd, 'mock:', 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('mock:')) {
// const lastRouteNode = node.getLastToken();
// const mocksToAdd = `\n mock: {\n enable: true,\n all: false,\n services: {\n getPets: true\n }\n }`;
// if (lastRouteNode) {
// changes.push(
// new InsertChange(
// projectEnvPath,
// lastRouteNode.getEnd(),
// `,${mocksToAdd}`
// )
// );
// } else {
// changes.push(
// new InsertChange(
// projectEnvPath,
// node.getEnd(),
// `${mocksToAdd}\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) => {
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, '../mocks');
const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
(0, schematics_1.template)({
...core_1.strings,
...options,
}),
(0, schematics_1.move)(parsedPath),
]);
return (0, schematics_1.chain)([
(0, schematics_1.branchAndMerge)((0, schematics_1.chain)([
applyIntoEnvironment(projectAppPath, options.project),
(0, schematics_1.mergeWith)(templateSource),
applyModuleImports(projectAppPath, parsedPath, options),
])),
]);
};
}
exports.default = default_1;