/
index.js
209 lines (168 loc) · 5.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"use strict";
const cosmiconfig = require("cosmiconfig");
const dedent = require("dedent");
const globby = require("globby");
const globParent = require("glob-parent");
const loadJsonFile = require("load-json-file");
const log = require("npmlog");
const pMap = require("p-map");
const path = require("path");
const writeJsonFile = require("write-json-file");
const ValidationError = require("@lerna/validation-error");
const Package = require("@lerna/package");
const applyExtends = require("./lib/apply-extends");
const deprecateConfig = require("./lib/deprecate-config");
const { makeFileFinder, makeSyncFileFinder } = require("./lib/make-file-finder");
class Project {
constructor(cwd) {
const explorer = cosmiconfig("lerna", {
searchPlaces: ["lerna.json", "package.json"],
transform(obj) {
// cosmiconfig returns null when nothing is found
if (!obj) {
return {
// No need to distinguish between missing and empty,
// saves a lot of noisy guards elsewhere
config: {},
// path.resolve(".", ...) starts from process.cwd()
filepath: path.resolve(cwd || ".", "lerna.json"),
};
}
// rename deprecated durable config
deprecateConfig(obj.config, obj.filepath);
obj.config = applyExtends(obj.config, path.dirname(obj.filepath));
return obj;
},
});
let loaded;
try {
loaded = explorer.searchSync(cwd);
} catch (err) {
// redecorate JSON syntax errors, avoid debug dump
if (err.name === "JSONError") {
throw new ValidationError(err.name, err.message);
}
// re-throw other errors, could be ours or third-party
throw err;
}
this.config = loaded.config;
this.rootConfigLocation = loaded.filepath;
this.rootPath = path.dirname(loaded.filepath);
log.verbose("rootPath", this.rootPath);
}
get version() {
return this.config.version;
}
set version(val) {
this.config.version = val;
}
get packageConfigs() {
if (this.config.useWorkspaces) {
const workspaces = this.manifest.get("workspaces");
if (!workspaces) {
throw new ValidationError(
"EWORKSPACES",
dedent`
Yarn workspaces need to be defined in the root package.json.
See: https://github.com/lerna/lerna/blob/master/commands/bootstrap/README.md#--use-workspaces
`
);
}
return workspaces.packages || workspaces;
}
return this.config.packages || [Project.PACKAGE_GLOB];
}
get packageParentDirs() {
return this.packageConfigs.map(globParent).map(parentDir => path.resolve(this.rootPath, parentDir));
}
get manifest() {
let manifest;
try {
const manifestLocation = path.join(this.rootPath, "package.json");
const packageJson = loadJsonFile.sync(manifestLocation);
if (!packageJson.name) {
// npm-lifecycle chokes if this is missing, so default like npm init does
packageJson.name = path.basename(path.dirname(manifestLocation));
}
// Encapsulate raw JSON in Package instance
manifest = new Package(packageJson, this.rootPath);
// redefine getter to lazy-loaded value
Object.defineProperty(this, "manifest", {
value: manifest,
});
} catch (err) {
// redecorate JSON syntax errors, avoid debug dump
if (err.name === "JSONError") {
throw new ValidationError(err.name, err.message);
}
// try again next time
}
return manifest;
}
get licensePath() {
let licensePath;
try {
const search = globby.sync(Project.LICENSE_GLOB, {
cwd: this.rootPath,
absolute: true,
case: false,
// Project license is always a sibling of the root manifest
deep: false,
// POSIX results always need to be normalized
transform: fp => path.normalize(fp),
});
licensePath = search.shift();
if (licensePath) {
// redefine getter to lazy-loaded value
Object.defineProperty(this, "licensePath", {
value: licensePath,
});
}
} catch (err) {
/* istanbul ignore next */
throw new ValidationError(err.name, err.message);
}
return licensePath;
}
get fileFinder() {
const finder = makeFileFinder(this.rootPath, this.packageConfigs);
// redefine getter to lazy-loaded value
Object.defineProperty(this, "fileFinder", {
value: finder,
});
return finder;
}
getPackages() {
const mapper = packageConfigPath =>
loadJsonFile(packageConfigPath).then(
packageJson => new Package(packageJson, path.dirname(packageConfigPath), this.rootPath)
);
return this.fileFinder("package.json", filePaths => pMap(filePaths, mapper, { concurrency: 50 }));
}
getPackagesSync() {
return makeSyncFileFinder(this.rootPath, this.packageConfigs)("package.json", packageConfigPath => {
return new Package(
loadJsonFile.sync(packageConfigPath),
path.dirname(packageConfigPath),
this.rootPath
);
});
}
getPackageLicensePaths() {
return this.fileFinder(Project.LICENSE_GLOB, null, { case: false });
}
isIndependent() {
return this.version === "independent";
}
serializeConfig() {
// TODO: might be package.json prop
return writeJsonFile(this.rootConfigLocation, this.config, { indent: 2, detectIndent: true }).then(
() => this.rootConfigLocation
);
}
}
Project.PACKAGE_GLOB = "packages/*";
Project.LICENSE_GLOB = "LICEN{S,C}E{,.*}";
module.exports = Project;
module.exports.getPackages = cwd => new Project(cwd).getPackages();
module.exports.getPackagesSync = cwd => new Project(cwd).getPackagesSync();