-
Notifications
You must be signed in to change notification settings - Fork 175
/
packrat.js
58 lines (55 loc) · 2.31 KB
/
packrat.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
const PackageJson = require('@npmcli/package-json');
async function prepack() {
const pkgJson = await PackageJson.load('./');
pkgJson.content.scripts['dev_postinstall'] =
pkgJson.content.scripts['postinstall'];
pkgJson.content.scripts['postinstall'] =
pkgJson.content.scripts['pack_postinstall'];
pkgJson.content.scripts['pack_postinstall'] = 'echo removed by packrat.js';
await pkgJson.save();
// eslint-disable-next-line no-console
console.log('scripts:', {
postinstall: pkgJson.content.scripts['postinstall'],
dev_postinstall: pkgJson.content.scripts['dev_postinstall'],
});
}
async function postpack() {
const pkgJson = await PackageJson.load('./');
pkgJson.content.scripts['pack_postinstall'] =
pkgJson.content.scripts['postinstall'];
pkgJson.content.scripts['postinstall'] =
pkgJson.content.scripts['dev_postinstall'];
delete pkgJson.content.scripts['dev_postinstall'];
await pkgJson.save();
// eslint-disable-next-line no-console
console.log('scripts:', {
postinstall: pkgJson.content.scripts['postinstall'],
pack_postinstall: pkgJson.content.scripts['pack_postinstall'],
});
}
if (process.argv.length !== 3) {
console.error(
'The pinst package not working for us anymore. We need two different postinstall hooks.'
);
console.error(
'1. postinstall that runs git pre-commit hooks - this is dev only'
);
console.error('2. postinstall that runs production hooks like ibmtelemetry');
console.error('This script toggles between the 2');
console.error('usage: node packrat.js');
console.error('Options');
console.error('\t--enable Enable pack (production) postinstall hook');
console.error('\t\trename postinstall to dev_postinstall');
console.error('\t\trename pack_postinstall to postinstall');
console.error('\t--disable Disable pack (production) postinstall hook');
console.error('\t\trename postinstall to pack_postinstall');
console.error('\t\trename dev_postinstall to postinstall');
process.exit(1);
}
if (process.argv[2] === '--disable') {
// eslint-disable-next-line no-console
prepack().then(() => console.log('updated package.json'));
} else if (process.argv[2] === '--enable') {
// eslint-disable-next-line no-console
postpack().then(() => console.log('updated package.json'));
} else console.error('unknown argument', process.argv[2]);