From fbecc16a28cb095b23ee25c63bdde1e9b5b061cb Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Tue, 2 Jul 2019 11:38:18 +0300 Subject: [PATCH 1/3] fix(preuninstall): add preuninstall script to remove old hooks During migration from one version to another or when the plugin is removed from application we need to remove its hooks. This is usually done in preuninstall script, however, it was missing until now. This causes several issues when the version is updated as old hooks remain, but they may not be valid anymore. --- package.json | 1 + postinstall.js | 1 - preuninstall.js | 11 +++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 preuninstall.js diff --git a/package.json b/package.json index 24e125e1..8521057f 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ }, "scripts": { "postinstall": "node postinstall.js", + "preuninstall": "node preuninstall.js", "postpack": "rm -rf node_modules", "prepare": "tsc && npm run jasmine", "test": "npm run prepare && npm run jasmine", diff --git a/postinstall.js b/postinstall.js index 54d6b163..35055760 100644 --- a/postinstall.js +++ b/postinstall.js @@ -1,6 +1,5 @@ "use strict"; -const { dirname } = require("path"); const hook = require("nativescript-hook")(__dirname); const { compareProjectFiles } = require("./projectFilesManager"); diff --git a/preuninstall.js b/preuninstall.js new file mode 100644 index 00000000..8632200f --- /dev/null +++ b/preuninstall.js @@ -0,0 +1,11 @@ +"use strict"; + +const hook = require("nativescript-hook")(__dirname); + +const { getProjectDir } = require("./projectHelpers"); + +const projectDir = getProjectDir(); + +if (projectDir) { + hook.preuninstall(); +} From 7acdb729f2b343b7932570e414eea89d4dafd380 Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Tue, 2 Jul 2019 12:03:59 +0300 Subject: [PATCH 2/3] fix(postinstall): remove old hooks As in 1.0.0 and CLI 6.0 we've changed the way nativescript-dev-webpack interacts with CLI, we need to remove hooks from previous nativescript-dev-webpack versions and use new ones. Usually this should happen with preuninstall script of the old version that removes the hooks. However, our current live version does not have such logic, so implement this in the postinstall of the current version. This way we try to ensure the current plugin will work correctly. --- postinstall.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/postinstall.js b/postinstall.js index 35055760..1956dcf5 100644 --- a/postinstall.js +++ b/postinstall.js @@ -4,12 +4,47 @@ const hook = require("nativescript-hook")(__dirname); const { compareProjectFiles } = require("./projectFilesManager"); const { getProjectDir } = require("./projectHelpers"); +const path = require("path"); +const fs = require("fs"); const projectDir = getProjectDir(); +// This method is introduced as in version 1.0.0 of nativescript-dev-webpack (compatible and required for NativeScript 6.0.0) +// we have changed a lot of hooks and old ones are incompatible. This should be automatically handled with preuninstall script of the old version. +// However, old versions of nativescript-dev-webpack do not have such logic, so remove them manually on postinstall of the current version. +// This logic can be removed later, once most of the projects are migrated to 1.0.0 of the package or later. +// These new versions have preuninstall script that will automatically handle this case. +function removeOldHooks() { + const oldHooks = [ + "before-prepareJSApp", + "before-cleanApp", + "before-watch", + "after-watch", + "before-watchPatterns", + "before-shouldPrepare", + "after-prepare", + "before-preview-sync" + ]; + + const hooksDir = path.join(projectDir, "hooks"); + const pkgName = require("./package.json").name; + const filename = `${pkgName}.js`; + oldHooks.forEach(hookName => { + const hookPath = path.join(hooksDir, hookName, filename); + + try { + if (fs.existsSync(hookPath)) { + fs.unlinkSync(hookPath); + } + } catch (err) { + console.warn(`${pkgName} postinstall task: unable to delete hook ${hookPath}. Error is: ${err}`); + } + }); +} + if (projectDir) { compareProjectFiles(projectDir); - + removeOldHooks(); hook.postinstall(); const installer = require("./installer"); installer.install(); @@ -17,3 +52,4 @@ if (projectDir) { // We are installing dev dependencies for the nativescript-dev-webpack plugin. console.log("Skipping postinstall artifacts! We assumed the nativescript-dev-webpack is installing devDependencies"); } + From 0aad0c81276b4e8c24179cde901b84f8300f8d93 Mon Sep 17 00:00:00 2001 From: rosen-vladimirov Date: Tue, 2 Jul 2019 12:37:11 +0300 Subject: [PATCH 3/3] feat(hooks): add before-checkForChanges hook Add before-checkForChanges hook to prevent users from using the current version of the plugin with CLI 5.x.x or older. These two versions are incompatible, so add an error in case older CLI is used. --- lib/before-checkForChanges.js | 16 ++++++++++++++++ package.json | 5 +++++ 2 files changed, 21 insertions(+) create mode 100644 lib/before-checkForChanges.js diff --git a/lib/before-checkForChanges.js b/lib/before-checkForChanges.js new file mode 100644 index 00000000..d456a1d3 --- /dev/null +++ b/lib/before-checkForChanges.js @@ -0,0 +1,16 @@ +module.exports = function ($staticConfig, hookArgs) { + const majorVersionMatch = ($staticConfig.version || '').match(/^(\d+)\./); + const majorVersion = majorVersionMatch && majorVersionMatch[1] && +majorVersionMatch[1]; + if (majorVersion && majorVersion < 6) { + // check if we are using the bundle workflow or the legacy one. + const isUsingBundleWorkflow = hookArgs && + hookArgs.checkForChangesOpts && + hookArgs.checkForChangesOpts.projectChangesOptions && + hookArgs.checkForChangesOpts.projectChangesOptions.bundle; + + if (isUsingBundleWorkflow) { + const packageJsonData = require("../package.json") + throw new Error(`The current version of ${packageJsonData.name} (${packageJsonData.version}) is not compatible with the used CLI: ${$staticConfig.version}. Please upgrade your NativeScript CLI version (npm i -g nativescript).`); + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index 8521057f..de7ef1b9 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,11 @@ "type": "after-prepare", "script": "lib/after-prepare.js", "inject": true + }, + { + "type": "before-checkForChanges", + "script": "lib/before-checkForChanges.js", + "inject": true } ] },