Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a cli executer #1255

Merged
merged 5 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/webpack-cli/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

'use strict';
require('v8-compile-cache');
const execa = require('execa');
const importLocal = require('import-local');
const logger = require('../lib/utils/logger');
const parseArgs = require('../lib/utils/parse-args');
const runner = require('../lib/runner');

// Prefer the local installation of webpack-cli
if (importLocal(__filename)) {
Expand All @@ -15,8 +15,5 @@ process.title = 'webpack';

const [, , ...rawArgs] = process.argv;
const { cliArgs, nodeArgs } = parseArgs(rawArgs);
const bootstrapPath = require.resolve('../lib/bootstrap');

execa('node', [...nodeArgs, bootstrapPath, ...cliArgs], { stdio: 'inherit' }).catch(e => {
process.exit(e.exitCode);
});
runner(nodeArgs, cliArgs);
3 changes: 2 additions & 1 deletion packages/webpack-cli/lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ async function runCLI(cli, commandIsUsed) {
.forEach(unknown => {
logger.warn('Unknown argument:', unknown);
});
return cli.runHelp(process.argv);
// return cli.runHelp(process.argv);
return;
}
const result = await cli.run(args, core);
if (!result) {
Expand Down
10 changes: 10 additions & 0 deletions packages/webpack-cli/lib/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const execa = require('execa');
const cliPath = require.resolve('./bootstrap.js');

function runner(nodeArgs, cliArgs) {
execa("node", [...nodeArgs, cliPath, ...cliArgs], {stdio: 'inherit'}).catch(e => {
process.exit(e.exitCode);
});
}

module.exports = runner;
53 changes: 53 additions & 0 deletions packages/webpack-cli/lib/utils/cli-executer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { MultiSelect, Input} = require('enquirer');
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
const runner = require('../runner');
const logger = require('./logger');
const allArgs = require('./cli-flags');
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved

logger.info(`Initiating webpack-cli executer \n`);
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved

const cliArgs = [];
const nodeArgs = [];
async function prompter() {
const typePrompt = new MultiSelect({
name: 'type',
message: 'Which flags do you want to use ?',
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
choices: allArgs.core.reduce((prev, curr) => {
return [...prev, `--${curr.name}`];
}, [])
});

const selections = await typePrompt.run();
const questions = selections.map(selection => {
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
const options = allArgs.core.find(flag => {
return flag.name === selection.slice(2);
});

if (options.type == Boolean) {
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
return {
run: () => Promise.resolve([selection])
};
}

const valuePrompt = new Input({
name: 'value',
message: `Enter value of the ${selection} flag`,
initial: options.defaultValue,
result: (value) => [selection, value]
});
return valuePrompt;
});
await questions.reduce((prev, curr) => {
return prev.then(() => curr.run().then((args) => {
cliArgs.push(...args);
}));
}, Promise.resolve(null))
}

async function run() {
await prompter();
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
process.stdout.write('\n');
logger.info(`Executing CLI\n`);
runner([], cliArgs);
}

run();