-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmodify-pkgjson.mjs
75 lines (62 loc) · 1.78 KB
/
modify-pkgjson.mjs
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
73
74
75
import fs from 'fs';
import path from 'path';
//THIS IS A WORK IN PROGRESS
//! DO NOT PASS STRINGS WITH SPACES AS ARGUMENTS
const args = process.argv;
if (args.length <= 2) {
console.error(`
This script needs minimum two arguments.
1: action type 'add' | 'remove'
2: key
3: value (optional)
example node modify-pkgjson.mjs add homepage https://github.com
`);
throw new Error('not enough args');
}
args.splice(0, 2);
const PKG_NAME = path.basename(path.resolve(process.cwd()));
const action = args[0]; //add, remove, modify
const key = args[1].includes('PKG_NAME')
? args[1].replace(/PKG_NAME/g, PKG_NAME)
: args[1];
const value =
args.length > 2
? args[2].includes('PKG_NAME')
? args[2].replace(/PKG_NAME/g, PKG_NAME)
: args[2]
: undefined;
function main() {
const folder = fs.realpathSync('.');
const pkg = readPackageJson(folder);
if (action === 'add') {
const fpath = path.join(folder, 'package.json');
console.debug(`Adding "${key}":"${value}" to ${fpath}...`);
pkg[`${key}`] = `${value}`;
fs.writeFileSync(fpath, JSON.stringify(pkg, null, 2) + '\n');
return;
}
if (action === 'remove') {
const fpath = path.join(folder, 'package.json');
if (!(key in pkg)) {
throw new Error(`There is no ${key} in ${fpath}`);
} else {
console.debug(`Deleting "${key}" from ${fpath}...`);
delete pkg[`${key}`];
fs.writeFileSync(fpath, JSON.stringify(pkg, null, 2) + '\n');
return;
}
}
}
export function readJsonFile(fpath) {
const content = fs.readFileSync(fpath);
return JSON.parse(content);
}
export function readPackageJson(folder) {
const packageJsonPath = path.join(folder, 'package.json');
return readJsonFile(packageJsonPath);
}
try {
main();
} catch (err) {
console.error(err);
}