@objectivity/angular-schematic-utils
Version:
Utilities for working with Schematics.
64 lines • 3.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProjectEnvironmentPath = exports.updateEnvironmentConfiguration = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const schematics_utilities_1 = require("schematics-utilities");
const version_agnostic_typescript_1 = require("./version-agnostic-typescript");
const project_targets_1 = require("./project-targets");
const get_project_1 = require("./get-project");
function updateEnvironmentConfiguration(project, insertion) {
return (tree, _context) => {
const workspace = schematics_utilities_1.getWorkspace(tree);
const workspaceProject = get_project_1.getProjectFromWorkspace(workspace, project);
const envDirPath = getProjectEnvironmentPath(workspaceProject);
const envDir = tree.getDir(envDirPath);
const rules = envDir.subfiles.map(envPath => updateEnvironment(`${envDir.path}/${envPath}`, insertion));
return schematics_1.chain(rules);
};
}
exports.updateEnvironmentConfiguration = updateEnvironmentConfiguration;
function getProjectEnvironmentPath(workspaceProject) {
const envFilePath = getProjectEnvironmentFile(workspaceProject);
return envFilePath.substr(0, envFilePath.lastIndexOf("/"));
}
exports.getProjectEnvironmentPath = getProjectEnvironmentPath;
function updateEnvironment(envPath, insertion) {
return (tree, _context) => {
const sourceFile = readIntoSourceFile(tree, envPath);
// verify insertion does not already exist
const sourceFileText = sourceFile.getText();
if (sourceFileText.includes(insertion)) {
return;
}
// get the array of top-level Node objects in the AST from the SourceFile
const nodes = schematics_utilities_1.getSourceNodes(sourceFile);
const start = nodes.find(node => node.kind === version_agnostic_typescript_1.ts.SyntaxKind.OpenBraceToken);
const end = nodes.find(node => node.kind === version_agnostic_typescript_1.ts.SyntaxKind.CloseBraceToken, start.end);
const recorder = tree.beginUpdate(envPath);
recorder.insertLeft(end.pos, insertion);
tree.commitUpdate(recorder);
return tree;
};
}
function getProjectEnvironmentFile(project) {
const configurations = project_targets_1.getProjectTargetConfigurations(project, 'build');
if (!configurations.production ||
!configurations.production.fileReplacements ||
configurations.production.fileReplacements.length === 0) {
throw new schematics_1.SchematicsException(`Could not find the configuration of the workspace config (${project.sourceRoot})`);
}
const fileReplacements = configurations.production.fileReplacements;
const fileReplacement = fileReplacements.find(replacement => /environment\.ts$/.test(replacement.replace));
if (fileReplacement === undefined) {
throw new schematics_1.SchematicsException(`Could not find the environment file replacement configuration of the workspace config (${project.sourceRoot})`);
}
return fileReplacement.replace;
}
function readIntoSourceFile(host, modulePath) {
const text = host.read(modulePath);
if (text === null) {
throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
}
return version_agnostic_typescript_1.ts.createSourceFile(modulePath, text.toString('utf-8'), version_agnostic_typescript_1.ts.ScriptTarget.Latest, true);
}
//# sourceMappingURL=environment.js.map