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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scripts): add ns-bundle and verify-bundle (#69)
- Loading branch information
Showing
9 changed files
with
241 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#!/usr/bin/env node | ||
|
||
const spawn = require("child_process").spawn; | ||
const path = require("path"); | ||
|
||
const PROJECT_DIR = path.resolve(__dirname, "../../../"); | ||
|
||
if (!process.env.npm_config_argv) { | ||
throwError({message: "No flags provided."}); | ||
} | ||
|
||
const npmArgs = JSON.parse(process.env.npm_config_argv).original; | ||
const flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2)); | ||
const options = getOptions(flags); | ||
|
||
execute(options); | ||
|
||
function execute(options) { | ||
let commands = [ | ||
() => runTns(options.command, options.platform), | ||
]; | ||
|
||
if (options.bundle) { | ||
commands.unshift(() => webpack(options.platform)); | ||
} | ||
|
||
return commands.reduce((current, next) => current.then(next), Promise.resolve()); | ||
} | ||
|
||
function webpack(platform) { | ||
return new Promise(function (resolve, reject) { | ||
console.log(`Running webpack for ${platform}...`); | ||
|
||
spawnChildProcess("tns", "clean-app", platform) | ||
.then(() => spawnChildProcess("webpack", `--config=webpack.${platform}.js`, "--progress")) | ||
.then(resolve) | ||
.catch(throwError); | ||
}); | ||
} | ||
|
||
function runTns(command, platform) { | ||
console.log(`Running tns ${command}...`); | ||
return new Promise((resolve, reject) => { | ||
spawnChildProcess("tns", command, platform, "--bundle", "--disable-npm-install") | ||
.then(resolve) | ||
.catch(throwError); | ||
}); | ||
} | ||
|
||
function getOptions(flags) { | ||
let options = {}; | ||
options.platform = getPlatform(flags); | ||
options.command = getCommand(flags); | ||
options.bundle = !flags.includes("nobundle"); | ||
|
||
return options; | ||
} | ||
|
||
function getPlatform(flags) { | ||
if (flags.includes("android") && flags.includes("ios")) { | ||
throwError({message: "You cannot use both --android and --ios flags!"}); | ||
} | ||
|
||
if (flags.includes("android")) { | ||
return "android"; | ||
} else if (flags.includes("ios")) { | ||
return "ios"; | ||
} else { | ||
throwError({message: "You must provide a target platform! Use either --android, or --ios flag."}); | ||
} | ||
} | ||
|
||
function getCommand(flags) { | ||
if (flags.includes("start-app") && flags.includes("build-app")) { | ||
throwError({message: "You cannot use both --start-app and --build-app flags!"}); | ||
} | ||
|
||
if (flags.includes("start-app")) { | ||
return "run"; | ||
} else if (flags.includes("build-app")) { | ||
return "build"; | ||
} else { | ||
throwError({message: "You must provide either --start-app, or --build-app flag!"}); | ||
} | ||
} | ||
|
||
function spawnChildProcess(command, ...args) { | ||
return new Promise((resolve, reject) => { | ||
const childProcess = spawn(command, args, { stdio: "inherit", pwd: PROJECT_DIR }); | ||
|
||
childProcess.on("close", (code) => { | ||
if (code === 0) { | ||
resolve(); | ||
} else { | ||
reject({ | ||
code, | ||
message: `child process exited with code ${code}`, | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function throwError(error) { | ||
console.error(error.message); | ||
process.exit(error.code || 1); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@node %~dp0\ns-bundle %* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env node | ||
|
||
const path = require("path"); | ||
const fs = require("fs"); | ||
|
||
const PROJECT_DIR = path.resolve(__dirname, "../../../"); | ||
console.log(PROJECT_DIR); | ||
const APP_ID = require(path.resolve(PROJECT_DIR, "./package.json")).nativescript.id; | ||
const APP_NAME = APP_ID.substring(APP_ID.lastIndexOf(".") + 1); | ||
const PROJECT_PATHS = { | ||
android: path.resolve(PROJECT_DIR, "platforms/android/src/main/assets/app"), | ||
ios: path.resolve(PROJECT_DIR, `platforms/ios/build/emulator/${APP_NAME}.app/app`), | ||
}; | ||
|
||
const npmArgs = JSON.parse(process.env.npm_config_argv).original; | ||
const flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2)); | ||
const file = getTargetFile(flags); | ||
const platform = getPlatform(flags); | ||
|
||
const filePath = path.resolve(PROJECT_PATHS[platform], file); | ||
|
||
console.log(`Checking ${filePath} exists`); | ||
if (!fs.existsSync(filePath)) { | ||
throwError({message: `${filePath} doesn not exist!`}); | ||
} | ||
|
||
const maxSize = getMaxSize(flags); | ||
if (maxSize) { | ||
checkFileSizeIsUnder(filePath, maxSize).then().catch(throwError); | ||
} | ||
|
||
function getTargetFile(flags) { | ||
let fileFlags = flags.filter(f => f.startsWith("file=")); | ||
|
||
if (fileFlags.length != 1) { | ||
throwError({message: "You must provide a target file!"}); | ||
} | ||
|
||
fileFlags = fileFlags[0]; | ||
return fileFlags.substring(fileFlags.indexOf("=") + 1); | ||
} | ||
|
||
function getMaxSize(flags) { | ||
let sizeFlags = flags.filter(f => f.startsWith("maxSize=")); | ||
|
||
if (sizeFlags.length == 0) { | ||
return; | ||
} else if (sizeFlags.length > 1) { | ||
throwError({message: "You must provide 0 or 1 maxSize flags!"}); | ||
} | ||
|
||
sizeFlags = sizeFlags[0]; | ||
return sizeFlags.substring(sizeFlags.indexOf("=") + 1); | ||
} | ||
|
||
function getPlatform(flags) { | ||
if (flags.includes("android") && flags.includes("ios")) { | ||
throwError({message: "You cannot use both --android and --ios flags!"}); | ||
} | ||
|
||
if (flags.includes("android")) { | ||
return "android"; | ||
} else if (flags.includes("ios")) { | ||
return "ios"; | ||
} else { | ||
throwError({message: "You must provide a target platform! Use either --android, or --ios flag."}); | ||
} | ||
} | ||
|
||
function checkFileSizeIsUnder(fileName, sizeInBytes) { | ||
console.log(`Checking ${fileName} size is under ${sizeInBytes}`); | ||
|
||
return new Promise((resolve, reject) => { | ||
readFile(fileName) | ||
.then(content => { | ||
if (content.length <= sizeInBytes) { | ||
resolve(); | ||
} else { | ||
reject({message: `File "${fileName}" exceeded file size of "${sizeInBytes}".`}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function readFile(fileName) { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(fileName, "utf-8", (err, data) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function throwError(error) { | ||
console.error(error.message); | ||
process.exit(error.code || 1); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@node %~dp0\ns-verify-bundle %* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
var installer = require("./installer"); | ||
|
||
installer.addProjectFiles(); | ||
installer.removeDeprecatedNpmScripts(); | ||
installer.addNpmScripts(); | ||
installer.addProjectDependencies(); |