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: enable plugin to run through {N} CLI hooks (#299)
* Enable plugin to run through hooks Instead of relying on npm scripts, rely on hooks so that certain parts of the prepare chain can be overriden. Do not skip moving `App_Resources` directory, as CLI expects it to be present in platforms and will take care of the resources inside. * chore(deps): unpin nativescript-hook version * enable webpack builds in debug and optional snapshot
- Loading branch information
1 parent
b4800c8
commit 9a57a53
Showing
9 changed files
with
127 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const snapshotGenerator = require("../snapshot/android/project-snapshot-generator"); | ||
const utils = require("./utils"); | ||
|
||
module.exports = function ($mobileHelper, $projectData, hookArgs) { | ||
const env = hookArgs.env || {}; | ||
|
||
if (env["snapshot"] && utils.shouldSnapshot($mobileHelper, hookArgs.platform, hookArgs.appFilesUpdaterOptions.bundle)) { | ||
snapshotGenerator.installSnapshotArtefacts($projectData.projectDir); | ||
} | ||
} |
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,78 @@ | ||
const utils = require("./utils"); | ||
const { spawn } = require("child_process"); | ||
const { join } = require("path"); | ||
let hasBeenInvoked = false; | ||
|
||
function escapeWithQuotes(arg) { | ||
return `"${arg}"`; | ||
} | ||
|
||
function spawnChildProcess(projectDir, command, ...args) { | ||
return new Promise((resolve, reject) => { | ||
const escapedArgs = args.map(escapeWithQuotes) | ||
|
||
const childProcess = spawn(command, escapedArgs, { | ||
stdio: "inherit", | ||
pwd: projectDir, | ||
shell: true, | ||
}); | ||
|
||
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); | ||
} | ||
|
||
function prepareJSWebpack(config, $mobileHelper, $projectData, originalArgs, originalMethod) { | ||
if (config.bundle) { | ||
return new Promise(function (resolve, reject) { | ||
console.log(`Running webpack for ${config.platform}...`); | ||
const envFlagNames = Object.keys(config.env).concat([config.platform.toLowerCase()]); | ||
|
||
const snapshotEnvIndex = envFlagNames.indexOf("snapshot"); | ||
if (snapshotEnvIndex !== -1 && !utils.shouldSnapshot($mobileHelper, config.platform, config.bundle)) { | ||
envFlagNames.splice(snapshotEnvIndex, 1); | ||
} | ||
|
||
const args = [ | ||
$projectData.projectDir, | ||
"node", | ||
"--preserve-symlinks", | ||
join($projectData.projectDir, "node_modules", "webpack", "bin", "webpack.js"), | ||
"--config=webpack.config.js", | ||
"--progress", | ||
...envFlagNames.map(item => `--env.${item}`) | ||
].filter(a => !!a); | ||
|
||
// TODO: require webpack instead of spawning | ||
spawnChildProcess(...args) | ||
.then(resolve) | ||
.catch(throwError); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = function ($mobileHelper, $projectData, hookArgs) { | ||
const env = hookArgs.config.env || {}; | ||
const platform = hookArgs.config.platform; | ||
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions; | ||
const config = { | ||
env, | ||
platform, | ||
bundle: appFilesUpdaterOptions.bundle | ||
}; | ||
|
||
return config.bundle && prepareJSWebpack.bind(prepareJSWebpack, config, $mobileHelper, $projectData); | ||
} |
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,9 @@ | ||
const os = require("os"); | ||
|
||
function shouldSnapshot($mobileHelper, platform, bundle) { | ||
const platformSupportsSnapshot = $mobileHelper.isAndroidPlatform(platform); | ||
const osSupportsSnapshot = os.type() !== "Windows_NT"; | ||
return bundle && platformSupportsSnapshot && osSupportsSnapshot; | ||
} | ||
|
||
module.exports.shouldSnapshot = shouldSnapshot; |
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,2 +1,7 @@ | ||
"use strict"; | ||
|
||
const hook = require("nativescript-hook")(__dirname); | ||
hook.postinstall(); | ||
|
||
const installer = require("./installer"); | ||
installer.install(); |
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