Skip to content

Commit

Permalink
feat: Create package.json automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Jun 18, 2017
1 parent c3d5518 commit 28312c0
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/commands/build/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* @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
*/
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);
}
5 changes: 4 additions & 1 deletion src/commands/dev-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
import {
installAllModules,
isModuleInstalled,
installModule
installModule,
createPackageJson
} from "../../utils/npm";

export function requestCreatingAnEntryPoint(
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/__test__/__snapshots__/messages.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/utils/__test__/messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
fileDoesNotExistMsg,
foundPackageJson,
installingModuleMsg,
packageJsonHasNotBeenFound,
devServerBanner,
devServerInvalidBuildMsg,
devServerCompiledSuccessfullyMsg,
Expand Down Expand Up @@ -76,6 +77,10 @@ describe("Common Messages", () => {
test("#installingModuleMsg", () => {
expect(print(installingModuleMsg("react"))).toMatchSnapshot();
});

test("#packageJsonHasNotBeenFound", () => {
expect(print(packageJsonHasNotBeenFound())).toMatchSnapshot();
});
});

describe("Dev Server Messages", () => {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}" ...`];
}
Expand Down
35 changes: 28 additions & 7 deletions src/utils/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import path from "path";
import resolveModule from "resolve";
import {
print,
addTopSpace,
addBottomSpace,
installingModuleMsg,
packageJsonHasNotBeenFound,
foundPackageJson
} from "./messages";

Expand All @@ -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"));
Expand All @@ -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"));
Expand All @@ -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" });
}
Expand Down

0 comments on commit 28312c0

Please sign in to comment.