UNPKG

@storm-software/workspace-tools

Version:

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

365 lines (328 loc) • 13.8 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 _chunkRB72JOT5js = require('./chunk-RB72JOT5.js'); var _chunkWCTC7R27js = require('./chunk-WCTC7R27.js'); var _chunkBSW6NZVBjs = require('./chunk-BSW6NZVB.js'); var _chunkIHMF6RANjs = require('./chunk-IHMF6RAN.js'); var _chunkLMYTVXGXjs = require('./chunk-LMYTVXGX.js'); var _chunkCQDBLKPFjs = require('./chunk-CQDBLKPF.js'); // ../tsdown/src/build.ts var _devkit = require('@nx/devkit'); var _defu = require('defu'); var _defu2 = _interopRequireDefault(_defu); var _fs = require('fs'); var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(_promises); var _tsdown = require('tsdown'); // ../tsdown/src/clean.ts async function cleanDirectories(name = "TSDown", directory, config) { await _promises.rm.call(void 0, directory, { recursive: true, force: true }); } // ../tsdown/src/config.ts function getDefaultOptions(config) { return { entry: ["./src/*.ts"], platform: "node", target: "esnext", mode: "production", dts: true, unused: { level: "error", ignore: ["typescript"] }, publint: true, fixedExtension: true, ...config }; } function toTSDownFormat(format) { if (!format || Array.isArray(format) && format.length === 0) { return ["cjs", "es"]; } else if (format === "esm") { return "es"; } else if (Array.isArray(format)) { return format.map((f) => f === "esm" ? "es" : f); } return format; } // ../tsdown/src/build.ts var resolveOptions = async (userOptions) => { const options = getDefaultOptions(userOptions); const workspaceRoot = _chunkLMYTVXGXjs.findWorkspaceRoot.call(void 0, options.projectRoot); if (!workspaceRoot) { throw new Error("Cannot find Nx workspace root"); } const workspaceConfig = await _chunkBSW6NZVBjs.getWorkspaceConfig.call(void 0, options.debug === true, { workspaceRoot }); _chunkIHMF6RANjs.writeDebug.call(void 0, " \u2699\uFE0F Resolving build options", workspaceConfig); const stopwatch = _chunkIHMF6RANjs.getStopwatch.call(void 0, "Build options resolution"); const projectGraph = await _devkit.createProjectGraphAsync.call(void 0, { exitOnError: true }); const projectJsonPath = _chunkCQDBLKPFjs.joinPaths.call(void 0, workspaceRoot, options.projectRoot, "project.json" ); if (!_fs.existsSync.call(void 0, projectJsonPath)) { throw new Error("Cannot find project.json configuration"); } const projectJsonFile = await _promises2.default.readFile(projectJsonPath, "utf8"); const projectJson = JSON.parse(projectJsonFile); const projectName = projectJson.name; const projectConfigurations = _devkit.readProjectsConfigurationFromProjectGraph.call(void 0, projectGraph); if (!_optionalChain([projectConfigurations, 'optionalAccess', _ => _.projects, 'optionalAccess', _2 => _2[projectName]])) { throw new Error( "The Build process failed because the project does not have a valid configuration in the project.json file. Check if the file exists in the root of the project." ); } const packageJsonPath = _chunkCQDBLKPFjs.joinPaths.call(void 0, workspaceRoot, options.projectRoot, "package.json" ); if (!_fs.existsSync.call(void 0, packageJsonPath)) { throw new Error("Cannot find package.json configuration"); } const debug = _nullishCoalesce(options.debug, () => ( (options.mode || workspaceConfig.mode) === "development")); const sourceRoot = projectJson.sourceRoot || _chunkCQDBLKPFjs.joinPaths.call(void 0, options.projectRoot, "src"); const result = { name: projectName, mode: "production", target: _chunkRB72JOT5js.DEFAULT_TARGET, generatePackageJson: true, outDir: _chunkCQDBLKPFjs.joinPaths.call(void 0, "dist", options.projectRoot), minify: !debug, plugins: [], assets: [], dts: true, shims: true, silent: !debug, logLevel: workspaceConfig.logLevel === "success" || workspaceConfig.logLevel === "performance" || workspaceConfig.logLevel === "debug" || workspaceConfig.logLevel === "trace" || workspaceConfig.logLevel === "all" ? "info" : workspaceConfig.logLevel === "fatal" ? "error" : workspaceConfig.logLevel, sourcemap: debug ? "inline" : false, clean: false, fixedExtension: true, nodeProtocol: true, tsconfig: _chunkCQDBLKPFjs.joinPaths.call(void 0, options.projectRoot, "tsconfig.json"), debug, sourceRoot, cwd: workspaceConfig.workspaceRoot, entry: { ["index"]: _chunkCQDBLKPFjs.joinPaths.call(void 0, sourceRoot, "index.ts") }, workspace: true, ...options, treeshake: options.treeShaking !== false, format: toTSDownFormat(options.format), workspaceConfig, projectName, projectGraph, projectConfigurations }; result.env = _defu2.default.call(void 0, options.env, _chunkRB72JOT5js.getEnv.call(void 0, "tsdown", result) ); stopwatch(); return result; }; async function generatePackageJson(options) { if (options.generatePackageJson !== false && _fs.existsSync.call(void 0, _chunkCQDBLKPFjs.joinPaths.call(void 0, options.projectRoot, "package.json"))) { _chunkIHMF6RANjs.writeDebug.call(void 0, " \u270D\uFE0F Writing package.json file", options.workspaceConfig); const stopwatch = _chunkIHMF6RANjs.getStopwatch.call(void 0, "Write package.json file"); const packageJsonPath = _chunkCQDBLKPFjs.joinPaths.call(void 0, options.projectRoot, "project.json"); if (!_fs.existsSync.call(void 0, packageJsonPath)) { throw new Error("Cannot find package.json configuration"); } const packageJsonFile = await _promises2.default.readFile( _chunkCQDBLKPFjs.joinPaths.call(void 0, options.workspaceConfig.workspaceRoot, options.projectRoot, "package.json" ), "utf8" ); if (!packageJsonFile) { throw new Error("Cannot find package.json configuration file"); } let packageJson = JSON.parse(packageJsonFile); packageJson = await _chunkRB72JOT5js.addPackageDependencies.call(void 0, options.workspaceConfig.workspaceRoot, options.projectRoot, options.projectName, packageJson ); packageJson = await _chunkRB72JOT5js.addWorkspacePackageJsonFields.call(void 0, options.workspaceConfig, options.projectRoot, options.sourceRoot, options.projectName, false, packageJson ); packageJson.exports ??= {}; packageJson.exports["./package.json"] ??= "./package.json"; packageJson.exports["."] ??= _chunkRB72JOT5js.addPackageJsonExport.call(void 0, "index", packageJson.type, options.sourceRoot ); let entry = [{ in: "./src/index.ts", out: "./src/index.ts" }]; if (options.entry) { if (Array.isArray(options.entry)) { entry = options.entry.map( (entryPoint) => typeof entryPoint === "string" ? { in: entryPoint, out: entryPoint } : entryPoint ); } for (const entryPoint of entry) { const split = entryPoint.out.split("."); split.pop(); const entry2 = split.join(".").replaceAll("\\", "/"); packageJson.exports[`./${entry2}`] ??= _chunkRB72JOT5js.addPackageJsonExport.call(void 0, entry2, options.fixedExtension ? "fixed" : packageJson.type, options.sourceRoot ); } } packageJson.main = !options.fixedExtension && packageJson.type === "commonjs" ? "./dist/index.js" : "./dist/index.cjs"; packageJson.module = !options.fixedExtension && packageJson.type === "module" ? "./dist/index.js" : "./dist/index.mjs"; packageJson.types = `./dist/index.d.${!options.fixedExtension ? "ts" : "mts"}`; packageJson.exports = Object.keys(packageJson.exports).reduce( (ret, key) => { if (key.endsWith("/index") && !ret[key.replace("/index", "")]) { ret[key.replace("/index", "")] = packageJson.exports[key]; } return ret; }, packageJson.exports ); await _devkit.writeJsonFile.call(void 0, _chunkCQDBLKPFjs.joinPaths.call(void 0, options.outDir, "package.json"), packageJson); stopwatch(); } return options; } async function executeTSDown(options) { _chunkIHMF6RANjs.writeDebug.call(void 0, ` \u{1F680} Running ${options.name} build`, options.workspaceConfig); const stopwatch = _chunkIHMF6RANjs.getStopwatch.call(void 0, `${options.name} build`); await _tsdown.build.call(void 0, { ...options, entry: options.entry, config: false }); stopwatch(); return options; } async function copyBuildAssets(options) { _chunkIHMF6RANjs.writeDebug.call(void 0, ` \u{1F4CB} Copying asset files to output directory: ${options.outDir}`, options.workspaceConfig ); const stopwatch = _chunkIHMF6RANjs.getStopwatch.call(void 0, `${options.name} asset copy`); await _chunkRB72JOT5js.copyAssets.call(void 0, options.workspaceConfig, _nullishCoalesce(options.assets, () => ( [])), options.outDir, options.projectRoot, options.sourceRoot, true, false ); stopwatch(); return options; } async function reportResults(options) { _chunkIHMF6RANjs.writeSuccess.call(void 0, ` \u{1F4E6} The ${options.name} build completed successfully`, options.workspaceConfig ); } async function cleanOutputPath(options) { if (options.clean !== false && options.workspaceConfig) { _chunkIHMF6RANjs.writeDebug.call(void 0, ` \u{1F9F9} Cleaning ${options.name} output path: ${options.workspaceConfig}`, options.workspaceConfig ); const stopwatch = _chunkIHMF6RANjs.getStopwatch.call(void 0, `${options.name} output clean`); await cleanDirectories( options.name, options.outDir, options.workspaceConfig ); stopwatch(); } return options; } async function build(options) { _chunkIHMF6RANjs.writeDebug.call(void 0, ` ${_chunkIHMF6RANjs.brandIcon.call(void 0, )} Executing Storm TSDown pipeline`); const stopwatch = _chunkIHMF6RANjs.getStopwatch.call(void 0, "TSDown pipeline"); try { const opts = Array.isArray(options) ? options : [options]; if (opts.length === 0) { throw new Error("No build options were provided"); } const resolved = await Promise.all( opts.map(async (opt) => await resolveOptions(opt)) ); if (resolved.length > 0) { await cleanOutputPath(resolved[0]); await generatePackageJson(resolved[0]); await Promise.all( resolved.map(async (opt) => { await executeTSDown(opt); await copyBuildAssets(opt); await reportResults(opt); }) ); } else { _chunkIHMF6RANjs.writeWarning.call(void 0, " \u{1F6A7} No options were passed to TSBuild. Please check the parameters passed to the `build` function." ); } _chunkIHMF6RANjs.writeSuccess.call(void 0, " \u{1F3C1} TSDown pipeline build completed successfully"); } catch (error) { _chunkIHMF6RANjs.writeFatal.call(void 0, "Fatal errors that the build process could not recover from have occured. The build process has been terminated." ); throw error; } finally { stopwatch(); } } // src/executors/tsdown/executor.ts async function tsdownExecutorFn(options, context, config) { _chunkIHMF6RANjs.writeInfo.call(void 0, "\u{1F4E6} Running Storm TSDown executor on the workspace", config); if (!_optionalChain([context, 'access', _3 => _3.projectsConfigurations, 'optionalAccess', _4 => _4.projects]) || !context.projectName || !context.projectsConfigurations.projects[context.projectName] || !_optionalChain([context, 'access', _5 => _5.projectsConfigurations, 'access', _6 => _6.projects, 'access', _7 => _7[context.projectName], 'optionalAccess', _8 => _8.root])) { throw new Error( "The Build process failed because the context is not valid. Please run this command from a workspace." ); } await build({ ...options, projectRoot: ( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion _optionalChain([context, 'access', _9 => _9.projectsConfigurations, 'access', _10 => _10.projects, 'optionalAccess', _11 => _11[context.projectName], 'access', _12 => _12.root]) ), name: context.projectName, sourceRoot: _optionalChain([context, 'access', _13 => _13.projectsConfigurations, 'access', _14 => _14.projects, 'optionalAccess', _15 => _15[context.projectName], 'optionalAccess', _16 => _16.sourceRoot]), format: options.format, platform: options.platform }); return { success: true }; } var executor_default = _chunkWCTC7R27js.withRunExecutor.call(void 0, "Storm TSDown build", tsdownExecutorFn, { skipReadingConfig: false, hooks: { applyDefaultOptions: async (options) => { options.entry ??= ["src/index.ts"]; options.outputPath ??= "dist/{projectRoot}"; options.tsconfig ??= "{projectRoot}/tsconfig.json"; return options; } } } ); exports.tsdownExecutorFn = tsdownExecutorFn; exports.executor_default = executor_default;