1 | const path = require('path');
|
2 | const fs = require('fs');
|
3 | const message = require('../utils/message');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function getPkgJSON(cwd) {
|
11 | const pkgJSONPath = path.join(cwd, './package.json');
|
12 | return getJSON(pkgJSONPath);
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function getJSON(jsonPath) {
|
21 | if (!fs.existsSync(jsonPath)) {
|
22 | throw new Error(message.invalid);
|
23 | }
|
24 | const jsonString = fs.readFileSync(jsonPath, 'utf-8');
|
25 | return JSON.parse(jsonString);
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function writePkgJSON(pkg, cwd) {
|
34 | const pkgJSONPath = path.join(cwd, './package.json');
|
35 | writeJSON(pkg, pkgJSONPath);
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function writeJSON(obj, jsonPath) {
|
44 | if (!fs.existsSync(jsonPath)) {
|
45 | throw new Error(message.invalid);
|
46 | }
|
47 | const jsonString = JSON.stringify(obj, null, 2);
|
48 | fs.writeFileSync(jsonPath, jsonString, 'utf-8');
|
49 | }
|
50 |
|
51 | module.exports = {
|
52 | getPkgJSON,
|
53 | writePkgJSON,
|
54 | getJSON,
|
55 | writeJSON,
|
56 | };
|