-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
523 lines (461 loc) · 15.8 KB
/
index.ts
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import fixturify from 'fixturify';
import tmp = require('tmp');
import fs = require('fs-extra');
import path = require('path');
import resolvePackagePath = require('resolve-package-path');
import { PackageJson } from 'type-fest';
tmp.setGracefulCleanup();
function deserializePackageJson(serialized: string): PackageJson {
return JSON.parse(serialized);
}
function keys(object: any) {
if (object !== null && (typeof object === 'object' || Array.isArray(object))) {
return Object.keys(object);
} else {
return [];
}
}
function isProjectCallback(maybe: ProjectCallback | any): maybe is ProjectCallback {
return typeof maybe === 'function';
}
function getString<Obj extends Object, KeyOfObj extends keyof Obj>(
obj: Obj,
propertyName: KeyOfObj,
errorMessage?: string
): string {
const value = obj[propertyName];
if (typeof value === 'string') {
return value;
} else {
throw new TypeError(errorMessage || `expected 'string' but got '${typeof value}'`);
}
}
/**
A utility method access a file from a DirJSON that is type-safe and runtime safe.
```ts
getFile(folder, 'package.json') // the files content, or it will throw
```
*/
function getFile<Dir extends fixturify.DirJSON, FileName extends keyof Dir>(dir: Dir, fileName: FileName): string {
const value = dir[fileName];
if (typeof value === 'string') {
return value;
} else if (typeof value === 'object' && value !== null) {
throw new TypeError(`Expected a file for name '${fileName}' but got a 'Folder'`);
} else {
throw new TypeError(`Expected a file for name '${fileName}' but got '${typeof value}'`);
}
}
/**
A utility method access a file from a DirJSON that is type-safe and runtime safe
```ts
getFolder(folder, 'node_modules') // => the DirJSON of folder['node_module'] or it will throw
```
*/
function getFolder<Dir extends fixturify.DirJSON, FileName extends keyof Dir>(
dir: Dir,
fileName: FileName
): fixturify.DirJSON {
const value = dir[fileName];
if (isDirJSON(value)) {
return value;
} else if (typeof value === 'string') {
throw new TypeError(`Expected a file for name '${fileName}' but got 'File'`);
} else {
throw new TypeError(`Expected a folder for name '${fileName}' but got '${typeof value}'`);
}
}
function isDirJSON(value: any): value is fixturify.DirJSON {
return typeof value === 'object' && value !== null;
}
function getPackageName(pkg: PackageJson): string {
return getString(pkg, 'name', `package.json is missing a name.`);
}
function getPackageVersion(pkg: PackageJson): string {
return getString(pkg, 'version', `${getPackageName(pkg)}'s package.json is missing a version.`);
}
interface ReadDirOpts {
linkDeps?: boolean;
}
// This only shallow-merges with any user-provided files, which is OK right now
// because this is only one level deep. If we ever make it deeper, we'll need to
// switch to a proper deep merge.
const defaultFiles = {
'index.js': `
'use strict';
module.exports = {};`,
};
export interface ProjectArgs {
name?: string;
version?: string;
files?: fixturify.DirJSON;
requestedRange?: string;
}
type ProjectCallback = (project: Project) => void;
export class Project {
pkg: PackageJson;
files: fixturify.DirJSON;
readonly isDependency = true;
private _dependencies: { [name: string]: Project } = {};
private _devDependencies: { [name: string]: Project } = {};
private _baseDir: string | undefined;
private _tmp: tmp.SynchrounousResult | undefined;
// when used as a dependency in another Project, this is the semver range it
// will appear as within the parent's package.json
private requestedRange: string;
private dependencyLinks: Map<string, { dir: string; requestedRange: string }> = new Map();
private linkIsDevDependency: Set<string> = new Set();
constructor(
name?: string,
version?: string,
args?: Omit<ProjectArgs, 'name' | 'version'>,
projectCallback?: ProjectCallback
);
constructor(name?: string, version?: string, args?: Omit<ProjectArgs, 'name' | 'version'>);
constructor(name?: string, version?: string, projectCallback?: ProjectCallback);
constructor(name?: string, args?: Omit<ProjectArgs, 'name'>, projectCallback?: ProjectCallback);
constructor(args?: ProjectArgs, projectCallback?: ProjectCallback);
constructor(
first?: string | ProjectArgs,
second?: string | Omit<ProjectArgs, 'name'> | ProjectCallback,
third?: Omit<ProjectArgs, 'name' | 'version'> | ProjectCallback,
fourth?: ProjectCallback
) {
let name: string | undefined;
let version: string | undefined;
let files: fixturify.DirJSON | undefined;
let requestedRange: string | undefined;
if (first == null) {
// all optional args stay undefined
} else if (typeof first === 'string') {
name = first;
if (typeof second === 'string') {
version = second;
if (third) {
if (!isProjectCallback(third)) {
({ files, requestedRange } = third);
}
}
} else {
if (second) {
if (!isProjectCallback(second)) {
({ version, files, requestedRange } = second);
}
}
}
} else {
({ name, version, files, requestedRange } = first);
}
let pkg: PackageJson = {};
if (files && typeof files?.['package.json'] === 'string') {
pkg = JSON.parse(files['package.json']);
files = Object.assign({}, files);
delete files['package.json'];
}
this.pkg = Object.assign({}, pkg, {
name: name || pkg.name || 'a-fixturified-project',
version: version || pkg.version || '0.0.0',
keywords: pkg.keywords || [],
});
if (files) {
this.files = { ...defaultFiles, ...files };
} else {
this.files = defaultFiles;
}
this.requestedRange = requestedRange || this.pkg.version!;
const arity = arguments.length;
if (arity > 1) {
fourth;
const projectCallback = arguments[arity - 1];
if (isProjectCallback(projectCallback)) {
projectCallback(this);
}
}
}
get root() {
throw new Error('.root has been removed, please review the readme but you likely actually want .baseDir now');
}
set baseDir(dir: string) {
if (this._baseDir) {
throw new Error(`this Project already has a baseDir`);
}
this._baseDir = dir;
}
get baseDir() {
if (!this._baseDir) {
throw new Error(
`this project has no baseDir yet. Either set one manually or call writeSync to have one chosen for you`
);
}
return this._baseDir;
}
private autoBaseDir(): string {
if (!this._baseDir) {
this._tmp = tmp.dirSync({ unsafeCleanup: true });
this._baseDir = fs.realpathSync(this._tmp.name);
}
return this._baseDir;
}
get name(): string {
return getPackageName(this.pkg);
}
set name(value: string) {
this.pkg.name = value;
}
get version(): string {
return getPackageVersion(this.pkg);
}
set version(value: string) {
this.pkg.version = value;
}
writeSync() {
this.autoBaseDir();
fixturify.writeSync(this.baseDir, this.files);
fs.outputJSONSync(path.join(this.baseDir, 'package.json'), this.pkgJSONWithDeps(), { spaces: 2 });
for (let [name, { dir: target }] of this.dependencyLinks) {
fs.ensureSymlinkSync(target, path.join(this.baseDir, 'node_modules', name), 'dir');
}
for (let dep of this.dependencyProjects()) {
dep.baseDir = path.join(this.baseDir, 'node_modules', dep.name);
dep.writeSync();
}
for (let dep of this.devDependencyProjects()) {
dep.baseDir = path.join(this.baseDir, 'node_modules', dep.name);
dep.writeSync();
}
}
static fromDir(root: string, opts?: ReadDirOpts): Project {
let project = new Project();
project.readSync(root, opts);
return project;
}
private readSync(root: string, opts?: ReadDirOpts): void {
const files = fixturify.readSync(root, {
// when linking deps, we don't need to crawl all of node_modules
ignore: opts?.linkDeps ? ['node_modules'] : [],
});
this.pkg = deserializePackageJson(getFile(files, 'package.json'));
delete files['package.json'];
this.files = files;
if (opts?.linkDeps) {
if (this.pkg.dependencies) {
for (let dep of Object.keys(this.pkg.dependencies)) {
this.linkDependency(dep, { baseDir: path.join(root, this.name) });
}
}
if (this.pkg.devDependencies) {
for (let dep of Object.keys(this.pkg.devDependencies)) {
this.linkDevDependency(dep, { baseDir: path.join(root, this.name) });
}
}
} else {
const nodeModules = getFolder(files, 'node_modules');
delete files['node_modules'];
keys(this.pkg.dependencies).forEach(dependency => {
this.addDependency(
new (this.constructor as typeof Project)({ files: unwrapPackageName(nodeModules, dependency) })
);
});
keys(this.pkg.devDependencies).forEach(dependency => {
this.addDevDependency(
new (this.constructor as typeof Project)({ files: unwrapPackageName(nodeModules, dependency) })
);
});
}
}
addDependency(
name?: string,
version?: string,
args?: Omit<ProjectArgs, 'name' | 'version'>,
projectCallback?: ProjectCallback
): Project;
addDependency(name?: string, version?: string, projectCallback?: ProjectCallback): Project;
addDependency(name?: string, args?: Omit<ProjectArgs, 'name'>, projectCallback?: ProjectCallback): Project;
addDependency(args?: ProjectArgs, projectCallback?: ProjectCallback): Project;
addDependency(args?: Project, projectCallback?: ProjectCallback): Project;
addDependency(
first?: string | ProjectArgs | Project,
second?: string | Omit<ProjectArgs, 'name'> | ProjectCallback,
third?: Omit<ProjectArgs, 'name' | 'version'> | ProjectCallback,
fourth?: ProjectCallback
): Project {
let projectCallback;
const arity = arguments.length;
if (arity > 1) {
fourth;
const maybeProjectCallback = arguments[arity - 1];
if (isProjectCallback(maybeProjectCallback)) {
projectCallback = maybeProjectCallback;
}
}
if (isProjectCallback(second)) {
second = undefined;
}
if (isProjectCallback(third)) {
third = undefined;
}
return this.addDep(first, second, third, '_dependencies', projectCallback);
}
private addDep(
first: string | ProjectArgs | Project | undefined,
second: string | Omit<ProjectArgs, 'name'> | undefined,
third: Omit<ProjectArgs, 'name' | 'version'> | undefined,
target: '_dependencies' | '_devDependencies',
projectCallback?: ProjectCallback
): Project {
let dep;
if (first == null) {
dep = new Project();
} else if (typeof first === 'string') {
let name = first;
if (typeof second === 'string') {
let version = second;
dep = new Project(name, version, third, projectCallback);
} else {
dep = new Project(name, second, projectCallback);
}
} else if ('isDependency' in first) {
dep = first;
} else {
dep = new Project(first, projectCallback);
}
this[target][dep.name] = dep;
this.dependencyLinks.delete(dep.name);
this.linkIsDevDependency.delete(dep.name);
if (isProjectCallback(projectCallback)) {
projectCallback(dep);
}
return dep;
}
removeDependency(name: string) {
delete this._dependencies[name];
this.dependencyLinks.delete(name);
this.linkIsDevDependency.delete(name);
}
removeDevDependency(name: string) {
delete this._devDependencies[name];
this.dependencyLinks.delete(name);
this.linkIsDevDependency.delete(name);
}
addDevDependency(
name?: string,
version?: string,
args?: Omit<ProjectArgs, 'name' | 'version'>,
projectCallback?: ProjectCallback
): Project;
addDevDependency(name?: string, version?: string, projectCallback?: ProjectCallback): Project;
addDevDependency(name?: string, args?: Omit<ProjectArgs, 'name'>, projectCallback?: ProjectCallback): Project;
addDevDependency(args?: ProjectArgs, projectCallback?: ProjectCallback): Project;
addDevDependency(args?: Project, projectCallback?: ProjectCallback): Project;
addDevDependency(
first?: string | ProjectArgs | Project,
second?: string | Omit<ProjectArgs, 'name'> | ProjectCallback,
third?: Omit<ProjectArgs, 'name' | 'version'> | ProjectCallback,
fourth?: ProjectCallback
): Project {
let projectCallback;
const arity = arguments.length;
if (arity > 1) {
fourth;
const maybeProjectCallback = arguments[arity - 1];
if (isProjectCallback(maybeProjectCallback)) {
projectCallback = maybeProjectCallback;
}
}
if (isProjectCallback(second)) {
second = undefined;
}
if (isProjectCallback(third)) {
third = undefined;
}
return this.addDep(first, second, third, '_devDependencies', projectCallback);
}
linkDependency(
name: string,
opts:
| { baseDir: string; resolveName?: string; requestedRange?: string }
| { target: string; requestedRange?: string }
) {
this.removeDependency(name);
this.removeDevDependency(name);
let dir: string;
if ('baseDir' in opts) {
let pkgJSONPath = resolvePackagePath(opts.resolveName || name, opts.baseDir);
if (!pkgJSONPath) {
throw new Error(`failed to locate ${opts.resolveName || name} in ${opts.baseDir}`);
}
dir = path.dirname(pkgJSONPath);
} else {
dir = opts.target;
}
let requestedRange = opts?.requestedRange ?? fs.readJsonSync(path.join(dir, 'package.json')).version;
this.dependencyLinks.set(name, { dir, requestedRange });
}
linkDevDependency(name: string, opts: { baseDir: string; resolveName?: string } | { target: string }) {
this.linkDependency(name, opts);
this.linkIsDevDependency.add(name);
}
dependencyProjects() {
return Object.keys(this._dependencies).map(dependency => this._dependencies[dependency]);
}
devDependencyProjects() {
return Object.keys(this._devDependencies).map(dependency => this._devDependencies[dependency]);
}
private pkgJSONWithDeps(): PackageJson {
let dependencies = this.depsToObject(this.dependencyProjects());
let devDependencies = this.depsToObject(this.devDependencyProjects());
for (let [name, { requestedRange }] of this.dependencyLinks.entries()) {
if (this.linkIsDevDependency.has(name)) {
devDependencies[name] = requestedRange;
} else {
dependencies[name] = requestedRange;
}
}
return Object.assign(this.pkg, {
dependencies,
devDependencies,
});
}
clone(): Project {
let cloned: Project = new (this.constructor as typeof Project)();
cloned.pkg = JSON.parse(JSON.stringify(this.pkg));
cloned.files = JSON.parse(JSON.stringify(this.files));
for (let [name, depProject] of Object.entries(this._dependencies)) {
cloned._dependencies[name] = depProject.clone();
}
for (let [name, depProject] of Object.entries(this._devDependencies)) {
cloned._devDependencies[name] = depProject.clone();
}
cloned.dependencyLinks = new Map(this.dependencyLinks);
cloned.linkIsDevDependency = new Set(this.linkIsDevDependency);
cloned.requestedRange = this.requestedRange;
return cloned;
}
dispose() {
if (this._tmp) {
this._tmp.removeCallback();
}
}
private depsToObject(deps: Project[]) {
let obj: { [name: string]: string } = {};
deps.forEach(dep => (obj[dep.name] = dep.requestedRange));
return obj;
}
}
function parseScoped(name: string) {
let matched = name.match(/(@[^@\/]+)\/(.*)/);
if (matched) {
return {
scope: matched[1],
name: matched[2],
};
}
return null;
}
function unwrapPackageName(obj: any, packageName: string): fixturify.DirJSON {
let scoped = parseScoped(packageName);
if (scoped) {
return getFolder(getFolder(obj, scoped.scope), scoped.name);
}
return getFolder(obj, packageName);
}