UNPKG

@storm-software/workspace-tools

Version:

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

326 lines (303 loc) • 9.89 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _chunkWCTC7R27js = require('../../../chunk-WCTC7R27.js'); require('../../../chunk-HXQRGLII.js'); require('../../../chunk-BSW6NZVB.js'); var _chunkIHMF6RANjs = require('../../../chunk-IHMF6RAN.js'); require('../../../chunk-LMYTVXGX.js'); var _chunkCQDBLKPFjs = require('../../../chunk-CQDBLKPF.js'); // src/executors/clean-package/executor.ts var _devkit = require('@nx/devkit'); var _fsextra = require('fs-extra'); var _glob = require('glob'); // src/executors/clean-package/constants.ts var IGNORE_FILES = [ ".circleci", ".DS_Store", ".editorconfig", ".eslintignore", ".flowconfig", ".git", ".github", ".travis.yml", ".vscode", ".watchmanconfig", ".yarnrc", "appveyor.yml", "karma.conf.js", "node_modules", "package-lock.json", "pnpm-debug.log", "pnpm-lock.yaml", ".devcontainer.json", "test", "yarn-error.log", "yarn.lock", /^(babel|commitlint|jest|vitest)\.config\.[cm]?(j|t)s$/, /^\.?nano-staged\.(c|m)?js(on)?$/, /^\.?simple-git-hooks.js(on)?$/, /^\.?simple-pre-commit.js(on)?$/, /^\.babelrc/, /^\.clean-publish/, /^\.commitlintrc/, /^\.eslintrc/, /^\.jsconfig/, /^\.jsdocrc/, /^\.lintstagedrc/, /^\.nanostagedrc/, /^\.prettierrc/, /^\.remarkrc/, /^\.renovaterc/, /^\.size-limit/, /^\.yaspellerrc/, /^changelog/i, /eslint.config.(c|m)?js/, /README\.[\w-]+\.md/ ]; var IGNORE_FIELDS = [ "babel", "browserslist", "c8", "clean-publish", "commitlint", "devDependencies", "eslintConfig", "eslintIgnore", "husky", "jest", "lint-staged", "nano-staged", "pre-commit", "prettier", "pwmetrics", "remarkConfig", "renovate", "resolutions", "sharec", "simple-git-hooks", "simple-pre-commit", "size-limit", "storm", "typeCoverage", "yaspeller" ]; var NPM_SCRIPTS = [ "postinstall", "postpack", "postpublish", "postversion", "prepublish", "publish", "uninstall", "version" ]; var PUBLISH_CONFIG_FIELDS = [ "bin", "main", "exports", "types", "typings", "module", "browser", "esnext", "es2015", "unpkg", "umd:main", "typesVersions", "cpu", "os" ]; // src/executors/clean-package/utils.ts var _micromatch = require('micromatch'); var _micromatch2 = _interopRequireDefault(_micromatch); var _path = require('path'); function isObject(object) { return Boolean(object) && typeof object === "object"; } function filterObjectByKey(object, filterByKey = (key) => true, deep = false) { const result = {}; let changed = false; for (const key in object) { if (filterByKey(key)) { if (deep && isObject(object[key])) { result[key] = filterObjectByKey(object[key], filterByKey, deep); if (result[key] !== object[key]) { changed = true; } } else { result[key] = object[key]; } } else { changed = true; } } return changed ? result : object; } function createIgnoreMatcher(ignorePattern = "**/*") { if (ignorePattern instanceof RegExp) { return (filename) => !ignorePattern.test(filename); } if (_glob.hasMagic.call(void 0, ignorePattern, { magicalBraces: true })) { const isMatch = _micromatch2.default.matcher(ignorePattern); return (_filename, path) => !isMatch(path); } return (filename) => filename !== ignorePattern; } async function createFilesFilter(ignoreFiles = "", cwd) { let ignoreFilesList = []; if (ignoreFiles) { const glob = new (0, _glob.Glob)(ignoreFiles, { cwd }); ignoreFilesList = await glob.walk(); } const ignorePatterns = ignoreFilesList.length > 0 ? IGNORE_FILES.concat(...ignoreFilesList).filter(Boolean) : IGNORE_FILES; const filter = ignorePatterns.reduce( (next, ignorePattern) => { const ignoreMatcher = createIgnoreMatcher(ignorePattern); if (!next) { return ignoreMatcher; } return (filename, path) => ignoreMatcher(filename, path) && next && next(filename, path); }, null ); return (path) => { const filename = _path.basename.call(void 0, path); return _optionalChain([filter, 'optionalCall', _2 => _2(filename, path)]); }; } // src/executors/clean-package/executor.ts async function cleanPackageExecutorFn(options, context, config) { const tempDirectoryName = _devkit.joinPathFragments.call(void 0, config.workspaceRoot, "tmp", context.root ); const exists = await _fsextra.pathExists.call(void 0, tempDirectoryName); if (exists) { _chunkIHMF6RANjs.writeInfo.call(void 0, `\u{1F9F9} Cleaning temporary directory path: ${tempDirectoryName}`, config ); await _fsextra.remove.call(void 0, tempDirectoryName); } await _fsextra.mkdir.call(void 0, tempDirectoryName); _chunkIHMF6RANjs.writeInfo.call(void 0, `Created temporary directory: ${tempDirectoryName}`, config); const packageJson = await _fsextra.readJson.call(void 0, options.packageJsonPath); await _fsextra.copy.call(void 0, options.outputPath, tempDirectoryName, { filter: createFilesFilter(options.ignoredFiles, options.outputPath) }); if (options.cleanReadMe) { await cleanReadMe( tempDirectoryName, packageJson.repository, packageJson.homepage ); } if (options.cleanComments) { await cleanComments(tempDirectoryName); } const tempPackageJson = clearPackageJSON(packageJson, options.fields); _fsextra.writeJson.call(void 0, _devkit.joinPathFragments.call(void 0, tempDirectoryName, "package.json"), tempPackageJson ); await _fsextra.copy.call(void 0, tempDirectoryName, options.outputPath, { override: true, preserveTimestamps: true }); await _fsextra.remove.call(void 0, tempDirectoryName); return { success: true }; } function getReadmeUrlFromRepository(repository) { const repoUrl = typeof repository === "object" ? repository.url : repository; if (repoUrl) { const name = _optionalChain([repoUrl, 'access', _3 => _3.match, 'call', _4 => _4(/[^/:]+\/[^/:]+$/), 'optionalAccess', _5 => _5[0], 'optionalAccess', _6 => _6.replace, 'call', _7 => _7(/\.git$/, "")]); return `https://github.com/${name}#README.md`; } return null; } async function cleanReadMe(directoryName, repository, homepage) { const readmePath = _devkit.joinPathFragments.call(void 0, directoryName, "README.md"); const readme = await _fsextra.readFile.call(void 0, readmePath); const readmeUrl = getReadmeUrlFromRepository(repository); if (homepage || readmeUrl) { const cleaned = readme.toString().split(/\n##\s*\w/m)[0] + ` ## Documentation Read full docs **[here](${homepage || readmeUrl})**. `; await _fsextra.writeFile.call(void 0, readmePath, cleaned); } } async function cleanComments(directoryName) { const glob = new (0, _glob.Glob)(["**/*.js"], { cwd: directoryName }); const files = await glob.walk(); await Promise.all( files.map(async (i) => { const file = _devkit.joinPathFragments.call(void 0, directoryName, i); const content = await _fsextra.readFile.call(void 0, file); const cleaned = content.toString().replace(/\s*\/\/.*\n/gm, "\n").replace(/\s*\/\*[^/]+\*\/\n?/gm, "\n").replace(/\n+/gm, "\n").replace(/^\n+/gm, ""); await _fsextra.writeFile.call(void 0, file, cleaned); }) ); } function clearPackageJSON(packageJson, inputIgnoreFields) { const ignoreFields = inputIgnoreFields ? IGNORE_FIELDS.concat(inputIgnoreFields) : IGNORE_FIELDS; if (!packageJson.publishConfig) { return packageJson; } const publishConfig = { ...packageJson.publishConfig }; PUBLISH_CONFIG_FIELDS.forEach((field) => { if (publishConfig[field]) { packageJson[field] = publishConfig[field]; delete publishConfig[field]; } }); if (!Object.keys(publishConfig).length) { const { publishConfig: _, ...pkg } = packageJson; return pkg; } const cleanPackageJSON = filterObjectByKey( { ...packageJson, publishConfig }, (key) => !!(key && !ignoreFields.includes(key) && key !== "scripts") ); if (packageJson.scripts && !ignoreFields.includes("scripts")) { cleanPackageJSON.scripts = filterObjectByKey( packageJson.scripts, (script) => !!(script && NPM_SCRIPTS.includes(script)) ); if (cleanPackageJSON.scripts.publish && /^clean-publish( |$)/.test(cleanPackageJSON.scripts.publish)) { delete cleanPackageJSON.scripts.publish; } } for (const i in cleanPackageJSON) { if (isObject(cleanPackageJSON[i]) && Object.keys(cleanPackageJSON[i]).length === 0) { delete cleanPackageJSON[i]; } } return cleanPackageJSON; } var executor_default = _chunkWCTC7R27js.withRunExecutor.call(void 0, "Storm Clean-Package executor", cleanPackageExecutorFn, { skipReadingConfig: false, hooks: { applyDefaultOptions: (options) => { options.outputPath ??= "dist/{projectRoot}"; options.packageJsonPath ??= _chunkCQDBLKPFjs.joinPaths.call(void 0, options.outputPath, "package.json" ); options.cleanReadMe ??= true; options.cleanComments ??= true; return options; } } } ); exports.cleanPackageExecutorFn = cleanPackageExecutorFn; exports.default = executor_default;