UNPKG

@routineless/nx-aws-cdk

Version:
135 lines 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const devkit_1 = require("@nx/devkit"); const eslint_1 = require("@nx/eslint"); const fs_extra_1 = require("fs-extra"); const path_1 = require("path"); const aws_lambda_1 = tslib_1.__importDefault(require("../../generators/aws-lambda")); const cdk_application_1 = tslib_1.__importDefault(require("../../generators/cdk-application")); const versions_1 = require("../../utils/versions"); const workspace_1 = require("../../utils/workspace"); const normalizeOptions = (_tree, options) => { return { ...options, linter: options.linter ?? eslint_1.Linter.EsLint, unitTestRunner: options.unitTestRunner ?? 'jest', infraAppName: options.infraAppName ?? 'infra', }; }; const updateNxConfig = (tree) => { const nxJson = (0, devkit_1.readNxJson)(tree); if (!nxJson) { throw new Error('Failed to read nx.json'); } nxJson.plugins = nxJson.plugins ? [...nxJson.plugins, '@routineless/nx-aws-cdk'] : ['@routineless/nx-aws-cdk']; (0, devkit_1.updateNxJson)(tree, nxJson); }; const addDependencies = (host, normalizedOptions) => { let devDependencies = { '@tsconfig/strictest': versions_1.TSCONFIG_STRICTEST_VERSION, '@trivago/prettier-plugin-sort-imports': versions_1.PRETTIER_PLUGIN_SORT_IMPORTS_VERSION, }; if (normalizedOptions.linter === eslint_1.Linter.EsLint) { devDependencies = { ...devDependencies, 'jsonc-eslint-parser': versions_1.JSON_ESLINT_PARSER_VERSION, 'eslint-plugin-prettier': versions_1.ESLINT_PLUGIN_PRETTIER_VERSION, }; } return (0, devkit_1.addDependenciesToPackageJson)(host, {}, devDependencies); }; const addFiles = (tree, normalizedOptions) => { const scope = (0, workspace_1.getNpmScope)(tree); const templateOptions = { template: '', workspaceName: scope || 'aws-cdk-app', }; (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, 'generatorFiles', 'files'), '.', templateOptions); if (normalizedOptions.unitTestRunner === 'jest') { (0, devkit_1.generateFiles)(tree, (0, path_1.join)(__dirname, 'generatorFiles', 'jest-files'), '.', templateOptions); } }; const updatePackageJson = (tree, normalizedOptions) => { (0, devkit_1.updateJson)(tree, `package.json`, (json) => { json.scripts = json.scripts || {}; json.scripts['build'] = 'nx run-many --target=build'; if (normalizedOptions.unitTestRunner !== 'none') { json.scripts['test'] = 'nx run-many --target=test'; json.scripts['test:coverage'] = 'nx run-many --target=test --skip-nx-cache --output-style=static --coverage --passWithNoTests=false'; } if (normalizedOptions.linter !== eslint_1.Linter.None) { json.scripts['lint'] = 'nx run-many --target=lint'; } return json; }); }; const updateTsConfig = (tree) => { (0, devkit_1.updateJson)(tree, `tsconfig.base.json`, (tsConfig) => { //TODO extend node-lts after nx typescript 5 support tsConfig.extends = './node_modules/@tsconfig/strictest/tsconfig.json'; tsConfig.compilerOptions.resolveJsonModule = true; tsConfig.moduleResolution = 'node'; delete tsConfig.compilerOptions.skipLibCheck; return tsConfig; }); }; const updateLintConfig = (tree) => { (0, devkit_1.updateJson)(tree, `.eslintrc.json`, (config) => { config.plugins = config?.plugins || []; config.plugins.push('prettier'); if (config.overrides && config.overrides[0]) { const baseConfigOverride = config.overrides[0]; baseConfigOverride.rules['prettier/prettier'] = 'error'; config.overrides.push({ files: '*.json', parser: 'jsonc-eslint-parser', rules: {}, }); } return config; }); }; // delete redundant const deleteRedundantFiles = (tree, normalizedOptions) => { if (normalizedOptions.lambdaAppName) { tree.delete('apps/.gitkeep'); } // detelint .editorconfig using dirrect fs operation because it is used as formatting style source for prettier // it will be added after generator changes will be flushed (0, fs_extra_1.removeSync)((0, path_1.join)(tree.root, '.editorconfig')); }; const presetGenerator = async (tree, options) => { const normalizedOptions = normalizeOptions(tree, options); const tasks = []; updateNxConfig(tree); tasks.push(addDependencies(tree, normalizedOptions)); tasks.push(await (0, cdk_application_1.default)(tree, { ...normalizedOptions, name: normalizedOptions.infraAppName, setAsRoutinelessInfraApp: true, skipFormat: true, })); if (normalizedOptions.lambdaAppName) { tasks.push(await (0, aws_lambda_1.default)(tree, { ...normalizedOptions, name: normalizedOptions.lambdaAppName, addLambdaToInfraApp: true, skipFormat: true, })); } if (normalizedOptions.linter === eslint_1.Linter.EsLint) { updateLintConfig(tree); } updateTsConfig(tree); updatePackageJson(tree, normalizedOptions); addFiles(tree, normalizedOptions); deleteRedundantFiles(tree, normalizedOptions); if (!normalizedOptions.skipFormat) { await (0, devkit_1.formatFiles)(tree); } return (0, devkit_1.runTasksInSerial)(...tasks); }; exports.default = presetGenerator; //# sourceMappingURL=index.js.map