This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathnpmScriptsManager.js
77 lines (61 loc) · 2.1 KB
/
npmScriptsManager.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
const SCRIPT_TEMPLATES = Object.freeze({
"ns-bundle": "ns-bundle",
"start-[PLATFORM]-bundle": "npm run ns-bundle --[PLATFORM] --run-app",
"build-[PLATFORM]-bundle": "npm run ns-bundle --[PLATFORM] --build-app",
"publish-ios-bundle": "npm run ns-bundle --ios --publish-app",
});
const DEPRECATED_SCRIPT_TEMPLATES = Object.freeze([
"clean-[PLATFORM]",
"prewebpack-[PLATFORM]",
"webpack-[PLATFORM]",
"prestart-[PLATFORM]-bundle",
"start-[PLATFORM]-bundle",
"prebuild-[PLATFORM]-bundle",
"build-[PLATFORM]-bundle",
]);
const PLATFORMS = Object.freeze(["android", "ios"]);
function addNpmScripts(scripts) {
scripts = scripts || {};
Object.keys(SCRIPT_TEMPLATES).forEach(name => {
packageJson = addPlatformScript(scripts, name, SCRIPT_TEMPLATES[name]);
});
return scripts;
}
function removeDeprecatedNpmScripts(scripts) {
return removeNpmScripts(scripts, DEPRECATED_SCRIPT_TEMPLATES);
}
function removeNpmScripts(scripts, scriptTemplates = Object.keys(SCRIPT_TEMPLATES)) {
scriptTemplates.forEach(function(templateName) {
scripts = removePlatformScripts(scripts, templateName);
});
return scripts;
}
function addPlatformScript(scripts, nameTemplate, commandTemplate) {
PLATFORMS.forEach(function (platform) {
const name = nameTemplate.replace(/\[PLATFORM\]/g, platform);
const command = commandTemplate.replace(/\[PLATFORM\]/g, platform);
if (!scripts[name]) {
console.info(`Registering script: ${name}`);
scripts[name] = command;
}
});
return scripts;
}
function removePlatformScripts(scripts, nameTemplate) {
if (!scripts || Object.keys(SCRIPT_TEMPLATES).includes(nameTemplate)) {
return scripts;
}
PLATFORMS.forEach(function (platform) {
const name = nameTemplate.replace(/\[PLATFORM\]/g, platform);
if (scripts[name]) {
console.info(`Removing script: ${name}`);
delete scripts[name];
}
});
return scripts;
}
module.exports = {
addNpmScripts,
removeDeprecatedNpmScripts,
removeNpmScripts,
};