Skip to content

Commit 7b80e07

Browse files
committed
feat(init): Improve ex-nihilo output
- 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
1 parent 3cb8666 commit 7b80e07

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

commands/init/index.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const fs = require("fs-extra");
4+
const pMap = require("p-map");
45
const writeJsonFile = require("write-json-file");
56
const writePkg = require("write-pkg");
67

@@ -34,23 +35,37 @@ class InitCommand extends Command {
3435
}
3536

3637
execute() {
37-
this.ensurePackageJSON();
38-
this.ensureLernaJson();
39-
this.ensurePackagesDir();
40-
this.logger.success("", "Initialized Lerna files");
38+
let chain = Promise.resolve();
39+
40+
chain = chain.then(() => this.ensurePackageJSON());
41+
chain = chain.then(() => this.ensureLernaJson());
42+
chain = chain.then(() => this.ensurePackagesDir());
43+
44+
return chain.then(() => {
45+
this.logger.success("", "Initialized Lerna files");
46+
});
4147
}
4248

4349
ensurePackageJSON() {
50+
const { packageJsonLocation } = this.repository;
4451
let { packageJson } = this.repository;
52+
let chain = Promise.resolve();
4553

4654
if (!packageJson) {
47-
packageJson = {};
55+
packageJson = {
56+
name: "root",
57+
private: true,
58+
};
4859
this.logger.info("", "Creating package.json");
60+
61+
// initialize with default indentation so write-pkg doesn't screw it up with tabs
62+
chain = chain.then(() => writeJsonFile(packageJsonLocation, packageJson, { indent: 2 }));
4963
} else {
5064
this.logger.info("", "Updating package.json");
5165
}
5266

5367
let targetDependencies;
68+
5469
if (packageJson.dependencies && packageJson.dependencies.lerna) {
5570
// lerna is a dependency in the current project
5671
targetDependencies = packageJson.dependencies;
@@ -59,17 +74,20 @@ class InitCommand extends Command {
5974
if (!packageJson.devDependencies) {
6075
packageJson.devDependencies = {};
6176
}
77+
6278
targetDependencies = packageJson.devDependencies;
6379
}
6480

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

67-
writePkg.sync(this.repository.packageJsonLocation, packageJson);
83+
chain = chain.then(() => writePkg(packageJsonLocation, packageJson));
84+
85+
return chain;
6886
}
6987

7088
ensureLernaJson() {
7189
// lernaJson already defaulted to empty object in Repository constructor
72-
const { lernaJson, version: repositoryVersion } = this.repository;
90+
const { lernaJson, lernaJsonLocation, version: repositoryVersion } = this.repository;
7391

7492
let version;
7593

@@ -81,7 +99,7 @@ class InitCommand extends Command {
8199
version = "0.0.0";
82100
}
83101

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

105-
writeJsonFile.sync(this.repository.lernaJsonLocation, lernaJson, { indent: 2 });
123+
return writeJsonFile(lernaJsonLocation, lernaJson, { indent: 2, detectIndent: true });
106124
}
107125

108126
ensurePackagesDir() {
109127
this.logger.info("", "Creating packages directory");
110-
this.repository.packageParentDirs.map(dir => fs.mkdirpSync(dir));
128+
129+
return pMap(this.repository.packageParentDirs, dir => fs.mkdirp(dir));
111130
}
112131
}
113132

commands/init/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@lerna/command": "file:../../core/command",
3535
"@lerna/git-utils": "file:../../core/git-utils",
3636
"fs-extra": "^5.0.0",
37+
"p-map": "^1.2.0",
3738
"write-json-file": "^2.3.0",
3839
"write-pkg": "^3.1.0"
3940
}

integration/__snapshots__/lerna-init.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Object {
1414
devDependencies: Object {
1515
lerna: "^__TEST_VERSION__",
1616
},
17+
name: root,
18+
private: true,
1719
}
1820
`;
1921

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)