执行过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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const chalk = require('chalk');
const minimist = require('minimist');
const out = require('./console-out');
const globalPkg = require('../package.json');
const args = process.argv.slice(2);
const argv = minimist(args);
 
module.exports = {getBypassAndGenerator, handleArgFlags};
 
/**
 * Parses the user input to identify the generator to run and any bypass data
 * @param plop - The plop context
 */
function getBypassAndGenerator(plop) {
    // See if there are args to pass to generator
    const eoaIndex = args.indexOf('--');
    const {plopArgV, eoaArg} = (eoaIndex === -1
        ? {plopArgV: []}
        : {
            plopArgV: minimist(args.slice(eoaIndex + 1, args.length)),
            eoaArg: args[eoaIndex + 1]
        }
    );
 
  // locate the generator name based on input and take the rest of the
    // user's input as prompt bypass data to be passed into the generator
    let generatorName = '';
    let bypassArr = [];
 
    const generatorNames = plop.getGeneratorList().map(v => v.name);
    for (let i=0; i < argv._.length; i++) {
        const nameTest = (generatorName.length ? generatorName + ' ' : '') + argv._[i];
        if (listHasOptionThatStartsWith(generatorNames, nameTest)) {
            generatorName = nameTest;
        } else {
            let index = argv._.findIndex(arg => arg === eoaArg);
            // If can't find index, slice until the very end - allowing all `_` to be passed
            index = index !== -1 ? index : argv._.length;
            // Force `'_'` to become undefined in nameless bypassArr
            bypassArr = argv._.slice(i, index).map(arg => (/^_+$/).test(arg) ? undefined : arg);
            break;
        }
    }
 
  return {generatorName, bypassArr, plopArgV};
}
 
function listHasOptionThatStartsWith(list, prefix) {
    return list.some(function (txt) {
        return txt.indexOf(prefix) === 0;
    });
}
 
/**
 * Handles all basic argument flags
 * @param env - Values parsed by Liftoff
 */
function handleArgFlags(env) {
    // Make sure that we're not overwritting `help`, `init,` or `version` args in generators
    if (argv._.length === 0) {
        // handle request for usage and options
        if (argv.help || argv.h) {
            out.displayHelpScreen();
            process.exit(0);
        }
 
        // handle request for initializing a new plopfile
        if (argv.init || argv.i) {
            return out.createInitPlopfile(env.cwd, function (err) {
                if (err) {
                    console.log(err);
                    process.exit(1);
                }
                process.exit(0);
            });
        }
 
        // handle request for version number
        if (argv.version || argv.v) {
 
            const localVersion = env.modulePackage.version;
            if (localVersion !== globalPkg.version && localVersion != null) {
                console.log(chalk.yellow('CLI version'), globalPkg.version);
                console.log(chalk.yellow('Local version'), localVersion);
            } else {
                console.log(globalPkg.version);
            }
            process.exit(0);
        }
    }
 
    // abort if there's no plopfile found
    if (env.configPath == null) {
        console.error(chalk.red('[PLOP] ') + 'No plopfile found');
        out.displayHelpScreen();
        process.exit(1);
    }
}