UNPKG

@storm-software/workspace-tools

Version:

Tools for managing a Storm workspace, including various Nx generators and executors for common development tasks.

313 lines (297 loc) • 12.7 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkHI4G4OOGjs = require('./chunk-HI4G4OOG.js'); var _chunk7YRW5HNXjs = require('./chunk-7YRW5HNX.js'); var _chunkJTAXCQX6js = require('./chunk-JTAXCQX6.js'); var _chunk3GQAWCBQjs = require('./chunk-3GQAWCBQ.js'); // src/base/typescript-library-generator.ts var _devkit = require('@nx/devkit'); var _projectnameandrootutils = require('@nx/devkit/src/generators/project-name-and-root-utils'); var _js = require('@nx/js'); var _init = require('@nx/js/src/generators/init/init'); var _init2 = _interopRequireDefault(_init); var _generator = require('@nx/js/src/generators/setup-verdaccio/generator'); var _generator2 = _interopRequireDefault(_generator); async function typeScriptLibraryGeneratorFn(tree, options, config) { const normalized = await normalizeOptions(tree, { ...options }); const tasks = []; tasks.push(await _init2.default.call(void 0, tree, { ...normalized, tsConfigName: normalized.rootProject ? "tsconfig.json" : "tsconfig.base.json" })); tasks.push(_devkit.addDependenciesToPackageJson.call(void 0, tree, {}, { "@storm-software/workspace-tools": "latest", "@storm-software/testing-tools": "latest", ..._nullishCoalesce(options.devDependencies, () => ( {})) })); if (normalized.publishable) { tasks.push(await _generator2.default.call(void 0, tree, { ...normalized, skipFormat: true })); } const projectConfig = { root: normalized.directory, projectType: "library", sourceRoot: _chunkJTAXCQX6js.joinPaths.call(void 0, _nullishCoalesce(normalized.directory, () => ( "")), "src"), targets: { build: { executor: options.buildExecutor, outputs: [ "{options.outputPath}" ], options: { entry: [ _chunkJTAXCQX6js.joinPaths.call(void 0, normalized.projectRoot, "src", "index.ts") ], outputPath: getOutputPath(normalized), tsconfig: _chunkJTAXCQX6js.joinPaths.call(void 0, normalized.projectRoot, "tsconfig.json"), project: _chunkJTAXCQX6js.joinPaths.call(void 0, normalized.projectRoot, "package.json"), defaultConfiguration: "production", platform: "neutral", assets: [ { input: normalized.projectRoot, glob: "*.md", output: "/" }, { input: "", glob: "LICENSE", output: "/" } ] }, configurations: { production: { debug: false, verbose: false }, development: { debug: true, verbose: true } } } } }; if (options.platform) { projectConfig.targets.build.options.platform = options.platform === "worker" ? "node" : options.platform; } _chunk7YRW5HNXjs.addProjectTag.call(void 0, projectConfig, _chunk7YRW5HNXjs.ProjectTagConstants.Platform.TAG_ID, options.platform === "node" ? _chunk7YRW5HNXjs.ProjectTagConstants.Platform.NODE : options.platform === "worker" ? _chunk7YRW5HNXjs.ProjectTagConstants.Platform.WORKER : options.platform === "browser" ? _chunk7YRW5HNXjs.ProjectTagConstants.Platform.BROWSER : _chunk7YRW5HNXjs.ProjectTagConstants.Platform.NEUTRAL, { overwrite: false }); createProjectTsConfigJson(tree, normalized); _devkit.addProjectConfiguration.call(void 0, tree, normalized.name, projectConfig); let repository = { type: "github", url: _optionalChain([config, 'optionalAccess', _ => _.repository]) || `https://github.com/${_optionalChain([config, 'optionalAccess', _2 => _2.organization]) || "storm-software"}/${_optionalChain([config, 'optionalAccess', _3 => _3.namespace]) || _optionalChain([config, 'optionalAccess', _4 => _4.name]) || "repository"}.git` }; let description = options.description || "A package developed by Storm Software used to create modern, scalable web applications."; if (tree.exists("package.json")) { const packageJson = _devkit.readJson.call(void 0, tree, "package.json"); if (_optionalChain([packageJson, 'optionalAccess', _5 => _5.repository])) { repository = packageJson.repository; } if (_optionalChain([packageJson, 'optionalAccess', _6 => _6.description])) { description = packageJson.description; } } if (!normalized.importPath) { normalized.importPath = normalized.name; } const packageJsonPath = _chunkJTAXCQX6js.joinPaths.call(void 0, normalized.projectRoot, "package.json"); if (tree.exists(packageJsonPath)) { _devkit.updateJson.call(void 0, tree, packageJsonPath, (json) => { if (!normalized.importPath) { normalized.importPath = normalized.name; } json.name = normalized.importPath; json.version = "0.0.1"; if (json.private && (normalized.publishable || normalized.rootProject)) { json.private = void 0; } return { ...json, version: "0.0.1", description, repository: { ...repository, directory: normalized.projectRoot }, type: "module", dependencies: { ...json.dependencies }, publishConfig: { access: "public" } }; }); } else { _devkit.writeJson.call(void 0, tree, packageJsonPath, { name: normalized.importPath, version: "0.0.1", description, repository: { ...repository, directory: normalized.projectRoot }, private: !normalized.publishable || normalized.rootProject, type: "module", publishConfig: { access: "public" } }); } if (tree.exists("package.json") && normalized.importPath) { _devkit.updateJson.call(void 0, tree, "package.json", (json) => ({ ...json, pnpm: { ..._optionalChain([json, 'optionalAccess', _7 => _7.pnpm]), overrides: { ..._optionalChain([json, 'optionalAccess', _8 => _8.pnpm, 'optionalAccess', _9 => _9.overrides]), [_nullishCoalesce(normalized.importPath, () => ( ""))]: "workspace:*" } } })); } _js.addTsConfigPath.call(void 0, tree, normalized.importPath, [ _chunkJTAXCQX6js.joinPaths.call(void 0, normalized.projectRoot, "./src", `index.${normalized.js ? "js" : "ts"}`) ]); _js.addTsConfigPath.call(void 0, tree, _chunkJTAXCQX6js.joinPaths.call(void 0, normalized.importPath, "/*"), [ _chunkJTAXCQX6js.joinPaths.call(void 0, normalized.projectRoot, "./src", "/*") ]); if (tree.exists("package.json")) { const packageJson = _devkit.readJson.call(void 0, tree, "package.json"); if (_optionalChain([packageJson, 'optionalAccess', _10 => _10.repository])) { repository = packageJson.repository; } if (_optionalChain([packageJson, 'optionalAccess', _11 => _11.description])) { description = packageJson.description; } } const tsconfigPath = _chunkJTAXCQX6js.joinPaths.call(void 0, normalized.projectRoot, "tsconfig.json"); if (tree.exists(tsconfigPath)) { _devkit.updateJson.call(void 0, tree, tsconfigPath, (json) => { json.composite ??= true; return json; }); } else { _devkit.writeJson.call(void 0, tree, tsconfigPath, { extends: `${_devkit.offsetFromRoot.call(void 0, normalized.projectRoot)}tsconfig.base.json`, composite: true, compilerOptions: { outDir: `${_devkit.offsetFromRoot.call(void 0, normalized.projectRoot)}dist/out-tsc` }, files: [], include: [ "src/**/*.ts", "src/**/*.js" ], exclude: [ "jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts" ] }); } await _devkit.formatFiles.call(void 0, tree); return null; } _chunk3GQAWCBQjs.__name.call(void 0, typeScriptLibraryGeneratorFn, "typeScriptLibraryGeneratorFn"); function getOutputPath(options) { const parts = [ "dist" ]; if (options.projectRoot === ".") { parts.push(options.name); } else { parts.push(options.projectRoot); } return _chunkJTAXCQX6js.joinPaths.call(void 0, ...parts); } _chunk3GQAWCBQjs.__name.call(void 0, getOutputPath, "getOutputPath"); function createProjectTsConfigJson(tree, options) { const tsconfig = { extends: options.rootProject ? void 0 : _js.getRelativePathToRootTsConfig.call(void 0, tree, options.projectRoot), ..._nullishCoalesce(_optionalChain([options, 'optionalAccess', _12 => _12.tsconfigOptions]), () => ( {})), compilerOptions: { ...options.rootProject ? _js.tsConfigBaseOptions : {}, outDir: _chunkJTAXCQX6js.joinPaths.call(void 0, _devkit.offsetFromRoot.call(void 0, options.projectRoot), "dist/out-tsc"), noEmit: true, ..._nullishCoalesce(_optionalChain([options, 'optionalAccess', _13 => _13.tsconfigOptions, 'optionalAccess', _14 => _14.compilerOptions]), () => ( {})) }, files: [ ..._nullishCoalesce(_optionalChain([options, 'optionalAccess', _15 => _15.tsconfigOptions, 'optionalAccess', _16 => _16.files]), () => ( [])) ], include: [ ..._nullishCoalesce(_optionalChain([options, 'optionalAccess', _17 => _17.tsconfigOptions, 'optionalAccess', _18 => _18.include]), () => ( [])), "src/**/*.ts", "src/**/*.js", "bin/**/*" ], exclude: [ ..._nullishCoalesce(_optionalChain([options, 'optionalAccess', _19 => _19.tsconfigOptions, 'optionalAccess', _20 => _20.exclude]), () => ( [])), "jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts" ] }; _devkit.writeJson.call(void 0, tree, _chunkJTAXCQX6js.joinPaths.call(void 0, options.projectRoot, "tsconfig.json"), tsconfig); } _chunk3GQAWCBQjs.__name.call(void 0, createProjectTsConfigJson, "createProjectTsConfigJson"); async function normalizeOptions(tree, options, config) { let importPath = options.importPath; if (!importPath && _optionalChain([config, 'optionalAccess', _21 => _21.namespace])) { importPath = `@${_optionalChain([config, 'optionalAccess', _22 => _22.namespace])}/${options.name}`; } if (options.publishable) { if (!importPath) { throw new Error(`For publishable libs you have to provide a proper "--importPath" which needs to be a valid npm package name (e.g. my-awesome-lib or @myorg/my-lib)`); } } let bundler = "tsc"; if (options.publishable === false && options.buildable === false) { bundler = "none"; } const { Linter } = _devkit.ensurePackage.call(void 0, "@nx/eslint", _chunkHI4G4OOGjs.nxVersion); const rootProject = false; const { projectName, names: projectNames, projectRoot, importPath: normalizedImportPath } = await _projectnameandrootutils.determineProjectNameAndRootOptions.call(void 0, tree, { name: options.name, projectType: "library", directory: options.directory, importPath, rootProject }); const normalized = _devkit.names.call(void 0, projectNames.projectFileName); const fileName = normalized.fileName; return { js: false, pascalCaseFiles: false, skipFormat: false, skipTsConfig: false, includeBabelRc: false, unitTestRunner: "jest", linter: Linter.EsLint, testEnvironment: "node", config: "project", compiler: "tsc", bundler, skipTypeCheck: false, minimal: false, hasPlugin: false, isUsingTsSolutionConfig: false, projectPackageManagerWorkspaceState: "included", ...options, fileName, name: projectName, projectNames, projectRoot, parsedTags: options.tags ? options.tags.split(",").map((s) => s.trim()) : [], importPath: normalizedImportPath, rootProject, shouldUseSwcJest: false }; } _chunk3GQAWCBQjs.__name.call(void 0, normalizeOptions, "normalizeOptions"); exports.typeScriptLibraryGeneratorFn = typeScriptLibraryGeneratorFn; exports.getOutputPath = getOutputPath; exports.createProjectTsConfigJson = createProjectTsConfigJson; exports.normalizeOptions = normalizeOptions;