|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const inquirer = require("inquirer"); |
| 4 | +const path = require("path"); |
| 5 | +const glob = require("glob-all"); |
| 6 | +const chalk = require("chalk"); |
| 7 | +const spawn = require("cross-spawn"); |
| 8 | +const List = require("webpack-addons").List; |
| 9 | +const processPromise = require("../utils/resolve-packages").processPromise; |
| 10 | + |
| 11 | +/** |
| 12 | + * |
| 13 | + * Installs WDS using NPM with --save --dev etc |
| 14 | + * |
| 15 | + * @param {Object} cmd - arg to spawn with |
| 16 | + * @returns {Void} |
| 17 | + */ |
| 18 | + |
| 19 | +const spawnNPMWithArg = cmd => |
| 20 | + spawn.sync("npm", ["install", "webpack-dev-server", cmd], { |
| 21 | + stdio: "inherit" |
| 22 | + }); |
| 23 | + |
| 24 | +/** |
| 25 | + * |
| 26 | + * Installs WDS using Yarn with add etc |
| 27 | + * |
| 28 | + * @param {Object} cmd - arg to spawn with |
| 29 | + * @returns {Void} |
| 30 | + */ |
| 31 | + |
| 32 | +const spawnYarnWithArg = cmd => |
| 33 | + spawn.sync("yarn", ["add", "webpack-dev-server", cmd], { |
| 34 | + stdio: "inherit" |
| 35 | + }); |
| 36 | + |
| 37 | +/** |
| 38 | + * |
| 39 | + * Find the path of a given module |
| 40 | + * |
| 41 | + * @param {Object} dep - dependency to find |
| 42 | + * @returns {String} string with given path |
| 43 | + */ |
| 44 | + |
| 45 | +const getRootPathModule = dep => |
| 46 | + glob |
| 47 | + .sync([ |
| 48 | + path.resolve(process.cwd(), dep), |
| 49 | + path.join(process.cwd(), "..", dep), |
| 50 | + path.join(process.cwd(), "..", "..", dep), |
| 51 | + path.join(process.cwd(), "..", "..", "..", dep) |
| 52 | + ]) |
| 53 | + .pop(); |
| 54 | + |
| 55 | +/** |
| 56 | + * |
| 57 | + * Prompts for installing the devServer and running it |
| 58 | + * |
| 59 | + * @param {Object} args - args processed from the CLI |
| 60 | + * @returns {Function} invokes the devServer API |
| 61 | + */ |
| 62 | +module.exports = function serve() { |
| 63 | + let packageJSONPath = getRootPathModule("package.json"); |
| 64 | + if (!packageJSONPath) { |
| 65 | + console.log( |
| 66 | + "\n", |
| 67 | + chalk.red("✖ Could not find your package.json file"), |
| 68 | + "\n" |
| 69 | + ); |
| 70 | + process.exit(1); |
| 71 | + } |
| 72 | + let packageJSON = require(packageJSONPath); |
| 73 | + /* |
| 74 | + * We gotta do this, cause some configs might not have devdep, |
| 75 | + * dep or optional dep, so we'd need sanity checks for each |
| 76 | + */ |
| 77 | + let hasDevServerDep = packageJSON |
| 78 | + ? Object.keys(packageJSON).filter(p => packageJSON[p]["webpack-dev-server"]) |
| 79 | + : []; |
| 80 | + |
| 81 | + if (hasDevServerDep.length) { |
| 82 | + let WDSPath = getRootPathModule( |
| 83 | + "node_modules/webpack-dev-server/bin/webpack-dev-server.js" |
| 84 | + ); |
| 85 | + if (!WDSPath) { |
| 86 | + console.log( |
| 87 | + "\n", |
| 88 | + chalk.red( |
| 89 | + "✖ Could not find the webpack-dev-server dependency in node_modules root path" |
| 90 | + ) |
| 91 | + ); |
| 92 | + console.log( |
| 93 | + chalk.bold.green(" ✔︎"), |
| 94 | + "Try this command:", |
| 95 | + chalk.bold.green("rm -rf node_modules && npm install") |
| 96 | + ); |
| 97 | + process.exit(1); |
| 98 | + } |
| 99 | + return require(WDSPath); |
| 100 | + } else { |
| 101 | + process.stdout.write( |
| 102 | + "\n" + |
| 103 | + chalk.bold( |
| 104 | + "✖ We didn't find any webpack-dev-server dependency in your project," |
| 105 | + ) + |
| 106 | + "\n" + |
| 107 | + chalk.bold.green(" 'webpack serve'") + |
| 108 | + " " + |
| 109 | + chalk.bold("requires you to have it installed ") + |
| 110 | + "\n\n" |
| 111 | + ); |
| 112 | + return inquirer |
| 113 | + .prompt([ |
| 114 | + { |
| 115 | + type: "confirm", |
| 116 | + name: "confirmDevserver", |
| 117 | + message: "Do you want to install it? (default: Y)", |
| 118 | + default: "Y" |
| 119 | + } |
| 120 | + ]) |
| 121 | + .then(answer => { |
| 122 | + if (answer["confirmDevserver"]) { |
| 123 | + return inquirer |
| 124 | + .prompt( |
| 125 | + List( |
| 126 | + "confirmDepType", |
| 127 | + "What kind of dependency do you want it to be under? (default: devDependency)", |
| 128 | + ["devDependency", "optionalDependency", "dependency"] |
| 129 | + ) |
| 130 | + ) |
| 131 | + .then(depTypeAns => { |
| 132 | + const packager = getRootPathModule("package-lock.json") |
| 133 | + ? "npm" |
| 134 | + : "yarn"; |
| 135 | + let spawnAction; |
| 136 | + if (depTypeAns["confirmDepType"] === "devDependency") { |
| 137 | + if (packager === "yarn") { |
| 138 | + spawnAction = () => spawnYarnWithArg("--dev"); |
| 139 | + } else { |
| 140 | + spawnAction = () => spawnNPMWithArg("--save-dev"); |
| 141 | + } |
| 142 | + } |
| 143 | + if (depTypeAns["confirmDepType"] === "dependency") { |
| 144 | + if (packager === "yarn") { |
| 145 | + spawnAction = () => spawnYarnWithArg(" "); |
| 146 | + } else { |
| 147 | + spawnAction = () => spawnNPMWithArg("--save"); |
| 148 | + } |
| 149 | + } |
| 150 | + if (depTypeAns["confirmDepType"] === "optionalDependency") { |
| 151 | + if (packager === "yarn") { |
| 152 | + spawnAction = () => spawnYarnWithArg("--optional"); |
| 153 | + } else { |
| 154 | + spawnNPMWithArg("--save-optional"); |
| 155 | + } |
| 156 | + } |
| 157 | + return processPromise(spawnAction()).then(() => { |
| 158 | + // Recursion doesn't work well with require call being cached |
| 159 | + delete require.cache[require.resolve(packageJSONPath)]; |
| 160 | + return serve(); |
| 161 | + }); |
| 162 | + }); |
| 163 | + } else { |
| 164 | + console.log(chalk.bold.red("✖ Serve aborted due cancelling")); |
| 165 | + process.exitCode = 1; |
| 166 | + } |
| 167 | + }) |
| 168 | + .catch(err => { |
| 169 | + console.log(chalk.red("✖ Serve aborted due to some errors")); |
| 170 | + console.error(err); |
| 171 | + process.exitCode = 1; |
| 172 | + }); |
| 173 | + } |
| 174 | +}; |
0 commit comments