funkophile 0.0.8 → 0.0.9

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.
@@ -1,2 +1,2 @@
1
- declare const _default: () => void;
1
+ declare const _default: (funkophileConfig: any) => void;
2
2
  export default _default;
package/dist/esm/index.js CHANGED
@@ -6,297 +6,300 @@ import fse from "fs-extra";
6
6
  import glob from "glob-promise";
7
7
  import path from "path";
8
8
  import Promise from "bluebird";
9
- export default () => {
10
- if (process.argv[2] &&
11
- (process.argv[3] === "watch" || process.argv[3] === "build")) {
12
- const configFile = path.resolve(process.argv[2]);
13
- const mode = process.argv[3];
14
- // console.log("configfile", configFile);
15
- import(configFile).then((funkophileConfigModule) => {
16
- const funkophileConfig = funkophileConfigModule.default;
17
- // console.log("funkophileConfig", (funkophileConfig));
18
- Promise.config({
19
- cancellation: true,
9
+ export default (funkophileConfig) => {
10
+ Promise.config({
11
+ cancellation: true,
12
+ });
13
+ const INITIALIZE = "INITIALIZE";
14
+ const UPSERT = "UPSERT";
15
+ const REMOVE = "REMOVE";
16
+ const previousState = {};
17
+ let outputPromise = Promise.resolve();
18
+ const logger = {
19
+ watchError: (p) => console.log("\u001b[7m ! \u001b[0m" + p),
20
+ watchReady: (p) => console.log("\u001b[7m\u001b[36m < \u001b[0m" + p),
21
+ watchAdd: (p) => console.log("\u001b[7m\u001b[34m + \u001b[0m./" + p),
22
+ watchChange: (p) => console.log("\u001b[7m\u001b[35m * \u001b[0m" + p),
23
+ watchUnlink: (p) => console.log("\u001b[7m\u001b[31m - \u001b[0m./" + p),
24
+ stateChange: () => console.log("\u001b[7m\u001b[31m --- Redux state changed --- \u001b[0m"),
25
+ cleaningEmptyfolder: (p) => console.log("\u001b[31m\u001b[7m XXX! \u001b[0m" + p),
26
+ readingFile: (p) => console.log("\u001b[31m <-- \u001b[0m" + p),
27
+ removedFile: (p) => console.log("\u001b[31m\u001b[7m ??? \u001b[0m./" + p),
28
+ writingString: (p) => console.log("\u001b[32m --> \u001b[0m" + p),
29
+ writingFunction: (p) => console.log("\u001b[33m ... \u001b[0m" + p),
30
+ writingPromise: (p) => console.log("\u001b[33m ... \u001b[0m" + p),
31
+ writingError: (p, message) => console.log("\u001b[31m !!! \u001b[0m" + p + " " + message),
32
+ waiting: () => console.log("\u001b[7m Funkophile is done for now but waiting on changes...\u001b[0m "),
33
+ done: () => console.log("\u001b[7m Funkophile is done!\u001b[0m "),
34
+ };
35
+ function cleanEmptyFoldersRecursively(folder) {
36
+ var isDir = fs.statSync(folder).isDirectory();
37
+ if (!isDir) {
38
+ return;
39
+ }
40
+ var files = fs.readdirSync(folder);
41
+ if (files.length > 0) {
42
+ files.forEach(function (file) {
43
+ var fullPath = path.join(folder, file);
20
44
  });
21
- const INITIALIZE = "INITIALIZE";
22
- const UPSERT = "UPSERT";
23
- const REMOVE = "REMOVE";
24
- const previousState = {};
25
- let outputPromise = Promise.resolve();
26
- const logger = {
27
- watchError: (p) => console.log("\u001b[7m ! \u001b[0m" + p),
28
- watchReady: (p) => console.log("\u001b[7m\u001b[36m < \u001b[0m" + p),
29
- watchAdd: (p) => console.log("\u001b[7m\u001b[34m + \u001b[0m./" + p),
30
- watchChange: (p) => console.log("\u001b[7m\u001b[35m * \u001b[0m" + p),
31
- watchUnlink: (p) => console.log("\u001b[7m\u001b[31m - \u001b[0m./" + p),
32
- stateChange: () => console.log("\u001b[7m\u001b[31m --- Redux state changed --- \u001b[0m"),
33
- cleaningEmptyfolder: (p) => console.log("\u001b[31m\u001b[7m XXX! \u001b[0m" + p),
34
- readingFile: (p) => console.log("\u001b[31m <-- \u001b[0m" + p),
35
- removedFile: (p) => console.log("\u001b[31m\u001b[7m ??? \u001b[0m./" + p),
36
- writingString: (p) => console.log("\u001b[32m --> \u001b[0m" + p),
37
- writingFunction: (p) => console.log("\u001b[33m ... \u001b[0m" + p),
38
- writingPromise: (p) => console.log("\u001b[33m ... \u001b[0m" + p),
39
- writingError: (p, message) => console.log("\u001b[31m !!! \u001b[0m" + p + " " + message),
40
- waiting: () => console.log("\u001b[7m Funkophile is done for now but waiting on changes...\u001b[0m "),
41
- done: () => console.log("\u001b[7m Funkophile is done!\u001b[0m "),
42
- };
43
- function cleanEmptyFoldersRecursively(folder) {
44
- var isDir = fs.statSync(folder).isDirectory();
45
- if (!isDir) {
46
- return;
47
- }
48
- var files = fs.readdirSync(folder);
49
- if (files.length > 0) {
50
- files.forEach(function (file) {
51
- var fullPath = path.join(folder, file);
52
- });
53
- // re-evaluate files; after deleting subfolder
54
- // we may have parent folder empty now
55
- files = fs.readdirSync(folder);
56
- }
57
- if (files.length == 0) {
58
- logger.cleaningEmptyfolder(folder);
59
- fs.rmdirSync(folder);
60
- return;
61
- }
45
+ // re-evaluate files; after deleting subfolder
46
+ // we may have parent folder empty now
47
+ files = fs.readdirSync(folder);
48
+ }
49
+ if (files.length == 0) {
50
+ logger.cleaningEmptyfolder(folder);
51
+ fs.rmdirSync(folder);
52
+ return;
53
+ }
54
+ }
55
+ const dispatchUpsert = (store, key, file, encodings) => {
56
+ const fileType = file.split(".").slice(-2, -1)[0];
57
+ let encoding = Object.keys(encodings).find((e) => encodings[e].includes(fileType));
58
+ if (!fileType || !encoding) {
59
+ console.log(`Unknown file type for `, file, `Defaulting to utf8`);
60
+ encoding = "utf8";
61
+ }
62
+ // console.log("dispatchUpsert", encoding, file)
63
+ logger.readingFile(file);
64
+ store.dispatch({
65
+ type: UPSERT,
66
+ payload: {
67
+ key: key,
68
+ src: file,
69
+ contents: fse.readFileSync(file, encoding),
70
+ },
71
+ });
72
+ };
73
+ function omit(key, obj) {
74
+ const { [key]: omitted, ...rest } = obj;
75
+ return rest;
76
+ }
77
+ const store = createStore((state = {
78
+ initialLoad: true,
79
+ ...funkophileConfig.initialState,
80
+ timestamp: Date.now(),
81
+ }, action) => {
82
+ // console.log("\u001b[7m\u001b[35m ||| Redux recieved action \u001b[0m", action.type)
83
+ if (!action.type.includes("@@redux")) {
84
+ if (action.type === INITIALIZE) {
85
+ return {
86
+ ...state,
87
+ initialLoad: false,
88
+ timestamp: Date.now(),
89
+ };
62
90
  }
63
- const dispatchUpsert = (store, key, file, encodings) => {
64
- const fileType = file.split(".").slice(-2, -1)[0];
65
- let encoding = Object.keys(encodings).find((e) => encodings[e].includes(fileType));
66
- if (!fileType || !encoding) {
67
- console.log(`Unknown file type for `, file, `Defaulting to utf8`);
68
- encoding = "utf8";
69
- }
70
- // console.log("dispatchUpsert", encoding, file)
71
- logger.readingFile(file);
72
- store.dispatch({
73
- type: UPSERT,
74
- payload: {
75
- key: key,
76
- src: file,
77
- contents: fse.readFileSync(file, encoding),
91
+ else if (action.type === UPSERT) {
92
+ return {
93
+ ...state,
94
+ [action["payload"].key]: {
95
+ ...state[action.payload.key],
96
+ ...{
97
+ [action["payload"].src]: action["payload"].contents,
98
+ },
78
99
  },
79
- });
80
- };
81
- function omit(key, obj) {
82
- const { [key]: omitted, ...rest } = obj;
83
- return rest;
100
+ timestamp: Date.now(),
101
+ };
84
102
  }
85
- const store = createStore((state = {
86
- initialLoad: true,
87
- ...funkophileConfig.initialState,
88
- timestamp: Date.now(),
89
- }, action) => {
90
- // console.log("\u001b[7m\u001b[35m ||| Redux recieved action \u001b[0m", action.type)
91
- if (!action.type.includes("@@redux")) {
92
- if (action.type === INITIALIZE) {
93
- return {
94
- ...state,
95
- initialLoad: false,
96
- timestamp: Date.now(),
97
- };
98
- }
99
- else if (action.type === UPSERT) {
100
- return {
101
- ...state,
102
- [action["payload"].key]: {
103
- ...state[action.payload.key],
104
- ...{
105
- [action["payload"].src]: action["payload"].contents,
106
- },
107
- },
108
- timestamp: Date.now(),
109
- };
110
- }
111
- else if (action.type === REMOVE) {
112
- return {
113
- ...state,
114
- [action["payload"].key]: omit(action["payload"].file, state[action["payload"].key]),
115
- timestamp: Date.now(),
116
- };
117
- }
118
- else {
119
- console.error("Redux was asked to handle an unknown action type: " +
120
- action.type);
121
- process.exit(-1);
122
- }
123
- // return state
124
- }
125
- });
126
- const finalSelector = funkophileConfig.outputs(Object.keys(funkophileConfig.inputs).reduce((mm, inputKey) => {
103
+ else if (action.type === REMOVE) {
127
104
  return {
128
- ...mm,
129
- [inputKey]: createSelector([(x) => x], (root) => root[inputKey]),
105
+ ...state,
106
+ [action["payload"].key]: omit(action["payload"].file, state[action["payload"].key]),
107
+ timestamp: Date.now(),
130
108
  };
131
- }, {}));
132
- // Wait for all the file watchers to check in
133
- Promise.all(Object.keys(funkophileConfig.inputs)
134
- .map((inputRuleKey) => {
135
- const p = path.resolve(`./${funkophileConfig.options.inFolder}/${funkophileConfig.inputs[inputRuleKey] || ""}`);
109
+ }
110
+ else {
111
+ console.error("Redux was asked to handle an unknown action type: " +
112
+ action.type);
113
+ process.exit(-1);
114
+ }
115
+ // return state
116
+ }
117
+ });
118
+ const finalSelector = funkophileConfig.outputs(Object.keys(funkophileConfig.inputs).reduce((mm, inputKey) => {
119
+ return {
120
+ ...mm,
121
+ [inputKey]: createSelector([(x) => x], (root) => root[inputKey]),
122
+ };
123
+ }, {}));
124
+ // Wait for all the file watchers to check in
125
+ Promise.all(Object.keys(funkophileConfig.inputs)
126
+ .map((inputRuleKey) => {
127
+ const p = path.resolve(`./${funkophileConfig.options.inFolder}/${funkophileConfig.inputs[inputRuleKey] || ""}`);
128
+ return new Promise((fulfill, reject) => {
129
+ if (mode === "build") {
130
+ glob(p, {})
131
+ .then((files) => {
132
+ files.forEach((file) => {
133
+ dispatchUpsert(store, inputRuleKey, file, funkophileConfig.encodings);
134
+ });
135
+ })
136
+ .then(() => {
137
+ fulfill();
138
+ });
139
+ }
140
+ else if (mode === "watch") {
141
+ chokidar
142
+ .watch(p, {})
143
+ .on("error", (error) => {
144
+ logger.watchError(p);
145
+ })
146
+ .on("ready", () => {
147
+ logger.watchReady(p);
148
+ fulfill();
149
+ })
150
+ .on("add", (p) => {
151
+ logger.watchAdd(p);
152
+ dispatchUpsert(store, inputRuleKey, "./" + p, funkophileConfig.encodings);
153
+ })
154
+ .on("change", (p) => {
155
+ logger.watchChange(p);
156
+ dispatchUpsert(store, inputRuleKey, "./" + p, funkophileConfig.encodings);
157
+ })
158
+ .on("unlink", (p) => {
159
+ logger.watchUnlink(p);
160
+ store.dispatch({
161
+ type: REMOVE,
162
+ payload: {
163
+ key: inputRuleKey,
164
+ file: "./" + p,
165
+ },
166
+ });
167
+ })
168
+ .on("unlinkDir", (p) => {
169
+ logger.watchUnlink(p);
170
+ });
171
+ // .on('raw', (event, p, details) => { // internal
172
+ // log('Raw event info:', event, p, details);
173
+ // })
174
+ }
175
+ else {
176
+ console.error(`The 3rd argument should be 'watch' or 'build', not "${mode}"`);
177
+ process.exit(-1);
178
+ }
179
+ });
180
+ })).then(function () {
181
+ // listen for changes to the store
182
+ store.subscribe(() => {
183
+ const s = store.getState();
184
+ logger.stateChange();
185
+ const outputs = finalSelector(s);
186
+ if (outputPromise.isPending()) {
187
+ console.log("cancelling previous write!");
188
+ outputPromise.cancel();
189
+ }
190
+ outputPromise = Promise.all(Array.from(new Set(Object.keys(previousState).concat(Object.keys(outputs)))).map((key) => {
136
191
  return new Promise((fulfill, reject) => {
137
- if (mode === "build") {
138
- glob(p, {})
139
- .then((files) => {
140
- files.forEach((file) => {
141
- dispatchUpsert(store, inputRuleKey, file, funkophileConfig.encodings);
142
- });
143
- })
144
- .then(() => {
145
- fulfill();
146
- });
147
- }
148
- else if (mode === "watch") {
149
- chokidar
150
- .watch(p, {})
151
- .on("error", (error) => {
152
- logger.watchError(p);
153
- })
154
- .on("ready", () => {
155
- logger.watchReady(p);
156
- fulfill();
157
- })
158
- .on("add", (p) => {
159
- logger.watchAdd(p);
160
- dispatchUpsert(store, inputRuleKey, "./" + p, funkophileConfig.encodings);
161
- })
162
- .on("change", (p) => {
163
- logger.watchChange(p);
164
- dispatchUpsert(store, inputRuleKey, "./" + p, funkophileConfig.encodings);
165
- })
166
- .on("unlink", (p) => {
167
- logger.watchUnlink(p);
168
- store.dispatch({
169
- type: REMOVE,
170
- payload: {
171
- key: inputRuleKey,
172
- file: "./" + p,
173
- },
174
- });
175
- })
176
- .on("unlinkDir", (p) => {
177
- logger.watchUnlink(p);
178
- });
179
- // .on('raw', (event, p, details) => { // internal
180
- // log('Raw event info:', event, p, details);
181
- // })
192
+ if (!outputs[key]) {
193
+ const file = funkophileConfig.options.outFolder + "/" + key;
194
+ logger.removedFile(file);
195
+ try {
196
+ fse.unlinkSync("./" + file);
197
+ cleanEmptyFoldersRecursively("./" + file.substring(0, file.lastIndexOf("/")));
198
+ }
199
+ catch (ex) {
200
+ // console.error('inner', ex.message);
201
+ // throw ex;
202
+ }
203
+ finally {
204
+ // console.log('finally');
205
+ return;
206
+ }
207
+ // delete previousState[key]
208
+ // fulfill()
182
209
  }
183
210
  else {
184
- console.error(`The 3rd argument should be 'watch' or 'build', not "${mode}"`);
185
- process.exit(-1);
186
- }
187
- });
188
- })).then(function () {
189
- // listen for changes to the store
190
- store.subscribe(() => {
191
- const s = store.getState();
192
- logger.stateChange();
193
- const outputs = finalSelector(s);
194
- if (outputPromise.isPending()) {
195
- console.log("cancelling previous write!");
196
- outputPromise.cancel();
197
- }
198
- outputPromise = Promise.all(Array.from(new Set(Object.keys(previousState).concat(Object.keys(outputs)))).map((key) => {
199
- return new Promise((fulfill, reject) => {
200
- if (!outputs[key]) {
201
- const file = funkophileConfig.options.outFolder + "/" + key;
202
- logger.removedFile(file);
203
- try {
204
- fse.unlinkSync("./" + file);
205
- cleanEmptyFoldersRecursively("./" + file.substring(0, file.lastIndexOf("/")));
206
- }
207
- catch (ex) {
208
- // console.error('inner', ex.message);
209
- // throw ex;
210
- }
211
- finally {
212
- // console.log('finally');
213
- return;
214
- }
215
- // delete previousState[key]
216
- // fulfill()
211
+ if (outputs[key] !== previousState[key]) {
212
+ previousState[key] = outputs[key];
213
+ const relativeFilePath = "./" + funkophileConfig.options.outFolder + "/" + key;
214
+ const contents = outputs[key];
215
+ if (typeof contents === "function") {
216
+ logger.writingFunction(relativeFilePath);
217
+ contents((err, res) => {
218
+ fse.outputFile(relativeFilePath, res, fulfill);
219
+ logger.writingString(relativeFilePath);
220
+ });
217
221
  }
218
- else {
219
- if (outputs[key] !== previousState[key]) {
220
- previousState[key] = outputs[key];
221
- const relativeFilePath = "./" + funkophileConfig.options.outFolder + "/" + key;
222
- const contents = outputs[key];
223
- if (typeof contents === "function") {
224
- logger.writingFunction(relativeFilePath);
225
- contents((err, res) => {
226
- fse.outputFile(relativeFilePath, res, fulfill);
227
- logger.writingString(relativeFilePath);
228
- });
229
- }
230
- else if (typeof contents === "string") {
231
- fse.outputFile(relativeFilePath, contents, fulfill);
232
- logger.writingString(relativeFilePath);
233
- }
234
- else if (Buffer.isBuffer(contents)) {
235
- fse.outputFile(relativeFilePath, contents, fulfill);
236
- logger.writingString(relativeFilePath);
237
- }
238
- else if (Array.isArray(contents)) {
239
- fse.outputFile(relativeFilePath, JSON.stringify(contents), fulfill);
240
- logger.writingString(relativeFilePath);
241
- }
242
- else if (typeof contents.then === "function") {
243
- logger.writingPromise(relativeFilePath);
244
- Promise.resolve(contents).then(function (value) {
245
- if (value instanceof Error) {
246
- logger.writingError(relativeFilePath, value.message);
247
- }
248
- else {
249
- fse.outputFile(relativeFilePath, value, fulfill);
250
- logger.writingString(relativeFilePath);
251
- }
252
- }, function (value) {
253
- // not called
254
- });
255
- }
256
- else if (typeof contents === "object") {
257
- fse.outputFile(relativeFilePath, JSON.stringify(contents), fulfill);
258
- logger.writingString(relativeFilePath);
222
+ else if (typeof contents === "string") {
223
+ fse.outputFile(relativeFilePath, contents, fulfill);
224
+ logger.writingString(relativeFilePath);
225
+ }
226
+ else if (Buffer.isBuffer(contents)) {
227
+ fse.outputFile(relativeFilePath, contents, fulfill);
228
+ logger.writingString(relativeFilePath);
229
+ }
230
+ else if (Array.isArray(contents)) {
231
+ fse.outputFile(relativeFilePath, JSON.stringify(contents), fulfill);
232
+ logger.writingString(relativeFilePath);
233
+ }
234
+ else if (typeof contents.then === "function") {
235
+ logger.writingPromise(relativeFilePath);
236
+ Promise.resolve(contents).then(function (value) {
237
+ if (value instanceof Error) {
238
+ logger.writingError(relativeFilePath, value.message);
259
239
  }
260
240
  else {
261
- console.log(`I don't recognize what this is but I will try to write it to a file: ` +
262
- relativeFilePath, typeof contents, contents);
263
- fse.outputFile(relativeFilePath, contents, fulfill);
241
+ fse.outputFile(relativeFilePath, value, fulfill);
264
242
  logger.writingString(relativeFilePath);
265
243
  }
266
- }
267
- else {
268
- fulfill();
269
- }
244
+ }, function (value) {
245
+ // not called
246
+ });
247
+ }
248
+ else if (typeof contents === "object") {
249
+ fse.outputFile(relativeFilePath, JSON.stringify(contents), fulfill);
250
+ logger.writingString(relativeFilePath);
251
+ }
252
+ else {
253
+ console.log(`I don't recognize what this is but I will try to write it to a file: ` +
254
+ relativeFilePath, typeof contents, contents);
255
+ fse.outputFile(relativeFilePath, contents, fulfill);
256
+ logger.writingString(relativeFilePath);
270
257
  }
271
- });
272
- })).then(() => {
273
- cleanEmptyFoldersRecursively(funkophileConfig.options.outFolder);
274
- if (mode === "build") {
275
- logger.done();
276
- }
277
- else if (mode === "watch") {
278
- logger.waiting();
279
258
  }
280
259
  else {
281
- console.error(`The 3rd argument should be 'watch' or 'build', not "${mode}"`);
282
- process.exit(-1);
260
+ fulfill();
283
261
  }
284
- });
285
- });
286
- // lastly, turn the store `on`.
287
- // This is to prevent unecessary recomputations when initialy adding files to redux
288
- store.dispatch({
289
- type: INITIALIZE,
290
- payload: true,
262
+ }
291
263
  });
264
+ })).then(() => {
265
+ cleanEmptyFoldersRecursively(funkophileConfig.options.outFolder);
266
+ if (mode === "build") {
267
+ logger.done();
268
+ }
269
+ else if (mode === "watch") {
270
+ logger.waiting();
271
+ }
272
+ else {
273
+ console.error(`The 3rd argument should be 'watch' or 'build', not "${mode}"`);
274
+ process.exit(-1);
275
+ }
292
276
  });
293
277
  });
294
- }
295
- else {
296
- console.error("command line arguments do not make sense");
297
- console.error("first argument should be a funkophile config file");
298
- console.error("second argument should be a 'build' or 'watch'");
299
- console.error("You passed", process.argv);
300
- process.exit(-1);
301
- }
278
+ // lastly, turn the store `on`.
279
+ // This is to prevent unecessary recomputations when initialy adding files to redux
280
+ store.dispatch({
281
+ type: INITIALIZE,
282
+ payload: true,
283
+ });
284
+ });
302
285
  };
286
+ // export default () => {
287
+ // if (
288
+ // process.argv[2] &&
289
+ // (process.argv[3] === "watch" || process.argv[3] === "build")
290
+ // ) {
291
+ // const configFile = path.resolve(process.argv[2]);
292
+ // const mode = process.argv[3];
293
+ // // console.log("configfile", configFile);
294
+ // import(configFile).then((funkophileConfigModule) => {
295
+ // const funkophileConfig = funkophileConfigModule.default;
296
+ // // console.log("funkophileConfig", (funkophileConfig));
297
+ // });
298
+ // } else {
299
+ // console.error("command line arguments do not make sense");
300
+ // console.error("first argument should be a funkophile config file");
301
+ // console.error("second argument should be a 'build' or 'watch'");
302
+ // console.error("You passed", process.argv);
303
+ // process.exit(-1);
304
+ // }
305
+ // };