forked from Theshedman/password-validator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package-root.updater.js
62 lines (48 loc) · 1.48 KB
/
package-root.updater.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
/* eslint-disable @typescript-eslint/explicit-function-return-type */
const fsPromise = import("fs");
class PackageRootUpdater {
parsePackageJson() {
// Read the package.json file
fsPromise
.then(fs => {
fs.readFile("./package.json", "utf8", (err, data) => {
this.handleFileError(err);
const modifiedPackageJson = this.modifyPackageJson(data);
this.writePackageJsonBackToDisk(fs, modifiedPackageJson);
});
})
.catch(error => {
console.log(`Error importing fs module: ${error}`);
});
}
writePackageJsonBackToDisk(fs, modifiedPackageJson) {
fs.writeFile("./package.json", modifiedPackageJson + "\n", "utf8", err => {
this.handleFileError(err);
});
}
modifyPackageJson(data) {
// Parse the file data (which is a string) as JSON
const packageJson = JSON.parse(data);
// Now you can modify the packageJson object as needed
packageJson.type = this.packageType();
// Convert the JSON object back to a string
return JSON.stringify(packageJson, null, 2);
}
handleFileError(err) {
if (err) {
throw new Error(`Error reading file from disk: ${err}`);
}
}
packageType() {
const argvElement = process.argv[2];
if (!argvElement) {
throw new Error("Missing required argument!");
}
const [key, value] = argvElement.split("=");
if (!value) {
return key;
}
return value;
}
}
new PackageRootUpdater().parsePackageJson();