UNPKG

@dev-thought/nx-deploy-it

Version:

[![npm version](https://badge.fury.io/js/%40dev-thought%2Fnx-deploy-it.svg)](https://www.npmjs.com/package/@dev-thought/nx-deploy-it) [![The MIT License](https://img.shields.io/badge/license-MIT-orange.svg?color=blue&style=flat-square)](http://opensource.

148 lines 6.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateProject = void 0; const tslib_1 = require("tslib"); const schematics_1 = require("@angular-devkit/schematics"); const workspace_1 = require("@nrwl/workspace"); const child_process_1 = require("child_process"); const path_1 = require("path"); const provider_1 = require("../../utils/provider"); const workspace_2 = require("../../utils/workspace"); const fs_1 = require("fs"); function updateProject(adapter) { return () => tslib_1.__awaiter(this, void 0, void 0, function* () { return schematics_1.chain([ workspace_1.updateWorkspace((workspace) => { const project = workspace.projects.get(adapter.options.project); project.targets.add(Object.assign({ name: 'deploy' }, adapter.getDeployActionConfiguration())); project.targets.add(Object.assign({ name: 'destroy' }, adapter.getDestroyActionConfiguration())); }), workspace_1.updateJsonInTree(path_1.join(adapter.project.root, 'tsconfig.app.json'), (json) => { const exclude = json.exclude; const excludePaths = 'infrastructure/**/*.ts'; if (!exclude) { json.exclude = [excludePaths]; } else { exclude.push(excludePaths); } return json; }), ]); }); } exports.updateProject = updateProject; function addDependenciesFromPulumiProjectToPackageJson(adapter) { return (host) => { const pulumiPackageJson = host.read(path_1.join(adapter.project.root, 'infrastructure', 'package.json')); if (!pulumiPackageJson) { throw new Error('Can not find generated pulumi package.json'); } const pulumiCloudProviderDependencies = JSON.parse(pulumiPackageJson.toString()).dependencies; const packageJson = workspace_1.readJsonInTree(host, 'package.json'); const dependencyList = []; for (const name in pulumiCloudProviderDependencies) { const version = pulumiCloudProviderDependencies[name]; if (version) { if (!packageJson.dependencies[name]) { dependencyList.push({ name, version, }); } } } dependencyList.push(...adapter.addRequiredDependencies()); if (!dependencyList.length) { return schematics_1.noop(); } return workspace_1.addDepsToPackageJson(dependencyList.reduce((dictionary, value) => { dictionary[value.name] = value.version; return dictionary; }, {}), {}); }; } function generateNewPulumiProject(adapter) { return () => { const template = provider_1.getCloudTemplateName(adapter.options.provider); const args = [ 'new', template, '--name', adapter.options.project, '--dir', path_1.resolve(path_1.join(adapter.project.root, 'infrastructure')), '--description', 'Infrastructure as Code based on Pulumi - managed by @dev-thought/nx-deploy-it', '--generate-only', '--yes', ]; const data = child_process_1.spawnSync(workspace_2.getPulumiBinaryPath(), args, { env: Object.assign(Object.assign({}, process.env), { PULUMI_SKIP_UPDATE_CHECK: '1' }), }); return addDependenciesFromPulumiProjectToPackageJson(adapter); }; } function mergePulumiProjectIntoTree(adapter) { return (host) => { const infraDir = path_1.join(adapter.project.root, 'infrastructure'); const PulumiFile = path_1.join(infraDir, 'Pulumi.yaml'); const pulumiContent = fs_1.readFileSync(PulumiFile); fs_1.unlinkSync(PulumiFile); host.create(PulumiFile, pulumiContent); return host; }; } function cleanupTempPulumiProject(adapter) { return (host) => { const infraDir = path_1.join(adapter.project.root, 'infrastructure'); fs_1.unlinkSync(path_1.resolve(infraDir, '.gitignore')); fs_1.unlinkSync(path_1.resolve(infraDir, 'index.ts')); fs_1.unlinkSync(path_1.resolve(infraDir, 'tsconfig.json')); fs_1.unlinkSync(path_1.resolve(infraDir, 'package.json')); return host; }; } function generateInfrastructureCode(adapter) { return (host, context) => { const template = adapter.getApplicationTypeTemplate(); if (!template) { throw new Error(`Can't find a supported build target for the project`); } const templateSource = schematics_1.apply(schematics_1.url(`./files/${adapter.getApplicationTemplatePath()}`), [template, schematics_1.move(path_1.join(adapter.project.root))]); const rule = schematics_1.chain([schematics_1.branchAndMerge(schematics_1.chain([schematics_1.mergeWith(templateSource)]))]); return rule(host, context); }; } function initializeCloudProviderApplication(adapter) { return schematics_1.chain([ generateNewPulumiProject(adapter), mergePulumiProjectIntoTree(adapter), cleanupTempPulumiProject(adapter), generateInfrastructureCode(adapter), updateProject(adapter), ]); } function default_1(options) { return (host, context) => tslib_1.__awaiter(this, void 0, void 0, function* () { const workspace = yield workspace_1.getWorkspace(host); const project = workspace.projects.get(options.project); if (!project) { context.logger.error(`Project doesn't exist`); return schematics_1.chain([]); } if (project.targets.has('deploy')) { context.logger.error(`Your project is already configured with a deploy job`); return schematics_1.chain([]); } if (host.exists(path_1.join(project.root, 'infrastructure', 'Pulumi.yaml'))) { context.logger.error(`This project already has an infrastructure`); return schematics_1.chain([]); } const adapter = workspace_2.getAdapter(project, options, host); yield adapter.extendOptionsByUserInput(); return schematics_1.chain([initializeCloudProviderApplication(adapter)]); }); } exports.default = default_1; //# sourceMappingURL=schematic.js.map