diff --git a/packages/serve/.gitignore b/packages/serve/.gitignore new file mode 100644 index 00000000000..a6c7c2852d0 --- /dev/null +++ b/packages/serve/.gitignore @@ -0,0 +1 @@ +*.js diff --git a/packages/serve/.npmignore b/packages/serve/.npmignore new file mode 100644 index 00000000000..a527d071cf9 --- /dev/null +++ b/packages/serve/.npmignore @@ -0,0 +1,2 @@ +tsconfig.json +*.ts diff --git a/packages/serve/index.js b/packages/serve/index.ts similarity index 50% rename from packages/serve/index.js rename to packages/serve/index.ts index 16f4ff89a4a..280e76ff193 100644 --- a/packages/serve/index.js +++ b/packages/serve/index.ts @@ -1,12 +1,11 @@ -"use strict"; +import chalk from "chalk"; +import { SpawnSyncReturns } from "child_process"; +import * as spawn from "cross-spawn"; +import * as inquirer from "inquirer"; +import * as path from "path"; -const inquirer = require("inquirer"); -const path = require("path"); -const chalk = require("chalk"); -const spawn = require("cross-spawn"); -const List = require("@webpack-cli/webpack-scaffold").List; -const processPromise = require("@webpack-cli/utils/resolve-packages") - .processPromise; +import { processPromise } from "@webpack-cli/utils/resolve-packages"; +import { List } from "@webpack-cli/webpack-scaffold"; /** * @@ -16,9 +15,9 @@ const processPromise = require("@webpack-cli/utils/resolve-packages") * @returns {Void} */ -const spawnNPMWithArg = cmd => +const spawnNPMWithArg = (cmd: string): SpawnSyncReturns => spawn.sync("npm", ["install", "webpack-dev-server", cmd], { - stdio: "inherit" + stdio: "inherit", }); /** @@ -29,9 +28,9 @@ const spawnNPMWithArg = cmd => * @returns {Void} */ -const spawnYarnWithArg = cmd => +const spawnYarnWithArg = (cmd: string): SpawnSyncReturns => spawn.sync("yarn", ["add", "webpack-dev-server", cmd], { - stdio: "inherit" + stdio: "inherit", }); /** @@ -42,7 +41,7 @@ const spawnYarnWithArg = cmd => * @returns {String} string with given path */ -const getRootPathModule = dep => path.resolve(process.cwd(), dep); +const getRootPathModule = (dep: string): string => path.resolve(process.cwd(), dep); /** * @@ -53,39 +52,39 @@ const getRootPathModule = dep => path.resolve(process.cwd(), dep); */ function serve() { - let packageJSONPath = getRootPathModule("package.json"); + const packageJSONPath: string = getRootPathModule("package.json"); if (!packageJSONPath) { console.log( "\n", chalk.red("✖ Could not find your package.json file"), - "\n" + "\n", ); process.exit(1); } - let packageJSON = require(packageJSONPath); + const packageJSON: object = require(packageJSONPath); /* * We gotta do this, cause some configs might not have devdep, * dep or optional dep, so we'd need sanity checks for each */ - let hasDevServerDep = packageJSON - ? Object.keys(packageJSON).filter(p => packageJSON[p]["webpack-dev-server"]) + const hasDevServerDep: string[] = packageJSON + ? Object.keys(packageJSON).filter((p: string) => packageJSON[p]["webpack-dev-server"]) : []; if (hasDevServerDep.length) { - let WDSPath = getRootPathModule( - "node_modules/webpack-dev-server/bin/webpack-dev-server.js" + const WDSPath: string = getRootPathModule( + "node_modules/webpack-dev-server/bin/webpack-dev-server.js", ); if (!WDSPath) { console.log( "\n", chalk.red( - "✖ Could not find the webpack-dev-server dependency in node_modules root path" - ) + "✖ Could not find the webpack-dev-server dependency in node_modules root path", + ), ); console.log( chalk.bold.green(" ✔︎"), "Try this command:", - chalk.bold.green("rm -rf node_modules && npm install") + chalk.bold.green("rm -rf node_modules && npm install"), ); process.exit(1); } @@ -94,63 +93,68 @@ function serve() { process.stdout.write( "\n" + chalk.bold( - "✖ We didn't find any webpack-dev-server dependency in your project," + "✖ We didn't find any webpack-dev-server dependency in your project,", ) + "\n" + chalk.bold.green(" 'webpack serve'") + " " + chalk.bold("requires you to have it installed ") + - "\n\n" + "\n\n", ); return inquirer .prompt([ { - type: "confirm", - name: "confirmDevserver", + default: "Y", message: "Do you want to install it? (default: Y)", - default: "Y" - } + name: "confirmDevserver", + type: "confirm", + }, ]) - .then(answer => { - if (answer["confirmDevserver"]) { + .then((answer: { + confirmDevserver: boolean, + }) => { + if (answer.confirmDevserver) { return inquirer .prompt( List( "confirmDepType", "What kind of dependency do you want it to be under? (default: devDependency)", - ["devDependency", "optionalDependency", "dependency"] - ) + ["devDependency", "optionalDependency", "dependency"], + ), ) - .then(depTypeAns => { - const packager = getRootPathModule("package-lock.json") + .then((depTypeAns: { + confirmDepType: string; + }) => { + const packager: string = getRootPathModule("package-lock.json") ? "npm" : "yarn"; - let spawnAction; - if (depTypeAns["confirmDepType"] === "devDependency") { + let spawnAction: (_?: void) => SpawnSyncReturns; + if (depTypeAns.confirmDepType === "devDependency") { if (packager === "yarn") { - spawnAction = _ => spawnYarnWithArg("--dev"); + spawnAction = (_?: void) => spawnYarnWithArg("--dev"); } else { - spawnAction = _ => spawnNPMWithArg("--save-dev"); + spawnAction = (_?: void) => spawnNPMWithArg("--save-dev"); } } - if (depTypeAns["confirmDepType"] === "dependency") { + if (depTypeAns.confirmDepType === "dependency") { if (packager === "yarn") { - spawnAction = _ => spawnYarnWithArg(" "); + spawnAction = (_?: void) => spawnYarnWithArg(" "); } else { - spawnAction = _ => spawnNPMWithArg("--save"); + spawnAction = (_?: void) => spawnNPMWithArg("--save"); } } - if (depTypeAns["confirmDepType"] === "optionalDependency") { + if (depTypeAns.confirmDepType === "optionalDependency") { if (packager === "yarn") { - spawnAction = _ => spawnYarnWithArg("--optional"); + spawnAction = (_?: void) => spawnYarnWithArg("--optional"); } else { - spawnAction = _ => spawnNPMWithArg("--save-optional"); + spawnAction = (_?: void) => spawnNPMWithArg("--save-optional"); } } - return processPromise(spawnAction()).then(_ => { - // Recursion doesn't work well with require call being cached - delete require.cache[require.resolve(packageJSONPath)]; - return serve(); + return processPromise(spawnAction()) + .then((_: void) => { + // Recursion doesn't work well with require call being cached + delete require.cache[require.resolve(packageJSONPath)]; + return serve(); }); }); } else { @@ -158,7 +162,7 @@ function serve() { process.exitCode = 1; } }) - .catch(err => { + .catch((err: object) => { console.log(chalk.red("✖ Serve aborted due to some errors")); console.error(err); process.exitCode = 1; @@ -166,9 +170,9 @@ function serve() { } } -module.exports = { - serve, +export = { getRootPathModule, + serve, spawnNPMWithArg, - spawnYarnWithArg + spawnYarnWithArg, }; diff --git a/packages/serve/package-lock.json b/packages/serve/package-lock.json index 86704b52a8a..c4e60d49e80 100644 --- a/packages/serve/package-lock.json +++ b/packages/serve/package-lock.json @@ -2,6 +2,149 @@ "requires": true, "lockfileVersion": 1, "dependencies": { + "@types/cross-spawn": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/@types/cross-spawn/-/cross-spawn-6.0.0.tgz", + "integrity": "sha512-evp2ZGsFw9YKprDbg8ySgC9NA15g3YgiI8ANkGmKKvvi0P2aDGYLPxQIC5qfeKNUOe3TjABVGuah6omPRpIYhg==", + "requires": { + "@types/node": "*" + } + }, + "@types/inquirer": { + "version": "0.0.42", + "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-0.0.42.tgz", + "integrity": "sha512-flMaNWU2g9NrtZ4bIV+7SEY2W7OdWNNhmJ0rE1lWVxGrkp3TfFGMcFCxRIBmGWigI8e6n+2HqLjizTTfgcpHLg==", + "requires": { + "@types/rx": "*", + "@types/through": "*" + } + }, + "@types/node": { + "version": "10.5.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.1.tgz", + "integrity": "sha512-AFLl1IALIuyt6oK4AYZsgWVJ/5rnyzQWud7IebaZWWV3YmgtPZkQmYio9R5Ze/2pdd7XfqF5bP+hWS11mAKoOQ==" + }, + "@types/rx": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/@types/rx/-/rx-4.1.1.tgz", + "integrity": "sha1-WY/JSla67ZdfGUV04PVy/Y5iekg=", + "requires": { + "@types/rx-core": "*", + "@types/rx-core-binding": "*", + "@types/rx-lite": "*", + "@types/rx-lite-aggregates": "*", + "@types/rx-lite-async": "*", + "@types/rx-lite-backpressure": "*", + "@types/rx-lite-coincidence": "*", + "@types/rx-lite-experimental": "*", + "@types/rx-lite-joinpatterns": "*", + "@types/rx-lite-testing": "*", + "@types/rx-lite-time": "*", + "@types/rx-lite-virtualtime": "*" + } + }, + "@types/rx-core": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/rx-core/-/rx-core-4.0.3.tgz", + "integrity": "sha1-CzNUsSOM7b4rdPYybxOdvHpZHWA=" + }, + "@types/rx-core-binding": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/@types/rx-core-binding/-/rx-core-binding-4.0.4.tgz", + "integrity": "sha512-5pkfxnC4w810LqBPUwP5bg7SFR/USwhMSaAeZQQbEHeBp57pjKXRlXmqpMrLJB4y1oglR/c2502853uN0I+DAQ==", + "requires": { + "@types/rx-core": "*" + } + }, + "@types/rx-lite": { + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/@types/rx-lite/-/rx-lite-4.0.5.tgz", + "integrity": "sha512-KZk5XTR1dm/kNgBx8iVpjno6fRYtAUQWBOmj+O8j724+nk097sz4fOoHJNpCkOJUtHUurZlJC7QvSFCZHbkC+w==", + "requires": { + "@types/rx-core": "*", + "@types/rx-core-binding": "*" + } + }, + "@types/rx-lite-aggregates": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/rx-lite-aggregates/-/rx-lite-aggregates-4.0.3.tgz", + "integrity": "sha512-MAGDAHy8cRatm94FDduhJF+iNS5//jrZ/PIfm+QYw9OCeDgbymFHChM8YVIvN2zArwsRftKgE33QfRWvQk4DPg==", + "requires": { + "@types/rx-lite": "*" + } + }, + "@types/rx-lite-async": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/rx-lite-async/-/rx-lite-async-4.0.2.tgz", + "integrity": "sha512-vTEv5o8l6702ZwfAM5aOeVDfUwBSDOs+ARoGmWAKQ6LOInQ8J4/zjM7ov12fuTpktUKdMQjkeCp07Vd73mPkxw==", + "requires": { + "@types/rx-lite": "*" + } + }, + "@types/rx-lite-backpressure": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/rx-lite-backpressure/-/rx-lite-backpressure-4.0.3.tgz", + "integrity": "sha512-Y6aIeQCtNban5XSAF4B8dffhIKu6aAy/TXFlScHzSxh6ivfQBQw6UjxyEJxIOt3IT49YkS+siuayM2H/Q0cmgA==", + "requires": { + "@types/rx-lite": "*" + } + }, + "@types/rx-lite-coincidence": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/rx-lite-coincidence/-/rx-lite-coincidence-4.0.3.tgz", + "integrity": "sha512-1VNJqzE9gALUyMGypDXZZXzR0Tt7LC9DdAZQ3Ou/Q0MubNU35agVUNXKGHKpNTba+fr8GdIdkC26bRDqtCQBeQ==", + "requires": { + "@types/rx-lite": "*" + } + }, + "@types/rx-lite-experimental": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@types/rx-lite-experimental/-/rx-lite-experimental-4.0.1.tgz", + "integrity": "sha1-xTL1y98/LBXaFt7Ykw0bKYQCPL0=", + "requires": { + "@types/rx-lite": "*" + } + }, + "@types/rx-lite-joinpatterns": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@types/rx-lite-joinpatterns/-/rx-lite-joinpatterns-4.0.1.tgz", + "integrity": "sha1-9w/jcFGKhDLykVjMkv+1a05K/D4=", + "requires": { + "@types/rx-lite": "*" + } + }, + "@types/rx-lite-testing": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@types/rx-lite-testing/-/rx-lite-testing-4.0.1.tgz", + "integrity": "sha1-IbGdEfTf1v/vWp0WSOnIh5v+Iek=", + "requires": { + "@types/rx-lite-virtualtime": "*" + } + }, + "@types/rx-lite-time": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/rx-lite-time/-/rx-lite-time-4.0.3.tgz", + "integrity": "sha512-ukO5sPKDRwCGWRZRqPlaAU0SKVxmWwSjiOrLhoQDoWxZWg6vyB9XLEZViKOzIO6LnTIQBlk4UylYV0rnhJLxQw==", + "requires": { + "@types/rx-lite": "*" + } + }, + "@types/rx-lite-virtualtime": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/rx-lite-virtualtime/-/rx-lite-virtualtime-4.0.3.tgz", + "integrity": "sha512-3uC6sGmjpOKatZSVHI2xB1+dedgml669ZRvqxy+WqmGJDVusOdyxcKfyzjW0P3/GrCiN4nmRkLVMhPwHCc5QLg==", + "requires": { + "@types/rx-lite": "*" + } + }, + "@types/through": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/@types/through/-/through-0.0.29.tgz", + "integrity": "sha512-9a7C5VHh+1BKblaYiq+7Tfc+EOmjMdZaD1MYtkQjSoxgB69tBjW98ry6SKsi4zEIWztLOMRuL87A3bdT/Fc/4w==", + "requires": { + "@types/node": "*" + } + }, "ansi-escapes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.1.0.tgz", @@ -278,6 +421,11 @@ "os-tmpdir": "~1.0.2" } }, + "typescript": { + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.9.2.tgz", + "integrity": "sha512-Gr4p6nFNaoufRIY4NMdpQRNmgxVIGMs4Fcu/ujdYk3nAZqk7supzBE9idmvfZIlH/Cuj//dvi+019qEue9lV0w==" + }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", diff --git a/packages/serve/package.json b/packages/serve/package.json index 53cfdb8058c..d981a3dedfe 100644 --- a/packages/serve/package.json +++ b/packages/serve/package.json @@ -11,8 +11,18 @@ "license": "MIT", "dependencies": { "@webpack-cli/webpack-scaffold": "^0.0.8", + "@webpack-cli/utils": "^0.0.8", "chalk": "^2.4.1", "cross-spawn": "^6.0.5", "inquirer": "^5.2.0" + }, + "devDependencies": { + "@types/cross-spawn": "^6.0.0", + "@types/inquirer": "0.0.42", + "@types/node": "^10.5.1", + "typescript": "^2.9.2" + }, + "scripts": { + "build": "tsc" } } diff --git a/packages/serve/tsconfig.json b/packages/serve/tsconfig.json new file mode 100644 index 00000000000..4082f16a5d9 --- /dev/null +++ b/packages/serve/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.json" +} diff --git a/packages/utils/types/NodePath.js b/packages/utils/types/NodePath.js new file mode 100644 index 00000000000..c8ad2e549bd --- /dev/null +++ b/packages/utils/types/NodePath.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/packages/utils/types/Yeoman.js b/packages/utils/types/Yeoman.js new file mode 100644 index 00000000000..c8ad2e549bd --- /dev/null +++ b/packages/utils/types/Yeoman.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/packages/utils/types/index.js b/packages/utils/types/index.js new file mode 100644 index 00000000000..e69de29bb2d