执行过npm install命令的vue-element-admin源码
康凯
2022-05-20 aa4c235a8ca67ea8b731f90c951a465e92c0a865
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const readPkg = require("read-pkg");
const hookList = {
    applypatchmsg: 'applypatch-msg',
    commitmsg: 'commit-msg',
    postapplypatch: 'post-applypatch',
    postcheckout: 'post-checkout',
    postcommit: 'post-commit',
    postmerge: 'post-merge',
    postreceive: 'post-receive',
    postrewrite: 'post-rewrite',
    postupdate: 'post-update',
    preapplypatch: 'pre-applypatch',
    preautogc: 'pre-auto-gc',
    precommit: 'pre-commit',
    preparecommitmsg: 'prepare-commit-msg',
    prepush: 'pre-push',
    prerebase: 'pre-rebase',
    prereceive: 'pre-receive',
    pushtocheckout: 'push-to-checkout',
    sendemailvalidate: 'sendemail-validate',
    update: 'update'
};
function upgrade(cwd) {
    const pkgFile = path.join(cwd, 'package.json');
    if (fs.existsSync(pkgFile)) {
        const pkg = readPkg.sync({ cwd, normalize: false });
        console.log(`husky > upgrading ${pkgFile}`);
        // Don't overwrite pkg.husky if it exists
        if (pkg.husky) {
            return console.log(`husky field in package.json isn't empty, skipping automatic upgrade`);
        }
        const hooks = {};
        // Loop trhough hooks and move them to husky.hooks
        Object.keys(hookList).forEach(name => {
            const script = pkg.scripts[name];
            if (script) {
                delete pkg.scripts[name];
                const newName = hookList[name];
                hooks[newName] = script.replace(/\bGIT_PARAMS\b/g, 'HUSKY_GIT_PARAMS');
                console.log(`moved scripts.${name} to husky.hooks.${newName}`);
            }
        });
        if (Object.keys(hooks).length) {
            pkg.husky = { hooks };
        }
        else {
            console.log('no hooks found');
        }
        // Update package.json
        fs.writeFileSync(pkgFile, `${JSON.stringify(pkg, null, 2)}\n`, 'utf-8');
        console.log(`husky > done`);
    }
}
exports.default = upgrade;