-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
107 lines (95 loc) · 2.64 KB
/
main.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
/// <reference path="./index.d.ts" />
// @ts-check
const path = require("path");
const fs = require("fs-extra");
const { getPkgJson } = require("./lib/dependences");
const { preProcessParams } = require("./lib/params");
const { installModules, installModulesSync } = require("./lib/modules");
const { getPackages, getPackagesSync } = require("@manypkg/get-packages");
/**
* Install dependencies for a package.json
*
* will ignore monorepo packages by default
* @param {InstallParamsType} params
*/
async function installPkg(params = {}) {
const {
cwd,
source,
dist,
command,
cacheDir,
outputJson,
generateJson,
onBeforeInstall,
} = preProcessParams(params);
// generate package.json
const sourcePackageJson = require(path.resolve(cwd, source));
const { packages } = await getPackages(cwd);
let pkgJson = getPkgJson(sourcePackageJson, packages);
if (generateJson) {
pkgJson = await generateJson(JSON.parse(JSON.stringify(pkgJson)));
}
const pkgJsonStr = JSON.stringify(pkgJson, null, 2);
await fs.ensureDir(dist);
// output new package.json to dist
if (outputJson) {
if (outputJson === true) {
await fs.writeFile(path.resolve(dist, "package.json"), pkgJsonStr);
} else {
await fs.writeFile(path.resolve(dist, outputJson), pkgJsonStr);
}
}
// get cache path
onBeforeInstall && (await onBeforeInstall({ pkgJson }));
await installModules({ pkgJsonStr, cacheDir, dist, command });
return {
pkgJson,
};
}
/**
* Install dependencies for a package.json
*
* will ignore monorepo packages by default
* @param {InstallParamsType} params
*/
function installPkgSync(params = {}) {
const {
cwd,
source,
dist,
command,
cacheDir,
outputJson,
generateJson,
onBeforeInstall,
} = preProcessParams(params);
// generate package.json
const sourcePackageJson = require(path.resolve(cwd, source));
const { packages } = getPackagesSync(cwd);
let pkgJson = getPkgJson(sourcePackageJson, packages);
if (generateJson) {
pkgJson = generateJson(JSON.parse(JSON.stringify(pkgJson)));
}
const pkgJsonStr = JSON.stringify(pkgJson, null, 2);
fs.ensureDirSync(dist);
// output new package.json to dist
if (outputJson) {
if (outputJson === true) {
fs.writeFileSync(path.resolve(dist, "package.json"), pkgJsonStr);
} else {
fs.writeFileSync(path.resolve(dist, outputJson), pkgJsonStr);
}
}
// get cache path
onBeforeInstall && onBeforeInstall({ pkgJson });
installModulesSync({ pkgJsonStr, cacheDir, dist, command });
return {
pkgJson,
};
}
module.exports = {
installPkg,
installPkgSync,
installModulesSync,
};