This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.js
109 lines (87 loc) · 3.08 KB
/
helpers.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
var fs = require('fs');
var q = require('q');
var colors = require('colors');
var sanitize = require("sanitize-filename");
var path = require('path');
// writes the given array to a set of files
// one file per array object
// file name is based on fileNameBase + array[i][propertyToAppend]
function writeArrayToFile(arrayObject, configRoot, fileNameBase, propertyToAppend, i) {
if (!i) {
i = 0;
}
if (i >= arrayObject.length) {
return;
}
let item = arrayObject[i];
let fileName = path.join(configRoot, item.opent2t.translator + fileNameBase + item.opent2t[propertyToAppend] + ".json");
console.log("------ Saving device %j to: %j", item.opent2t[propertyToAppend], fileName);
let contents = JSON.stringify(item);
fs.writeFile(fileName, contents, function (err, data) {
if (err) {
return console.log(err);
}
return writeArrayToFile(arrayObject, configRoot, fileNameBase, propertyToAppend, i + 1);
});
}
// reads the file and returns its contents
// if the file is not found, rejects with the provided error message
// if any other error, rejects with the error
function readFile(fileName, errorMessage) {
let deferred = q.defer();
fs.readFile(fileName, 'utf8', function (err, data) {
if (err) {
if (err.errno === -4058) {
deferred.reject(errorMessage);
}
deferred.reject(err);
}
deferred.resolve(data);
});
return deferred.promise;
}
// logs the given error in red
function logError(error) {
// do anything special
console.log("");
console.log(colors.red("***************** Error Received *****************"));
console.log(colors.red(error));
if (error.stack) {
console.log(colors.red(error.stack));
}
}
function createConfigData(name, packageName, authInfo) {
return { translator: name, translatorPackageName: packageName, authInfo: authInfo };
}
function createHubDeviceFileName(hubName, deviceId) {
return createSaveFileName(hubName, "_device_" + deviceId);
}
function sanitizeFileName(fileName) {
return sanitize(fileName);
}
function createOnboardingFileName(baseName) {
return createSaveFileName(sanitizeFileName(baseName), "_onboardingInfo");
}
function createSaveFileName(baseName, trailing) {
return "./" + baseName + trailing + ".json";
}
function logObject(objectToLog) {
console.log(colors.green(JSON.stringify(objectToLog, null, 2)));
}
function logHeader(stringToLog) {
console.log(colors.cyan(stringToLog));
}
function logDebug(stringToLog) {
console.log(colors.grey(stringToLog));
}
module.exports.writeArrayToFile = writeArrayToFile;
module.exports.readFile = readFile;
module.exports.logObject = logObject;
module.exports.logError = logError;
module.exports.logHeader = logHeader;
module.exports.logDebug = logDebug;
module.exports.createOnboardingFileName = createOnboardingFileName;
module.exports.sanitizeFileName = sanitizeFileName;
module.exports.createHubDeviceFileName = createHubDeviceFileName;
module.exports.createConfigData = createConfigData;