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

feat: enable plugin to run through {N} CLI hooks #299

Merged
merged 3 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/after-prepare.js
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);
}
}
78 changes: 78 additions & 0 deletions lib/before-prepareJS.js
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);
}
9 changes: 9 additions & 0 deletions lib/utils.js
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;
19 changes: 17 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
{
"name": "nativescript-dev-webpack",
"version": "0.8.0",
"version": "0.9.0",
"main": "index",
"description": "",
"homepage": "http://www.telerik.com",
"bugs": "http://www.telerik.com",
"contributors": [
"Hristo Deshev <[email protected]>"
],
"nativescript": {
"hooks": [
{
"type": "before-prepareJSApp",
"script": "lib/before-prepareJS.js",
"inject": true
},
{
"type": "after-prepare",
"script": "lib/after-prepare.js",
"inject": true
}
]
},
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand All @@ -28,7 +42,8 @@
"dependencies": {
"minimatch": "^3.0.4",
"semver": "^5.4.1",
"shelljs": "^0.6.0"
"shelljs": "^0.6.0",
"nativescript-hook": "^0.2.2"
},
"devDependencies": {}
}
5 changes: 5 additions & 0 deletions postinstall.js
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();
4 changes: 2 additions & 2 deletions prepublish/common/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ module.exports = `
{ from: "**/*.jpg" },
{ from: "**/*.png" },
{ from: "**/*.xml" },
], { ignore: ["App_Resources/**"] }),
]),

// Generate a bundle starter script and activate it in package.json
new nsWebpack.GenerateBundleStarterPlugin([
"./vendor",
"./bundle",
]),

// Support for web workers since v3.2
new NativeScriptWorkerPlugin(),

Expand Down
4 changes: 2 additions & 2 deletions templates/webpack.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ function getPlugins(platform, env) {
{ from: "**/*.jpg" },
{ from: "**/*.png" },
{ from: "**/*.xml" },
], { ignore: ["App_Resources/**"] }),
]),

// Generate a bundle starter script and activate it in package.json
new nsWebpack.GenerateBundleStarterPlugin([
"./vendor",
"./bundle",
]),

// Support for web workers since v3.2
new NativeScriptWorkerPlugin(),

Expand Down
4 changes: 2 additions & 2 deletions templates/webpack.javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ function getPlugins(platform, env) {
{ from: "**/*.jpg" },
{ from: "**/*.png" },
{ from: "**/*.xml" },
], { ignore: ["App_Resources/**"] }),
]),

// Generate a bundle starter script and activate it in package.json
new nsWebpack.GenerateBundleStarterPlugin([
"./vendor",
"./bundle",
]),

// Support for web workers since v3.2
new NativeScriptWorkerPlugin(),

Expand Down
4 changes: 2 additions & 2 deletions templates/webpack.typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ function getPlugins(platform, env) {
{ from: "**/*.jpg" },
{ from: "**/*.png" },
{ from: "**/*.xml" },
], { ignore: ["App_Resources/**"] }),
]),

// Generate a bundle starter script and activate it in package.json
new nsWebpack.GenerateBundleStarterPlugin([
"./vendor",
"./bundle",
]),

// Support for web workers since v3.2
new NativeScriptWorkerPlugin(),

Expand Down