UNPKG

4.36 kBJavaScriptView Raw
1const fs = require('fs-extra');
2const path = require('path');
3const R = require('ramda');
4
5const CliFileNotFoundError = require('@src/exceptions/cli-file-not-found-error');
6const yaml = require('@src/model/yaml-parser');
7const jsonView = require('@src/view/json-view');
8
9module.exports = class DialogReplayFile {
10 /**
11 * Constructor for GlobalConfig class
12 * @param {string} filePath
13 * @throws {Error}
14 */
15 constructor(filePath) {
16 this.filePath = filePath;
17 this.content = this.readFileContent(this.filePath);
18 }
19
20 // Getters and Setters
21
22 getSkillId() {
23 return this.getProperty(['skillId']);
24 }
25
26 setSkillId(skillId) {
27 return this.setProperty(['skillId'], skillId);
28 }
29
30 getLocale() {
31 return this.getProperty(['locale']);
32 }
33
34 setLocale(locale) {
35 return this.setProperty(['locale'], locale);
36 }
37
38 getType() {
39 return this.getProperty(['type']);
40 }
41
42 setType(type) {
43 return this.setProperty(['type'], type);
44 }
45
46 getUserInput() {
47 return this.getProperty(['userInput']);
48 }
49
50 setUserInput(userInput) {
51 return this.setProperty(['userInput'], userInput);
52 }
53
54 // TODO: move these operations to a model interface since replay file doesn't support yaml files currently.
55
56 /**
57 * Reads contents of a given file. Currently supports files of the following types: .json, .yaml and .yml
58 * Throws error if filePath is invalid, file does not have read permissions or is of unsupported file type.
59 * @param {String} filePath path to the given file.
60 */
61 readFileContent(filePath) {
62 let fileType;
63 try {
64 fileType = path.extname(filePath).toLowerCase();
65 this.doesFileExist(filePath);
66 fs.accessSync(filePath, fs.constants.R_OK);
67 if (fileType === '.json') {
68 return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
69 }
70 if (fileType === '.yaml' || fileType === '.yml') {
71 return yaml.load(filePath);
72 }
73 throw new Error('ASK CLI does not support this file type.');
74 } catch (error) {
75 throw `Failed to parse ${fileType} file ${filePath}.\n${error.message}`;
76 }
77 }
78
79 /**
80 * Writes contents to a given file. Currently supports files of the following types: .json, .yaml and .yml
81 * Throws error if filePath is invalid, file does not have write permissions or is of unsupported file type.
82 * @param {Object} content data to ve written to the file
83 * @param {String} filePath path to the given file.
84 */
85 writeContentToFile(content, filePath) {
86 try {
87 this.doesFileExist(filePath);
88 fs.accessSync(filePath, fs.constants.W_OK);
89 const fileType = path.extname(filePath).toLowerCase();
90 if (fileType === '.json') {
91 fs.writeFileSync(filePath, jsonView.toString(content), 'utf-8');
92 } else if (fileType === '.yaml' || fileType === '.yml') {
93 yaml.dump(filePath, content);
94 } else {
95 throw new Error('ASK CLI does not support this file type.');
96 }
97 } catch (error) {
98 throw `Failed to write to file ${filePath}.\n${error.message}`;
99 }
100 }
101
102 /**
103 * Check if the file exists on the given path. Throws error if it doesn't exist.
104 * @param {String} filePath path to the given file.
105 */
106 doesFileExist(filePath) {
107 if (!fs.existsSync(filePath)) {
108 throw new CliFileNotFoundError(`File ${filePath} not exists.`);
109 }
110 }
111
112 // TODO: these two methods can be in jsonView since we are reading/modifying JSON content
113 /**
114 * Get property based on the property array.
115 * Return undefined if not found.
116 * @param {string} pathArray e.g. ['path', 'to', 'the', '3rd', 'object', 2, 'done']
117 */
118 getProperty(pathArray) {
119 return R.view(R.lensPath(pathArray), this.content);
120 }
121
122 /**
123 * Set property to the runtime object based on the property array.
124 * Create field if path does not exist.
125 * @param {string} pathArray
126 * @param {string} newValue
127 */
128 setProperty(pathArray, newValue) {
129 this.content = R.set(R.lensPath(pathArray), newValue, this.content);
130 }
131};