From 8147b726f4da16b5bf21af763f61b7bebf6430af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgardo=20Avile=CC=81s-Lo=CC=81pez?= Date: Wed, 6 Oct 2021 17:16:52 -0400 Subject: [PATCH] Introduce a clean-types command --- bin/cli.js | 9 +++-- lib/commands/build-types.js | 10 +---- lib/commands/check-types.js | 3 -- lib/commands/clean-types.js | 19 +++++++++ lib/utils.js | 30 ++++++++++++-- package-lock.json | 78 +++++++++++++++++------------------- package.json | 6 +-- shared/git-hooks/pre-commit | 5 ++- shared/git-hooks/pre-push | 2 +- types/jsdoc-to-markdown.d.ts | 1 + 10 files changed, 97 insertions(+), 66 deletions(-) create mode 100644 lib/commands/clean-types.js create mode 100644 types/jsdoc-to-markdown.d.ts diff --git a/bin/cli.js b/bin/cli.js index e4b0a38..5f44420 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -6,18 +6,19 @@ const chalk = require('chalk'); const path = require('path'); const yargs = require('yargs'); +const { description, homepage } = require('../package.json'); + (async function start() { const argv = process.argv.slice(2); - const header = 'Development framework by and for the Core Team.'; - const learnMore = 'Read the manual at https://core.lifion.oneadp.com/libs/core-commons'; + const learnMore = `Read the manual at ${homepage}`; const { npm_lifecycle_event: lifecycleEvent } = process.env; - if (lifecycleEvent) { + if (argv.length === 0 && lifecycleEvent) { argv.push(lifecycleEvent); } yargs(argv) - .usage(`${header}\n\n${chalk.bold('USAGE')}\n $0 [options]`) + .usage(`${description}\n\n${chalk.bold('USAGE')}\n $0 [options]`) .epilogue(`${chalk.bold('LEARN MORE')}\n ${learnMore}`) .commandDir(path.resolve(__dirname, '../lib/commands')) .demandCommand() diff --git a/lib/commands/build-types.js b/lib/commands/build-types.js index ca8c293..6255a6f 100644 --- a/lib/commands/build-types.js +++ b/lib/commands/build-types.js @@ -10,14 +10,8 @@ module.exports = { describe: 'Build the types with TypeScript.', handler: async () => { $.verbose = false; - await Promise.all( - ['./app', './bin', './lib'].map((i) => - $`find ${i} -name '*.d.ts' -exec rm {} \\;`.catch(() => {}) - ) - ); - if (process.argv.slice(-1)[0] === command) { - process.argv.splice(2, 1); - } + const cli = path.relative(process.cwd(), path.resolve(__dirname, '../../bin/cli.js')); + await $`${cli} clean-types`; process.argv.push('--emitDeclarationOnly'); require('typescript/bin/tsc'); } diff --git a/lib/commands/check-types.js b/lib/commands/check-types.js index 6189713..1c2084c 100644 --- a/lib/commands/check-types.js +++ b/lib/commands/check-types.js @@ -8,9 +8,6 @@ module.exports = { command, describe: 'Check the types with TypeScript.', handler: async () => { - if (process.argv.slice(-1)[0] === command) { - process.argv.splice(2, 1); - } process.argv.push('--noEmit'); require('typescript/bin/tsc'); } diff --git a/lib/commands/clean-types.js b/lib/commands/clean-types.js new file mode 100644 index 0000000..be33f3c --- /dev/null +++ b/lib/commands/clean-types.js @@ -0,0 +1,19 @@ +'use strict'; + +const path = require('path'); +const { $ } = require('zx'); + +const command = path.parse(__filename).name; + +module.exports = { + command, + describe: 'Delete all the types generated with TypeScript.', + handler: async () => { + $.verbose = false; + await Promise.all( + ['./app', './bin', './lib'].map((i) => + $`find ${i} -name '*.d.ts' -exec rm {} \\;`.catch(() => {}) + ) + ); + } +}; diff --git a/lib/utils.js b/lib/utils.js index c980f64..8b25ea4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,10 +2,12 @@ const path = require('path'); const { cosmiconfig } = require('cosmiconfig'); -const { readFile } = require('fs').promises; +const { readFile, writeFile } = require('fs').promises; const { name } = require('../package.json'); +const PKG_FILE = 'package.json'; + /** * Loads the configuration for this tool from the target package.json file. * @@ -15,7 +17,7 @@ async function loadConfig() { try { /** @type {*} */ const { filepath } = await cosmiconfig(name, { - searchPlaces: ['package.json'] + searchPlaces: [PKG_FILE] }).search(); const pkg = JSON.parse(await readFile(filepath, 'utf8')); return pkg[name] || {}; @@ -34,4 +36,26 @@ function pathFromCwd(...args) { return path.relative(process.cwd(), path.resolve(...args)); } -module.exports = { loadConfig, pathFromCwd }; +/** + * Loads the contents of the package.json file at the target folder. + * + * @returns {Promise<*>} + */ +async function loadPackage() { + const filePath = path.resolve(process.cwd(), PKG_FILE); + return JSON.parse(await readFile(filePath, 'utf8')); +} + +/** + * Stores the given object as the package.json file at the target folder. + * + * @param {*} pkg - The contents to store. + * @returns {Promise} + */ +async function storePackage(pkg) { + const filePath = path.resolve(process.cwd(), PKG_FILE); + const pkgStr = `${JSON.stringify(pkg, null, 2)}\n`; + return writeFile(filePath, pkgStr, 'utf8'); +} + +module.exports = { loadConfig, loadPackage, pathFromCwd, storePackage }; diff --git a/package-lock.json b/package-lock.json index 4c0fd60..72a3d7c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "@babel/code-frame": { - "version": "7.14.5", - "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.14.5.tgz", - "integrity": "sha512-9pzDqyc6OLDaqe+zbACgFkb6fKMNG6CObKpnYXChRsvYGyEdc7CA2BaqeOM+vOtCS5ndmJicPJhKAwYRI6UfFw==", + "version": "7.15.8", + "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.15.8.tgz", + "integrity": "sha512-2IAnmn8zbvC/jKYhq5Ki9I+DwjlrtMPUCH/CpHvqI4dNnlwHwsxoIhlc8WcYY5LSYknXQtAlFYuHfqAFCvQ4Wg==", "requires": { "@babel/highlight": "^7.14.5" } @@ -18,19 +18,19 @@ "integrity": "sha512-0NqAC1IJE0S0+lL1SWFMxMkz1pKCNCjI4tr2Zx4LJSXxCLAdr6KyArnY+sno5m3yH9g737ygOyPABDsnXkpxiA==" }, "@babel/core": { - "version": "7.15.5", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.15.5.tgz", - "integrity": "sha512-pYgXxiwAgQpgM1bNkZsDEq85f0ggXMA5L7c+o3tskGMh2BunCI9QUwB9Z4jpvXUOuMdyGKiGKQiRe11VS6Jzvg==", + "version": "7.15.8", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.15.8.tgz", + "integrity": "sha512-3UG9dsxvYBMYwRv+gS41WKHno4K60/9GPy1CJaH6xy3Elq8CTtvtjT5R5jmNhXfCYLX2mTw+7/aq5ak/gOE0og==", "requires": { - "@babel/code-frame": "^7.14.5", - "@babel/generator": "^7.15.4", + "@babel/code-frame": "^7.15.8", + "@babel/generator": "^7.15.8", "@babel/helper-compilation-targets": "^7.15.4", - "@babel/helper-module-transforms": "^7.15.4", + "@babel/helper-module-transforms": "^7.15.8", "@babel/helpers": "^7.15.4", - "@babel/parser": "^7.15.5", + "@babel/parser": "^7.15.8", "@babel/template": "^7.15.4", "@babel/traverse": "^7.15.4", - "@babel/types": "^7.15.4", + "@babel/types": "^7.15.6", "convert-source-map": "^1.7.0", "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", @@ -55,9 +55,9 @@ } }, "@babel/eslint-parser": { - "version": "7.15.7", - "resolved": "https://registry.npmjs.org/@babel/eslint-parser/-/eslint-parser-7.15.7.tgz", - "integrity": "sha512-yJkHyomClm6A2Xzb8pdAo4HzYMSXFn1O5zrCYvbFP0yQFvHueLedV8WiEno8yJOKStjUXzBZzJFeWQ7b3YMsqQ==", + "version": "7.15.8", + "resolved": "https://registry.npmjs.org/@babel/eslint-parser/-/eslint-parser-7.15.8.tgz", + "integrity": "sha512-fYP7QFngCvgxjUuw8O057SVH5jCXsbFFOoE77CFDcvzwBVgTOkMD/L4mIC5Ud1xf8chK/no2fRbSSn1wvNmKuQ==", "requires": { "eslint-scope": "^5.1.1", "eslint-visitor-keys": "^2.1.0", @@ -65,11 +65,11 @@ } }, "@babel/generator": { - "version": "7.15.4", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.15.4.tgz", - "integrity": "sha512-d3itta0tu+UayjEORPNz6e1T3FtvWlP5N4V5M+lhp/CxT4oAA7/NcScnpRyspUMLK6tu9MNHmQHxRykuN2R7hw==", + "version": "7.15.8", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.15.8.tgz", + "integrity": "sha512-ECmAKstXbp1cvpTTZciZCgfOt6iN64lR0d+euv3UZisU5awfRawOvg07Utn/qBGuH4bRIEZKrA/4LzZyXhZr8g==", "requires": { - "@babel/types": "^7.15.4", + "@babel/types": "^7.15.6", "jsesc": "^2.5.1", "source-map": "^0.5.0" }, @@ -135,9 +135,9 @@ } }, "@babel/helper-module-transforms": { - "version": "7.15.7", - "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.7.tgz", - "integrity": "sha512-ZNqjjQG/AuFfekFTY+7nY4RgBSklgTu970c7Rj3m/JOhIu5KPBUuTA9AY6zaKcUvk4g6EbDXdBnhi35FAssdSw==", + "version": "7.15.8", + "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.8.tgz", + "integrity": "sha512-DfAfA6PfpG8t4S6npwzLvTUpp0sS7JrcuaMiy1Y5645laRJIp/LiLGIBbQKaXSInK8tiGNI7FL7L8UvB8gdUZg==", "requires": { "@babel/helper-module-imports": "^7.15.4", "@babel/helper-replace-supers": "^7.15.4", @@ -266,9 +266,9 @@ } }, "@babel/parser": { - "version": "7.15.7", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.7.tgz", - "integrity": "sha512-rycZXvQ+xS9QyIcJ9HXeDWf1uxqlbVFAUq0Rq0dbc50Zb/+wUe/ehyfzGfm9KZZF0kBejYgxltBXocP+gKdL2g==" + "version": "7.15.8", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.8.tgz", + "integrity": "sha512-BRYa3wcQnjS/nqI8Ac94pYYpJfojHVvVXJ97+IDCImX4Jc8W8Xv1+47enbruk+q1etOpsQNwnfFcNGw+gtPGxA==" }, "@babel/plugin-syntax-async-generators": { "version": "7.8.4", @@ -838,12 +838,6 @@ "@types/istanbul-lib-report": "*" } }, - "@types/jsdoc-to-markdown": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/@types/jsdoc-to-markdown/-/jsdoc-to-markdown-7.0.1.tgz", - "integrity": "sha512-awGyJGrqtZtdYZu6Fnhm0KxStA6ZivTszmS0JrM7G/xm9YZrmlkP9+1drXIMHjYT+b8Wd3F08pZCv73ZOq21Dg==", - "dev": true - }, "@types/json-schema": { "version": "7.0.9", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", @@ -868,9 +862,9 @@ "integrity": "sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==" }, "@types/node": { - "version": "16.10.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-16.10.2.tgz", - "integrity": "sha512-zCclL4/rx+W5SQTzFs9wyvvyCwoK9QtBpratqz2IYJ3O8Umrn0m3nsTv0wQBk9sRGpvUe9CwPDrQFB10f1FIjQ==" + "version": "16.10.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-16.10.3.tgz", + "integrity": "sha512-ho3Ruq+fFnBrZhUYI46n/bV2GjwzSkwuT4dTf0GkuNFmnb8nq4ny2z9JEVemFi6bdEJanHLlYfy9c6FN9B9McQ==" }, "@types/node-fetch": { "version": "2.5.12", @@ -1334,9 +1328,9 @@ "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==" }, "caniuse-lite": { - "version": "1.0.30001264", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001264.tgz", - "integrity": "sha512-Ftfqqfcs/ePiUmyaySsQ4PUsdcYyXG2rfoBVsk3iY1ahHaJEw65vfb7Suzqm+cEkwwPIv/XWkg27iCpRavH4zA==" + "version": "1.0.30001265", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001265.tgz", + "integrity": "sha512-YzBnspggWV5hep1m9Z6sZVLOt7vrju8xWooFAgN6BA5qvy98qPAPb7vNUzypFaoh2pb3vlfzbDO8tB57UPGbtw==" }, "catharsis": { "version": "0.9.0", @@ -1762,9 +1756,9 @@ "integrity": "sha512-jtD6YG370ZCIi/9GTaJKQxWTZD045+4R4hTk/x1UyoqadyJ9x9CgSi1RlVDQF8U2sxLLSnFkCaMihqljHIWgMg==" }, "electron-to-chromium": { - "version": "1.3.859", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.859.tgz", - "integrity": "sha512-gXRXKNWedfdiKIzwr0Mg/VGCvxXzy+4SuK9hp1BDvfbCwx0O5Ot+2f4CoqQkqEJ3Zj/eAV/GoAFgBVFgkBLXuQ==" + "version": "1.3.860", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.860.tgz", + "integrity": "sha512-gWwGZ+Wv4Mou2SJRH6JQzhTPjL5f95SX7n6VkLTQ/Q/INsZLZNQ1vH2GlZjozKyvT0kkFuCmWTwIoCj+/hUDPw==" }, "emittery": { "version": "0.8.1", @@ -3405,9 +3399,9 @@ } }, "istanbul-reports": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/istanbul-reports/-/istanbul-reports-3.0.2.tgz", - "integrity": "sha512-9tZvz7AiR3PEDNGiV9vIouQ/EAcqMXFmkcA1CDFTwOB98OZVDL0PH9glHotf5Ugp6GCOTypfzGWI/OqjWNCRUw==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/istanbul-reports/-/istanbul-reports-3.0.3.tgz", + "integrity": "sha512-0i77ZFLsb9U3DHi22WzmIngVzfoyxxbQcZRqlF3KoKmCJGq9nhFHoGi8FqBztN2rE8w6hURnZghetn0xpkVb6A==", "requires": { "html-escaper": "^2.0.0", "istanbul-lib-report": "^3.0.0" diff --git a/package.json b/package.json index ce98c8c..ac69ddc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "core-commons", "version": "1.0.0-alpha.0", - "description": "Lifion's development setup in one command for our public Node.js projects.", + "description": "Lifion's development setup in one command for public Node.js projects.", "author": "Edgardo Avilés-López ", "maintainers": [ "Edgardo Avilés-López ", @@ -29,6 +29,7 @@ "node": ">=14.18.0" }, "scripts": { + "build-types": "./bin/cli.js", "check-types": "./bin/cli.js", "eslint": "./bin/cli.js", "prepare": "./bin/cli.js", @@ -51,8 +52,5 @@ "typescript": "^4.4.3", "yargs": "^17.2.1", "zx": "^4.2.0" - }, - "devDependencies": { - "@types/jsdoc-to-markdown": "^7.0.1" } } diff --git a/shared/git-hooks/pre-commit b/shared/git-hooks/pre-commit index 258e8b6..c9ccd5b 100755 --- a/shared/git-hooks/pre-commit +++ b/shared/git-hooks/pre-commit @@ -3,4 +3,7 @@ set -e set -o pipefail -npm run check-types && npm run build-docs && npx --no-install core-commons lint-staged +npm run eslint +npm run check-types +npm run build-docs +npm run prettier diff --git a/shared/git-hooks/pre-push b/shared/git-hooks/pre-push index 46d4ca4..687c424 100755 --- a/shared/git-hooks/pre-push +++ b/shared/git-hooks/pre-push @@ -3,4 +3,4 @@ set -e set -o pipefail -npm test && npm run check-types +npm test diff --git a/types/jsdoc-to-markdown.d.ts b/types/jsdoc-to-markdown.d.ts new file mode 100644 index 0000000..7da0c0e --- /dev/null +++ b/types/jsdoc-to-markdown.d.ts @@ -0,0 +1 @@ +declare module 'jsdoc-to-markdown';