UNPKG

1.54 kBJavaScriptView Raw
1'use strict';
2const fs = require('fs');
3const path = require('path');
4
5module.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 * Saves data to file with given type into given filename and directory, firstly checks if directory exists
19 * @param {object|string} data
20 * @param {string} type
21 * @param {string} filename
22 * @param {string} directory
23 */
24const 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 * Checks if given directory exists, if not it will be created
32 * @param {string} directory
33 */
34const checkDirectory = (directory) => {
35 try {
36 fs.statSync(directory);
37 } catch (e) {
38 fs.mkdirSync(directory);
39 }
40};
41
42/**
43 * Deletes all files in directory
44 * @param {string} directory
45 */
46const 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};