1 | 'use strict';
|
2 | const fs = require('fs');
|
3 | const path = require('path');
|
4 |
|
5 | module.exports = (assets, directory) => {
|
6 | saveToFile(assets.intentSchema, 'json', 'intentSchema', directory);
|
7 | saveToFile(assets.utterances, 'txt', 'utterances', directory);
|
8 |
|
9 | const customSlotsDir = path.join(directory, 'customSlots');
|
10 | deleteFilesInDir(customSlotsDir);
|
11 | Object.keys(assets.customSlots).forEach((key) => {
|
12 | const newLineFormat = assets.customSlots[key].join('\n');
|
13 | saveToFile(newLineFormat, 'txt', `${key}`, customSlotsDir);
|
14 | });
|
15 | };
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | const saveToFile = (data, type, filename, directory) => {
|
25 | const pathname = path.join(directory, `${filename}.${type}`);
|
26 | checkDirectory(directory);
|
27 | fs.writeFileSync(pathname, data);
|
28 | };
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | const checkDirectory = (directory) => {
|
35 | try {
|
36 | fs.statSync(directory);
|
37 | } catch (e) {
|
38 | fs.mkdirSync(directory);
|
39 | }
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | const deleteFilesInDir = (directory) => {
|
47 | if (fs.existsSync(directory)) {
|
48 | fs.readdirSync(directory).forEach((file) => {
|
49 | const customSlotFile = path.join(directory, file);
|
50 | fs.unlinkSync(customSlotFile);
|
51 | });
|
52 | }
|
53 | };
|