Repository navigation
Expand file tree
/
Copy pathjson.js
More file actions
126 lines (115 loc) · 3.95 KB
/
Copy pathjson.js
File metadata and controls
126 lines (115 loc) · 3.95 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import fs from 'fs';
import chalk from 'chalk';
// eslint-disable-next-line no-console
const log = console.log;
/**
* Reads a JSON file and return a parsed JSON object..
*
* @param {string} project - The project's name.
* @param {string} packageManager - Which package manager.
* @param {boolean} output - Should an information message be outputted.
* @return {object} JS object of the json data.
*/
function readJson( project, packageManager, output ) {
const file = packageManager + '.json';
let data;
try {
data = fs.readFileSync( `projects/${ project }/${ file }`, 'utf8' );
} catch ( err ) {
output && log( chalk.yellow( `This project does not have a ${ file } file.` ) );
return undefined;
}
return parseJSON( data, output );
}
/**
* Writes a JSON file.
*
* @param {string} project - The project's name.
* @param {string} packageManager - Which package manager.
* @param {object} json - JSON to write.
* @param {string} pkgDir - The project's directory.
* @param {boolean} output - Should an information message be outputted.
*/
function writeJson( project, packageManager, json, pkgDir, output ) {
const file = packageManager + '.json';
try {
fs.writeFileSync( pkgDir + '/' + file, createJSON( json ) );
} catch ( err ) {
output && console.error( chalk.red( `Could not write the json file.` ), err );
}
}
/**
* Reads the composer.json file and returns a parsed JS object.
*
* @param {string} project - The project's name.
* @param {boolean} output - Should an information message be outputted.
* @return {object} Composer.json as a JS object.
*/
export function readComposerJson( project, output = true ) {
return readJson( project, 'composer', output );
}
/**
* Reads the package.json file and returns a parsed JS object.
*
* @param {string} project - The project's name.
* @param {boolean} output - Should an information message be outputted.
* @return {object} Package.json as a JS object.
*/
export function readPackageJson( project, output = true ) {
return readJson( project, 'package', output );
}
/**
* Writes the composer.json file.
*
* @param {string} project - The project's name.
* @param {object} json - Object to convert.
* @param {string} pkgDir - The project's directory.
* @param {boolean} output - Should an information message be outputted.
* @return {void}
*/
export function writeComposerJson( project, json, pkgDir, output = true ) {
return writeJson( project, 'composer', json, pkgDir, output );
}
/**
* Writes the package.json file.
*
* @param {string} project - The project's name.
* @param {object} json - Object to convert.
* @param {string} pkgDir - The project's directory.
* @param {boolean} output - Should an information message be outputted.
* @return {void}
*/
export function writePackageJson( project, json, pkgDir, output = true ) {
return writeJson( project, 'package', json, pkgDir, output );
}
/**
* Parses the JSON data or throws an log piece on failure.
*
* @param {string} data - string of JSON data.
* @param {boolean } output - should the console output a message if it can't parse the JSON.
* @return {object|undefined} JSON Object or undefined if unable to read.
*/
function parseJSON( data, output = false ) {
try {
return JSON.parse( data );
} catch ( parseError ) {
output && log( chalk.red( 'Could not parse JSON. Something is pretty wrong.' ), parseError );
return undefined;
}
}
/**
* Stringify the JSON data or throws an log piece on failure.
*
* @param {object} data - data object.
* @param {boolean} output - should the console output a message if it can't parse the JSON.
* @return {string|undefined} JSON string or undefined if unable to read.
*/
function createJSON( data, output = false ) {
try {
return JSON.stringify( data, null, 2 );
} catch ( parseError ) {
output &&
log( chalk.red( 'Could not stringify JSON. Something is pretty wrong.' ), parseError );
return undefined;
}
}