-
Notifications
You must be signed in to change notification settings - Fork 4
/
pre-uninstall.js
72 lines (58 loc) · 2.16 KB
/
pre-uninstall.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const fs = require('fs');
const path = require('path');
const Conf = require('conf');
const { red, yellow, dim, cyan } = require('kleur');
const { getDeleteConfigConfirmation } = require('../lib/prompt');
function printWarningMessage() {
const warningMessage = [
cyan('App configuration exists!'),
dim(
'Please note if you delete the app configuration, you will lose previously initialized project settings.'
),
dim(
'That means if you install Jenni again, you have to reinitialize it on your every project again.'
),
].join('\n');
console.log(`${warningMessage}\n`);
}
async function deleteConfig(env = process.env) {
const argv = env.npm_config_argv || '{}';
const { original = [] } = JSON.parse(argv);
// The `preuninstall` hook also runs when the package gets updated to a new version (i.e. npm update -g jenni).
// But we should Delete the config only when Jenni gets uninstalled explicitly. (i.e. npm uninstall -g jenni)
if (!original.includes('uninstall')) return;
// Initializing `new Conf()` will create an empty directory.
const store = new Conf();
const configFilePath = store.path;
const configDirPath = path.dirname(configFilePath);
try {
const isConfigExists = fs.existsSync(configFilePath);
if (isConfigExists) {
printWarningMessage();
const { confirmation } = await getDeleteConfigConfirmation();
if (!confirmation) {
console.log(
dim('\nWise choice! We will keep the app configuration for future use.')
);
return;
}
fs.unlinkSync(configFilePath);
}
fs.rmdirSync(configDirPath);
if (isConfigExists) {
console.log(dim('\nThe app configuration has been successfully deleted.'));
}
} catch (err) {
console.log(red('\nFailed to delete the app configuration.'));
console.log(`Manually delete the following directory: ${yellow(configDirPath)}\n`);
}
}
if (require.main === module) {
// execute the fn only if script called from the command line. i.e. node <path/to/file>
deleteConfig().catch(error => {
console.log(error);
});
} else {
// for testing purpose export the fn
module.exports = deleteConfig;
}