1 | const fs = require('fs-extra');
|
2 | const path = require('path');
|
3 | const R = require('ramda');
|
4 |
|
5 | const CliFileNotFoundError = require('@src/exceptions/cli-file-not-found-error');
|
6 | const yaml = require('@src/model/yaml-parser');
|
7 | const jsonView = require('@src/view/json-view');
|
8 |
|
9 | module.exports = class DialogReplayFile {
|
10 | |
11 |
|
12 |
|
13 |
|
14 |
|
15 | constructor(filePath) {
|
16 | this.filePath = filePath;
|
17 | this.content = this.readFileContent(this.filePath);
|
18 | }
|
19 |
|
20 |
|
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 |
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
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 |
|
81 |
|
82 |
|
83 |
|
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 |
|
104 |
|
105 |
|
106 | doesFileExist(filePath) {
|
107 | if (!fs.existsSync(filePath)) {
|
108 | throw new CliFileNotFoundError(`File ${filePath} not exists.`);
|
109 | }
|
110 | }
|
111 |
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 | getProperty(pathArray) {
|
119 | return R.view(R.lensPath(pathArray), this.content);
|
120 | }
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | setProperty(pathArray, newValue) {
|
129 | this.content = R.set(R.lensPath(pathArray), newValue, this.content);
|
130 | }
|
131 | };
|