@notifiles/sdk 1.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.
Files changed (132) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +26 -0
  3. package/dist/index.modern.cjs +856 -0
  4. package/dist/index.modern.cjs.map +1 -0
  5. package/dist/index.modern.modern.js +690 -0
  6. package/dist/index.modern.modern.js.map +1 -0
  7. package/package.json +152 -0
  8. package/src/cloud/add/index.js +38 -0
  9. package/src/cloud/add/index.test.js +38 -0
  10. package/src/cloud/add/lib/validate.js +16 -0
  11. package/src/cloud/index.js +3 -0
  12. package/src/cloud/list/index.js +18 -0
  13. package/src/cloud/list/index.test.js +13 -0
  14. package/src/email/build copy/lib/perform/email.js +52 -0
  15. package/src/index.js +3 -0
  16. package/src/lib/adaptSettings.js +12 -0
  17. package/src/lib/cloudLibrary/bunny/index.js +2 -0
  18. package/src/lib/cloudLibrary/bunny/isValid.js +32 -0
  19. package/src/lib/cloudLibrary/bunny/upload.js +35 -0
  20. package/src/lib/cloudLibrary/index.js +20 -0
  21. package/src/lib/cloudLibrary/minio/index.js +4 -0
  22. package/src/lib/cloudLibrary/minio/isValid.js +33 -0
  23. package/src/lib/cloudLibrary/minio/remove.js +58 -0
  24. package/src/lib/cloudLibrary/minio/upload.js +61 -0
  25. package/src/lib/cloudLibrary/minio/uploadFolder.js +26 -0
  26. package/src/lib/entries/getAtPath.js +105 -0
  27. package/src/lib/entries/isPathEntry.js +29 -0
  28. package/src/lib/entries.js +144 -0
  29. package/src/lib/fs/checkFileExists.js +6 -0
  30. package/src/lib/fs/directoryFilesRecursive.js +83 -0
  31. package/src/lib/fs/ensureDirectoryExists.js +10 -0
  32. package/src/lib/fs/getFileCallerURL.js +12 -0
  33. package/src/lib/fs/importFileAsText.js +15 -0
  34. package/src/lib/fs/importJSONAsync.js +16 -0
  35. package/src/lib/fs/importYAMLAsync.js +14 -0
  36. package/src/lib/remark/hastToHtml.js +6 -0
  37. package/src/lib/remark/htmlToHast.js +6 -0
  38. package/src/lib/remark/htmlToMarkdown copy.js +12 -0
  39. package/src/lib/remark/htmlToMarkdown.js +23 -0
  40. package/src/lib/remark/htmlToMdast.js +13 -0
  41. package/src/lib/remark/htmlToMdast_.js +21 -0
  42. package/src/lib/remark/htmlToMdast_3.js +21 -0
  43. package/src/lib/remark/markdownToHTML.js +22 -0
  44. package/src/lib/remark/markdownToLexer.js +19 -0
  45. package/src/lib/remark/mdastToHTML.js +8 -0
  46. package/src/lib/remark/mdastToMarkdown.js +24 -0
  47. package/src/lib/remark/mdastToString.js +12 -0
  48. package/src/project/index.js +4 -0
  49. package/src/project/isProject/index.js +26 -0
  50. package/src/project/isProject/index.test.js +13 -0
  51. package/src/project/isProject/sync.js +18 -0
  52. package/src/project/lib/activity/createFileIfNeeded.js +33 -0
  53. package/src/project/lib/activity/defaultContent.json +5 -0
  54. package/src/project/lib/activity/updateToFile.js +32 -0
  55. package/src/project/lib/manifest/createFileIfNeeded.js +44 -0
  56. package/src/project/lib/manifest/read.js +21 -0
  57. package/src/project/lib/manifest/update.js +36 -0
  58. package/src/project/lib/manifest/updateToFile.js +22 -0
  59. package/src/project/new/index.js +2 -0
  60. package/src/project/new/plain.js +49 -0
  61. package/src/project/new/plain.test.js +38 -0
  62. package/src/template/build/entry.js +37 -0
  63. package/src/template/build/index.js +2 -0
  64. package/src/template/build/lib/email/index.js +100 -0
  65. package/src/template/build/lib/email/translate.js +16 -0
  66. package/src/template/build/lib/textmessage/index.js +44 -0
  67. package/src/template/build/lib/textmessage/translate.js +25 -0
  68. package/src/template/build/path.js +18 -0
  69. package/src/template/build/path.test.js +29 -0
  70. package/src/template/cloudify/entry.js +20 -0
  71. package/src/template/cloudify/index.js +2 -0
  72. package/src/template/cloudify/lib/email/attachments/addAttachment.js +24 -0
  73. package/src/template/cloudify/lib/email/attachments/index.js +137 -0
  74. package/src/template/cloudify/lib/email/attachments/treatLocalFile.js +13 -0
  75. package/src/template/cloudify/lib/email/attachments/treatRemoteFile.js +26 -0
  76. package/src/template/cloudify/lib/email/image/index.js +90 -0
  77. package/src/template/cloudify/lib/email/image/treatLocalFile.js +13 -0
  78. package/src/template/cloudify/lib/email/image/treatRemoteFile.js +26 -0
  79. package/src/template/cloudify/lib/email/index.js +77 -0
  80. package/src/template/cloudify/lib/remove.js +14 -0
  81. package/src/template/cloudify/lib/textMessage.js +9 -0
  82. package/src/template/cloudify/lib/upload.js +14 -0
  83. package/src/template/cloudify/path.js +20 -0
  84. package/src/template/cloudify/path.test.js +24 -0
  85. package/src/template/consolidate/entry.js +18 -0
  86. package/src/template/consolidate/index.js +2 -0
  87. package/src/template/consolidate/lib/email.js +63 -0
  88. package/src/template/consolidate/lib/image/index.js +35 -0
  89. package/src/template/consolidate/lib/image/treatLocalFile.js +14 -0
  90. package/src/template/consolidate/lib/image/treatRemoteFile.js +25 -0
  91. package/src/template/consolidate/lib/textMessage.js +5 -0
  92. package/src/template/consolidate/path.js +18 -0
  93. package/src/template/consolidate/path.test.js +14 -0
  94. package/src/template/format/entry.js +57 -0
  95. package/src/template/format/index.js +2 -0
  96. package/src/template/format/path.js +18 -0
  97. package/src/template/format/path.test.js +29 -0
  98. package/src/template/index.js +8 -0
  99. package/src/template/lib/activity/createFileIfNeeded.js +33 -0
  100. package/src/template/lib/activity/defaultContent.json +4 -0
  101. package/src/template/lib/activity/get.js +20 -0
  102. package/src/template/lib/activity/updateToFile.js +33 -0
  103. package/src/template/lib/manifest/createFileIfNeeded.js +41 -0
  104. package/src/template/lib/manifest/read.js +22 -0
  105. package/src/template/lib/manifest/update.js +18 -0
  106. package/src/template/lib/manifest/updateToFile.js +22 -0
  107. package/src/template/new/index.js +66 -0
  108. package/src/template/new/index.test.js +19 -0
  109. package/src/template/optimize/entry.js +18 -0
  110. package/src/template/optimize/index.js +2 -0
  111. package/src/template/optimize/lib/email.js +58 -0
  112. package/src/template/optimize/lib/image/index.js +40 -0
  113. package/src/template/optimize/lib/textMessage.js +8 -0
  114. package/src/template/optimize/path.js +18 -0
  115. package/src/template/optimize/path.test.js +16 -0
  116. package/src/template/sync/entry.js +18 -0
  117. package/src/template/sync/index.js +2 -0
  118. package/src/template/sync/lib/email.js +86 -0
  119. package/src/template/sync/lib/textMessage.js +80 -0
  120. package/src/template/sync/lib/upload.js +14 -0
  121. package/src/template/sync/path.js +18 -0
  122. package/src/template/sync/path.test.js +29 -0
  123. package/src/template/update/index.js +1 -0
  124. package/src/template/update/path.js +41 -0
  125. package/src/template/update/path.test.js +46 -0
  126. package/src/template/version/entry.js +10 -0
  127. package/src/template/version/index.js +2 -0
  128. package/src/template/version/lib/perform.js +74 -0
  129. package/src/template/version/lib/reset.js +16 -0
  130. package/src/template/version/lib/updatePost.js +16 -0
  131. package/src/template/version/path.js +20 -0
  132. package/src/template/version/path.test.js +16 -0
