This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
dependencyManager.js
110 lines (88 loc) · 2.75 KB
/
dependencyManager.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
const { isAngular } = require("./projectHelpers");
const NEW_DEPS_MESSAGE = `\
A few new dependencies were added. \
Run "npm install" before building your project.
`;
const ALREADY_ADDED_MESSAGE = `\
Some dependencies have already been added. \
If you want to force update them, please run "node_modules/.bin/update-ns-webpack".
`;
function addProjectDeps(packageJson, force = false) {
packageJson.devDependencies = packageJson.devDependencies || {};
const postinstallOptions = {
deps: Object.assign({}, packageJson.devDependencies),
};
const depsToAdd = getRequiredDeps(packageJson);
Object.keys(depsToAdd).forEach(function (name) {
const version = depsToAdd[name];
Object.assign(postinstallOptions,
addDependency(postinstallOptions.deps, name, version, force));
});
return postinstallOptions;
}
function forceUpdateProjectDeps(packageJson) {
removeObsoleteDeps(packageJson);
return addProjectDeps(packageJson, true);
}
function removeObsoleteDeps(packageJson) {
const depsToRemove = [
"webpack",
"webpack-cli",
"webpack-bundle-analyzer",
"webpack-sources",
"clean-webpack-plugin",
"copy-webpack-plugin",
"raw-loader",
"css-loader",
"nativescript-worker-loader",
"terser-webpack-plugin",
"@angular-devkit/core",
"resolve-url-loader",
"sass-loader",
];
depsToRemove.forEach(dep => delete packageJson.devDependencies[dep]);
}
function addDependency(deps, name, version, force) {
const options = { deps };
if (!deps[name] || force) {
deps[name] = version;
options.newDepsAdded = true;
console.info(`Adding dev dependency: ${name}@${version}`);
} else if (deps[name] !== version) {
options.hasOldDeps = true;
console.info(`Dev dependency: ${name} already added. Leaving version: ${deps[name]}`);
}
return options;
}
function getRequiredDeps(packageJson) {
if (!isAngular({ packageJson })) {
return false;
}
const deps = {
"@angular/compiler-cli": "~8.2.0",
};
if (!dependsOn(packageJson, "@angular-devkit/build-angular")) {
deps["@ngtools/webpack"] = "8.2.0";
}
return deps;
}
function dependsOn(packageJson, package) {
if (!packageJson) {
return false;
}
return packageJson.dependencies.hasOwnProperty(package) ||
packageJson.devDependencies.hasOwnProperty(package);
}
function showHelperMessages({ newDepsAdded, hasOldDeps }) {
if (hasOldDeps) {
console.info(ALREADY_ADDED_MESSAGE);
}
if (newDepsAdded) {
console.info(NEW_DEPS_MESSAGE);
}
}
module.exports = {
addProjectDeps,
forceUpdateProjectDeps,
showHelperMessages,
};