diff --git a/src/commands/build/index.js b/src/commands/build/index.js index a63c741..b1b4b69 100644 --- a/src/commands/build/index.js +++ b/src/commands/build/index.js @@ -1,7 +1,7 @@ /* @flow */ import runWebpackBuilder from "./webpack-build"; import createParams from "./../../utils/params"; -import { installAllModules } from "../../utils/npm"; +import { installAllModules, createPackageJson } from "../../utils/npm"; /** * Aik build command @@ -9,6 +9,7 @@ import { installAllModules } from "../../utils/npm"; export default function aikBuild(input: string[], flags: CLIFlags): Promise<*> { const [filename] = input; const params = createParams(filename, flags, "", true); + createPackageJson(process.cwd()); installAllModules(process.cwd()); return runWebpackBuilder(filename, flags, params); } diff --git a/src/commands/dev-server/index.js b/src/commands/dev-server/index.js index 4714839..834cf3e 100644 --- a/src/commands/dev-server/index.js +++ b/src/commands/dev-server/index.js @@ -17,7 +17,8 @@ import { import { installAllModules, isModuleInstalled, - installModule + installModule, + createPackageJson } from "../../utils/npm"; export function requestCreatingAnEntryPoint( @@ -96,6 +97,8 @@ export default async function aikDevServer( await prepareEntryPoint(filename); print(devServerInvalidBuildMsg(), /* clear console */ true); + + createPackageJson(process.cwd()); installAllModules(process.cwd()); if (flags.react) { diff --git a/src/utils/__test__/__snapshots__/messages.test.js.snap b/src/utils/__test__/__snapshots__/messages.test.js.snap index b369c44..f7c47b0 100644 --- a/src/utils/__test__/__snapshots__/messages.test.js.snap +++ b/src/utils/__test__/__snapshots__/messages.test.js.snap @@ -93,6 +93,14 @@ accidentally updated versions of npm modules Aik will run \\"npm install\\" exports[`Common Messages #installingModuleMsg 1`] = `"Installing module \\"react\\" ..."`; +exports[`Common Messages #packageJsonHasNotBeenFound 1`] = ` +" WARNING  File \\"package.json\\" hasn't been found. + +In order to make subsequent builds more predictable Aik needs to create one. + + WAIT  Creating package.json..." +`; + exports[`Dev Server Messages #devServerBanner all flags enabled 1`] = ` "Entry point: ./src/index.js Custom template: index.html diff --git a/src/utils/__test__/messages.test.js b/src/utils/__test__/messages.test.js index 10d3622..664c5e8 100644 --- a/src/utils/__test__/messages.test.js +++ b/src/utils/__test__/messages.test.js @@ -6,6 +6,7 @@ import { fileDoesNotExistMsg, foundPackageJson, installingModuleMsg, + packageJsonHasNotBeenFound, devServerBanner, devServerInvalidBuildMsg, devServerCompiledSuccessfullyMsg, @@ -76,6 +77,10 @@ describe("Common Messages", () => { test("#installingModuleMsg", () => { expect(print(installingModuleMsg("react"))).toMatchSnapshot(); }); + + test("#packageJsonHasNotBeenFound", () => { + expect(print(packageJsonHasNotBeenFound())).toMatchSnapshot(); + }); }); describe("Dev Server Messages", () => { diff --git a/src/utils/messages.js b/src/utils/messages.js index 283e0c6..074e889 100644 --- a/src/utils/messages.js +++ b/src/utils/messages.js @@ -124,6 +124,20 @@ export function foundPackageJson(): string[] { ]; } +export function packageJsonHasNotBeenFound(): string[] { + return [ + warningBadge() + + " " + + chalk.yellow('File "package.json" hasn\'t been found.'), + "", + `In order to make subsequent builds more ${chalk.yellow( + "predictable" + )} Aik needs to create one.`, + "", + waitBadge() + " " + chalk.blue("Creating package.json...") + ]; +} + export function installingModuleMsg(moduleName: string): string[] { return [`Installing module "${chalk.yellow(moduleName)}" ...`]; } diff --git a/src/utils/npm.js b/src/utils/npm.js index dda0115..628b8ea 100644 --- a/src/utils/npm.js +++ b/src/utils/npm.js @@ -5,8 +5,10 @@ import path from "path"; import resolveModule from "resolve"; import { print, + addTopSpace, addBottomSpace, installingModuleMsg, + packageJsonHasNotBeenFound, foundPackageJson } from "./messages"; @@ -19,11 +21,6 @@ export function isModuleInstalled(moduleName: string): boolean { } } -export function installModule(moduleName: string) { - execSync(`npm install ${moduleName} --silent`, { cwd: process.cwd() }); - print(installingModuleMsg(moduleName)); -} - export function hasPackageJson(cwd: string) { try { fs.statSync(path.join(cwd, "package.json")); @@ -33,6 +30,28 @@ export function hasPackageJson(cwd: string) { } } +export function hasDependencies(cwd: string): boolean { + try { + const packageJson = JSON.parse( + fs.readFileSync(path.join(cwd, "package.json"), "utf8") + ); + return packageJson.dependencies || packageJson.devDependencies; + } catch (error) { + return false; + } +} + +export function createPackageJson(cwd: string) { + if (hasPackageJson(cwd)) return; + print(addBottomSpace(addTopSpace(packageJsonHasNotBeenFound()))); + execSync(`npm init -y`, { cwd, stdio: "inherit" }); +} + +export function installModule(moduleName: string) { + execSync(`npm install ${moduleName} --silent`, { cwd: process.cwd() }); + print(installingModuleMsg(moduleName)); +} + export function hasNodeModules(cwd: string) { try { fs.statSync(path.join(cwd, "node_modules")); @@ -43,8 +62,10 @@ export function hasNodeModules(cwd: string) { } export function installAllModules(cwd: string) { - if (!hasPackageJson(cwd)) return; - if (hasNodeModules(cwd)) return; + if (!hasPackageJson(cwd) || hasNodeModules(cwd) || !hasDependencies(cwd)) { + return; + } + print(addBottomSpace(foundPackageJson()), /* clear console */ true); spawnSync("npm", ["install", "--silent"], { cwd, stdio: "inherit" }); }