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

Commit f558607

Browse files
feat(hooks): improve hooks handling (#961)
* 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. * 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. * 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.
1 parent aba0313 commit f558607

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

Diff for: lib/before-checkForChanges.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = function ($staticConfig, hookArgs) {
2+
const majorVersionMatch = ($staticConfig.version || '').match(/^(\d+)\./);
3+
const majorVersion = majorVersionMatch && majorVersionMatch[1] && +majorVersionMatch[1];
4+
if (majorVersion && majorVersion < 6) {
5+
// check if we are using the bundle workflow or the legacy one.
6+
const isUsingBundleWorkflow = hookArgs &&
7+
hookArgs.checkForChangesOpts &&
8+
hookArgs.checkForChangesOpts.projectChangesOptions &&
9+
hookArgs.checkForChangesOpts.projectChangesOptions.bundle;
10+
11+
if (isUsingBundleWorkflow) {
12+
const packageJsonData = require("../package.json")
13+
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).`);
14+
}
15+
}
16+
}

Diff for: package.json

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"type": "after-prepare",
1515
"script": "lib/after-prepare.js",
1616
"inject": true
17+
},
18+
{
19+
"type": "before-checkForChanges",
20+
"script": "lib/before-checkForChanges.js",
21+
"inject": true
1722
}
1823
]
1924
},
@@ -24,6 +29,7 @@
2429
},
2530
"scripts": {
2631
"postinstall": "node postinstall.js",
32+
"preuninstall": "node preuninstall.js",
2733
"postpack": "rm -rf node_modules",
2834
"prepare": "tsc && npm run jasmine",
2935
"test": "npm run prepare && npm run jasmine",

Diff for: postinstall.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,55 @@
11
"use strict";
22

3-
const { dirname } = require("path");
43
const hook = require("nativescript-hook")(__dirname);
54

65
const { compareProjectFiles } = require("./projectFilesManager");
76
const { getProjectDir } = require("./projectHelpers");
7+
const path = require("path");
8+
const fs = require("fs");
89

910
const projectDir = getProjectDir();
1011

12+
// This method is introduced as in version 1.0.0 of nativescript-dev-webpack (compatible and required for NativeScript 6.0.0)
13+
// we have changed a lot of hooks and old ones are incompatible. This should be automatically handled with preuninstall script of the old version.
14+
// However, old versions of nativescript-dev-webpack do not have such logic, so remove them manually on postinstall of the current version.
15+
// This logic can be removed later, once most of the projects are migrated to 1.0.0 of the package or later.
16+
// These new versions have preuninstall script that will automatically handle this case.
17+
function removeOldHooks() {
18+
const oldHooks = [
19+
"before-prepareJSApp",
20+
"before-cleanApp",
21+
"before-watch",
22+
"after-watch",
23+
"before-watchPatterns",
24+
"before-shouldPrepare",
25+
"after-prepare",
26+
"before-preview-sync"
27+
];
28+
29+
const hooksDir = path.join(projectDir, "hooks");
30+
const pkgName = require("./package.json").name;
31+
const filename = `${pkgName}.js`;
32+
oldHooks.forEach(hookName => {
33+
const hookPath = path.join(hooksDir, hookName, filename);
34+
35+
try {
36+
if (fs.existsSync(hookPath)) {
37+
fs.unlinkSync(hookPath);
38+
}
39+
} catch (err) {
40+
console.warn(`${pkgName} postinstall task: unable to delete hook ${hookPath}. Error is: ${err}`);
41+
}
42+
});
43+
}
44+
1145
if (projectDir) {
1246
compareProjectFiles(projectDir);
13-
47+
removeOldHooks();
1448
hook.postinstall();
1549
const installer = require("./installer");
1650
installer.install();
1751
} else {
1852
// We are installing dev dependencies for the nativescript-dev-webpack plugin.
1953
console.log("Skipping postinstall artifacts! We assumed the nativescript-dev-webpack is installing devDependencies");
2054
}
55+

Diff for: preuninstall.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
const hook = require("nativescript-hook")(__dirname);
4+
5+
const { getProjectDir } = require("./projectHelpers");
6+
7+
const projectDir = getProjectDir();
8+
9+
if (projectDir) {
10+
hook.preuninstall();
11+
}

0 commit comments

Comments
 (0)