Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(init): use yargs command module #655

Merged
merged 1 commit into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
}