执行过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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
 
'use strict';
 
const Liftoff = require('liftoff');
const args = process.argv.slice(2);
const argv = require('minimist')(args);
const v8flags = require('v8flags');
const interpret = require('interpret');
const chalk = require('chalk');
const ora = require('ora');
 
const nodePlop = require('node-plop');
const out = require('./console-out');
const {combineBypassData} = require('./bypass');
const {getBypassAndGenerator, handleArgFlags} = require('./input-processing');
 
const Plop = new Liftoff({
    name: 'plop',
    extensions: interpret.jsVariants,
    v8flags: v8flags
});
 
Plop.launch({
    cwd: argv.cwd,
    configPath: argv.plopfile,
    require: argv.require,
    completion: argv.completion
}, run);
 
function run(env) {
    const plopfilePath = env.configPath;
 
    // handle basic argument flags like --help, --version, etc
    handleArgFlags(env);
 
    // set the default base path to the plopfile directory
    const plop = nodePlop(plopfilePath, {
        force: argv.force || argv.f
    });
 
    const generators = plop.getGeneratorList();
    const generatorNames = generators.map(v => v.name);
    const {generatorName, bypassArr, plopArgV} = getBypassAndGenerator(plop);
 
    // look up a generator and run it with calculated bypass data
    const runGeneratorByName = name => {
        const generator = plop.getGenerator(name);
        const bypassData = combineBypassData(generator, bypassArr, plopArgV);
        doThePlop(generator, bypassData);
    };
 
    // hmmmm, couldn't identify a generator in the user's input
    if (!generators.length) {
        // no generators?! there's clearly something wrong here
        console.error(chalk.red('[PLOP] ') + 'No generator found in plopfile');
        process.exit(1);
    } else if (!generatorName && generators.length === 1) {
        // only one generator in this plopfile... let's assume they
        // want to run that one!
        runGeneratorByName(generatorNames[0]);
    } else if (!generatorName && generators.length > 1 && !bypassArr.length) {
        // more than one generator? we'll have to ask the user which
        // one they want to run.
        out.chooseOptionFromList(generators, plop.getWelcomeMessage())
            .then(runGeneratorByName)
            .catch((err) => {
                console.error(chalk.red('[PLOP] ') + 'Something went wrong with selecting a generator', err);
            });
    } else if (generatorNames.includes(generatorName)) {
        // we have found the generator, run it!
        runGeneratorByName(generatorName);
    } else {
        // we just can't make sense of your input... sorry :-(
        const fuzyGenName = (generatorName + ' ' + args.join(' ')).trim();
        console.error(chalk.red('[PLOP] ') + 'Could not find a generator for "' + fuzyGenName + '"');
        process.exit(1);
    }
 
}
 
/////
// everybody to the plop!
//
function doThePlop(generator, bypassArr) {
    generator.runPrompts(bypassArr)
        .then(answers => {
            const noMap = (argv['show-type-names'] || argv.t);
            const progress = ora();
            const onComment = (msg) => {
                progress.info(msg); progress.start();
            };
            const onSuccess = (change) => {
                let line = '';
                if (change.type) { line += ` ${out.typeMap(change.type, noMap)}`; }
                if (change.path) { line += ` ${change.path}`; }
                progress.succeed(line); progress.start();
            };
            const onFailure = (fail) => {
                let line = '';
                if (fail.type) { line += ` ${out.typeMap(fail.type, noMap)}`; }
                if (fail.path) { line += ` ${fail.path}`; }
                const errMsg = fail.error || fail.message;
                if (errMsg) { line += ` ${errMsg}` };
                progress.fail(line); progress.start();
            };
            progress.start();
            return generator.runActions(answers, {onSuccess, onFailure, onComment})
                .then(() => progress.stop());
        })
        .catch(function (err) {
            console.error(chalk.red('[ERROR]'), err.message);
            process.exit(1);
        });
}