Skip to content

Commit

Permalink
feat(init): Improve ex-nihilo output
Browse files Browse the repository at this point in the history
- Create package.json with 2-space indent
- Add stub name and "private": true when creating root package.json
- Detect indent of existing lerna.json
- sync -> async
  • Loading branch information
evocateur committed Mar 20, 2018
1 parent 3cb8666 commit 7b80e07
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
39 changes: 29 additions & 10 deletions commands/init/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const fs = require("fs-extra");
const pMap = require("p-map");
const writeJsonFile = require("write-json-file");
const writePkg = require("write-pkg");

Expand Down Expand Up @@ -34,23 +35,37 @@ class InitCommand extends Command {
}

execute() {
this.ensurePackageJSON();
this.ensureLernaJson();
this.ensurePackagesDir();
this.logger.success("", "Initialized Lerna files");
let chain = Promise.resolve();

chain = chain.then(() => this.ensurePackageJSON());
chain = chain.then(() => this.ensureLernaJson());
chain = chain.then(() => this.ensurePackagesDir());

return chain.then(() => {
this.logger.success("", "Initialized Lerna files");
});
}

ensurePackageJSON() {
const { packageJsonLocation } = this.repository;
let { packageJson } = this.repository;
let chain = Promise.resolve();

if (!packageJson) {
packageJson = {};
packageJson = {
name: "root",
private: true,
};
this.logger.info("", "Creating package.json");

// initialize with default indentation so write-pkg doesn't screw it up with tabs
chain = chain.then(() => writeJsonFile(packageJsonLocation, packageJson, { indent: 2 }));
} else {
this.logger.info("", "Updating package.json");
}

let targetDependencies;

if (packageJson.dependencies && packageJson.dependencies.lerna) {
// lerna is a dependency in the current project
targetDependencies = packageJson.dependencies;
Expand All @@ -59,17 +74,20 @@ class InitCommand extends Command {
if (!packageJson.devDependencies) {
packageJson.devDependencies = {};
}

targetDependencies = packageJson.devDependencies;
}

targetDependencies.lerna = this.exact ? this.lernaVersion : `^${this.lernaVersion}`;

writePkg.sync(this.repository.packageJsonLocation, packageJson);
chain = chain.then(() => writePkg(packageJsonLocation, packageJson));

return chain;
}

ensureLernaJson() {
// lernaJson already defaulted to empty object in Repository constructor
const { lernaJson, version: repositoryVersion } = this.repository;
const { lernaJson, lernaJsonLocation, version: repositoryVersion } = this.repository;

let version;

Expand All @@ -81,7 +99,7 @@ class InitCommand extends Command {
version = "0.0.0";
}

if (!this.repository.version) {
if (!repositoryVersion) {
this.logger.info("", "Creating lerna.json");
} else {
this.logger.info("", "Updating lerna.json");
Expand All @@ -102,12 +120,13 @@ class InitCommand extends Command {
initConfig.exact = true;
}

writeJsonFile.sync(this.repository.lernaJsonLocation, lernaJson, { indent: 2 });
return writeJsonFile(lernaJsonLocation, lernaJson, { indent: 2, detectIndent: true });
}

ensurePackagesDir() {
this.logger.info("", "Creating packages directory");
this.repository.packageParentDirs.map(dir => fs.mkdirpSync(dir));

return pMap(this.repository.packageParentDirs, dir => fs.mkdirp(dir));
}
}

Expand Down
1 change: 1 addition & 0 deletions commands/init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@lerna/command": "file:../../core/command",
"@lerna/git-utils": "file:../../core/git-utils",
"fs-extra": "^5.0.0",
"p-map": "^1.2.0",
"write-json-file": "^2.3.0",
"write-pkg": "^3.1.0"
}
Expand Down
2 changes: 2 additions & 0 deletions integration/__snapshots__/lerna-init.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Object {
devDependencies: Object {
lerna: "^__TEST_VERSION__",
},
name: root,
private: true,
}
`;
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

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

0 comments on commit 7b80e07

Please sign in to comment.