Skip to content

Commit

Permalink
Introduce a clean-types command
Browse files Browse the repository at this point in the history
  • Loading branch information
eaviles committed Oct 6, 2021
1 parent 113638f commit 8147b72
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 66 deletions.
9 changes: 5 additions & 4 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command> [options]`)
.usage(`${description}\n\n${chalk.bold('USAGE')}\n $0 <command> [options]`)
.epilogue(`${chalk.bold('LEARN MORE')}\n ${learnMore}`)
.commandDir(path.resolve(__dirname, '../lib/commands'))
.demandCommand()
Expand Down
10 changes: 2 additions & 8 deletions lib/commands/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
3 changes: 0 additions & 3 deletions lib/commands/check-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
19 changes: 19 additions & 0 deletions lib/commands/clean-types.js
Original file line number Diff line number Diff line change
@@ -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(() => {})
)
);
}
};
30 changes: 27 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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] || {};
Expand All @@ -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<void>}
*/
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 };
78 changes: 36 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <Edgardo.Aviles@ADP.com>",
"maintainers": [
"Edgardo Avilés-López <Edgardo.Aviles@ADP.com>",
Expand Down Expand Up @@ -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",
Expand All @@ -51,8 +52,5 @@
"typescript": "^4.4.3",
"yargs": "^17.2.1",
"zx": "^4.2.0"
},
"devDependencies": {
"@types/jsdoc-to-markdown": "^7.0.1"
}
}
5 changes: 4 additions & 1 deletion shared/git-hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion shared/git-hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -e
set -o pipefail

npm test && npm run check-types
npm test
1 change: 1 addition & 0 deletions types/jsdoc-to-markdown.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'jsdoc-to-markdown';

0 comments on commit 8147b72

Please sign in to comment.