Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit e80cbdc

Browse files
authored
feat(scripts): add ns-bundle and verify-bundle (#69)
1 parent 325cb90 commit e80cbdc

9 files changed

+241
-11
lines changed

Diff for: bin/install-ns-webpack

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
var installer = require("../installer");
33

44
installer.addProjectFiles();
5+
installer.removeDeprecatedNpmScripts();
56
installer.addNpmScripts();
67
installer.addProjectDependencies();

Diff for: bin/ns-bundle

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env node
2+
3+
const spawn = require("child_process").spawn;
4+
const path = require("path");
5+
6+
const PROJECT_DIR = path.resolve(__dirname, "../../../");
7+
8+
if (!process.env.npm_config_argv) {
9+
throwError({message: "No flags provided."});
10+
}
11+
12+
const npmArgs = JSON.parse(process.env.npm_config_argv).original;
13+
const flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2));
14+
const options = getOptions(flags);
15+
16+
execute(options);
17+
18+
function execute(options) {
19+
let commands = [
20+
() => runTns(options.command, options.platform),
21+
];
22+
23+
if (options.bundle) {
24+
commands.unshift(() => webpack(options.platform));
25+
}
26+
27+
return commands.reduce((current, next) => current.then(next), Promise.resolve());
28+
}
29+
30+
function webpack(platform) {
31+
return new Promise(function (resolve, reject) {
32+
console.log(`Running webpack for ${platform}...`);
33+
34+
spawnChildProcess("tns", "clean-app", platform)
35+
.then(() => spawnChildProcess("webpack", `--config=webpack.${platform}.js`, "--progress"))
36+
.then(resolve)
37+
.catch(throwError);
38+
});
39+
}
40+
41+
function runTns(command, platform) {
42+
console.log(`Running tns ${command}...`);
43+
return new Promise((resolve, reject) => {
44+
spawnChildProcess("tns", command, platform, "--bundle", "--disable-npm-install")
45+
.then(resolve)
46+
.catch(throwError);
47+
});
48+
}
49+
50+
function getOptions(flags) {
51+
let options = {};
52+
options.platform = getPlatform(flags);
53+
options.command = getCommand(flags);
54+
options.bundle = !flags.includes("nobundle");
55+
56+
return options;
57+
}
58+
59+
function getPlatform(flags) {
60+
if (flags.includes("android") && flags.includes("ios")) {
61+
throwError({message: "You cannot use both --android and --ios flags!"});
62+
}
63+
64+
if (flags.includes("android")) {
65+
return "android";
66+
} else if (flags.includes("ios")) {
67+
return "ios";
68+
} else {
69+
throwError({message: "You must provide a target platform! Use either --android, or --ios flag."});
70+
}
71+
}
72+
73+
function getCommand(flags) {
74+
if (flags.includes("start-app") && flags.includes("build-app")) {
75+
throwError({message: "You cannot use both --start-app and --build-app flags!"});
76+
}
77+
78+
if (flags.includes("start-app")) {
79+
return "run";
80+
} else if (flags.includes("build-app")) {
81+
return "build";
82+
} else {
83+
throwError({message: "You must provide either --start-app, or --build-app flag!"});
84+
}
85+
}
86+
87+
function spawnChildProcess(command, ...args) {
88+
return new Promise((resolve, reject) => {
89+
const childProcess = spawn(command, args, { stdio: "inherit", pwd: PROJECT_DIR });
90+
91+
childProcess.on("close", (code) => {
92+
if (code === 0) {
93+
resolve();
94+
} else {
95+
reject({
96+
code,
97+
message: `child process exited with code ${code}`,
98+
});
99+
}
100+
});
101+
});
102+
}
103+
104+
function throwError(error) {
105+
console.error(error.message);
106+
process.exit(error.code || 1);
107+
}
108+

Diff for: bin/ns-bundle.cmd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@node %~dp0\ns-bundle %*

