Skip to content

Instantly share code, notes, and snippets.

@matheus1lva
Created February 9, 2018 14:22
Show Gist options
  • Save matheus1lva/efb007cd17cd5954d49f1c52f44fd1ba to your computer and use it in GitHub Desktop.
Save matheus1lva/efb007cd17cd5954d49f1c52f44fd1ba to your computer and use it in GitHub Desktop.
require.resolve problem
#!/usr/bin/env node
const { exec } = require("child_process");
const inquirer = require("inquirer");
function runCommand(command) {
exec(command, (error, stdout, stderr) => {
if(!error) {
console.log("Webpack-cli installed successfully");
return true;
}
console.log("failed to install webpack-cli");
console.error(stderr);
return false;
});
}
const path = require("path");
let webpackCliInstalled = false;
try {
require.resolve('webpack-cli');
webpackCliInstalled = true;
} catch (err) {
webpackCliInstalled = false;
}
if(!webpackCliInstalled) {
const path = require("path");
const fs = require("fs");
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
let command;
if(isYarn) {
command = "yarn add -D webpack-cli";
} else {
command = "npm install --save-dev webpack-cli";
}
const question = {
type: "confirm",
name: "shouldInstall",
message: "Would you like to install webpack-cli?",
default: true
};
console.error("The CLI moved into a separate package: webpack-cli");
inquirer.prompt(question).then((answer) => {
if(answer) {
console.error("Installing webpack-cli");
if(runCommand(command)) {
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
}
}
});
} else {
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment