@ibgib/node-gib 0.0.2

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 (127) hide show
  1. package/.ibgibignore +30 -0
  2. package/CHANGELOG.md +18 -0
  3. package/README.md +16 -0
  4. package/dist/assumptions.respec.d.mts +2 -0
  5. package/dist/assumptions.respec.d.mts.map +1 -0
  6. package/dist/assumptions.respec.mjs +41 -0
  7. package/dist/assumptions.respec.mjs.map +1 -0
  8. package/dist/common/prompt-functions.d.mts +89 -0
  9. package/dist/common/prompt-functions.d.mts.map +1 -0
  10. package/dist/common/prompt-functions.mjs +418 -0
  11. package/dist/common/prompt-functions.mjs.map +1 -0
  12. package/dist/constants.d.mts +3 -0
  13. package/dist/constants.d.mts.map +1 -0
  14. package/dist/constants.mjs +3 -0
  15. package/dist/constants.mjs.map +1 -0
  16. package/dist/foo.d.mts +2 -0
  17. package/dist/foo.d.mts.map +1 -0
  18. package/dist/foo.mjs +19 -0
  19. package/dist/foo.mjs.map +1 -0
  20. package/dist/helpers.d.mts +2 -0
  21. package/dist/helpers.d.mts.map +1 -0
  22. package/dist/helpers.mjs +3 -0
  23. package/dist/helpers.mjs.map +1 -0
  24. package/dist/respec-gib.node.d.mts +2 -0
  25. package/dist/respec-gib.node.d.mts.map +1 -0
  26. package/dist/respec-gib.node.mjs +213 -0
  27. package/dist/respec-gib.node.mjs.map +1 -0
  28. package/dist/spec-helper.node.respec.d.mts +13 -0
  29. package/dist/spec-helper.node.respec.d.mts.map +1 -0
  30. package/dist/spec-helper.node.respec.mjs +50 -0
  31. package/dist/spec-helper.node.respec.mjs.map +1 -0
  32. package/dist/types.d.mts +2 -0
  33. package/dist/types.d.mts.map +1 -0
  34. package/dist/types.mjs +2 -0
  35. package/dist/types.mjs.map +1 -0
  36. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.d.mts +35 -0
  37. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.d.mts.map +1 -0
  38. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.mjs +267 -0
  39. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.mjs.map +1 -0
  40. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.d.mts +98 -0
  41. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.d.mts.map +1 -0
  42. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.mjs +160 -0
  43. package/dist/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.mjs.map +1 -0
  44. package/dist/witness/space/node-filesystem-space/node-filesystem-space-helper.d.mts +42 -0
  45. package/dist/witness/space/node-filesystem-space/node-filesystem-space-helper.d.mts.map +1 -0
  46. package/dist/witness/space/node-filesystem-space/node-filesystem-space-helper.mjs +312 -0
  47. package/dist/witness/space/node-filesystem-space/node-filesystem-space-helper.mjs.map +1 -0
  48. package/dist/witness/space/node-filesystem-space/node-filesystem-space-types.d.mts +84 -0
  49. package/dist/witness/space/node-filesystem-space/node-filesystem-space-types.d.mts.map +1 -0
  50. package/dist/witness/space/node-filesystem-space/node-filesystem-space-types.mjs +65 -0
  51. package/dist/witness/space/node-filesystem-space/node-filesystem-space-types.mjs.map +1 -0
  52. package/dist/witness/space/node-filesystem-space/node-filesystem-space-v1.d.mts +77 -0
  53. package/dist/witness/space/node-filesystem-space/node-filesystem-space-v1.d.mts.map +1 -0
  54. package/dist/witness/space/node-filesystem-space/node-filesystem-space-v1.mjs +672 -0
  55. package/dist/witness/space/node-filesystem-space/node-filesystem-space-v1.mjs.map +1 -0
  56. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.d.mts +5 -0
  57. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.d.mts.map +1 -0
  58. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.mjs +5 -0
  59. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.mjs.map +1 -0
  60. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.d.mts +30 -0
  61. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.d.mts.map +1 -0
  62. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.mjs +38 -0
  63. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.mjs.map +1 -0
  64. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.d.mts +64 -0
  65. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.d.mts.map +1 -0
  66. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.mjs +353 -0
  67. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.mjs.map +1 -0
  68. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.d.mts +2 -0
  69. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.d.mts.map +1 -0
  70. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.mjs +52 -0
  71. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.mjs.map +1 -0
  72. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.d.mts +2 -0
  73. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.d.mts.map +1 -0
  74. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.mjs +53 -0
  75. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.mjs.map +1 -0
  76. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.d.mts +2 -0
  77. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.d.mts.map +1 -0
  78. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.mjs +59 -0
  79. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.mjs.map +1 -0
  80. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.d.mts +2 -0
  81. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.d.mts.map +1 -0
  82. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.mjs +55 -0
  83. package/dist/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.mjs.map +1 -0
  84. package/dist/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.d.mts +2 -0
  85. package/dist/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.d.mts.map +1 -0
  86. package/dist/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.mjs +67 -0
  87. package/dist/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.mjs.map +1 -0
  88. package/dist/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.d.mts +2 -0
  89. package/dist/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.d.mts.map +1 -0
  90. package/dist/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.mjs +67 -0
  91. package/dist/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.mjs.map +1 -0
  92. package/dist/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.d.mts +2 -0
  93. package/dist/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.d.mts.map +1 -0
  94. package/dist/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.mjs +68 -0
  95. package/dist/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.mjs.map +1 -0
  96. package/dist/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.d.mts +2 -0
  97. package/dist/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.d.mts.map +1 -0
  98. package/dist/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.mjs +69 -0
  99. package/dist/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.mjs.map +1 -0
  100. package/generate-version-file.js +29 -0
  101. package/package.json +41 -0
  102. package/src/assumptions.respec.mts +51 -0
  103. package/src/common/prompt-functions.mts +429 -0
  104. package/src/constants.mts +2 -0
  105. package/src/foo.mts +13 -0
  106. package/src/helpers.mts +5 -0
  107. package/src/respec-gib.node.mts +201 -0
  108. package/src/spec-helper.node.respec.mts +50 -0
  109. package/src/types.mts +0 -0
  110. package/src/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace-helper.mts +290 -0
  111. package/src/witness/space/metaspace/metaspace-nodespace/metaspace-nodespace.mts +166 -0
  112. package/src/witness/space/node-filesystem-space/node-filesystem-space-helper.mts +298 -0
  113. package/src/witness/space/node-filesystem-space/node-filesystem-space-types.mts +150 -0
  114. package/src/witness/space/node-filesystem-space/node-filesystem-space-v1.mts +672 -0
  115. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-constants.mts +6 -0
  116. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-types.mts +68 -0
  117. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/node-indexed-filesystem-space-v1.mts +403 -0
  118. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_createAndInit.node-indexed-filesystem-space-v1.respec.mts +68 -0
  119. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_persistTransformResult.node-indexed-filesystem-space-v1.respec.mts +70 -0
  120. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_putGetDelete.node-indexed-filesystem-space-v1.respec.mts +72 -0
  121. package/src/witness/space/node-filesystem-space/node-indexed-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-indexed-filesystem-space-v1.respec.mts +72 -0
  122. package/src/witness/space/node-filesystem-space/respec/testSpace_createAndInit.node-filesystem-space-v1.respec.mts +87 -0
  123. package/src/witness/space/node-filesystem-space/respec/testSpace_persistTransformResult.node-filesystem-space-v1.respec.mts +88 -0
  124. package/src/witness/space/node-filesystem-space/respec/testSpace_putGetDelete.node-filesystem-space-v1.respec.mts +88 -0
  125. package/src/witness/space/node-filesystem-space/respec/testSpace_registerNewIbGib_GetLatest.node-filesystem-space-v1.respec.mts +90 -0
  126. package/tsconfig.json +17 -0
  127. package/tsconfig.test.json +10 -0