@@ -0,0 +1,690 @@
1
+ import _yargs from 'yargs';
2
+ import { hideBin } from 'yargs/helpers';
3
+ import fs$1 from 'fs';
4
+ import Bluebird from 'bluebird';
5
+ import chalk from 'chalk';
6
+ import inquirer from 'inquirer';
7
+ import inquirerPromptAutocomplete from 'inquirer-autocomplete-prompt';
8
+ import inquirerFileTreeSelection from 'inquirer-file-tree-selection-prompt';
9
+ import inquirerParseJsonFile from 'inquirer-parse-json-file';
10
+ import ejs$1 from 'ejs';
11
+ import jetpack from 'fs-jetpack';
12
+ import _path from 'path';
13
+ import cp from 'child_process';
14
+ import _ from 'underscore';
15
+ import lodash from 'lodash';
16
+ import parseArgv from 'tiny-parse-argv';
17
+
18
+ function _extends() {
19
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
20
+ for (var i = 1; i < arguments.length; i++) {
21
+ var source = arguments[i];
22
+ for (var key in source) {
23
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
24
+ target[key] = source[key];
25
+ }
26
+ }
27
+ }
28
+ return target;
29
+ };
30
+ return _extends.apply(this, arguments);
31
+ }
32
+
33
+ var nonEmpty = (async ({
34
+ input,
35
+ params
36
+ }) => {
37
+ return input && input.length > 0;
38
+ });
39
+
40
+ var getValidators = (a => {
41
+ switch (a.type) {
42
+ default:
43
+ case 'nonEmpty':
44
+ {
45
+ return nonEmpty;
46
+ }
47
+ }
48
+ });
49
+
50
+ var ask = (async props => {
51
+ const {
52
+ payload,
53
+ question,
54
+ generator,
55
+ promptModule,
56
+ promptType
57
+ } = props;
58
+ let {
59
+ name,
60
+ message,
61
+ defaultValue,
62
+ validators = [{
63
+ type: 'nonEmpty'
64
+ }]
65
+ } = question;
66
+ if (!name) {
67
+ return;
68
+ }
69
+ let value = payload[name];
70
+ if (!value && question.alias) {
71
+ value = payload[question.alias];
72
+ }
73
+ const valueIsDefined = !(value === null || value === undefined);
74
+ if (valueIsDefined) {
75
+ generator.print.log(`${chalk.green('✓')} ${chalk.bold(message ? message : name)} ${chalk.italic(value)}`);
76
+ return;
77
+ }
78
+ const isQuick = payload['quick'] || payload['q'];
79
+ if (isQuick && valueIsDefined) {
80
+ generator.print.log(`${chalk.green('✓')} ${chalk.bold(message ? message : name)} ${chalk.italic(value)}`);
81
+ return;
82
+ }
83
+ if (isQuick && !(defaultValue === null || defaultValue === undefined)) {
84
+ payload[name] = defaultValue;
85
+ generator.print.log(`${chalk.green('✓')} ${chalk.bold(message ? message : name)} ${chalk.italic(payload[name])}`);
86
+ return;
87
+ }
88
+ payload[name] = (await promptModule.prompt(_extends({}, props.question, {
89
+ type: promptType,
90
+ name,
91
+ message,
92
+ default: value ? value : defaultValue,
93
+ validate: async input => {
94
+ let isValid = true;
95
+ await Bluebird.Promise.mapSeries(validators, async validator => {
96
+ const validate = getValidators(validator);
97
+ if (validate) {
98
+ const _i = await validate(_extends({
99
+ input
100
+ }, validator));
101
+ if (!_i) {
102
+ isValid = false;
103
+ }
104
+ return;
105
+ }
106
+ if (validate.regex) {
107
+ const f = new RegExp(validate.regex, 'g');
108
+ // isValid = validator.regex.test(input)
109
+ isValid = f.test(input);
110
+ }
111
+ });
112
+ if (props.question.validate) {
113
+ isValid = props.question.validate(input);
114
+ }
115
+ return isValid;
116
+ }
117
+ })))[name];
118
+ return payload[name];
119
+ });
120
+
121
+ var _promptModule = (id => {
122
+ switch (id) {
123
+ default:
124
+ case 'inquirer':
125
+ return inquirer;
126
+ }
127
+ });
128
+
129
+ var prompt = (({
130
+ generator
131
+ }) => {
132
+ const prompt = {
133
+ ask: async value => {
134
+ const questions = Array.isArray(value) ? value : [value];
135
+ const result = {};
136
+ await Bluebird.Promise.mapSeries(questions, async question => {
137
+ let fullQuestion = _extends({}, question);
138
+ const items = generator.options.filter(a => a.name === question.name);
139
+ if (items && items.length) {
140
+ fullQuestion = _extends({}, items[0], fullQuestion);
141
+ }
142
+ const {
143
+ type: promptType = 'input',
144
+ module: _f
145
+ } = fullQuestion.prompt ? fullQuestion.prompt : {};
146
+ let promptModule = _f ? _f : _promptModule();
147
+ if (typeof promptModule === 'string') {
148
+ promptModule = _promptModule(_f);
149
+ }
150
+ const v = await ask({
151
+ question: fullQuestion,
152
+ payload: generator.payload,
153
+ generator,
154
+ promptModule,
155
+ promptType
156
+ });
157
+ result[question.name] = v;
158
+ return v;
159
+ });
160
+ return result;
161
+ },
162
+ confirm: async () => {},
163
+ registerPrompt: (promptModule, key, module) => {
164
+ promptModule.registerPrompt(key, module);
165
+ }
166
+ };
167
+ const promptModule = _promptModule();
168
+ prompt.registerPrompt(promptModule, 'autocomplete', inquirerPromptAutocomplete);
169
+ prompt.registerPrompt(promptModule, 'file-tree-selection', inquirerFileTreeSelection);
170
+ prompt.registerPrompt(promptModule, 'json-file', inquirerParseJsonFile);
171
+ return prompt;
172
+ });
173
+
174
+ var template = (({
175
+ generator
176
+ }) => {
177
+ return {
178
+ render: (text, data) => {
179
+ return ejs$1.render(text, data);
180
+ }
181
+ };
182
+ });
183
+
184
+ var getFileCallerURL = (() => {
185
+ var _error$stack;
186
+ const error = new Error();
187
+ const stack = (_error$stack = error.stack) == null ? void 0 : _error$stack.split('\n');
188
+ const data = stack[3];
189
+ const filePathPattern = new RegExp(`(file:[/]{2}.+[^:0-9]):{1}[0-9]+:{1}[0-9]+`);
190
+ const result = filePathPattern.exec(data);
191
+ let filePath = '';
192
+ if (result && result.length > 1) {
193
+ filePath = result[1];
194
+ }
195
+ return filePath;
196
+ });
197
+
198
+ var fs = (({
199
+ generator
200
+ }) => {
201
+ return _extends({}, jetpack, {
202
+ copy: async ({
203
+ source,
204
+ destination,
205
+ useRelativeCall: _useRelativeCall = false
206
+ }) => {
207
+ return fs$1.promises.cp(source, destination);
208
+ },
209
+ copyTpl: async (source, destination, data) => {
210
+ const entry = await fs$1.promises.readFile(source);
211
+ const result = ejs.render(entry, data);
212
+ await fs$1.promises.writeFile(destination, result);
213
+ },
214
+ copyFraction: async ({
215
+ source,
216
+ destination,
217
+ data,
218
+ useRelativeCall: _useRelativeCall2 = true
219
+ }) => {
220
+ let _source = source;
221
+ if (_useRelativeCall2) {
222
+ let sou = getFileCallerURL();
223
+ sou = _path.dirname(sou);
224
+ sou = sou.replace('file://', '');
225
+ _source = `${sou}/template/${_source}`;
226
+ }
227
+ const re = await jetpack.copyAsync(_source, destination, {
228
+ overwrite: true
229
+ });
230
+ return re;
231
+ // return fs.promises.copyFile(_source, destination)
232
+ }
233
+ });
234
+ });
235
+
236
+ var ui = (({
237
+ generator
238
+ }) => {
239
+ return {
240
+ drawSectionHeader: ({
241
+ title,
242
+ subTitle,
243
+ type: _type = 'text'
244
+ }) => {
245
+ switch (_type) {
246
+ case 'h1':
247
+ {
248
+ generator.print.log(`\n`);
249
+ generator.print.log(chalk.white.bgRed.bold(`${title}`));
250
+ if (subTitle) {
251
+ generator.print.log(chalk.italic(`${subTitle}\n`));
252
+ }
253
+ generator.print.log(`----`);
254
+ generator.print.log(``);
255
+ }
256
+ break;
257
+ case 'h2':
258
+ {
259
+ generator.print.log(`\n`);
260
+ generator.print.log(chalk.white.bgGreen.bold(`${title}`));
261
+ if (subTitle) {
262
+ generator.print.log(chalk.italic(`${subTitle}\n`));
263
+ }
264
+ }
265
+ break;
266
+ default:
267
+ {
268
+ generator.print.log(chalk.blue.bold(`\n${title}`));
269
+ if (subTitle) {
270
+ generator.print.log(chalk.italic(`${subTitle}\n`));
271
+ }
272
+ }
273
+ break;
274
+ }
275
+ }
276
+ };
277
+ });
278
+
279
+ var mergeOptions = (({
280
+ handlerOptions: _handlerOptions = [],
281
+ generator
282
+ }) => {
283
+ generator.options = _handlerOptions.map(option => {
284
+ switch (option.scope) {
285
+ case 'private':
286
+ {
287
+ return option;
288
+ }
289
+ }
290
+ const i = _.findWhere(generator.libraryOptions, {
291
+ name: option.name
292
+ });
293
+ if (!i) {
294
+ return option;
295
+ }
296
+ let result = _extends({}, option);
297
+ lodash.merge(result, i);
298
+ return result;
299
+ });
300
+ });
301
+
302
+ var buildGenerator = (({
303
+ payload,
304
+ options: _options = []
305
+ }) => {
306
+ const generator = {
307
+ payload,
308
+ print: console,
309
+ libraryOptions: _options,
310
+ options: []
311
+ };
312
+ generator.mergeOptions = op => mergeOptions({
313
+ handlerOptions: op,
314
+ generator
315
+ });
316
+ generator.ui = ui({
317
+ generator
318
+ });
319
+ generator.prompt = prompt({
320
+ generator
321
+ });
322
+ generator.template = template({
323
+ generator
324
+ });
325
+ generator.fs = fs({
326
+ generator
327
+ });
328
+ generator.spawn = async (command, args, options) => {
329
+ return new Promise(resolve => {
330
+ const result = cp.spawn(command, args, options);
331
+ result.on('close', () => {
332
+ resolve();
333
+ });
334
+ });
335
+ };
336
+ return generator;
337
+ });
338
+
339
+ var buildCommand = (async ({
340
+ path,
341
+ generator,
342
+ payload,
343
+ fileName
344
+ }) => {
345
+ const data = (await import(path)).default;
346
+ const {
347
+ name = '',
348
+ description = '',
349
+ // options = {},
350
+ handler,
351
+ example
352
+ } = data;
353
+ const command = {
354
+ command: name,
355
+ desc: description,
356
+ builder: {},
357
+ handler: argv => {
358
+ data.handler({
359
+ generator,
360
+ payload,
361
+ argv
362
+ });
363
+ }
364
+ };
365
+ return {
366
+ command,
367
+ data: _extends({}, data, {
368
+ options: data.options && data.options.length ? data.options : []
369
+ })
370
+ };
371
+ });
372
+
373
+ var formatOptionForYargs = (({
374
+ option,
375
+ yargs
376
+ }) => {
377
+ const {
378
+ name,
379
+ description,
380
+ message,
381
+ type = 'string',
382
+ global = false,
383
+ alias,
384
+ defaultValue
385
+ } = option;
386
+ yargs.option(name, {
387
+ desc: description ? description : message,
388
+ type,
389
+ global,
390
+ alias,
391
+ default: defaultValue
392
+ });
393
+ });
394
+
395
+ const operation = async ({
396
+ path,
397
+ generator,
398
+ yargs,
399
+ root: _root = false,
400
+ payload
401
+ }) => {
402
+ const candidates = await jetpack.listAsync(path);
403
+ if (!candidates || !candidates.length) {
404
+ return;
405
+ }
406
+ const commands = [];
407
+ let index = null;
408
+ let subCommands = [];
409
+ await Promise.all(candidates.map(async item => {
410
+ const __path = `${path}/${item}`;
411
+ const stat = await fs$1.promises.stat(__path);
412
+ if (!stat || !stat.isDirectory()) {
413
+ return null;
414
+ }
415
+ const subCommand = await operation({
416
+ path: __path,
417
+ generator,
418
+ payload,
419
+ yargs
420
+ });
421
+ subCommands.push(subCommand);
422
+ }));
423
+ await Promise.all(candidates.map(async item => {
424
+ const __path = `${path}/${item}`;
425
+ const stat = await fs$1.promises.stat(__path);
426
+ if (!stat || stat.isDirectory()) {
427
+ return null;
428
+ }
429
+ const {
430
+ data: commandData,
431
+ command
432
+ } = await buildCommand({
433
+ path: __path,
434
+ generator,
435
+ fileName: item,
436
+ payload
437
+ });
438
+ if (item === 'index.js') {
439
+ return;
440
+ }
441
+ command.builder = yargs => {
442
+ fixOptions({
443
+ generator,
444
+ commandOptions: commandData.options,
445
+ yargs
446
+ });
447
+ if (commandData.example) {
448
+ yargs.example(commandData.example);
449
+ }
450
+ };
451
+ commands.push(command);
452
+ }));
453
+ await Promise.all(candidates.map(async item => {
454
+ const __path = `${path}/${item}`;
455
+ const stat = await fs$1.promises.stat(__path);
456
+ if (!stat || stat.isDirectory()) {
457
+ return null;
458
+ }
459
+ if (item !== 'index.js') {
460
+ return;
461
+ }
462
+ const {
463
+ data: commandData,
464
+ command
465
+ } = await buildCommand({
466
+ path: __path,
467
+ generator,
468
+ fileName: item,
469
+ payload
470
+ });
471
+ if (!_root) {
472
+ command.builder = yargs => {
473
+ fixOptions({
474
+ generator,
475
+ commandOptions: commandData.options,
476
+ yargs
477
+ });
478
+ // commandData.options.forEach(option => formatOptionForYargs({ option, yargs }))
479
+ if (commandData.example) {
480
+ yargs.example(commandData.example);
481
+ }
482
+ commands.forEach(subCommand => {
483
+ yargs.command(subCommand);
484
+ });
485
+ subCommands.forEach(subCommand => {
486
+ yargs.command(subCommand.index);
487
+ });
488
+ };
489
+ } else {
490
+ subCommands.forEach(subCommand => {
491
+ yargs.command(subCommand.index);
492
+ });
493
+ }
494
+ index = command;
495
+ }));
496
+ return {
497
+ index,
498
+ commands
499
+ };
500
+ };
501
+ const fixOptions = ({
502
+ generator,
503
+ commandOptions,
504
+ yargs
505
+ }) => {
506
+ let nativeArgv = parseArgv(process.argv);
507
+ delete nativeArgv["--"];
508
+ delete nativeArgv["_"];
509
+ Object.keys(nativeArgv).forEach(n => {
510
+ generator.payload[n] = nativeArgv[n];
511
+ });
512
+ const _options = commandOptions && commandOptions.length ? commandOptions : [];
513
+ const options = _options.map(option => {
514
+ const value = nativeArgv[option.name];
515
+ return _extends({}, option, {
516
+ value
517
+ });
518
+ });
519
+ generator.mergeOptions(options);
520
+ generator.options.forEach(option => formatOptionForYargs({
521
+ option,
522
+ yargs
523
+ }));
524
+ };
525
+
526
+ var registerCommands = (async ({
527
+ path,
528
+ yargs,
529
+ options
530
+ }) => {
531
+ const commandsPath = `${path}/commands`;
532
+ const payload = {};
533
+ const generator = buildGenerator({
534
+ payload,
535
+ options,
536
+ yargs
537
+ });
538
+ const {
539
+ index,
540
+ commands
541
+ } = await operation({
542
+ path: commandsPath,
543
+ generator,
544
+ yargs,
545
+ root: true,
546
+ payload
547
+ });
548
+ commands.forEach(command => {
549
+ yargs.command(command);
550
+ });
551
+ return commands;
552
+ });
553
+
554
+ var checkFileExists = (async file => {
555
+ return fs$1.promises.access(file, fs$1.constants.F_OK).then(() => true).catch(() => false);
556
+ });
557
+
558
+ //https://www.npmjs.com/package/directory-import
559
+
560
+ const perform = async ({
561
+ path,
562
+ includeMeta: _includeMeta = false,
563
+ excludes: _excludes = ['spec.js'],
564
+ includeExtensions: _includeExtensions = ['.js', '.ts', '.json']
565
+ }) => {
566
+ try {
567
+ if (!(await checkFileExists(path))) {
568
+ return null;
569
+ }
570
+ const items = await fs$1.promises.readdir(path);
571
+ if (!items || !items.length) {
572
+ return null;
573
+ }
574
+ let results = (await Promise.all(items.map(async item => {
575
+ const __path = _path.join(path, item);
576
+ const _stat = await fs$1.promises.stat(__path);
577
+ if (!_stat) {
578
+ return null;
579
+ }
580
+ const isDir = _stat.isDirectory();
581
+ if (isDir) {
582
+ return perform({
583
+ path: __path
584
+ });
585
+ }
586
+ if (!(await checkFileExists(__path))) {
587
+ return null;
588
+ }
589
+ if (item.includes('spec.js')) {
590
+ return null;
591
+ }
592
+ const extension = getExtension(item);
593
+ if (!_includeExtensions.includes(extension)) {
594
+ return null;
595
+ }
596
+ if (_includeMeta) {
597
+ const racine = getFileRaw(__path);
598
+ const _module = await import(__path);
599
+ const md = `${racine}.md`;
600
+ let documentation = null;
601
+ if (await checkFileExists(md)) {
602
+ documentation = await fs$1.promises.readFile(md, 'utf8');
603
+ }
604
+ return [{
605
+ module: _module,
606
+ path: __path,
607
+ documentation
608
+ }];
609
+ } else {
610
+ return [await import(__path)];
611
+ }
612
+ }))).filter(a => a);
613
+ results = _.flatten(results);
614
+ return results;
615
+ } catch (e) {
616
+ console.error(e);
617
+ return null;
618
+ }
619
+ };
620
+ const getExtension = str => str.slice(str.lastIndexOf("."));
621
+ const getFileRaw = str => str.split('.').slice(0, -1).join('.');
622
+
623
+ var loadOptions = (async ({
624
+ path,
625
+ config
626
+ }) => {
627
+ const optionsPath = `${path}/options`;
628
+ let files = await perform({
629
+ path: optionsPath,
630
+ includeMeta: false
631
+ });
632
+ files = files ? files.map(f => f.default) : [];
633
+ return files;
634
+ });
635
+
636
+ // https://medium.com/netscape/a-guide-to-create-a-nodejs-command-line-package-c2166ad0452e
637
+ var launch = (async ({
638
+ path,
639
+ npmPackage,
640
+ config
641
+ } = {}) => {
642
+ // import options from './options.js';
643
+ let __actualPath = path;
644
+ if (!__actualPath) {
645
+ const ce = getFileCallerURL();
646
+ __actualPath = _path.dirname(ce);
647
+ __actualPath = __actualPath.replace('file://', '');
648
+ }
649
+ let __actualNpmPackage = npmPackage;
650
+ if (!__actualNpmPackage) {
651
+ const __d = _path.resolve(__actualPath, '../package.json');
652
+ if (fs$1.existsSync(__d)) {
653
+ __actualNpmPackage = JSON.parse(fs$1.readFileSync(__d).toString());
654
+ }
655
+ }
656
+ if (!__actualNpmPackage) {
657
+ __actualNpmPackage = {
658
+ version: "0.0.0"
659
+ };
660
+ }
661
+ let __actualConfig = config;
662
+ if (!__actualConfig) {
663
+ const __d = _path.resolve(__actualPath, '../cli.config.json');
664
+ if (fs$1.existsSync(__d)) {
665
+ __actualConfig = JSON.parse(fs$1.readFileSync(__d).toString());
666
+ }
667
+ }
668
+ if (!__actualConfig) {
669
+ __actualConfig = {};
670
+ }
671
+ //https://github.com/yargs/yargs/issues/569
672
+
673
+ const yargs = _yargs(hideBin(process.argv));
674
+ yargs
675
+ // .options(options)
676
+ .usage('Usage: servable <command>').demandCommand(1).wrap(Math.min(yargs.terminalWidth(), 160)).help('help').alias('help', 'h').version(__actualNpmPackage.version).alias('version', 'v').hide('help').hide('version').epilog('Made by Servable.');
677
+ const options = await loadOptions({
678
+ path: __actualPath,
679
+ config: __actualConfig
680
+ });
681
+ await registerCommands({
682
+ path: __actualPath,
683
+ yargs,
684
+ config: __actualConfig,
685
+ options
686
+ });
687
+ });
688
+
689
+ export { launch };
690
+ //# sourceMappingURL=index.modern.modern.js.map