Skip to content

Commit

Permalink
feat(serve): add typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvdutt committed Jul 6, 2018
1 parent 4b574d9 commit d313421
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 53 deletions.
1 change: 1 addition & 0 deletions packages/serve/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.js
2 changes: 2 additions & 0 deletions packages/serve/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tsconfig.json
*.ts
110 changes: 57 additions & 53 deletions packages/serve/index.js → packages/serve/index.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
*
Expand All @@ -16,9 +15,9 @@ const processPromise = require("@webpack-cli/utils/resolve-packages")
* @returns {Void}
*/

const spawnNPMWithArg = cmd =>
const spawnNPMWithArg = (cmd: string): SpawnSyncReturns<Buffer> =>
spawn.sync("npm", ["install", "webpack-dev-server", cmd], {
stdio: "inherit"
stdio: "inherit",
});

/**
Expand All @@ -29,9 +28,9 @@ const spawnNPMWithArg = cmd =>
* @returns {Void}
*/

const spawnYarnWithArg = cmd =>
const spawnYarnWithArg = (cmd: string): SpawnSyncReturns<Buffer> =>
spawn.sync("yarn", ["add", "webpack-dev-server", cmd], {
stdio: "inherit"
stdio: "inherit",
});

/**
Expand All @@ -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);

/**
*
Expand All @@ -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);
}
Expand All @@ -94,81 +93,86 @@ 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<Buffer>;
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 {
console.log(chalk.bold.red("✖ Serve aborted due cancelling"));
process.exitCode = 1;
}
})
.catch(err => {
.catch((err: object) => {
console.log(chalk.red("✖ Serve aborted due to some errors"));
console.error(err);
process.exitCode = 1;
});
}
}

module.exports = {
serve,
export = {
getRootPathModule,
serve,
spawnNPMWithArg,
spawnYarnWithArg
spawnYarnWithArg,
};
148 changes: 148 additions & 0 deletions packages/serve/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d313421

Please sign in to comment.