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): add lint:types script #654

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function cli() {
// eslint-disable-next-line no-unused-expressions
yargs
.usage("$0 <command>")
.command("init", init.desc, {}, init)
.command("init", init.desc, {}, () => init())
.demandCommand(1)
.strict()
.alias("help", "h")
Expand Down
49 changes: 45 additions & 4 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,45 @@ const write = promisify(fs.writeFile);
const read = promisify(fs.readFile);
const mkdir = promisify(fs.mkdir);

/**
* @param {string} elem
* @param {...string} elems
*/
const packagePath = (elem, ...elems) => path.join(...[__dirname, "..", elem, ...elems]);

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

/**
* @param {string} baseDir
* @param {Logger} logger
*/
// eslint-disable-next-line max-lines-per-function
const initCommand = (baseDir, logger) => {
/**
* @param {string} elem
* @param {...string} elems
*/
const currentPath = (elem, ...elems) => path.join(...[baseDir, elem, ...elems]);

/**
* @param {string} fileName
*/
const readFile = (fileName) => read(currentPath(fileName), "utf8");

/**
* @param {string} src
* @param {string} dest
*/
const copyFile = async (src, dest) => {
await mkdir(path.resolve(dest, ".."), { recursive: true });
await copy(src, dest);
logger(`=> \`${path.relative(baseDir, dest)}\` was updated`);
};

/**
* @param {string} fileName
* @param {string} fileContent
*/
const writeFile = async (fileName, fileContent) => {
const file = currentPath(fileName);
await write(file, `${fileContent}\n`);
Expand All @@ -47,23 +72,31 @@ const initCommand = (baseDir, logger) => {
Object.keys(originalPackage.scripts)
.filter((key) => !(key === "test" || key.startsWith("test:")))
.forEach((key) => {
// @ts-ignore
scripts[key] = originalPackage.scripts[key];
});

// update other keys
const keys = ["husky", "lint-staged", "standard-version", "remarkConfig"];
keys.forEach((key) => {
/**
* @param {"husky" | "lint-staged" | "standard-version" | "remarkConfig"} key
*/
const updateOtherKey = (key) => {
if (!(key in packageInfo)) {
packageInfo[key] = {};
}
Object.assign(packageInfo[key], originalPackage[key]);
});
};
updateOtherKey("husky");
updateOtherKey("lint-staged");
updateOtherKey("standard-version");
updateOtherKey("remarkConfig");

packageInfo.commitlint = {
extends: ["@commitlint/config-conventional"],
rules: {
"body-max-line-length": [1, "always", 100],
},
};

packageInfo.eslintConfig = {
root: true,
extends: ["ybiquitous"],
Expand All @@ -72,14 +105,22 @@ const initCommand = (baseDir, logger) => {
await writeFile("package.json", JSON.stringify(packageInfo, null, 2));
},

/**
* @param {string} name
* @param {...string} names
*/
async writePackageFile(name, ...names) {
await copyFile(packagePath(name, ...names), currentPath(name, ...names));
},
};
};

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

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

Expand Down
27 changes: 27 additions & 0 deletions package-lock.json

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

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@
"yargs": "^15.4.1"
},
"devDependencies": {
"@types/node": "^14.0.27",
"@types/yargs": "^15.0.5",
"eslint-config-ybiquitous": "^11.0.0",
"fs-extra": "^9.0.1",
"nodemon": "^2.0.4",
"nyc": "^15.1.0",
"tape": "^5.0.1"
"tape": "^5.0.1",
"typescript": "^3.9.7"
},
"scripts": {
"test": "nyc --check-coverage --lines 100 --branches 90 tape \"test/**/*.test.js\"",
Expand All @@ -56,6 +59,7 @@
"lint:js:fix": "npm run lint:js -- --fix",
"lint:md": "remark . --frail",
"lint:md:fix": "remark . --output",
"lint:types": "tsc --noEmit",
"lint": "npm-run-all --print-label --parallel lint:*",
"prettier": "prettier --ignore-path .gitignore .",
"prettier:check": "npm run prettier -- --check",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/package-empty_expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"lint:js:fix": "npm run lint:js -- --fix",
"lint:md": "remark . --frail",
"lint:md:fix": "remark . --output",
"lint:types": "tsc --noEmit",
"lint": "npm-run-all --print-label --parallel lint:*",
"prettier": "prettier --ignore-path .gitignore .",
"prettier:check": "npm run prettier -- --check",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/package-normal_expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"lint:js:fix": "npm run lint:js -- --fix",
"lint:md": "remark . --frail",
"lint:md:fix": "remark . --output",
"lint:types": "tsc --noEmit",
"lint": "npm-run-all --print-label --parallel lint:*",
"prettier": "prettier --ignore-path .gitignore .",
"prettier:check": "npm run prettier -- --check",
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
},
"include": ["lib"]
}