quilltap 4.15.0-dev.2 → 4.15.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.
@@ -1,13 +1,12 @@
1
1
  'use strict';
2
2
 
3
- const fs = require('fs');
4
- const path = require('path');
5
3
  const {
6
4
  resolveDataDirAndPassphrase,
7
5
  printDefaultInstanceHint,
8
6
  openMainDb,
9
7
  loadDbKey,
10
8
  } = require('./db-helpers');
9
+ const { extractMigrationId, loadMigrationList } = require('./migrations-source');
11
10
 
12
11
  // ---------- argument parsing ----------
13
12
 
@@ -41,84 +40,6 @@ function parseFlags(args) {
41
40
  return { flags, positional };
42
41
  }
43
42
 
44
- // ---------- migration list parsing ----------
45
-
46
- /**
47
- * A migration's id is either a string literal on the object (`id: 'foo-v1'`)
48
- * or a module constant (`id: MIGRATION_ID` beside `const MIGRATION_ID = 'foo-v1'`).
49
- * Missing the constant form drops those migrations from "source", which then
50
- * report as retired on every instance that ran them.
51
- */
52
- function extractMigrationId(fileContent) {
53
- const constMatch = fileContent.match(/^\s*const\s+MIGRATION_ID\s*(?::\s*\w+\s*)?=\s*['"]([^'"]+)['"]/m);
54
- if (constMatch && /^\s*id:\s*MIGRATION_ID\b/m.test(fileContent)) return constMatch[1];
55
- const literalMatch = fileContent.match(/^\s*id:\s*['"]([^'"]+)['"]/m);
56
- return literalMatch ? literalMatch[1] : null;
57
- }
58
-
59
- // Reads the registered migration list from migrations/scripts/index.ts and resolves each
60
- // entry's real `id:` value by reading the imported source file. Heuristic camelCase→kebab
61
- // conversion is unreliable (some migrations omit `-v1`, others use `-v2`), so we always
62
- // trust the file.
63
- function extractMigrationsFromSource() {
64
- const scriptsDir = path.join(__dirname, '../../..', 'migrations/scripts');
65
- const indexPath = path.join(scriptsDir, 'index.ts');
66
- if (!fs.existsSync(indexPath)) {
67
- return [];
68
- }
69
- const indexContent = fs.readFileSync(indexPath, 'utf-8');
70
-
71
- // Pass 1: build name → { filePath, comment } from imports and preceding comments.
72
- const lines = indexContent.split('\n');
73
- const importInfo = {};
74
- let lastComment = '';
75
- for (const line of lines) {
76
- if (line.match(/^\s*\/\/\s+/)) {
77
- lastComment = line.replace(/^\s*\/\/\s+/, '').trim();
78
- continue;
79
- }
80
- const importMatch = line.match(/import\s+\{\s*(\w+)\s*\}\s+from\s+['"]\.\/([^'"]+)['"]/);
81
- if (importMatch) {
82
- const [, name, filePath] = importMatch;
83
- importInfo[name] = { filePath, comment: lastComment || '' };
84
- lastComment = '';
85
- } else if (line.trim() && !line.match(/^\s*\/\//) && !line.includes('import type')) {
86
- // Reset comment if we hit a non-comment non-import line
87
- if (!line.match(/^\s*$/)) lastComment = '';
88
- }
89
- }
90
-
91
- // Pass 2: find the active migrations array section and collect names in order.
92
- const arrayStart = indexContent.indexOf('export const migrations: Migration[] = [');
93
- if (arrayStart === -1) return [];
94
- const arrayEnd = indexContent.indexOf('];', arrayStart);
95
- if (arrayEnd === -1) return [];
96
- const arrayContent = indexContent.substring(arrayStart, arrayEnd);
97
-
98
- const activeNames = arrayContent.match(/\b([a-zA-Z]\w*Migration)\b/g) || [];
99
- const seen = new Set();
100
-
101
- // Pass 3: for each active name, read the corresponding file and extract the real `id:`.
102
- const migrations = [];
103
- for (const name of activeNames) {
104
- if (seen.has(name)) continue;
105
- seen.add(name);
106
- const info = importInfo[name];
107
- if (!info) continue;
108
- const filePath = path.join(scriptsDir, info.filePath + '.ts');
109
- if (!fs.existsSync(filePath)) continue;
110
- const fileContent = fs.readFileSync(filePath, 'utf-8');
111
- const id = extractMigrationId(fileContent);
112
- if (!id) continue;
113
- migrations.push({
114
- id,
115
- description: info.comment || id,
116
- });
117
- }
118
-
119
- return migrations;
120
- }
121
-
122
43
  // ---------- help text ----------
123
44
 
124
45
  function printHelp() {
@@ -178,7 +99,7 @@ async function handleStatus(flags) {
178
99
  }
179
100
 
180
101
  const applied = getAppliedMigrations(db);
181
- const source = extractMigrationsFromSource();
102
+ const source = loadMigrationList();
182
103
 
183
104
  const appliedIds = new Set(applied.map(m => m.id));
184
105
  const sourceIds = new Set(source.map(m => m.id));
@@ -227,7 +148,7 @@ async function handlePending(flags) {
227
148
  }
228
149
 
229
150
  const applied = getAppliedMigrations(db);
230
- const source = extractMigrationsFromSource();
151
+ const source = loadMigrationList();
231
152
 
232
153
  const appliedIds = new Set(applied.map(m => m.id));
233
154
  const pending = source.filter(m => !appliedIds.has(m.id));
@@ -265,7 +186,7 @@ async function handleRun(flags) {
265
186
  }
266
187
 
267
188
  const applied = getAppliedMigrations(db);
268
- const source = extractMigrationsFromSource();
189
+ const source = loadMigrationList();
269
190
 
270
191
  const appliedIds = new Set(applied.map(m => m.id));
271
192
  const pending = source.filter(m => !appliedIds.has(m.id));
@@ -0,0 +1,109 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * The list of migrations this version of Quilltap contains, for
5
+ * `quilltap migrations`.
6
+ *
7
+ * In a source checkout the list is read live from migrations/scripts/. The npm
8
+ * package ships only bin/ and lib/, so an installed CLI has no such folder; it
9
+ * reads migrations.generated.js instead, which scripts/build-cli-migrations.mjs
10
+ * writes from the same parser (issue #362). `npm run lint` fails while that
11
+ * file is stale.
12
+ */
13
+
14
+ const fs = require('fs');
15
+ const path = require('path');
16
+
17
+ const SOURCE_SCRIPTS_DIR = path.join(__dirname, '../../..', 'migrations/scripts');
18
+
19
+ /**
20
+ * A migration's id is either a string literal on the object (`id: 'foo-v1'`)
21
+ * or a module constant (`id: MIGRATION_ID` beside `const MIGRATION_ID = 'foo-v1'`).
22
+ * Missing the constant form drops those migrations from "source", which then
23
+ * report as retired on every instance that ran them.
24
+ */
25
+ function extractMigrationId(fileContent) {
26
+ const constMatch = fileContent.match(/^\s*const\s+MIGRATION_ID\s*(?::\s*\w+\s*)?=\s*['"]([^'"]+)['"]/m);
27
+ if (constMatch && /^\s*id:\s*MIGRATION_ID\b/m.test(fileContent)) return constMatch[1];
28
+ const literalMatch = fileContent.match(/^\s*id:\s*['"]([^'"]+)['"]/m);
29
+ return literalMatch ? literalMatch[1] : null;
30
+ }
31
+
32
+ // Reads the registered migration list from migrations/scripts/index.ts and resolves each
33
+ // entry's real `id:` value by reading the imported source file. Heuristic camelCase→kebab
34
+ // conversion is unreliable (some migrations omit `-v1`, others use `-v2`), so we always
35
+ // trust the file.
36
+ function extractMigrationsFromSource(scriptsDir = SOURCE_SCRIPTS_DIR) {
37
+ const indexPath = path.join(scriptsDir, 'index.ts');
38
+ if (!fs.existsSync(indexPath)) {
39
+ return [];
40
+ }
41
+ const indexContent = fs.readFileSync(indexPath, 'utf-8');
42
+
43
+ // Pass 1: build name → { filePath, comment } from imports and preceding comments.
44
+ const lines = indexContent.split('\n');
45
+ const importInfo = {};
46
+ let lastComment = '';
47
+ for (const line of lines) {
48
+ if (line.match(/^\s*\/\/\s+/)) {
49
+ lastComment = line.replace(/^\s*\/\/\s+/, '').trim();
50
+ continue;
51
+ }
52
+ const importMatch = line.match(/import\s+\{\s*(\w+)\s*\}\s+from\s+['"]\.\/([^'"]+)['"]/);
53
+ if (importMatch) {
54
+ const [, name, filePath] = importMatch;
55
+ importInfo[name] = { filePath, comment: lastComment || '' };
56
+ lastComment = '';
57
+ } else if (line.trim() && !line.match(/^\s*\/\//) && !line.includes('import type')) {
58
+ // Reset comment if we hit a non-comment non-import line
59
+ if (!line.match(/^\s*$/)) lastComment = '';
60
+ }
61
+ }
62
+
63
+ // Pass 2: find the active migrations array section and collect names in order.
64
+ const arrayStart = indexContent.indexOf('export const migrations: Migration[] = [');
65
+ if (arrayStart === -1) return [];
66
+ const arrayEnd = indexContent.indexOf('];', arrayStart);
67
+ if (arrayEnd === -1) return [];
68
+ const arrayContent = indexContent.substring(arrayStart, arrayEnd);
69
+
70
+ const activeNames = arrayContent.match(/\b([a-zA-Z]\w*Migration)\b/g) || [];
71
+ const seen = new Set();
72
+
73
+ // Pass 3: for each active name, read the corresponding file and extract the real `id:`.
74
+ const migrations = [];
75
+ for (const name of activeNames) {
76
+ if (seen.has(name)) continue;
77
+ seen.add(name);
78
+ const info = importInfo[name];
79
+ if (!info) continue;
80
+ const filePath = path.join(scriptsDir, info.filePath + '.ts');
81
+ if (!fs.existsSync(filePath)) continue;
82
+ const fileContent = fs.readFileSync(filePath, 'utf-8');
83
+ const id = extractMigrationId(fileContent);
84
+ if (!id) continue;
85
+ migrations.push({
86
+ id,
87
+ description: info.comment || id,
88
+ });
89
+ }
90
+
91
+ return migrations;
92
+ }
93
+
94
+ /**
95
+ * The migration list: live from a source checkout when there is one, otherwise
96
+ * the generated list shipped in the package.
97
+ */
98
+ function loadMigrationList(scriptsDir = SOURCE_SCRIPTS_DIR) {
99
+ if (fs.existsSync(path.join(scriptsDir, 'index.ts'))) {
100
+ return extractMigrationsFromSource(scriptsDir);
101
+ }
102
+ return require('./migrations.generated.js');
103
+ }
104
+
105
+ module.exports = {
106
+ extractMigrationId,
107
+ extractMigrationsFromSource,
108
+ loadMigrationList,
109
+ };