From a37b3f55f4df54e0a90f22c95744d45dbcc6df25 Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Fri, 31 May 2019 10:23:14 +0300 Subject: [PATCH] fix: make hooks compatible with CLI's 6.0.0 changes In NativeScript CLI 6.0.0 release the bundle workflow will be the only one available. Currently, the nativescript-vue has some hooks that verify bundle is passed. There's no need of them for 6.0.0 release. Implement a check based on the CLI's version and skip the validation in case CLI is 6.x.x or newer. --- .../hooks/before-checkForChanges.js | 27 ++++++++++++------- platform/nativescript/hooks/before-watch.js | 26 +++++++++++------- platform/nativescript/hooks/helpers.js | 14 ++++++++++ 3 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 platform/nativescript/hooks/helpers.js diff --git a/platform/nativescript/hooks/before-checkForChanges.js b/platform/nativescript/hooks/before-checkForChanges.js index 2d2b5307..c047d66f 100644 --- a/platform/nativescript/hooks/before-checkForChanges.js +++ b/platform/nativescript/hooks/before-checkForChanges.js @@ -1,12 +1,19 @@ -module.exports = function(hookArgs, $errors) { - const bundle = - hookArgs && - hookArgs.checkForChangesOpts && - hookArgs.checkForChangesOpts.projectChangesOptions && - hookArgs.checkForChangesOpts.projectChangesOptions.bundle - if (!bundle) { - $errors.failWithoutHelp( - "Nativescript-vue doesn't work without --bundle option. Please specify --bundle option to the command and execute it again." - ) +const { isBundleCheckRequired } = require('./helpers') + +module.exports = function(hookArgs, $errors, $injector) { + const shouldCheckBundleOption = isBundleCheckRequired($injector) + + if (shouldCheckBundleOption) { + const bundle = + hookArgs && + hookArgs.checkForChangesOpts && + hookArgs.checkForChangesOpts.projectChangesOptions && + hookArgs.checkForChangesOpts.projectChangesOptions.bundle + + if (!bundle) { + $errors.failWithoutHelp( + "Nativescript-vue doesn't work without --bundle option. Please specify --bundle option to the command and execute it again." + ) + } } } diff --git a/platform/nativescript/hooks/before-watch.js b/platform/nativescript/hooks/before-watch.js index 46b4b618..aad240a1 100644 --- a/platform/nativescript/hooks/before-watch.js +++ b/platform/nativescript/hooks/before-watch.js @@ -1,12 +1,18 @@ -module.exports = function(hookArgs, $errors) { - const bundle = - hookArgs && - hookArgs.config && - hookArgs.config.appFilesUpdaterOptions && - hookArgs.config.appFilesUpdaterOptions.bundle - if (!bundle) { - $errors.failWithoutHelp( - "Nativescript-vue doesn't work without --bundle option. Please specify --bundle option to the command and execute it again." - ) +const { isBundleCheckRequired } = require('./helpers') + +module.exports = function(hookArgs, $errors, $injector) { + const shouldCheckBundleOption = isBundleCheckRequired($injector) + + if (shouldCheckBundleOption) { + const bundle = + hookArgs && + hookArgs.config && + hookArgs.config.appFilesUpdaterOptions && + hookArgs.config.appFilesUpdaterOptions.bundle + if (!bundle) { + $errors.failWithoutHelp( + "Nativescript-vue doesn't work without --bundle option. Please specify --bundle option to the command and execute it again." + ) + } } } diff --git a/platform/nativescript/hooks/helpers.js b/platform/nativescript/hooks/helpers.js new file mode 100644 index 00000000..b5b4a2d1 --- /dev/null +++ b/platform/nativescript/hooks/helpers.js @@ -0,0 +1,14 @@ +function isBundleCheckRequired($injector) { + try { + const $staticConfig = $injector.resolve('$staticConfig') + const version = $staticConfig && $staticConfig.version + const majorVersion = (version || '').split('.')[0] + // If the major version is not available, probably we are in some new version of CLI, where the staticConfig is not available + // So it definitely should work with bundle; + return !majorVersion || +majorVersion < 6 + } catch (err) { + return false + } +} + +module.exports.isBundleCheckRequired = isBundleCheckRequired