json-to-fs-structure 1.3.0 → 2.0.0

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/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # json-to-fs-structure
2
- Clean and simple JavaScript project for turning JSON objects into directory structures.
2
+ Clean and simple TypeScript project for turning JSON objects into directory structures.
3
3
 
4
4
  # Development
5
5
  For development, clone into this repository and to install run:
6
- `npm install`
6
+ `yarn install`
7
7
  To test run:
8
- `npm test`
8
+ `yarn test`
9
9
 
10
10
  # Usage
11
11
  To use this node module, install it like so:
@@ -0,0 +1,51 @@
1
+ export type LeafProcedure<T = unknown> = (filePath: string, accumulator: T, value: unknown, property: string) => void;
2
+ export type NonLeafProcedure<T = unknown> = (filePath: string, accumulator: T, value: unknown, property: string) => T | void;
3
+ export type Procedure<T = unknown> = (filePath: string, accumulator: T, value: unknown, property: string) => T | void;
4
+ export interface ExecutorObject<T> {
5
+ leafProcedure?: LeafProcedure<T>;
6
+ nonLeafProcedure?: NonLeafProcedure<T>;
7
+ procedure?: Procedure<T>;
8
+ accumulator?: T;
9
+ }
10
+ export interface JsonToFsStructureOptions {
11
+ jsonObject: unknown;
12
+ filePath?: string;
13
+ callback?: () => void;
14
+ stopWord?: string | string[];
15
+ spaceReplace?: string;
16
+ ignoredWords?: string[];
17
+ }
18
+ export interface JsonToFsWithLeafFunctionOptions<T = unknown> {
19
+ jsonObject: unknown;
20
+ leafProcedure?: LeafProcedure<T>;
21
+ context?: T;
22
+ filePath?: string;
23
+ callback?: () => void;
24
+ stopWord?: string | string[];
25
+ spaceReplace?: string;
26
+ ignoredWords?: string[];
27
+ }
28
+ export interface JsonToFsWithNonLeafFunctionOptions<T = unknown> {
29
+ jsonObject: unknown;
30
+ nonLeafProcedure?: NonLeafProcedure<T>;
31
+ context?: T;
32
+ filePath?: string;
33
+ callback?: () => void;
34
+ stopWord?: string | string[];
35
+ spaceReplace?: string;
36
+ ignoredWords?: string[];
37
+ }
38
+ export interface JsonToFsWithFunctionOptions<T = unknown> {
39
+ jsonObject: unknown;
40
+ procedure?: Procedure<T>;
41
+ context?: T;
42
+ filePath?: string;
43
+ callback?: () => void;
44
+ stopWord?: string | string[];
45
+ spaceReplace?: string;
46
+ ignoredWords?: string[];
47
+ }
48
+ export declare function jsonToFsStructure({ jsonObject, filePath, callback, stopWord, spaceReplace, ignoredWords, }: JsonToFsStructureOptions): Promise<void>;
49
+ export declare function jsonToFsWithLeafFunction<T = unknown>({ jsonObject, leafProcedure, context, filePath, callback, stopWord, spaceReplace, ignoredWords, }: JsonToFsWithLeafFunctionOptions<T>): Promise<void>;
50
+ export declare function jsonToFsWithNonLeafFunction<T = unknown>({ jsonObject, nonLeafProcedure, context, filePath, callback, stopWord, spaceReplace, ignoredWords, }: JsonToFsWithNonLeafFunctionOptions<T>): Promise<void>;
51
+ export declare function jsonToFsWithFunction<T = unknown>({ jsonObject, procedure, context, filePath, callback, stopWord, spaceReplace, ignoredWords, }: JsonToFsWithFunctionOptions<T>): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.jsonToFsStructure = jsonToFsStructure;
37
+ exports.jsonToFsWithLeafFunction = jsonToFsWithLeafFunction;
38
+ exports.jsonToFsWithNonLeafFunction = jsonToFsWithNonLeafFunction;
39
+ exports.jsonToFsWithFunction = jsonToFsWithFunction;
40
+ const fs = __importStar(require("fs"));
41
+ const path_1 = require("path");
42
+ const isLeaf = (testNode) => testNode !== null &&
43
+ testNode !== undefined &&
44
+ typeof testNode === "object" &&
45
+ testNode.constructor === Object &&
46
+ Object.keys(testNode).length === 0;
47
+ const isEqualOrIsInArray = (property, stopWord) => !!stopWord &&
48
+ (property === stopWord ||
49
+ (Array.isArray(stopWord) && stopWord.indexOf(property) > -1));
50
+ const cloneAccumulator = (acc) => {
51
+ return Object.assign({}, acc);
52
+ };
53
+ const levelPropertiesToDirectories = (obj, filePath, stopWord, ignoredWords, spaceReplace, executorObject = {}) => {
54
+ if (obj && (typeof obj === "string" || obj instanceof String)) {
55
+ return [];
56
+ }
57
+ const { leafProcedure, nonLeafProcedure, procedure } = executorObject;
58
+ let { accumulator } = executorObject;
59
+ let promiseArray = [];
60
+ let fields = [];
61
+ if (obj && Array.isArray(obj)) {
62
+ obj.forEach((arrayObject) => {
63
+ promiseArray = promiseArray.concat(levelPropertiesToDirectories(arrayObject, filePath, stopWord, ignoredWords, spaceReplace, {
64
+ leafProcedure,
65
+ nonLeafProcedure,
66
+ procedure,
67
+ accumulator,
68
+ }));
69
+ });
70
+ }
71
+ else if (obj && typeof obj === "object") {
72
+ fields = Object.keys(obj);
73
+ }
74
+ fields.forEach((property) => {
75
+ if (!isEqualOrIsInArray(property, stopWord) &&
76
+ ignoredWords.indexOf(property) === -1) {
77
+ const newPath = spaceReplace
78
+ ? `${filePath}${path_1.sep}${property.replace(/ /g, spaceReplace)}`
79
+ : `${filePath}${path_1.sep}${property}`;
80
+ let accumulatorCopy = cloneAccumulator(accumulator);
81
+ try {
82
+ fs.mkdirSync(newPath);
83
+ }
84
+ catch (e) { }
85
+ const objRecord = obj;
86
+ const value = objRecord[property];
87
+ if (value && typeof value === "object" && Object.keys(value).length > 0) {
88
+ if (nonLeafProcedure) {
89
+ accumulatorCopy = nonLeafProcedure(newPath, cloneAccumulator(accumulator), value, property);
90
+ }
91
+ if (procedure) {
92
+ accumulatorCopy = procedure(newPath, cloneAccumulator(accumulator), value, property);
93
+ }
94
+ promiseArray = promiseArray.concat(levelPropertiesToDirectories(value, newPath, stopWord, ignoredWords, spaceReplace, {
95
+ leafProcedure,
96
+ nonLeafProcedure,
97
+ procedure,
98
+ accumulator: accumulatorCopy,
99
+ }));
100
+ }
101
+ else {
102
+ if (leafProcedure && isLeaf(value)) {
103
+ leafProcedure(newPath, cloneAccumulator(accumulator), value, property);
104
+ }
105
+ if (procedure && isLeaf(value)) {
106
+ accumulatorCopy = procedure(newPath, cloneAccumulator(accumulator), value, property);
107
+ }
108
+ }
109
+ }
110
+ else if (ignoredWords.indexOf(property) === -1) {
111
+ if (leafProcedure) {
112
+ const objRecord = obj;
113
+ const value = objRecord[property];
114
+ leafProcedure(`${filePath}${path_1.sep}`, cloneAccumulator(accumulator), value, property);
115
+ }
116
+ }
117
+ });
118
+ return promiseArray;
119
+ };
120
+ function jsonToFsStructure({ jsonObject, filePath = ".", callback = () => { }, stopWord, spaceReplace, ignoredWords = [], }) {
121
+ return Promise.all(levelPropertiesToDirectories(jsonObject, filePath, stopWord, ignoredWords, spaceReplace)).then(callback);
122
+ }
123
+ function jsonToFsWithLeafFunction({ jsonObject, leafProcedure = () => { }, context = {}, filePath = ".", callback = () => { }, stopWord, spaceReplace, ignoredWords = [], }) {
124
+ return Promise.all(levelPropertiesToDirectories(jsonObject, filePath, stopWord, ignoredWords, spaceReplace, {
125
+ leafProcedure,
126
+ accumulator: context,
127
+ })).then(callback);
128
+ }
129
+ function jsonToFsWithNonLeafFunction({ jsonObject, nonLeafProcedure = () => { }, context = {}, filePath = ".", callback = () => { }, stopWord, spaceReplace, ignoredWords = [], }) {
130
+ return Promise.all(levelPropertiesToDirectories(jsonObject, filePath, stopWord, ignoredWords, spaceReplace, {
131
+ nonLeafProcedure,
132
+ accumulator: context,
133
+ })).then(callback);
134
+ }
135
+ function jsonToFsWithFunction({ jsonObject, procedure = () => { }, context = {}, filePath = ".", callback = () => { }, stopWord, spaceReplace, ignoredWords = [], }) {
136
+ return Promise.all(levelPropertiesToDirectories(jsonObject, filePath, stopWord, ignoredWords, spaceReplace, {
137
+ procedure,
138
+ accumulator: context,
139
+ })).then(callback);
140
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,424 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const assert = __importStar(require("assert"));
37
+ const fs = __importStar(require("fs"));
38
+ const index_1 = require("../index");
39
+ describe("json-to-fs-structure", () => {
40
+ describe("jsonToFsStructure", () => {
41
+ it("should produce a simple directory with a first layer object", (done) => {
42
+ const options = {
43
+ jsonObject: {
44
+ testDirectoryField: {},
45
+ },
46
+ filePath: ".",
47
+ callback: () => {
48
+ assert.strictEqual(fs.statSync("testDirectoryField").size, 4096);
49
+ fs.rmdirSync("testDirectoryField");
50
+ done();
51
+ },
52
+ };
53
+ (0, index_1.jsonToFsStructure)(options);
54
+ });
55
+ it("should produce a simple directory with a first layer object with siblings", (done) => {
56
+ const options = {
57
+ jsonObject: {
58
+ testDirectoryField0: {},
59
+ testDirectoryField1: {},
60
+ testDirectoryField2: {},
61
+ },
62
+ filePath: ".",
63
+ callback: () => {
64
+ assert.strictEqual(fs.statSync("testDirectoryField0").size, 4096);
65
+ assert.strictEqual(fs.statSync("testDirectoryField1").size, 4096);
66
+ assert.strictEqual(fs.statSync("testDirectoryField2").size, 4096);
67
+ fs.rmdirSync("testDirectoryField0");
68
+ fs.rmdirSync("testDirectoryField1");
69
+ fs.rmdirSync("testDirectoryField2");
70
+ done();
71
+ },
72
+ };
73
+ (0, index_1.jsonToFsStructure)(options);
74
+ });
75
+ it("should produce a nested directory structure", (done) => {
76
+ const options = {
77
+ jsonObject: {
78
+ testDirectoryField3: { innerTestField: {} },
79
+ },
80
+ filePath: ".",
81
+ callback: () => {
82
+ assert.strictEqual(fs.statSync("testDirectoryField3/innerTestField").size, 4096);
83
+ fs.rmdirSync("testDirectoryField3/innerTestField");
84
+ fs.rmdirSync("testDirectoryField3");
85
+ done();
86
+ },
87
+ };
88
+ (0, index_1.jsonToFsStructure)(options);
89
+ });
90
+ it("should produce a nested directory structure when there is an array of strings", (done) => {
91
+ const options = {
92
+ jsonObject: {
93
+ testArrayField5: [
94
+ { somedir: {} },
95
+ { anotherdir: {} },
96
+ { andanotherdir: {} },
97
+ ],
98
+ },
99
+ filePath: ".",
100
+ callback: () => {
101
+ assert.strictEqual(fs.statSync("testArrayField5/somedir").size, 4096);
102
+ assert.strictEqual(fs.statSync("testArrayField5/anotherdir").size, 4096);
103
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir").size, 4096);
104
+ fs.rmdirSync("testArrayField5/somedir");
105
+ fs.rmdirSync("testArrayField5/anotherdir");
106
+ fs.rmdirSync("testArrayField5/andanotherdir");
107
+ fs.rmdirSync("testArrayField5");
108
+ done();
109
+ },
110
+ };
111
+ (0, index_1.jsonToFsStructure)(options);
112
+ });
113
+ it("should produce a nested deep complex directory structure", (done) => {
114
+ const options = {
115
+ jsonObject: {
116
+ testArrayField5: [
117
+ { somedir: {} },
118
+ { anotherdir: {} },
119
+ {
120
+ andanotherdir: {
121
+ interiorone: {
122
+ interiortwo: {
123
+ interiorthree: [{ interiorfour: {} }, { interiorfive: {} }],
124
+ },
125
+ },
126
+ },
127
+ },
128
+ ],
129
+ },
130
+ filePath: ".",
131
+ callback: () => {
132
+ assert.strictEqual(fs.statSync("testArrayField5/somedir").size, 4096);
133
+ assert.strictEqual(fs.statSync("testArrayField5/anotherdir").size, 4096);
134
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir").size, 4096);
135
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone").size, 4096);
136
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo")
137
+ .size, 4096);
138
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree").size, 4096);
139
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour").size, 4096);
140
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive").size, 4096);
141
+ fs.rmdirSync("testArrayField5/somedir");
142
+ fs.rmdirSync("testArrayField5/anotherdir");
143
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive");
144
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour");
145
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree");
146
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo");
147
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone");
148
+ fs.rmdirSync("testArrayField5/andanotherdir");
149
+ fs.rmdirSync("testArrayField5");
150
+ done();
151
+ },
152
+ };
153
+ (0, index_1.jsonToFsStructure)(options);
154
+ });
155
+ });
156
+ describe("jsonToFsWithLeafFunction", () => {
157
+ it("should produce a nested deep complex directory structure and execute the procedure on leaves", (done) => {
158
+ const endpoints = [];
159
+ const options = {
160
+ jsonObject: {
161
+ testArrayField5: [
162
+ { somedir: {} },
163
+ { anotherdir: {} },
164
+ {
165
+ andanotherdir: {
166
+ interiorone: {
167
+ interiortwo: {
168
+ interiorthree: [{ interiorfour: {} }, { interiorfive: {} }],
169
+ },
170
+ },
171
+ },
172
+ },
173
+ ],
174
+ },
175
+ filePath: ".",
176
+ leafProcedure: (filePath) => {
177
+ endpoints.push(filePath);
178
+ },
179
+ callback: () => {
180
+ assert.strictEqual(fs.statSync("testArrayField5/somedir").size, 4096);
181
+ assert.strictEqual(fs.statSync("testArrayField5/anotherdir").size, 4096);
182
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir").size, 4096);
183
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone").size, 4096);
184
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo")
185
+ .size, 4096);
186
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree").size, 4096);
187
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour").size, 4096);
188
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive").size, 4096);
189
+ fs.rmdirSync("testArrayField5/somedir");
190
+ fs.rmdirSync("testArrayField5/anotherdir");
191
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive");
192
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour");
193
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree");
194
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo");
195
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone");
196
+ fs.rmdirSync("testArrayField5/andanotherdir");
197
+ fs.rmdirSync("testArrayField5");
198
+ done();
199
+ },
200
+ };
201
+ (0, index_1.jsonToFsWithLeafFunction)(options);
202
+ expect(endpoints).toMatchSnapshot();
203
+ });
204
+ it("should produce a nested directory not past a stop word with a leaf function", (done) => {
205
+ const endpoints = [];
206
+ const options = {
207
+ jsonObject: {
208
+ testArrayField5: [
209
+ { somedir: {} },
210
+ { anotherdir: {} },
211
+ {
212
+ andanotherdir: {
213
+ interiorone: {
214
+ interiortwo: {
215
+ interiorthree: [{ interiorfour: {} }, { interiorfive: {} }],
216
+ },
217
+ },
218
+ },
219
+ },
220
+ ],
221
+ },
222
+ leafProcedure: (filePath) => {
223
+ endpoints.push(filePath);
224
+ },
225
+ filePath: ".",
226
+ callback: () => {
227
+ assert.strictEqual(fs.statSync("testArrayField5/somedir").size, 4096);
228
+ assert.strictEqual(fs.statSync("testArrayField5/anotherdir").size, 4096);
229
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir").size, 4096);
230
+ fs.rmdirSync("testArrayField5/somedir");
231
+ fs.rmdirSync("testArrayField5/anotherdir");
232
+ fs.rmdirSync("testArrayField5/andanotherdir");
233
+ fs.rmdirSync("testArrayField5");
234
+ done();
235
+ },
236
+ stopWord: "interiorone",
237
+ };
238
+ (0, index_1.jsonToFsWithLeafFunction)(options);
239
+ expect(endpoints).toMatchSnapshot();
240
+ });
241
+ });
242
+ describe("jsonToFsWithNonLeafFunction", () => {
243
+ it("should produce a nested deep complex directory structure and execute the procedure on non leaves", (done) => {
244
+ const endpoints = [];
245
+ const options = {
246
+ jsonObject: {
247
+ testArrayField5: [
248
+ { somedir: {} },
249
+ { anotherdir: {} },
250
+ {
251
+ andanotherdir: {
252
+ interiorone: {
253
+ interiortwo: {
254
+ interiorthree: [{ interiorfour: {} }, { interiorfive: {} }],
255
+ },
256
+ },
257
+ },
258
+ },
259
+ ],
260
+ },
261
+ nonLeafProcedure: (filePath) => {
262
+ endpoints.push(filePath);
263
+ },
264
+ filePath: ".",
265
+ context: {},
266
+ callback: () => {
267
+ assert.strictEqual(fs.statSync("testArrayField5/somedir").size, 4096);
268
+ assert.strictEqual(fs.statSync("testArrayField5/anotherdir").size, 4096);
269
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir").size, 4096);
270
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone").size, 4096);
271
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo")
272
+ .size, 4096);
273
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree").size, 4096);
274
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour").size, 4096);
275
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive").size, 4096);
276
+ fs.rmdirSync("testArrayField5/somedir");
277
+ fs.rmdirSync("testArrayField5/anotherdir");
278
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive");
279
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour");
280
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree");
281
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo");
282
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone");
283
+ fs.rmdirSync("testArrayField5/andanotherdir");
284
+ fs.rmdirSync("testArrayField5");
285
+ done();
286
+ },
287
+ };
288
+ (0, index_1.jsonToFsWithNonLeafFunction)(options);
289
+ expect(endpoints).toMatchSnapshot();
290
+ });
291
+ });
292
+ describe("jsonToFsWithFunction", () => {
293
+ it("should produce a nested deep complex directory structure and execute the procedure on everything", (done) => {
294
+ const endpoints = [];
295
+ const options = {
296
+ jsonObject: {
297
+ testArrayField5: [
298
+ { somedir: {} },
299
+ { anotherdir: {} },
300
+ {
301
+ andanotherdir: {
302
+ interiorone: {
303
+ interiortwo: {
304
+ interiorthree: [{ interiorfour: {} }, { interiorfive: {} }],
305
+ },
306
+ },
307
+ },
308
+ },
309
+ ],
310
+ },
311
+ procedure: (filePath) => {
312
+ endpoints.push(filePath);
313
+ },
314
+ filePath: ".",
315
+ context: {},
316
+ callback: () => {
317
+ assert.strictEqual(fs.statSync("testArrayField5/somedir").size, 4096);
318
+ assert.strictEqual(fs.statSync("testArrayField5/anotherdir").size, 4096);
319
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir").size, 4096);
320
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone").size, 4096);
321
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo")
322
+ .size, 4096);
323
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree").size, 4096);
324
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour").size, 4096);
325
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive").size, 4096);
326
+ fs.rmdirSync("testArrayField5/somedir");
327
+ fs.rmdirSync("testArrayField5/anotherdir");
328
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfive");
329
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour");
330
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree");
331
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo");
332
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone");
333
+ fs.rmdirSync("testArrayField5/andanotherdir");
334
+ fs.rmdirSync("testArrayField5");
335
+ done();
336
+ },
337
+ };
338
+ (0, index_1.jsonToFsWithFunction)(options);
339
+ expect(endpoints).toMatchSnapshot();
340
+ });
341
+ it("should not go below the stop word andanotherdir provided", (done) => {
342
+ const endpoints = [];
343
+ const options = {
344
+ jsonObject: {
345
+ testArrayField5: [
346
+ { somedir: {} },
347
+ { anotherdir: {} },
348
+ {
349
+ andanotherdir: {
350
+ interiorone: {
351
+ interiortwo: {
352
+ interiorthree: [{ interiorfour: {} }, { interiorfive: {} }],
353
+ },
354
+ },
355
+ },
356
+ },
357
+ ],
358
+ },
359
+ procedure: (filePath) => {
360
+ endpoints.push(filePath);
361
+ },
362
+ filePath: ".",
363
+ context: {},
364
+ callback: () => {
365
+ assert.strictEqual(fs.statSync("testArrayField5/somedir").size, 4096);
366
+ assert.strictEqual(fs.statSync("testArrayField5/anotherdir").size, 4096);
367
+ fs.rmdirSync("testArrayField5/somedir");
368
+ fs.rmdirSync("testArrayField5/anotherdir");
369
+ fs.rmdirSync("testArrayField5");
370
+ done();
371
+ },
372
+ stopWord: "andanotherdir",
373
+ };
374
+ (0, index_1.jsonToFsWithFunction)(options);
375
+ expect(endpoints).toMatchSnapshot();
376
+ });
377
+ it("should not do anything with ignored words like meta", (done) => {
378
+ const endpoints = [];
379
+ const options = {
380
+ jsonObject: {
381
+ testArrayField5: [
382
+ { somedir: { meta: {}, apple: {} } },
383
+ { anotherdir: {} },
384
+ {
385
+ meta: { somethingweird: {} },
386
+ andanotherdir: {
387
+ interiorone: {
388
+ interiortwo: {
389
+ apple: { dontdonothing: {} },
390
+ interiorthree: [{ interiorfour: {} }, { interiorfive: {} }],
391
+ },
392
+ },
393
+ },
394
+ },
395
+ ],
396
+ },
397
+ procedure: (filePath) => {
398
+ endpoints.push(filePath);
399
+ },
400
+ filePath: ".",
401
+ context: {},
402
+ callback: () => {
403
+ assert.strictEqual(fs.statSync("testArrayField5/somedir").size, 4096);
404
+ assert.strictEqual(fs.statSync("testArrayField5/anotherdir").size, 4096);
405
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir").size, 4096);
406
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone").size, 4096);
407
+ assert.strictEqual(fs.statSync("testArrayField5/andanotherdir/interiorone/interiortwo")
408
+ .size, 4096);
409
+ fs.rmdirSync("testArrayField5/somedir");
410
+ fs.rmdirSync("testArrayField5/anotherdir");
411
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone/interiortwo");
412
+ fs.rmdirSync("testArrayField5/andanotherdir/interiorone");
413
+ fs.rmdirSync("testArrayField5/andanotherdir");
414
+ fs.rmdirSync("testArrayField5");
415
+ done();
416
+ },
417
+ ignoredWords: ["meta", "apple"],
418
+ stopWord: "interiorthree",
419
+ };
420
+ (0, index_1.jsonToFsWithFunction)(options);
421
+ expect(endpoints).toMatchSnapshot();
422
+ });
423
+ });
424
+ });