Skip to content

Commit

Permalink
fix(init): support husky v5 migration (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored Mar 10, 2021
1 parent dbac640 commit fecfa46
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ const initCommand = (baseDir, logger) => {
extends: ["ybiquitous"],
};

// Delete husky v4 configuration.
delete packageInfo.husky;

await writeFile("package.json", JSON.stringify(packageInfo, null, 2));
},

Expand All @@ -113,6 +116,7 @@ const initCommand = (baseDir, logger) => {
};

/** @type {import("../types/ybiq").InitCommand} */
// eslint-disable-next-line max-statements
async function init({ cwd = process.cwd(), logger = defaultLogger } = {}) {
const cmd = initCommand(cwd, logger);

Expand All @@ -123,6 +127,9 @@ async function init({ cwd = process.cwd(), logger = defaultLogger } = {}) {
await cmd.writePackageFile(".github", "workflows", "npm-audit-fix.yml");
await cmd.writePackageFile(".github", "workflows", "release.yml");
await cmd.writePackageFile(".github", "workflows", "test.yml");
await cmd.writePackageFile(".husky", "commit-msg");
await cmd.writePackageFile(".husky", "post-commit");
await cmd.writePackageFile(".husky", "pre-commit");
}
module.exports.init = init;

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
".github/workflows/commitlint.yml",
".github/workflows/npm-audit-fix.yml",
".github/workflows/release.yml",
".github/workflows/test.yml"
".github/workflows/test.yml",
".husky/commit-msg",
".husky/post-commit",
".husky/pre-commit"
],
"engines": {
"node": ">=12.13.0"
Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/init.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,30 @@ jobs:
"
`;
exports[`write ".husky/commit-msg" 1`] = `
"#!/bin/sh
. \\"$(dirname \\"$0\\")/_/husky.sh\\"
npx --no-install commitlint --edit \\"$1\\"
"
`;
exports[`write ".husky/post-commit" 1`] = `
"#!/bin/sh
. \\"$(dirname \\"$0\\")/_/husky.sh\\"
npm run release:dry-run
"
`;
exports[`write ".husky/pre-commit" 1`] = `
"#!/bin/sh
. \\"$(dirname \\"$0\\")/_/husky.sh\\"
npx --no-install lint-staged
"
`;
exports[`write ".remarkignore" 1`] = `
"CHANGELOG.md
"
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/package-normal.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
},
"eslintConfig": {
"extends": []
},
"husky": {
"hooks": {}
}
}
6 changes: 6 additions & 0 deletions test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ test('update "package.json" without fields', () =>
".github/workflows/npm-audit-fix.yml",
".github/workflows/release.yml",
".github/workflows/test.yml",
".husky/commit-msg",
".husky/post-commit",
".husky/pre-commit",
].forEach((file) => {
test(`write "${file}"`, () =>
sandbox(async (ctx) => {
Expand Down Expand Up @@ -90,6 +93,9 @@ test("End-to-End via CLI", () =>
=> \`.github/workflows/npm-audit-fix.yml\` was updated
=> \`.github/workflows/release.yml\` was updated
=> \`.github/workflows/test.yml\` was updated
=> \`.husky/commit-msg\` was updated
=> \`.husky/post-commit\` was updated
=> \`.husky/pre-commit\` was updated
"
`);
expect(stderr).toEqual("");
Expand Down

0 comments on commit fecfa46

Please sign in to comment.