1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const fs_1 = __importDefault(require("fs"));
|
7 | const path_1 = __importDefault(require("path"));
|
8 | const read_pkg_1 = __importDefault(require("read-pkg"));
|
9 | const hookList = {
|
10 | applypatchmsg: 'applypatch-msg',
|
11 | commitmsg: 'commit-msg',
|
12 | postapplypatch: 'post-applypatch',
|
13 | postcheckout: 'post-checkout',
|
14 | postcommit: 'post-commit',
|
15 | postmerge: 'post-merge',
|
16 | postreceive: 'post-receive',
|
17 | postrewrite: 'post-rewrite',
|
18 | postupdate: 'post-update',
|
19 | preapplypatch: 'pre-applypatch',
|
20 | preautogc: 'pre-auto-gc',
|
21 | precommit: 'pre-commit',
|
22 | preparecommitmsg: 'prepare-commit-msg',
|
23 | prepush: 'pre-push',
|
24 | prerebase: 'pre-rebase',
|
25 | prereceive: 'pre-receive',
|
26 | pushtocheckout: 'push-to-checkout',
|
27 | sendemailvalidate: 'sendemail-validate',
|
28 | update: 'update'
|
29 | };
|
30 | function upgrade(cwd) {
|
31 | const pkgFile = path_1.default.join(cwd, 'package.json');
|
32 | if (fs_1.default.existsSync(pkgFile)) {
|
33 | const pkg = read_pkg_1.default.sync({ cwd, normalize: false });
|
34 | console.log(`husky > upgrading ${pkgFile}`);
|
35 |
|
36 | if (pkg.husky) {
|
37 | return console.log(`husky field in package.json isn't empty, skipping automatic upgrade`);
|
38 | }
|
39 | const hooks = {};
|
40 |
|
41 | Object.keys(hookList).forEach((name) => {
|
42 | if (pkg.scripts) {
|
43 | const script = pkg.scripts[name];
|
44 | if (script) {
|
45 | delete pkg.scripts[name];
|
46 | const newName = hookList[name];
|
47 | hooks[newName] = script.replace(/\bGIT_PARAMS\b/g, 'HUSKY_GIT_PARAMS');
|
48 | console.log(`moved scripts.${name} to husky.hooks.${newName}`);
|
49 | }
|
50 | }
|
51 | });
|
52 |
|
53 | if (Object.keys(hooks).length) {
|
54 | pkg.husky = { hooks };
|
55 | }
|
56 | else {
|
57 | console.log('no hooks found');
|
58 | }
|
59 |
|
60 | fs_1.default.writeFileSync(pkgFile, `${JSON.stringify(pkg, null, 2)}\n`, 'utf-8');
|
61 | console.log(`husky > done`);
|
62 | }
|
63 | }
|
64 | exports.default = upgrade;
|