Skip to content

Commit

Permalink
feat: Pre-installing npm modules if project contains package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Apr 24, 2017
1 parent 400440c commit 7015901
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/commands/build/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* @flow */
import runWebpackBuilder from "./webpack-build";
import createParams from "./../../utils/params";
import preinstallNpmModules from "../../preinstall-npm-modules";

/**
* Aik build command
*/
export default function aikBuild(input: string[], flags: CLIFlags): Promise<*> {
const [filename] = input;
const params = createParams(filename, flags, "", true);
preinstallNpmModules(process.cwd());
return runWebpackBuilder(filename, flags, params);
}
4 changes: 3 additions & 1 deletion src/commands/dev-server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* @flow */

import { execSync } from "child_process";
import fs from "fs";
import readline from "readline";
Expand All @@ -17,6 +16,7 @@ import {
devServerInstallingModuleMsg,
devServerSkipInstallingModuleMsg
} from "./../../utils/messages";
import preinstallNpmModules from "../../preinstall-npm-modules";

export function requestCreatingAnEntryPoint(
filename: string
Expand Down Expand Up @@ -82,12 +82,14 @@ export default async function aikDevServer(
const shouldCreateAnEntryPoint = await requestCreatingAnEntryPoint(
filename
);

if (shouldCreateAnEntryPoint) {
await createFile(filename);
}
}

devServerInvalidBuildMsg();
preinstallNpmModules(process.cwd());

if (flags.react) {
devServerReactRequired();
Expand Down
30 changes: 30 additions & 0 deletions src/preinstall-npm-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* @flow */
import { spawnSync } from "child_process";
import fs from "fs";
import path from "path";
import { foundPackageJson } from "./utils/messages";

export function hasPackageJson(cwd: string) {
try {
fs.statSync(path.join(cwd, "package.json"));
return true;
} catch (error) {
return false;
}
}

export function hasNodeModules(cwd: string) {
try {
fs.statSync(path.join(cwd, "node_modules"));
return true;
} catch (error) {
return false;
}
}

export default function preinstallNpmModules(cwd: string) {
if (!hasPackageJson(cwd)) return;
if (hasNodeModules(cwd)) return;
foundPackageJson();
spawnSync("npm", ["install", "--silent"], { cwd, stdio: "inherit" });
}
18 changes: 17 additions & 1 deletion src/utils/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ export function fileDoesNotExistMsg(filename: string) {
]);
}

export function foundPackageJson() {
return print([
"",
warningBadge() +
" " +
chalk.yellow('File "package.json" has been discovered.'),
"",
`Since ${chalk.yellow('"node_modules"')} folder doesn't exist and in order to avoid possible artifacts caused by`,
`accidentally updated versions of npm modules Aik will run ${chalk.yellow('"npm install"')} in current directory.`,
"",
waitBadge() + " " + chalk.blue("Installing npm modules..."),
""
]);
}

/**
*
* Dev Server Messages
Expand Down Expand Up @@ -193,11 +208,12 @@ export function devServerModuleDoesntExists(module: string, filename: string) {

export function devServerReactRequired() {
return print([
"",
warningBadge() + " " + chalk.yellow('"react" required.'),
"",
'In order to make "react-hot-loader" work, "react" and "react-dom" are required.',
"",
chalk.blue("Installing required modules..."),
waitBadge() + " " + chalk.blue("Installing required modules..."),
""
]);
}
Expand Down

0 comments on commit 7015901

Please sign in to comment.