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

fix: Hooks are not executed correctly when CLI is used as a library #479

Merged
merged 1 commit into from
Mar 30, 2018
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
4 changes: 2 additions & 2 deletions lib/after-prepare.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { installSnapshotArtefacts } = require("../snapshot/android/project-snapshot-generator");
const { shouldSnapshot } = require("./utils");

module.exports = function ($projectData, hookArgs) {
module.exports = function (hookArgs) {
const env = hookArgs.env || {};
const shouldSnapshotOptions = {
platform: hookArgs.platform,
Expand All @@ -10,6 +10,6 @@ module.exports = function ($projectData, hookArgs) {
};

if (env.snapshot && shouldSnapshot(shouldSnapshotOptions)) {
installSnapshotArtefacts($projectData.projectDir);
installSnapshotArtefacts(hookArgs.projectData.projectDir);
}
}
4 changes: 2 additions & 2 deletions lib/before-prepareJS.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { runWebpackCompiler } = require("./compiler");

module.exports = function ($projectData, $logger, hookArgs) {
module.exports = function ($logger, hookArgs) {
const env = hookArgs.config.env || {};
const platform = hookArgs.config.platform;
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
Expand All @@ -10,6 +10,6 @@ module.exports = function ($projectData, $logger, hookArgs) {
bundle: appFilesUpdaterOptions.bundle,
release: appFilesUpdaterOptions.release,
};
const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, $projectData, $logger, hookArgs);
const result = config.bundle && runWebpackCompiler.bind(runWebpackCompiler, config, hookArgs.config.projectData, $logger, hookArgs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this (hookArgs.config...) is intentionally different from the rest but I couldn't sort it out why by looking briefly through the cli source.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hookArgs object contains the arguments passed to the decorated method. The current hook is executed when the preparePlatform method in CLI is called: https://github.com/NativeScript/nativescript-cli/blob/97d72e9f414b1911982af715b36ae21f599cf32c/lib/services/prepare-platform-js-service.ts#L36
As you can see, this method accepts a single argument called config. So the hookArgs object in the current context will have a single property - config and its value will be the value passed to the preparePlatform method. We know this value will be object which has projectData property.

For the other hooks the projectData is often a direct argument of the decorated method, for example: https://github.com/NativeScript/nativescript-cli/blob/master/lib/services/platform-service.ts#L283-L287

return result;
}
4 changes: 2 additions & 2 deletions lib/before-watch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { runWebpackCompiler } = require("./compiler");

module.exports = function ($projectData, $logger, hookArgs) {
module.exports = function ($logger, hookArgs) {
if (hookArgs.config) {
const appFilesUpdaterOptions = hookArgs.config.appFilesUpdaterOptions;
if (appFilesUpdaterOptions.bundle) {
Expand All @@ -15,7 +15,7 @@ module.exports = function ($projectData, $logger, hookArgs) {
watch: true
};

return runWebpackCompiler(config, $projectData, $logger, hookArgs);
return runWebpackCompiler(config, hookArgs.projectData, $logger, hookArgs);
}));
}
}
Expand Down
12 changes: 6 additions & 6 deletions lib/before-watchPatterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const {
setProcessInitDirectory,
} = require("./utils");

module.exports = function ($projectData, hookArgs) {
module.exports = function (hookArgs) {
const { liveSyncData } = hookArgs;
if (!liveSyncData || !liveSyncData.bundle) {
return;
}

setProcessInitDirectory($projectData.projectDir);
setProcessInitDirectory(hookArgs.projectData.projectDir);
const { platforms } = hookArgs;
const { env } = liveSyncData;
return (args, originalMethod) => {
Expand All @@ -21,7 +21,7 @@ module.exports = function ($projectData, hookArgs) {
}

const compilationContexts = platforms.map(platform =>
getContext($projectData, platform, env));
getContext(hookArgs.projectData, platform, env));

const ignorePatterns = compilationContexts.map(
context => `!${context}`
Expand All @@ -32,7 +32,7 @@ module.exports = function ($projectData, hookArgs) {
};
}

function getContext($projectData, platform, env) {
const fullEnvData = buildEnvData($projectData, platform, env);
return getCompilationContext($projectData.projectDir, fullEnvData);
function getContext(projectData, platform, env) {
const fullEnvData = buildEnvData(projectData, platform, env);
return getCompilationContext(projectData.projectDir, fullEnvData);
}