|
1 | 1 | const path = require('path');
|
2 | 2 | const { promises } = require('fs');
|
3 | 3 |
|
| 4 | +const inquirer = require('inquirer'); |
4 | 5 | const webpack = require('webpack');
|
5 | 6 | const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
|
6 | 7 | const HtmlWebpackPlugin = require('html-webpack-plugin');
|
7 | 8 |
|
8 |
| -async function init(scenario) { |
9 |
| - if (!hasCurrentScenario(scenario)) { |
10 |
| - throw new Error(`Scenario "${scenario}" does not exist`); |
11 |
| - } |
| 9 | +async function init() { |
| 10 | + const scenarios = await getScenariosFromDirectories(); |
12 | 11 |
|
13 |
| - console.log(`Bundling scenario: ${scenario}`); |
| 12 | + const answers = await inquirer.prompt([ |
| 13 | + { |
| 14 | + type: 'rawlist', |
| 15 | + name: 'scenario', |
| 16 | + message: 'Which scenario you want to run?', |
| 17 | + choices: scenarios, |
| 18 | + pageSize: scenarios.length, |
| 19 | + loop: false, |
| 20 | + }, |
| 21 | + ]); |
14 | 22 |
|
15 |
| - await runWebpack(scenario); |
| 23 | + console.log(`Bundling scenario: ${answers.scenario}`); |
| 24 | + |
| 25 | + await runWebpack(answers.scenario); |
16 | 26 | }
|
17 | 27 |
|
18 | 28 | async function runWebpack(scenario) {
|
@@ -59,11 +69,14 @@ async function generateAlias() {
|
59 | 69 | );
|
60 | 70 | }
|
61 | 71 |
|
62 |
| -async function hasCurrentScenario(scenario) { |
| 72 | +/** |
| 73 | + * Generates an array of available scenarios |
| 74 | + */ |
| 75 | +async function getScenariosFromDirectories() { |
| 76 | + const exclude = ['node_modules', 'dist', '~', 'package.json', 'yarn.lock', 'webpack.js']; |
| 77 | + |
63 | 78 | const dirents = await promises.readdir(__dirname, { withFileTypes: true });
|
64 |
| - return dirents.filter(dir => dir.isDirectory()).find(dir => dir.name === scenario); |
| 79 | + return dirents.map(dirent => dirent.name).filter(mape => !exclude.includes(mape)); |
65 | 80 | }
|
66 | 81 |
|
67 |
| -const CURRENT_SCENARIO = 'basic'; |
68 |
| - |
69 |
| -init(CURRENT_SCENARIO); |
| 82 | +init(); |
0 commit comments