Diff for: bin/ns-verify-bundle

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env node
2+
3+
const path = require("path");
4+
const fs = require("fs");
5+
6+
const PROJECT_DIR = path.resolve(__dirname, "../../../");
7+
console.log(PROJECT_DIR);
8+
const APP_ID = require(path.resolve(PROJECT_DIR, "./package.json")).nativescript.id;
9+
const APP_NAME = APP_ID.substring(APP_ID.lastIndexOf(".") + 1);
10+
const PROJECT_PATHS = {
11+
android: path.resolve(PROJECT_DIR, "platforms/android/src/main/assets/app"),
12+
ios: path.resolve(PROJECT_DIR, `platforms/ios/build/emulator/${APP_NAME}.app/app`),
13+
};
14+
15+
const npmArgs = JSON.parse(process.env.npm_config_argv).original;
16+
const flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2));
17+
const file = getTargetFile(flags);
18+
const platform = getPlatform(flags);
19+
20+
const filePath = path.resolve(PROJECT_PATHS[platform], file);
21+
22+
console.log(`Checking ${filePath} exists`);
23+
if (!fs.existsSync(filePath)) {
24+
throwError({message: `${filePath} doesn not exist!`});
25+
}
26+
27+
const maxSize = getMaxSize(flags);
28+
if (maxSize) {
29+
checkFileSizeIsUnder(filePath, maxSize).then().catch(throwError);
30+
}
31+
32+
function getTargetFile(flags) {
33+
let fileFlags = flags.filter(f => f.startsWith("file="));
34+
35+
if (fileFlags.length != 1) {
36+
throwError({message: "You must provide a target file!"});
37+
}
38+
39+
fileFlags = fileFlags[0];
40+
return fileFlags.substring(fileFlags.indexOf("=") + 1);
41+
}
42+
43+
function getMaxSize(flags) {
44+
let sizeFlags = flags.filter(f => f.startsWith("maxSize="));
45+
46+
if (sizeFlags.length == 0) {
47+
return;
48+
} else if (sizeFlags.length > 1) {
49+
throwError({message: "You must provide 0 or 1 maxSize flags!"});
50+
}
51+
52+
sizeFlags = sizeFlags[0];
53+
return sizeFlags.substring(sizeFlags.indexOf("=") + 1);
54+
}
55+
56+
function getPlatform(flags) {
57+
if (flags.includes("android") && flags.includes("ios")) {
58+
throwError({message: "You cannot use both --android and --ios flags!"});
59+
}
60+
61+
if (flags.includes("android")) {
62+
return "android";
63+
} else if (flags.includes("ios")) {
64+
return "ios";
65+
} else {
66+
throwError({message: "You must provide a target platform! Use either --android, or --ios flag."});
67+
}
68+
}
69+
70+
function checkFileSizeIsUnder(fileName, sizeInBytes) {
71+
console.log(`Checking ${fileName} size is under ${sizeInBytes}`);
72+
73+
return new Promise((resolve, reject) => {
74+
readFile(fileName)
75+
.then(content => {
76+
if (content.length <= sizeInBytes) {
77+
resolve();
78+
} else {
79+
reject({message: `File "${fileName}" exceeded file size of "${sizeInBytes}".`});
80+
}
81+
});
82+
});
83+
}
84+
85+
function readFile(fileName) {
86+
return new Promise((resolve, reject) => {
87+
fs.readFile(fileName, "utf-8", (err, data) => {
88+
if (err) {
89+
reject(err);
90+
} else {
91+
resolve(data);
92+
}
93+
});
94+
});
95+
}
96+
97+
function throwError(error) {
98+
console.error(error.message);
99+
process.exit(error.code || 1);
100+
}
101+

Diff for: bin/ns-verify-bundle.cmd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@node %~dp0\ns-verify-bundle %*

Diff for: bin/remove-ns-webpack

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
var installer = require("../installer");
33

44
installer.removeProjectFiles();
5+
installer.removeDeprecatedNpmScripts();
56
installer.removeNpmScripts();
67
installer.removeProjectDependencies();

Diff for: installer.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,24 @@ exports.removeProjectFiles = removeProjectFiles;
7878

7979
function getScriptTemplates() {
8080
return {
81-
"clean-[PLATFORM]": "tns clean-app [PLATFORM]",
82-
"prewebpack-[PLATFORM]": "npm run clean-[PLATFORM]",
83-
"webpack-[PLATFORM]": "webpack --config=webpack.[PLATFORM].js --progress",
84-
"prestart-[PLATFORM]-bundle": "npm run webpack-[PLATFORM]",
85-
"start-[PLATFORM]-bundle": "tns run [PLATFORM] --bundle --disable-npm-install",
86-
"prebuild-[PLATFORM]-bundle": "npm run webpack-[PLATFORM]",
87-
"build-[PLATFORM]-bundle": "tns build [PLATFORM] --bundle --disable-npm-install",
81+
"ns-bundle": "ns-bundle",
82+
"start-[PLATFORM]-bundle": "npm run ns-bundle --[PLATFORM] --start-app",
83+
"build-[PLATFORM]-bundle": "npm run ns-bundle --[PLATFORM] --build-app",
8884
};
8985
}
9086

87+
function getDeprecatedScriptTemplates() {
88+
return [
89+
"clean-[PLATFORM]",
90+
"prewebpack-[PLATFORM]",
91+
"webpack-[PLATFORM]",
92+
"prestart-[PLATFORM]-bundle",
93+
"start-[PLATFORM]-bundle",
94+
"prebuild-[PLATFORM]-bundle",
95+
"build-[PLATFORM]-bundle",
96+
]
97+
}
98+
9199
function addNpmScripts() {
92100
var scriptTemplates = getScriptTemplates();
93101
Object.keys(scriptTemplates).forEach(function(templateName) {
@@ -96,9 +104,15 @@ function addNpmScripts() {
96104
}
97105
exports.addNpmScripts = addNpmScripts;
98106

99-
function removeNpmScripts() {
100-
var scriptTemplates = getScriptTemplates();
101-
Object.keys(scriptTemplates).forEach(function(templateName) {
107+
function removeDeprecatedNpmScripts() {
108+
removeNpmScripts(getDeprecatedScriptTemplates());
109+
}
110+
111+
exports.removeDeprecatedNpmScripts = removeDeprecatedNpmScripts;
112+
113+
function removeNpmScripts(scriptTemplates) {
114+
var scriptTemplates = scriptTemplates || Object.keys(getScriptTemplates());
115+
scriptTemplates.forEach(function(templateName) {
102116
removePlatformScripts(packageJson, templateName);
103117
});
104118
}

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
},
1919
"bin": {
2020
"install-ns-webpack": "./bin/install-ns-webpack",
21-
"remove-ns-webpack": "./bin/remove-ns-webpack"
21+
"remove-ns-webpack": "./bin/remove-ns-webpack",
22+
"ns-bundle": "./bin/ns-bundle",
23+
"ns-verify-bundle": "./bin/ns-verify-bundle"
2224
},
2325
"dependencies": {},
2426
"devDependencies": {}

Diff for: postinstall.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var installer = require("./installer");
22

33
installer.addProjectFiles();
4+
installer.removeDeprecatedNpmScripts();
45
installer.addNpmScripts();
56
installer.addProjectDependencies();

0 commit comments

Comments
 (0)