@@ -0,0 +1,201 @@
1
+ import { readdir, open } from 'node:fs/promises';
2
+ import { pathToFileURL } from 'node:url';
3
+ import { statSync } from 'node:fs';
4
+ import * as pathUtils from 'path';
5
+
6
+ import { pretty } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
7
+ import { getGlobalRespecGib } from '@ibgib/helper-gib/dist/respec-gib/respec-gib.mjs';
8
+
9
+ // #region settings
10
+ /**
11
+ * This is how I enable/disable verbose logging. Do with it what you will.
12
+ */
13
+ const logalot = false;
14
+
15
+ /** set this to the root of the respecs to look at */
16
+ const RESPEC_ROOT_DIR_RELATIVE_TO_BASE = './dist';
17
+
18
+ /** change this to suit your naming convention */
19
+ const RESPEC_FILE_REG_EXP = /^.+respec\.mjs$/;
20
+ // const RESPEC_FILE_REG_EXP = /^.*respec-gib.respec\.mjs$/;
21
+ // if (respecPath.includes('respec-gib.respec.mjs')) {
22
+
23
+ /**
24
+ * If on, will first load a file and see if there is an extra respecful
25
+ * `await respecfully`/`ifWe` block. Use these if you want to focus on a single or
26
+ * subset of respecs.
27
+ *
28
+ * If there are no extra respecful blocks found in an entire file, that file
29
+ * will be skipped.
30
+ *
31
+ * Note: this only is a flag to search through respec files.
32
+ */
33
+ const LOOK_FOR_EXTRA_RESPEC = true;
34
+ /**
35
+ * The names of the functions that indicate that we want to focus on just those
36
+ * blocks.
37
+ *
38
+ * ATOW, for first run implementation here, I am implementing it such that it
39
+ * will filter out files that don't have these indicators. The respec files that
40
+ * do have these will execute fully, but the output will only include these
41
+ * particular blocks.
42
+ */
43
+ const EXTRA_RESPEC_FUNCTION_NAMES: string[] = ['await respecfullyDear', 'ifWeMight'];
44
+
45
+ // #endregion settings
46
+
47
+ // #region 1. get respec paths
48
+
49
+ const basePath = process.cwd();
50
+ const srcPath = pathUtils.join(basePath, RESPEC_ROOT_DIR_RELATIVE_TO_BASE);
51
+
52
+ if (logalot) { console.log(`cwd: ${process.cwd()}`); }
53
+ if (logalot) { console.log(`basePath: ${basePath}`); }
54
+ if (logalot) { console.log(`srcPath: ${srcPath}`); }
55
+
56
+
57
+ const respecGib = getGlobalRespecGib();
58
+ const allRespecPaths = await getRespecFileFullPaths(srcPath, []);
59
+
60
+ if (logalot) { console.log(`allRespecPaths: ${allRespecPaths} (I: f5182a455375a8cf2aa6e1127a082423)`); }
61
+ let filteredRespecPaths: string[] | undefined = undefined;
62
+
63
+ if (LOOK_FOR_EXTRA_RESPEC) {
64
+ const hasExtraRespecPromises = allRespecPaths.map(async respecPath => {
65
+ const hasExtra = await respecFileHasExtraRespec(respecPath);
66
+ return [respecPath, hasExtra] as [string, boolean];
67
+ });
68
+ const resPathHasExtraTuples = await Promise.all(hasExtraRespecPromises);
69
+ filteredRespecPaths = resPathHasExtraTuples
70
+ .filter(([_respecPath, hasExtra]) => hasExtra)
71
+ .map(([respecPath, _hasExtra]) => respecPath);
72
+
73
+ // if there are no files that have extra respec then we do all files
74
+ if (filteredRespecPaths.length === 0) {
75
+ if (logalot) { console.log(`filteredRespecPaths is empty. doing allRespecPaths found (I: b98f54656899646025eecb4c028ab523)`); }
76
+ filteredRespecPaths = allRespecPaths.concat();
77
+ } else {
78
+ console.log(`filteredRespecPaths for extra respec: ${filteredRespecPaths} (I: b98f54656899646025eecb4c028ab523)`);
79
+ respecGib.extraRespecOnly = true;
80
+ }
81
+ }
82
+
83
+ // #endregion 1. get respec paths
84
+
85
+ respecGib.allRespecPaths = allRespecPaths;
86
+ respecGib.filteredRespecPaths = filteredRespecPaths;
87
+ const respecPaths = filteredRespecPaths ?? allRespecPaths;
88
+ respecGib.respecPaths = respecPaths;
89
+ if (logalot) { console.log(`respecPaths found:\n${respecPaths}`); }
90
+
91
+ // #region 2. execute paths' respective respecs
92
+
93
+ // for now, we'll do sequentially, but in the future we could conceivable farm
94
+ // these out to other node processes, or at least Promise.all
95
+
96
+ for (let i = 0; i < respecPaths.length; i++) {
97
+ const respecPath = respecPaths[i];
98
+ if (logalot) { console.log(respecPath); }
99
+ const respecPathUrl = pathToFileURL(respecPath).href;
100
+ const esm = await import(respecPathUrl);
101
+ if (logalot) { console.log(pretty(Object.keys(esm))); }
102
+ }
103
+
104
+ const skippedRespecPathCount = respecGib.allRespecPaths.length - respecGib.respecPaths.length;
105
+ if (skippedRespecPathCount > 0) {
106
+ console.log('');
107
+ console.error('\x1b[33m%s\x1b[0m', `${skippedRespecPathCount} respec files completely skipped.`); // yellow
108
+ }
109
+ if (respecGib.ifWeBlocksSkipped > 0) {
110
+ console.log('');
111
+ console.error('\x1b[33m%s\x1b[0m', `${respecGib.ifWeBlocksSkipped} ifWe blocks ran but skipped reporting`); // yellow
112
+ }
113
+
114
+ if (respecGib.errorMsgs.length === 0) {
115
+ console.log('');
116
+ console.error('\x1b[32m%s\x1b[0m', `💚💚 nothing but respec 💚💚`); // green
117
+ } else {
118
+ console.log('');
119
+ console.error('\x1b[31m%s\x1b[0m', `💔💔 DISrespec found 💔💔`); // red
120
+ for (const errorMsg of respecGib.errorMsgs) {
121
+ console.error('\x1b[31m%s\x1b[0m', errorMsg); // red
122
+ }
123
+ }
124
+
125
+ // #endregion 2. execute paths' respective respecs
126
+
127
+ // #region helper functions
128
+
129
+ /**
130
+ * builds a list of respec file paths, recursively traversing subdirectories
131
+ * starting from `dirPath`.
132
+ *
133
+ * @param dirPath a full path corresponding to a directory
134
+ * @param found respec paths already found (used in recursive calls)
135
+ * @returns list of all respec paths according to the respec regexp constant {@link RESPEC_FILE_REG_EXP}
136
+ */
137
+ async function getRespecFileFullPaths(dirPath: string, found: string[]): Promise<string[]> {
138
+ const lc = `[${getRespecFileFullPaths.name}][${dirPath}]`;
139
+ try {
140
+ if (logalot) { console.log(`${lc} starting... (I: 16026290523925f79ba1933847e2a623)`); }
141
+ found ??= [];
142
+ const children = await readdir(dirPath);
143
+ if (logalot) { for (let i = 0; i < children.length; i++) { console.log(children[i]); } }
144
+ const files: string[] = [];
145
+ const dirs: string[] = [];
146
+ children.forEach(name => {
147
+ const fullPath = pathUtils.join(dirPath, name);
148
+ const stat = statSync(fullPath);
149
+ if (stat.isDirectory()) {
150
+ // symbolic link could create a loop
151
+ if (!stat.isSymbolicLink()) { dirs.push(fullPath); }
152
+ } else if (!!name.match(RESPEC_FILE_REG_EXP)) {
153
+ files.push(fullPath);
154
+ }
155
+ });
156
+
157
+ found = found.concat(files);
158
+ for (let i = 0; i < dirs.length; i++) {
159
+ const subfound = await getRespecFileFullPaths(dirs[i], found);
160
+ found = found.concat(subfound);
161
+ }
162
+ return Array.from(new Set(found)); // unique
163
+ } catch (error) {
164
+ console.error(`${lc} ${error.message}`);
165
+ throw error;
166
+ } finally {
167
+ if (logalot) { console.log(`${lc} complete.`); }
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Searches through the file (without importing it) for extra respecful
173
+ * functions.
174
+ *
175
+ * @param respecPath
176
+ * @returns true if extra respecful functions found in file
177
+ */
178
+ async function respecFileHasExtraRespec(respecPath: string): Promise<boolean> {
179
+ const lc = `[${respecFileHasExtraRespec.name}]`;
180
+ try {
181
+ if (logalot) { console.log(`${lc} starting... (I: 61f3221917ba77175efa305b14defc23)`); }
182
+ const file = await open(respecPath);
183
+ for await (const line of file.readLines()) {
184
+ const hasExtraRespecInLine =
185
+ EXTRA_RESPEC_FUNCTION_NAMES.some(fnName => {
186
+ if (line.includes(`${fnName}(`)) { return true; }
187
+ });
188
+ if (hasExtraRespecInLine) {
189
+ return true;
190
+ }
191
+ }
192
+ return false;
193
+ } catch (error) {
194
+ console.error(`${lc} ${error.message}`);
195
+ throw error;
196
+ } finally {
197
+ if (logalot) { console.log(`${lc} complete.`); }
198
+ }
199
+ }
200
+
201
+ // #endregion helper functions
@@ -0,0 +1,50 @@
1
+ import { default as pathUtils } from 'path';
2
+
3
+ import { delay, getUUID, } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs';
4
+
5
+ /**
6
+ * helper function to get the filename for use in specs.
7
+ *
8
+ * @param importMetaUrl import.meta.url
9
+ * @returns currently executing filename sans .respec.mjs
10
+ */
11
+ export async function getCurrentFilename(importMetaUrl: string): Promise<string> {
12
+ const lc = `[${getCurrentFilename.name}]`;
13
+ try {
14
+ const pieces = importMetaUrl.split(pathUtils.sep);
15
+ let filename = pieces.at(-1)?.replace(/\.respec\.mjs$/, '')
16
+ if (!filename) {
17
+ console.warn(`filename not able to be gotten?`);
18
+ filename = (new Date()).toTimeString() + (await getUUID());
19
+ }
20
+ return `ibgib${pathUtils.sep}${filename}`;
21
+ } catch (error) {
22
+ console.error(`${lc} ${error.message}`);
23
+ throw error;
24
+ }
25
+ }
26
+
27
+ export async function specGetSpaceId(): Promise<string> {
28
+ await delay(10);
29
+ let uuid = await getUUID();
30
+ let now = new Date();
31
+ now.getMinutes();
32
+ let timeComponent = now.getMinutes().toString() + now.getSeconds().toString() + now.getMilliseconds().toString()
33
+ uuid = timeComponent + uuid.substring(timeComponent.length, uuid.length);
34
+ return uuid;
35
+ }
36
+ export function specGetSpaceName(uuid: string): string {
37
+ return uuid.substring(0, 8) + '-space';
38
+ }
39
+
40
+ export function specGetSpaceDescription(uuid: string): string {
41
+ return 'description for ' + uuid.substring(0, 8);
42
+ }
43
+ export function specGetBaseDir(testBaseDirRoot: string, uuid: string, logalot: boolean, lc: string): string {
44
+ const dir = pathUtils.join(".", testBaseDirRoot, specGetSpaceName(uuid) + "_baseDir");
45
+ if (logalot) { console.log(`${lc}[${specGetBaseDir.name}] dir: ${dir} (I: 410c57b7d02ac2ddf9ca32493ad19523)`); }
46
+ return dir;
47
+ }
48
+ export function specGetBaseSubPath(testBaseDirRoot: string, uuid: string, logalot: boolean, lc: string): string {
49
+ return "tmp";
50
+ }
package/src/types.mts ADDED
File without changes
@@ -0,0 +1,290 @@
1
+ import { extractErrorMsg, getTimestamp, getUUID, pretty } from "@ibgib/helper-gib/dist/helpers/utils-helper.mjs";
2
+ import { getGib, isPrimitive } from "@ibgib/ts-gib/dist/V1/transforms/transform-helper.mjs";
3
+ import { GIB } from "@ibgib/ts-gib/dist/V1/constants.mjs";
4
+ import { IbGib_V1 } from "@ibgib/ts-gib/dist/V1/types.mjs";
5
+ import { IbGibAddr } from "@ibgib/ts-gib/dist/types.mjs";
6
+ import { getIbAndGib } from "@ibgib/ts-gib/dist/helper.mjs";
7
+ import { SyncSagaInfo, SyncStatusIbGib } from "@ibgib/core-gib/dist/witness/space/outer-space/outer-space-types.mjs";
8
+ import { SubscriptionWitness } from "@ibgib/core-gib/dist/common/pubsub/subscription/subscription-types.mjs";
9
+ import { fnObs } from "@ibgib/core-gib/dist/common/pubsub/observer/observer-helper.mjs";
10
+ import { ErrorIbGib_V1 } from "@ibgib/core-gib/dist/common/error/error-types.mjs";
11
+ import { IbGibSpaceAny } from "@ibgib/core-gib/dist/witness/space/space-base-v1.mjs";
12
+ import {
13
+ DtoToSpaceFunction, LocalSpaceFactoryFunction, MetaspaceService,
14
+ } from "@ibgib/core-gib/dist/witness/space/metaspace/metaspace-types.mjs";
15
+ import { isSpaceIb, spaceNameIsValid } from "@ibgib/core-gib/dist/witness/space/space-helper.mjs";
16
+ import {
17
+ IBGIB_BASE_DIR, IBGIB_BASE_SUBPATH, IBGIB_BIN_SUBPATH,
18
+ IBGIB_DNA_SUBPATH, IBGIB_ENCODING, IBGIB_IBGIBS_SUBPATH, IBGIB_META_SUBPATH
19
+ } from "@ibgib/core-gib/dist/witness/space/filesystem-space/filesystem-constants.mjs";
20
+ import {
21
+ DEFAULT_LOCAL_SPACE_DESCRIPTION, DEFAULT_LOCAL_SPACE_POLLING_INTERVAL_MS,
22
+ PERSIST_OPTS_AND_RESULTS_IBGIBS_DEFAULT
23
+ } from "@ibgib/core-gib/dist/witness/space/space-constants.mjs";
24
+
25
+ import { GLOBAL_LOG_A_LOT } from "../../../../constants.mjs";
26
+ import { getFnPrompt } from "../../../../common/prompt-functions.mjs";
27
+ import { NodeFilesystemSpace_V1 } from "../../node-filesystem-space/node-filesystem-space-v1.mjs";
28
+ import { NodeFilesystemSpaceData_V1, NodeFilesystemSpaceRel8ns_V1 } from "../../node-filesystem-space/node-filesystem-space-types.mjs";
29
+
30
+ /**
31
+ * used in verbose logging (across all ibgib libs atow)
32
+ */
33
+ const logalot = GLOBAL_LOG_A_LOT;
34
+
35
+ export const fnCreateNewLocalSpace: LocalSpaceFactoryFunction = async ({
36
+ allowCancel,
37
+ spaceName,
38
+ logalot,
39
+ }) => {
40
+ const lc = `[fnCreateNewLocalSpace]`;
41
+ try {
42
+ if (logalot) { console.log(`${lc} starting... (I: 15bcc16f4f3f9e40e743931a3965ff22)`); }
43
+
44
+ if (spaceName && !spaceNameIsValid(spaceName)) {
45
+ throw new Error(`spaceName (${spaceName}) is invalid. (E: 820b291cccf9f405f9e0cce87143e323)`);
46
+ // throw because this is used in RCLI and automated testing...
47
+ }
48
+ if (!spaceName) {
49
+
50
+ const promptName: () => Promise<void> = async () => {
51
+ const fnPrompt = getFnPrompt();
52
+ const resName = await fnPrompt({
53
+ title: 'greetings program', msg: `all of your data is stored locally on your device's "local space".
54
+
55
+ so name your space with alphanumerics & underscores, like 'phone_alice' or 'web_bob_foo', or leave blank and get a random name.
56
+
57
+ Enter space name:
58
+ `
59
+ });
60
+
61
+ if (resName === '' && !allowCancel) {
62
+ spaceName = 'space_' + (await getUUID()).slice(0, 10);
63
+ } else {
64
+ if (resName && spaceNameIsValid(resName)) {
65
+ spaceName = resName;
66
+ }
67
+ }
68
+ };
69
+
70
+ // ...prompt for name
71
+ await promptName();
72
+
73
+ if (!spaceName) { return undefined; /* <<<< returns early */ }
74
+ }
75
+
76
+ // ...create in memory with defaults
77
+ const newLocalSpace = new NodeFilesystemSpace_V1(/*initialData*/ {
78
+ version: '1',
79
+ uuid: await getUUID(),
80
+ isTjp: true,
81
+ timestamp: getTimestamp(),
82
+ name: spaceName,
83
+ baseDir: IBGIB_BASE_DIR,
84
+ spaceSubPath: spaceName,
85
+ baseSubPath: IBGIB_BASE_SUBPATH,
86
+ binSubPath: IBGIB_BIN_SUBPATH,
87
+ dnaSubPath: IBGIB_DNA_SUBPATH,
88
+ ibgibsSubPath: IBGIB_IBGIBS_SUBPATH,
89
+ metaSubPath: IBGIB_META_SUBPATH,
90
+ encoding: IBGIB_ENCODING,
91
+ persistOptsAndResultIbGibs: PERSIST_OPTS_AND_RESULTS_IBGIBS_DEFAULT,
92
+ validateIbGibAddrsMatchIbGibs: true,
93
+ trace: false,
94
+ description: DEFAULT_LOCAL_SPACE_DESCRIPTION,
95
+ allowPrimitiveArgs: false,
96
+ catchAllErrors: true,
97
+ longPollingIntervalMs: DEFAULT_LOCAL_SPACE_POLLING_INTERVAL_MS,
98
+ } as NodeFilesystemSpaceData_V1, /*initialRel8ns*/ undefined);
99
+ if (logalot) { console.log(`${lc} localSpace.ib: ${newLocalSpace.ib}`); }
100
+ if (logalot) { console.log(`${lc} localSpace.gib: ${newLocalSpace.gib} (before sha256v1)`); }
101
+ if (logalot) { console.log(`${lc} localSpace.data: ${pretty(newLocalSpace.data || 'falsy')}`); }
102
+ if (logalot) { console.log(`${lc} localSpace.rel8ns: ${pretty(newLocalSpace.rel8ns || 'falsy')}`); }
103
+
104
+ // trying out with tjp, can't remember why didn't have it except for
105
+ // backwards compatability which i'm no longer shooting for (will be
106
+ // able to project old spaces to new ones i believe)
107
+ // newLocalSpace.gib = await getGib({ ibGib: newLocalSpace, hasTjp: false });
108
+ newLocalSpace.gib = await getGib({ ibGib: newLocalSpace, hasTjp: true });
109
+
110
+ if (newLocalSpace.gib === GIB) { throw new Error(`localSpace.gib not updated correctly.`); }
111
+ if (logalot) { console.log(`${lc} localSpace.gib: ${newLocalSpace.gib} (after sha256v1)`); }
112
+
113
+ return newLocalSpace as IbGibSpaceAny;
114
+ } catch (error) {
115
+ console.error(`${lc} ${extractErrorMsg(error)}`);
116
+ throw error;
117
+ } finally {
118
+ if (logalot) { console.log(`${lc} complete.`); }
119
+ }
120
+ }
121
+
122
+ export const fnDtoToSpace_nodeFilesystem: DtoToSpaceFunction = (spaceDto) => {
123
+ return Promise.resolve(
124
+ NodeFilesystemSpace_V1.createFromDto(spaceDto as IbGib_V1<NodeFilesystemSpaceData_V1, NodeFilesystemSpaceRel8ns_V1>)
125
+ );
126
+ }
127
+
128
+ /**
129
+ * wrapper around metaspace.syncIbGibs
130
+ * @param param0
131
+ * @returns
132
+ */
133
+ export async function doSync_awaitAllSagas({
134
+ addr,
135
+ metaspace,
136
+ localSpace,
137
+ syncSpaces,
138
+ sagaReporter,
139
+ verbose,
140
+ }: {
141
+ /**
142
+ * the addr whose live dependency graph is going to be built and synced.
143
+ */
144
+ addr: IbGibAddr,
145
+ /**
146
+ * it's meta yo
147
+ */
148
+ metaspace: MetaspaceService,
149
+ /**
150
+ * the space which contains the syncSpace (proxy outer space).
151
+ */
152
+ localSpace: IbGibSpaceAny,
153
+ /**
154
+ * sync space witnesses (not just ibgib dtos)
155
+ */
156
+ syncSpaces: IbGibSpaceAny[],
157
+ sagaReporter: (msg: string, saga: SyncSagaInfo | null) => Promise<void>,
158
+ /**
159
+ * If true, will report more in {@link sagaReporter}
160
+ */
161
+ verbose?: boolean,
162
+ }): Promise<SyncSagaInfo[] | undefined> {
163
+ const lc = `[${doSync_awaitAllSagas.name}]`;
164
+ try {
165
+ if (logalot || verbose) { console.log(`${lc} starting... (I: 6110576156c3f9a946626ff299870324)`); }
166
+ const { ib, gib } = getIbAndGib({ ibGibAddr: addr });
167
+
168
+ if (isPrimitive({ gib })) { throw new Error(`can't sync primitive ibgib (${addr}) (E: 7f87261b5378e38d0f237afa35f52d24)`); }
169
+ if (isSpaceIb({ ib })) { throw new Error(`can't sync entire space ibgib (${addr}) (E: 00f8d70c4b6c7f8c3c8b26cf097ba724)`); }
170
+ if (syncSpaces.length === 0) { throw new Error(`syncSpaces required (E: 6015c1c2920fa5301defb91933998d24)`); }
171
+ if (syncSpaces.some(x => !x.witness)) { throw new Error(`(UNEXPECTED) syncSpaces.some(x => !x.witness)? sync spaces should all be witnesses (and have a witness function). these are not supposed to be just dtos (with ib, gib, data, rel8ns props). (E: 2a670f794b065576ee6df6cf22ffb324)`); }
172
+
173
+ if (verbose) { await sagaReporter('getDependencyGraph starting... (I: d20cf9d59432492ebafdc206e1ecb5dc)', null); }
174
+
175
+ const dependencyGraph = await metaspace.getDependencyGraph({
176
+ space: localSpace,
177
+ ibGibAddr: addr,
178
+ live: true,
179
+ msBetweenRetries: 1_000,
180
+ });
181
+
182
+ if (verbose) {
183
+ await sagaReporter('getDependencyGraph complete. (I: a816ca4d1612409a914661bfe97bb4d1)', null);
184
+ await sagaReporter('syncIbGibs spinning off... (I: 83c8502f93ee46b7ae103d8ff5fa9b42)', null);
185
+ }
186
+
187
+ const sagas = await metaspace.syncIbGibs({
188
+ dependencyGraphIbGibs: Object.values(dependencyGraph),
189
+ syncSpaceIbGibs: syncSpaces,
190
+ localSpace,
191
+ }) ?? [];
192
+ if (sagas.length === 0) { throw new Error(`(UNEXPECTED) metaspace.syncIbGibs returned undefined (no sagas)? (E: 14989ed4c654eb9419a1faaf35a3e224)`); }
193
+ if (verbose) {
194
+ await sagaReporter('syncIbGibs spin off complete. these are not yet done, it/they are just spun off and going. (I: b99dd5938064415d87a5420dc37dc1b1)', sagas.at(0) ?? null);
195
+ }
196
+
197
+ let sagaCount = sagas.length;
198
+ let sagaErrorCounter = 0;
199
+ /**
200
+ * using this during debugging...
201
+ */
202
+ const sagaErrorMsgs: string[] = [];
203
+ let sagaCompleteOrErrorCounter = 0;
204
+ let allSagasCompletePromise = new Promise<void>((resolve, reject) => {
205
+ const interval = setInterval(async () => {
206
+ if (sagaCompleteOrErrorCounter === sagaCount) {
207
+ if (sagaErrorCounter > 0) {
208
+ reject(`saga had errors: ${sagaErrorMsgs.join('|')} (E: 15d75f475612492e8d5d0bcf947fc72b)`);
209
+ }
210
+ clearInterval(interval);
211
+ resolve();
212
+ }
213
+ }, 500);
214
+ });
215
+
216
+ const fnIncCompleteOrError = async (saga: SyncSagaInfo) => {
217
+ const lcInner = `${lc}[fnIncCompleteOrError]`;
218
+ if (logalot || verbose) { console.log(`${lcInner} starting... (I: 782b3304e67d4ebdb0acce8147a9cd9f)`); }
219
+ try {
220
+ sagaCompleteOrErrorCounter++;
221
+ let updateText: string;
222
+ if (sagaCompleteOrErrorCounter < sagaCount) {
223
+ updateText = `saga (${saga.sagaId}) complete or errored. ONLY SOME (${sagaCompleteOrErrorCounter}/${sagaCount}) have completed or errored. still more to go...`;
224
+ } else if (sagaCompleteOrErrorCounter === sagaCount) {
225
+ updateText = `saga (${saga.sagaId}) complete or errored. ALL ${sagaCount} sync sagas COMPLETE OR ERRORED.`;
226
+ } else {
227
+ debugger; // unexpected error state. sagaCompleteOrErrorCounter > sagaCount
228
+ updateText = `uhh, there is an error here. we have incremented sagaCompleteOrErrorCounter (${sagaCompleteOrErrorCounter}) to be greater than sagaCount (${sagaCount}) (E: 4d8373ee93f2466ab9e5c382ae9a3218)`;
229
+ }
230
+ if (logalot || verbose) {
231
+ console.log(`${lc} ${updateText} (I: 976c3deaf7f1a592680f3364a0793524)`);
232
+ await sagaReporter(`${lcInner} ${updateText}`, saga);
233
+ }
234
+ } catch (error) {
235
+ console.error(`${lc} ${extractErrorMsg(error)}`);
236
+ throw error;
237
+ } finally {
238
+ if (logalot || verbose) { console.log(`${lc}[fnIncCompleteOrError] complete. (I: 3eb96688a5880fe039445fc3d2374b24)`); }
239
+ }
240
+ };
241
+
242
+ for (let i = 0; i < sagas.length; i++) {
243
+ const saga = sagas[i];
244
+ let subscription: SubscriptionWitness = await saga.syncStatus$.subscribe(fnObs({
245
+ next: async (status: SyncStatusIbGib) => {
246
+ // debugger; // syncStatus$ fnobs next
247
+ if (logalot || verbose) {
248
+ console.log(`${lc} console.dir(status)... (I: 87589e683c7be2067d3596a509deaf24)`);
249
+ console.dir(status);
250
+ } else {
251
+ let statusCode = status.data?.statusCode;
252
+ if (statusCode === "already_synced") {
253
+ process.stdout.write('.');
254
+ } else {
255
+ console.log(statusCode);
256
+ }
257
+ }
258
+ },
259
+ complete: async () => {
260
+ // debugger; // syncStatus$ fnobs complete before fnIncCompleteOrError
261
+ if (logalot || verbose) {
262
+ console.log(`${lc}[saga complete] triggered for saga (${saga.spaceId}, ${saga.multiSpaceOpId}). (I: 237a2d0db5944359b90335c83c9df60f)`);
263
+ }
264
+ await fnIncCompleteOrError(saga);
265
+ await subscription.unsubscribe();
266
+ },
267
+ error: async (err: string | Error | ErrorIbGib_V1) => {
268
+ // debugger; // syncStatus$ fnobs error before fnIncCompleteOrError
269
+ const errorMsg = extractErrorMsg(err);
270
+ sagaErrorMsgs.push(errorMsg);
271
+ if (errorMsg.includes('EAI_AGAIN')) {
272
+ console.error(`this error may be because of no internet connection. ${errorMsg}`)
273
+ }
274
+ console.error(`${lc}[error] err: ${errorMsg} (E: fb1b0af934354cc6b41cfe455d1ba12d)`);
275
+ sagaErrorCounter++;
276
+ await fnIncCompleteOrError(saga);
277
+ await subscription.unsubscribe();
278
+ // sagaCompleteOrErrorCounter++;
279
+ }
280
+ }));
281
+ }
282
+ await allSagasCompletePromise;
283
+ return sagas;
284
+ } catch (error) {
285
+ console.error(`${lc} ${extractErrorMsg(error)}`);
286
+ throw error;
287
+ } finally {
288
+ if (logalot || verbose) { console.log(`${lc} complete.`); }
289
+ }
290
+ }