-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-utils.ts
44 lines (37 loc) · 1.21 KB
/
file-utils.ts
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
import fs from 'fs';
export class FileUtils {
/**
* Create/overwrite a new JSON file saving the inputted data at the specified path location.
* @static
* @param {any} data The data to write to file.
* @param {string} path A file descriptor like 'User/documents/new-data.json'.
*/
static saveJsonToFile(data: any, path: string): void {
try {
fs.writeFileSync(path, JSON.parse(JSON.stringify(data, null, 4)));
} catch (err: any) {
throw new Error(err);
}
}
// ```js
// // import fs, express, cors, bodyParser
// // do the usual express setup
// app.use(cors());
// app.use(bodyParser.json());
// // set up a port listener that the frontend will call to make request
// app.get('/api/get-file-contents', (req, res) => {
// res.json(loadFiles('myPath/here/'));
// });
// loadFiles(path) {
// results = [];
// fs.readdirSync(path).forEach(file => {
// results.push(readFile(path, file));
// });
// return results;
// }
// readFile(path, file) {
// let contents = fs.readFileSync(path+'/'+fileName, 'utf8');
// return contents;
// }
// ```
}