ng-afelio
Version:
Extended Angular CLI
87 lines (86 loc) • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const workspace_1 = require("@schematics/angular/utility/workspace");
const environment_1 = require("../util/environment");
// 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 projectEnvPath = join(projectAppPath as Path, path/*'../environments/environment.ts'*/);
// return host => {
// const 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();
// if (lastRouteNode) {
// changes.push(
// new InsertChange(
// projectEnvPath,
// lastRouteNode.getEnd(),
// toAdd
// // `,\n mock: {\n enable: true,\n all: false,\n services: {\n getPets: true\n }\n }`
// )
// );
// }
// }
// applyChangesToHost(host, projectEnvPath, changes);
// return host;
// applyIntoEnvironment(projectAppPath, options.project, `,\n baseUrls: {\n main: 'http://localhost:4200/api'\n }`, false),
// applyIntoEnvironment(projectAppPath, options.project, `,\n baseUrls: {\n main: '\${BASE_URL}'\n }`, true),
return (0, schematics_1.chain)([
(0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, `\n baseUrls: {\n main: 'http://localhost:4200/api'\n }`, 'baseUrls:', false),
(0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, `\n baseUrls: {\n main: '\${BASE_URL}'\n }`, 'baseUrls:', true)
]);
}
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 templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
(0, schematics_1.template)({
...core_1.strings,
...options,
}),
(0, schematics_1.move)('/'),
]);
return (0, schematics_1.chain)([
(0, schematics_1.branchAndMerge)((0, schematics_1.chain)([
(0, schematics_1.mergeWith)(templateSource),
applyIntoEnvironment(projectAppPath, options.project),
])),
]);
};
}
exports.default = default_1;