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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fable",
3
- "version": "3.0.51",
3
+ "version": "3.0.52",
4
4
  "description": "An entity behavior management and API bundling library.",
5
5
  "main": "source/Fable.js",
6
6
  "scripts": {
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
- super(pFable, pOptions, pServiceHash);
11
+ super(pFable, pOptions, pServiceHash);
12
12
 
13
- this.serviceType = 'FilePersistence';
13
+ this.serviceType = 'FilePersistence';
14
14
 
15
- if (!this.options.hasOwnProperty('Mode'))
16
- {
17
- this.options.Mode = parseInt('0777', 8) & ~process.umask();
18
- }
15
+ if (!this.options.hasOwnProperty('Mode'))
16
+ {
17
+ this.options.Mode = parseInt('0777', 8) & ~process.umask();
18
+ }
19
19
 
20
- this.currentInputFolder = `/tmp`;
21
- this.currentOutputFolder = `/tmp`;
20
+ this.currentInputFolder = `/tmp`;
21
+ this.currentOutputFolder = `/tmp`;
22
22
  }
23
23
 
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
- libFS.writeFileSync(pFileName, pFileContent, tmpOptions);
45
- }
46
-
47
- appendFileSync(pFileName, pAppendContent, pOptions)
48
- {
49
- let tmpOptions = (typeof(pOptions) === 'undefined') ? 'utf8' : pOptions;
50
- libFS.appendFileSync(pFileName, pAppendContent, tmpOptions);
51
- }
52
-
53
- writeFileSyncFromObject(pFileName, pObject)
54
- {
55
- this.writeFileSync(pFileName, JSON.stringify(pObject, null, 4));
56
- }
57
-
58
- writeFileSyncFromArray(pFileName, pFileArray)
59
- {
60
- if (!Array.isArray(pFileArray))
61
- {
62
- 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)}).`);
63
- return Error('Attempted to write ${pFileName} from array but the expected array was not an array (it was a ${typeof(pFileArray)}).');
64
- }
65
- else
66
- {
67
- for (let i = 0; i < pFileArray.length; i++)
68
- {
69
- this.appendFileSync(pFileName, `${pFileArray[i]}\n`);
70
- }
71
- }
72
- }
73
-
74
- // Default folder behaviors
75
-
76
- getDefaultOutputPath(pFileName)
77
- {
78
- return libPath.join(this.currentOutputFolder, pFileName);
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
- // Folder management
97
-
98
- makeFolderRecursive (pParameters, fCallback)
99
- {
100
- let tmpParameters = pParameters;
101
-
102
- if (typeof(pParameters) == 'string')
103
- {
104
- tmpParameters = { Path: pParameters };
105
- }
106
- else if (typeof(pParameters) !== 'object')
107
- {
108
- fCallback(new Error('Parameters object or string not properly passed to recursive folder create.'));
109
- return false;
110
- }
111
-
112
- if ((typeof(tmpParameters.Path) !== 'string'))
113
- {
114
- fCallback(new Error('Parameters object needs a path to run the folder create operation.'));
115
- return false;
116
- }
117
-
118
- if (!tmpParameters.hasOwnProperty('Mode'))
119
- {
120
- tmpParameters.Mode = this.options.Mode;
121
- }
122
-
123
- // Check if we are just starting .. if so, build the initial state for our recursive function
124
- if (typeof(tmpParameters.CurrentPathIndex) === 'undefined')
125
- {
126
- // Build the tools to start recursing
127
- tmpParameters.ActualPath = libPath.normalize(tmpParameters.Path);
128
- tmpParameters.ActualPathParts = tmpParameters.ActualPath.split(libPath.sep);
129
- tmpParameters.CurrentPathIndex = 0;
130
- tmpParameters.CurrentPath = '';
131
- }
132
- else
133
- {
134
- // This is not our first run, so we will continue the recursion.
135
- // Build the new base path
136
- if (tmpParameters.CurrentPath == libPath.sep)
137
- {
138
- tmpParameters.CurrentPath = tmpParameters.CurrentPath + tmpParameters.ActualPathParts[tmpParameters.CurrentPathIndex];
139
- }
140
- else
141
- {
142
- tmpParameters.CurrentPath = tmpParameters.CurrentPath + libPath.sep + tmpParameters.ActualPathParts[tmpParameters.CurrentPathIndex];
143
- }
144
-
145
- // Increment the path index
146
- tmpParameters.CurrentPathIndex++;
147
- }
148
-
149
- // Check if the path is fully complete
150
- if (tmpParameters.CurrentPathIndex >= tmpParameters.ActualPathParts.length)
151
- {
152
- fCallback(null);
153
- return true;
154
- }
155
-
156
- // Check if the path exists (and is a folder)
157
- libFS.open(tmpParameters.CurrentPath + libPath.sep + tmpParameters.ActualPathParts[tmpParameters.CurrentPathIndex], 'r',
158
- function(pError, pFileDescriptor)
159
- {
160
- if (pFileDescriptor)
161
- {
162
- libFS.closeSync(pFileDescriptor);
163
- }
164
-
165
- if (pError && pError.code=='ENOENT')
166
- {
167
- /* Path doesn't exist, create it */
168
- libFS.mkdir(tmpParameters.CurrentPath + libPath.sep + tmpParameters.ActualPathParts[tmpParameters.CurrentPathIndex], tmpParameters.Mode,
169
- (pCreateError) =>
170
- {
171
- if (!pCreateError)
172
- {
173
- // We have now created our folder and there was no error -- continue.
174
- return this.makeFolderRecursive(tmpParameters, fCallback);
175
- }
176
- else
177
- {
178
- fCallback(pCreateError);
179
- return false;
180
- }
181
- });
182
- }
183
- else
184
- {
185
- return this.makeFolderRecursive(tmpParameters, fCallback);
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, 99);
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
- 'File Persistence',
18
+ 'Fable FilePersistence',
17
19
  function()
18
20
  {
21
+ setup
22
+ (
23
+ function() { }
24
+ );
25
+
19
26
  suite
20
27
  (
21
- 'Work with Files',
28
+ 'Basic File Management',
22
29
  function()
23
30
  {
24
31
  test
25
32
  (
26
- 'Recursively Create a Folder',
27
- function(fDone)
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 fDone();
98
+ return fTestComplete();
32
99
  }
33
100
  );
34
101
  }