Skip to content

Commit

Permalink
feat(init): use yargs command module (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored Aug 6, 2020
1 parent 0abf160 commit 152f75e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const init = require(`./lib/init`);
const { init } = require("./lib/init");

module.exports = { init };
3 changes: 1 addition & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const yargs = require("yargs");
const init = require("./init");

module.exports = function cli() {
// eslint-disable-next-line no-unused-expressions
yargs
.usage("$0 <command>")
.command("init", init.desc, {}, () => init())
.command(require("./init"))
.demandCommand(1)
.strict()
.alias("help", "h")
Expand Down
24 changes: 13 additions & 11 deletions lib/init.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require("path");
const { EOL } = require("os");
const { promisify } = require("util");
const fs = require("fs");
const originalPackage = require("../package.json");
const { defaultLogger } = require("./logger");

const copy = promisify(fs.copyFile);
const write = promisify(fs.writeFile);
Expand All @@ -15,11 +15,9 @@ const mkdir = promisify(fs.mkdir);
*/
const packagePath = (elem, ...elems) => path.join(...[__dirname, "..", elem, ...elems]);

/** @typedef {(msg: string) => boolean} Logger */

/**
* @param {string} baseDir
* @param {Logger} logger
* @param {import("ybiq").Logger} logger
*/
// eslint-disable-next-line max-lines-per-function
const initCommand = (baseDir, logger) => {
Expand Down Expand Up @@ -115,13 +113,10 @@ const initCommand = (baseDir, logger) => {
};
};

/** @type {Logger} */
const defaultLogger = (msg) => process.stdout.write(`${msg}${EOL}`);

/**
* @param {{ cwd?: string, logger?: Logger }} params
* @param {import("ybiq").CommandParams} params
*/
module.exports = async function init({ cwd = process.cwd(), logger = defaultLogger } = {}) {
async function init({ cwd = process.cwd(), logger = defaultLogger } = {}) {
const cmd = initCommand(cwd, logger);

await cmd.updatePackageFile();
Expand All @@ -131,10 +126,17 @@ module.exports = async function init({ cwd = process.cwd(), logger = defaultLogg
await cmd.writePackageFile(".github", "workflows", "npm-audit-fix.yml");
await cmd.writePackageFile(".github", "workflows", "release.yml");
await cmd.writePackageFile(".github", "workflows", "test.yml");
};
}
module.exports.init = init;

module.exports.desc = `Setup npm project:
module.exports.command = "init";

module.exports.describe = `Setup npm project:
- Update \`package.json\`
- Create \`.editorconfig\`
- Create \`.remarkignore\`
- Create \`.github/workflows/*.yml\``;

module.exports.handler = async () => {
await init();
};
4 changes: 4 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { EOL } = require("os");

/** @type {import("ybiq").Logger} */
module.exports.defaultLogger = (msg) => process.stdout.write(`${msg}${EOL}`);
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,15 @@
},
"eslintConfig": {
"root": true,
"ignorePatterns": [
"*.d.ts"
],
"extends": [
"ybiquitous/node"
],
"rules": {
"import/no-internal-modules": "off"
},
"overrides": [
{
"files": [
Expand All @@ -156,7 +162,6 @@
]
}
],
"import/no-internal-modules": "off",
"max-lines-per-function": [
"error",
100
Expand Down
2 changes: 1 addition & 1 deletion test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require("fs");
const fse = require("fs-extra");
const test = require("tape");
const pkg = require("../package.json");
const init = require("../lib/init");
const { init } = require("../lib/init");
const exec = require("./helpers/exec");

/* eslint-disable node/no-unsupported-features/node-builtins */
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
},
"include": ["lib"]
"include": ["lib", "types"]
}
4 changes: 4 additions & 0 deletions types/ybiq.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "ybiq" {
type Logger = (msg: string) => any;
type CommandParams = { cwd?: string; logger?: Logger };
}

0 comments on commit 152f75e

Please sign in to comment.