-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpkg.js
112 lines (94 loc) · 2.88 KB
/
pkg.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
const fsPromises = require("fs/promises");
const fs = require("fs");
const { parseManifest } = require("./cli.js");
const { npm, npmInstall } = require("./npm.js");
const { logDetail } = require("./log.js");
const build = require("./index.js");
function extractDependencies(manifest) {
const allDependencies = {
...manifest.dependencies,
...manifest.devDependencies,
};
const localDependencies = [];
const remoteDependencies = [];
for (let [name, url] of Object.entries(allDependencies)) {
const group = url.startsWith("file:")
? localDependencies
: remoteDependencies;
group.push(name);
}
return { localDependencies, remoteDependencies };
}
class Pkg {
constructor(path) {
const manifest = parseManifest(`${path}/package.json`);
const { localDependencies, remoteDependencies } =
extractDependencies(manifest);
this.name = manifest.name;
this.path = path;
this.localDependencies = localDependencies;
this.remoteDependencies = remoteDependencies;
this.scripts = Object.keys(manifest.scripts || {});
this.processed = false;
}
async sync() {
logDetail(`🕑 ${this.name} ...`);
if (fs.existsSync("package-lock.json")) {
fs.rmSync("package-lock.json");
}
await npm("i");
}
async npmRun(script) {
if (!this.scripts.includes(script)) return false;
logDetail(`${this.name}: Executing ${script} script ...`);
await npm("run", script);
return true;
}
async clean() {
if (await this.npmRun("clean")) return;
// Just so that we don't have to add a 'clean' script to every single
// package, we cover the conventional case as a fallback - but it's
// arguably a bit dangerous.
logDetail(`${this.name}: Executing fallback clean logic ...`);
for (let path of ["node_modules", "dist"])
await fsPromises.rm(path, { recursive: true, force: true });
}
async install() {
await npmInstall(this.name);
}
async offlineInstall() {
await npmInstall(this.name, true);
}
async build() {
await this.npmRun("build");
}
async installAndBuild() {
await this.install();
await this.build();
}
async test() {
await this.npmRun("test");
}
async syncdeps() {
await this.sync();
}
async doc() {
await this.npmRun("doc");
}
async executeCommand(command) {
const oldPath = process.cwd();
try {
process.chdir(this.path);
} catch {
throw `Directory ${this.path} for package ${this.name} does not exist`;
}
try {
await this[command]();
} finally {
process.chdir(oldPath);
}
}
}
module.exports = {
Pkg,
};