fable 3.0.51 → 3.0.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/fable.compatible.js +37 -27
- package/dist/fable.compatible.min.js +2 -2
- package/dist/fable.compatible.min.js.map +1 -1
- package/dist/fable.js +19 -9
- package/dist/fable.min.js +2 -2
- package/dist/fable.min.js.map +1 -1
- package/package.json +1 -1
- package/source/Fable.js +1 -0
- package/source/services/Fable-Service-FilePersistence.js +163 -158
- package/test/DataGeneration_tests.js +1 -1
- package/test/FilePersistence_tests.js +73 -6
package/package.json
CHANGED
package/source/Fable.js
CHANGED
|
@@ -54,6 +54,7 @@ class Fable
|
|
|
54
54
|
this.serviceManager.addServiceType('CSVParser', require('./services/Fable-Service-CSVParser.js'));
|
|
55
55
|
this.serviceManager.addServiceType('Manifest', require('manyfest'));
|
|
56
56
|
this.serviceManager.addServiceType('ObjectCache', require('cachetrax'));
|
|
57
|
+
this.serviceManager.addServiceType('FilePersistence', require('./services/Fable-Service-FilePersistence.js'));
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
get isFable()
|
|
@@ -8,75 +8,80 @@ class FableServiceFilePersistence extends libFableServiceBase
|
|
|
8
8
|
{
|
|
9
9
|
constructor(pFable, pOptions, pServiceHash)
|
|
10
10
|
{
|
|
11
|
-
|
|
11
|
+
super(pFable, pOptions, pServiceHash);
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
this.serviceType = 'FilePersistence';
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
if (!this.options.hasOwnProperty('Mode'))
|
|
16
|
+
{
|
|
17
|
+
this.options.Mode = parseInt('0777', 8) & ~process.umask();
|
|
18
|
+
}
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
this.currentInputFolder = `/tmp`;
|
|
21
|
+
this.currentOutputFolder = `/tmp`;
|
|
22
22
|
}
|
|
23
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
|
-
|
|
24
|
+
joinPath(pPathArray)
|
|
25
|
+
{
|
|
26
|
+
return libPath.resolve(...pPathArray);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
existsSync(pPath)
|
|
30
|
+
{
|
|
31
|
+
return libFS.existsSync(pPath);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
exists(pPath, fCallback)
|
|
35
|
+
{
|
|
36
|
+
let tmpFileExists = this.existsSync(pPath);;
|
|
37
|
+
|
|
38
|
+
return fCallback(null, tmpFileExists);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
writeFileSync(pFileName, pFileContent, pOptions)
|
|
42
|
+
{
|
|
43
|
+
let tmpOptions = (typeof(pOptions) === 'undefined') ? 'utf8' : pOptions;
|
|
44
|
+
return libFS.writeFileSync(pFileName, pFileContent, tmpOptions);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
appendFileSync(pFileName, pAppendContent, pOptions)
|
|
48
|
+
{
|
|
49
|
+
let tmpOptions = (typeof(pOptions) === 'undefined') ? 'utf8' : pOptions;
|
|
50
|
+
return libFS.appendFileSync(pFileName, pAppendContent, tmpOptions);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
deleteFileSync(pFileName)
|
|
54
|
+
{
|
|
55
|
+
return libFS.unlinkSync(pFileName);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
writeFileSyncFromObject(pFileName, pObject)
|
|
59
|
+
{
|
|
60
|
+
return this.writeFileSync(pFileName, JSON.stringify(pObject, null, 4));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
writeFileSyncFromArray(pFileName, pFileArray)
|
|
64
|
+
{
|
|
65
|
+
if (!Array.isArray(pFileArray))
|
|
66
|
+
{
|
|
67
|
+
this.log.error(`File Persistence Service attempted to write ${pFileName} from array but the expected array was not an array (it was a ${typeof(pFileArray)}).`);
|
|
68
|
+
return Error('Attempted to write ${pFileName} from array but the expected array was not an array (it was a ${typeof(pFileArray)}).');
|
|
69
|
+
}
|
|
70
|
+
else
|
|
71
|
+
{
|
|
72
|
+
for (let i = 0; i < pFileArray.length; i++)
|
|
73
|
+
{
|
|
74
|
+
return this.appendFileSync(pFileName, `${pFileArray[i]}\n`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Default folder behaviors
|
|
80
|
+
|
|
81
|
+
getDefaultOutputPath(pFileName)
|
|
82
|
+
{
|
|
83
|
+
return libPath.join(this.currentOutputFolder, pFileName);
|
|
84
|
+
}
|
|
80
85
|
|
|
81
86
|
dataFolderWriteSync(pFileName, pFileContent)
|
|
82
87
|
{
|
|
@@ -93,100 +98,100 @@ class FableServiceFilePersistence extends libFableServiceBase
|
|
|
93
98
|
return this.writeFileSyncFromArray(this.getDefaultOutputPath(pFileName), pFileArray);
|
|
94
99
|
}
|
|
95
100
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
101
|
+
// Folder management
|
|
102
|
+
|
|
103
|
+
makeFolderRecursive (pParameters, fCallback)
|
|
104
|
+
{
|
|
105
|
+
let tmpParameters = pParameters;
|
|
106
|
+
|
|
107
|
+
if (typeof(pParameters) == 'string')
|
|
108
|
+
{
|
|
109
|
+
tmpParameters = { Path: pParameters };
|
|
110
|
+
}
|
|
111
|
+
else if (typeof(pParameters) !== 'object')
|
|
112
|
+
{
|
|
113
|
+
fCallback(new Error('Parameters object or string not properly passed to recursive folder create.'));
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if ((typeof(tmpParameters.Path) !== 'string'))
|
|
118
|
+
{
|
|
119
|
+
fCallback(new Error('Parameters object needs a path to run the folder create operation.'));
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!tmpParameters.hasOwnProperty('Mode'))
|
|
124
|
+
{
|
|
125
|
+
tmpParameters.Mode = this.options.Mode;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Check if we are just starting .. if so, build the initial state for our recursive function
|
|
129
|
+
if (typeof(tmpParameters.CurrentPathIndex) === 'undefined')
|
|
130
|
+
{
|
|
131
|
+
// Build the tools to start recursing
|
|
132
|
+
tmpParameters.ActualPath = libPath.normalize(tmpParameters.Path);
|
|
133
|
+
tmpParameters.ActualPathParts = tmpParameters.ActualPath.split(libPath.sep);
|
|
134
|
+
tmpParameters.CurrentPathIndex = 0;
|
|
135
|
+
tmpParameters.CurrentPath = '';
|
|
136
|
+
}
|
|
137
|
+
else
|
|
138
|
+
{
|
|
139
|
+
// This is not our first run, so we will continue the recursion.
|
|
140
|
+
// Build the new base path
|
|
141
|
+
if (tmpParameters.CurrentPath == libPath.sep)
|
|
142
|
+
{
|
|
143
|
+
tmpParameters.CurrentPath = tmpParameters.CurrentPath + tmpParameters.ActualPathParts[tmpParameters.CurrentPathIndex];
|
|
144
|
+
}
|
|
145
|
+
else
|
|
146
|
+
{
|
|
147
|
+
tmpParameters.CurrentPath = tmpParameters.CurrentPath + libPath.sep + tmpParameters.ActualPathParts[tmpParameters.CurrentPathIndex];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Increment the path index
|
|
151
|
+
tmpParameters.CurrentPathIndex++;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Check if the path is fully complete
|
|
155
|
+
if (tmpParameters.CurrentPathIndex >= tmpParameters.ActualPathParts.length)
|
|
156
|
+
{
|
|
157
|
+
fCallback(null);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Check if the path exists (and is a folder)
|
|
162
|
+
libFS.open(tmpParameters.CurrentPath + libPath.sep + tmpParameters.ActualPathParts[tmpParameters.CurrentPathIndex], 'r',
|
|
163
|
+
function(pError, pFileDescriptor)
|
|
164
|
+
{
|
|
165
|
+
if (pFileDescriptor)
|
|
166
|
+
{
|
|
167
|
+
libFS.closeSync(pFileDescriptor);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (pError && pError.code=='ENOENT')
|
|
171
|
+
{
|
|
172
|
+
/* Path doesn't exist, create it */
|
|
173
|
+
libFS.mkdir(tmpParameters.CurrentPath + libPath.sep + tmpParameters.ActualPathParts[tmpParameters.CurrentPathIndex], tmpParameters.Mode,
|
|
174
|
+
(pCreateError) =>
|
|
175
|
+
{
|
|
176
|
+
if (!pCreateError)
|
|
177
|
+
{
|
|
178
|
+
// We have now created our folder and there was no error -- continue.
|
|
179
|
+
return this.makeFolderRecursive(tmpParameters, fCallback);
|
|
180
|
+
}
|
|
181
|
+
else
|
|
182
|
+
{
|
|
183
|
+
fCallback(pCreateError);
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
else
|
|
189
|
+
{
|
|
190
|
+
return this.makeFolderRecursive(tmpParameters, fCallback);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
}
|
|
190
195
|
}
|
|
191
196
|
|
|
192
197
|
module.exports = FableServiceFilePersistence;
|
|
@@ -27,7 +27,7 @@ suite
|
|
|
27
27
|
{
|
|
28
28
|
let testFable = new libFable();
|
|
29
29
|
let tmpDataGeneration = testFable.serviceManager.instantiateServiceProvider('DataGeneration');
|
|
30
|
-
Expect(tmpDataGeneration.randomIntegerUpTo(100)).to.be.within(0,
|
|
30
|
+
Expect(tmpDataGeneration.randomIntegerUpTo(100)).to.be.within(0, 100);
|
|
31
31
|
return fTestComplete();
|
|
32
32
|
}
|
|
33
33
|
);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Unit tests for Fable
|
|
2
|
+
* Unit tests for the Fable simple-get RestClient
|
|
3
3
|
*
|
|
4
4
|
* @license MIT
|
|
5
5
|
*
|
|
@@ -11,24 +11,91 @@ var libFable = require('../source/Fable.js');
|
|
|
11
11
|
var Chai = require("chai");
|
|
12
12
|
var Expect = Chai.expect;
|
|
13
13
|
|
|
14
|
+
// https://en.wiktionary.org/w/api.php?action=parse&prop=wikitext&format=json&page=dog
|
|
15
|
+
|
|
14
16
|
suite
|
|
15
17
|
(
|
|
16
|
-
'
|
|
18
|
+
'Fable FilePersistence',
|
|
17
19
|
function()
|
|
18
20
|
{
|
|
21
|
+
setup
|
|
22
|
+
(
|
|
23
|
+
function() { }
|
|
24
|
+
);
|
|
25
|
+
|
|
19
26
|
suite
|
|
20
27
|
(
|
|
21
|
-
'
|
|
28
|
+
'Basic File Management',
|
|
22
29
|
function()
|
|
23
30
|
{
|
|
24
31
|
test
|
|
25
32
|
(
|
|
26
|
-
'
|
|
27
|
-
function(
|
|
33
|
+
'Check that a file exists',
|
|
34
|
+
function(fTestComplete)
|
|
35
|
+
{
|
|
36
|
+
let testFable = new libFable();
|
|
37
|
+
let tmpFilePersistence = testFable.serviceManager.instantiateServiceProvider('FilePersistence');
|
|
38
|
+
Expect(tmpFilePersistence).is.an('object');
|
|
39
|
+
Expect(tmpFilePersistence.existsSync(`${__dirname}/../package.json`)).to.equal(true);
|
|
40
|
+
Expect(tmpFilePersistence.existsSync(`${__dirname}/package.json`)).to.equal(false);
|
|
41
|
+
return fTestComplete();
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
test
|
|
45
|
+
(
|
|
46
|
+
'Create, write, read and then delete a file.',
|
|
47
|
+
function(fTestComplete)
|
|
28
48
|
{
|
|
29
49
|
let testFable = new libFable();
|
|
50
|
+
let tmpFilePersistence = testFable.serviceManager.instantiateServiceProvider('FilePersistence');
|
|
51
|
+
let tmpDataGeneration = testFable.serviceManager.instantiateServiceProvider('DataGeneration');
|
|
52
|
+
|
|
53
|
+
let tmpLogFilePath = `/tmp/Fable-Test-${tmpDataGeneration.randomNumericString()}.log`;
|
|
54
|
+
testFable.log.info(`Writing test log file: [${tmpLogFilePath}]`);
|
|
55
|
+
|
|
56
|
+
Expect(tmpFilePersistence.existsSync(tmpLogFilePath)).to.equal(false);
|
|
57
|
+
|
|
58
|
+
// Now write some data to the file.
|
|
59
|
+
tmpFilePersistence.writeFileSyncFromObject(tmpLogFilePath, testFable.settings);
|
|
60
|
+
|
|
61
|
+
Expect(tmpFilePersistence.existsSync(tmpLogFilePath)).to.equal(true);
|
|
62
|
+
|
|
63
|
+
// Now delete the file
|
|
64
|
+
tmpFilePersistence.deleteFileSync(tmpLogFilePath);
|
|
65
|
+
|
|
66
|
+
Expect(tmpFilePersistence.existsSync(tmpLogFilePath)).to.equal(false);
|
|
67
|
+
|
|
68
|
+
return fTestComplete();
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
test
|
|
72
|
+
(
|
|
73
|
+
'Create, write, read and then delete a file.',
|
|
74
|
+
function(fTestComplete)
|
|
75
|
+
{
|
|
76
|
+
let testFable = new libFable();
|
|
77
|
+
let tmpFilePersistence = testFable.serviceManager.instantiateServiceProvider('FilePersistence');
|
|
78
|
+
let tmpDataGeneration = testFable.serviceManager.instantiateServiceProvider('DataGeneration');
|
|
79
|
+
|
|
80
|
+
let tmpLogFilePath = `/tmp/Fable-Test-${tmpDataGeneration.randomNumericString()}.log`;
|
|
81
|
+
testFable.log.info(`Writing test log file: [${tmpLogFilePath}]`);
|
|
82
|
+
|
|
83
|
+
Expect(tmpFilePersistence.existsSync(tmpLogFilePath)).to.equal(false);
|
|
84
|
+
|
|
85
|
+
// Now write some data to the file.
|
|
86
|
+
for (let i = 0; i < 100; i++)
|
|
87
|
+
{
|
|
88
|
+
tmpFilePersistence.appendFileSync(tmpLogFilePath, `Line ${i} got a number like ${tmpDataGeneration.randomColor()} ${tmpDataGeneration.randomNumericString(3,789)} for ${tmpDataGeneration.randomName()} ${tmpDataGeneration.randomSurname()}!\n`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
Expect(tmpFilePersistence.existsSync(tmpLogFilePath)).to.equal(true);
|
|
92
|
+
|
|
93
|
+
// Now delete the file
|
|
94
|
+
tmpFilePersistence.deleteFileSync(tmpLogFilePath);
|
|
95
|
+
|
|
96
|
+
Expect(tmpFilePersistence.existsSync(tmpLogFilePath)).to.equal(false);
|
|
30
97
|
|
|
31
|
-
return
|
|
98
|
+
return fTestComplete();
|
|
32
99
|
}
|
|
33
100
|
);
|
|
34
101
|
}
|