@naseej/blox 0.1.29 → 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.
- package/bin/run.ts +9 -5
- package/dist/run.js +621 -326
- package/package.json +2 -1
- package/readme.md +13 -19
package/bin/run.ts
CHANGED
|
@@ -3,9 +3,13 @@ import { cli } from '@/shared/command';
|
|
|
3
3
|
import { version } from '../package.json';
|
|
4
4
|
import { commands } from '../src';
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
cli.
|
|
8
|
-
cli.option('-
|
|
6
|
+
async function main() {
|
|
7
|
+
cli.name('blox').version(version);
|
|
8
|
+
cli.option('-d, --debug', 'work in debug mode');
|
|
9
|
+
cli.option('-m, --minimal', 'work in minimal mode');
|
|
9
10
|
|
|
10
|
-
commands.forEach((cmd) => cli.addCommand(cmd));
|
|
11
|
-
cli.parse();
|
|
11
|
+
commands.forEach((cmd) => cli.addCommand(cmd));
|
|
12
|
+
cli.parse();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
main();
|
package/dist/run.js
CHANGED
|
@@ -31,47 +31,63 @@ var option = (flags, description) => {
|
|
|
31
31
|
};
|
|
32
32
|
|
|
33
33
|
// package.json
|
|
34
|
-
var version = "0.
|
|
34
|
+
var version = "1.0.0";
|
|
35
35
|
|
|
36
36
|
// src/commands/add/index.ts
|
|
37
37
|
var import_fs_extra7 = __toESM(require("fs-extra"));
|
|
38
38
|
|
|
39
|
-
// src/
|
|
40
|
-
var
|
|
39
|
+
// src/config/runtime.ts
|
|
40
|
+
var isDebugMode = () => !!import_extra_typings2.program.opts()?.debug;
|
|
41
|
+
var isMinimalMode = () => !!import_extra_typings2.program.opts()?.minimal;
|
|
42
|
+
|
|
43
|
+
// src/migrations/index.ts
|
|
44
|
+
var import_node_path4 = __toESM(require("path"));
|
|
45
|
+
var import_fs_extra3 = __toESM(require("fs-extra"));
|
|
41
46
|
|
|
42
47
|
// src/config/index.ts
|
|
43
48
|
var defaultConfig = {
|
|
44
|
-
syncFrequency: "1d",
|
|
45
49
|
registries: ["https://tfs.aas.com.sa/Medad/BLOX/_git/default-registry"]
|
|
46
50
|
};
|
|
47
|
-
var
|
|
51
|
+
var bloxFilePath = ".blox";
|
|
48
52
|
var blocksDir = "blocks";
|
|
49
53
|
|
|
50
|
-
// src/
|
|
51
|
-
var
|
|
52
|
-
var
|
|
53
|
-
var import_fs_extra4 = __toESM(require("fs-extra"));
|
|
54
|
-
var import_klaw = __toESM(require("klaw"));
|
|
55
|
-
var import_merge = __toESM(require("lodash/merge"));
|
|
56
|
-
var import_ms = __toESM(require("ms"));
|
|
57
|
-
|
|
58
|
-
// src/config/runtime.ts
|
|
59
|
-
var isDebugMode = () => !!import_extra_typings2.program.opts()?.debug;
|
|
60
|
-
var isMinimalMode = () => !!import_extra_typings2.program.opts()?.minimal;
|
|
54
|
+
// src/migrations/utils.ts
|
|
55
|
+
var import_node_path3 = __toESM(require("path"));
|
|
56
|
+
var import_fs_extra2 = __toESM(require("fs-extra"));
|
|
61
57
|
|
|
62
|
-
// src/
|
|
63
|
-
var
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
58
|
+
// src/migrations/list/v1.ts
|
|
59
|
+
var v1 = {
|
|
60
|
+
dependencies: {
|
|
61
|
+
packageJson: {
|
|
62
|
+
location: "package.json"
|
|
63
|
+
},
|
|
64
|
+
bloxFile: {
|
|
65
|
+
location: ".blox"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
run: async ({ packageJson, bloxFile: oldBloxFile }) => {
|
|
69
|
+
const bloxFile = {
|
|
70
|
+
schemaVersion: 1,
|
|
71
|
+
definition: packageJson.blox,
|
|
72
|
+
checkpoints: {}
|
|
73
|
+
};
|
|
74
|
+
if (oldBloxFile.commit) {
|
|
75
|
+
bloxFile.checkpoints[bloxFile.definition.name] = { commit: oldBloxFile.commit };
|
|
76
|
+
}
|
|
77
|
+
Object.entries(oldBloxFile.bases || {}).forEach(([k, v]) => {
|
|
78
|
+
bloxFile.checkpoints[k] = v;
|
|
79
|
+
});
|
|
80
|
+
delete packageJson.blox;
|
|
81
|
+
return { bloxFile, packageJson };
|
|
69
82
|
}
|
|
70
83
|
};
|
|
71
84
|
|
|
85
|
+
// src/migrations/list/index.ts
|
|
86
|
+
var migrationsList = [v1];
|
|
87
|
+
|
|
72
88
|
// src/services/git.ts
|
|
73
89
|
var import_node_path2 = __toESM(require("path"));
|
|
74
|
-
var
|
|
90
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
75
91
|
|
|
76
92
|
// src/services/path.ts
|
|
77
93
|
var import_node_path = __toESM(require("path"));
|
|
@@ -109,6 +125,9 @@ var logWarningSection = (title, content) => {
|
|
|
109
125
|
content && console2.log(content);
|
|
110
126
|
console2.log(line + "\n");
|
|
111
127
|
};
|
|
128
|
+
var normalizeLogMessage = (content) => {
|
|
129
|
+
return content.split("\n").map((l) => l.trim()).filter(Boolean).join("\n");
|
|
130
|
+
};
|
|
112
131
|
|
|
113
132
|
// src/services/shell.ts
|
|
114
133
|
var $ = async (bin, options = [], { cwd, onStdout } = {}) => {
|
|
@@ -156,6 +175,9 @@ var clone = (repoUrl, options, shellOptions = {}) => {
|
|
|
156
175
|
...shellOptions
|
|
157
176
|
});
|
|
158
177
|
};
|
|
178
|
+
var resetToCommit = async (commit2, cwd = ".") => {
|
|
179
|
+
await $("git", ["reset --hard", `${commit2}`], { cwd });
|
|
180
|
+
};
|
|
159
181
|
var updateRemote = async (name, source, shellOptions = {}) => {
|
|
160
182
|
debug("updateRemote");
|
|
161
183
|
debug({ name, source });
|
|
@@ -172,10 +194,10 @@ var updateRemote = async (name, source, shellOptions = {}) => {
|
|
|
172
194
|
});
|
|
173
195
|
await $("git", ["fetch", name], { ...defaultShellOptions, ...shellOptions });
|
|
174
196
|
};
|
|
175
|
-
var
|
|
197
|
+
var readJsonFromCommit = async (commit2, file) => {
|
|
176
198
|
try {
|
|
177
199
|
let content = "";
|
|
178
|
-
await $("git", ["show", `${
|
|
200
|
+
await $("git", ["show", `${commit2}:${file}`], {
|
|
179
201
|
onStdout: (text2) => {
|
|
180
202
|
content = text2;
|
|
181
203
|
}
|
|
@@ -185,6 +207,9 @@ var readJsonFromRemote = async (remote, file) => {
|
|
|
185
207
|
return {};
|
|
186
208
|
}
|
|
187
209
|
};
|
|
210
|
+
var readJsonFromRemote = async (remote, file, commit2) => {
|
|
211
|
+
return readJsonFromCommit(commit2 || `${remote}/main`, file);
|
|
212
|
+
};
|
|
188
213
|
var isCommitOlder = async (a, b) => {
|
|
189
214
|
try {
|
|
190
215
|
await $("git", ["merge-base --is-ancestor", a, b]);
|
|
@@ -199,19 +224,19 @@ var update = (shellOptions = {}) => {
|
|
|
199
224
|
var getLatestCommitFromRemote = async (remote) => {
|
|
200
225
|
return getLatestCommit(`${remote}/main`);
|
|
201
226
|
};
|
|
202
|
-
var getLatestCommitFromPath = async (
|
|
203
|
-
return getLatestCommit("HEAD",
|
|
227
|
+
var getLatestCommitFromPath = async (path10) => {
|
|
228
|
+
return getLatestCommit("HEAD", path10);
|
|
204
229
|
};
|
|
205
230
|
var getLatestCommit = async (rev, cwd = ".") => {
|
|
206
231
|
debug(`Get latest commit for rev: ${rev} and cwd: ${cwd}`);
|
|
207
|
-
let
|
|
232
|
+
let commit2 = "";
|
|
208
233
|
await $("git", ["rev-parse", rev], {
|
|
209
234
|
cwd,
|
|
210
235
|
onStdout: (text2) => {
|
|
211
|
-
|
|
236
|
+
commit2 = text2;
|
|
212
237
|
}
|
|
213
238
|
});
|
|
214
|
-
return
|
|
239
|
+
return commit2.replace(/\n/g, "");
|
|
215
240
|
};
|
|
216
241
|
var createPatch = async (fromCommit, toCommit, outputPath, ignore = []) => {
|
|
217
242
|
return $("git", [
|
|
@@ -220,6 +245,7 @@ var createPatch = async (fromCommit, toCommit, outputPath, ignore = []) => {
|
|
|
220
245
|
toCommit,
|
|
221
246
|
">",
|
|
222
247
|
outputPath,
|
|
248
|
+
"--",
|
|
223
249
|
...ignore.map((file) => `':!${file}'`)
|
|
224
250
|
]);
|
|
225
251
|
};
|
|
@@ -243,7 +269,7 @@ var applyPatch = (patch, cwd = ".") => {
|
|
|
243
269
|
};
|
|
244
270
|
var unInit = (cwd) => {
|
|
245
271
|
debug("UnInit");
|
|
246
|
-
return
|
|
272
|
+
return import_fs_extra.default.remove(import_node_path2.default.join(cwd, ".git"));
|
|
247
273
|
};
|
|
248
274
|
var init = async (cwd) => {
|
|
249
275
|
debug("Init");
|
|
@@ -252,6 +278,29 @@ var init = async (cwd) => {
|
|
|
252
278
|
await $("git", ["commit", "-m", "init"], { cwd });
|
|
253
279
|
await $("git", ["branch", "-m", "main"], { cwd });
|
|
254
280
|
};
|
|
281
|
+
var add = async (toAdd) => {
|
|
282
|
+
debug(`Git Add: ${toAdd}`);
|
|
283
|
+
await $("git", ["add", toAdd]);
|
|
284
|
+
};
|
|
285
|
+
var commit = async (msg) => {
|
|
286
|
+
debug(`Git commit: ${msg}`);
|
|
287
|
+
await $("git", ["commit", "-m", `"${msg}"`]);
|
|
288
|
+
};
|
|
289
|
+
var rollback = async () => {
|
|
290
|
+
debug("Rollback");
|
|
291
|
+
await $("git", ["add", "."]);
|
|
292
|
+
await $("git", ["reset", "--hard"]);
|
|
293
|
+
};
|
|
294
|
+
var hasUncommittedChanges = async (cwd = ".") => {
|
|
295
|
+
let status = "";
|
|
296
|
+
await $("git", ["status", "--porcelain"], {
|
|
297
|
+
cwd,
|
|
298
|
+
onStdout: (text2) => {
|
|
299
|
+
status += text2;
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
return status.trim().length > 0;
|
|
303
|
+
};
|
|
255
304
|
var git = {
|
|
256
305
|
clone,
|
|
257
306
|
update,
|
|
@@ -263,21 +312,152 @@ var git = {
|
|
|
263
312
|
getChangesLog,
|
|
264
313
|
applyPatch,
|
|
265
314
|
updateRemote,
|
|
315
|
+
readJsonFromCommit,
|
|
266
316
|
readJsonFromRemote,
|
|
267
|
-
isCommitOlder
|
|
317
|
+
isCommitOlder,
|
|
318
|
+
rollback,
|
|
319
|
+
add,
|
|
320
|
+
resetToCommit,
|
|
321
|
+
commit,
|
|
322
|
+
hasUncommittedChanges
|
|
268
323
|
};
|
|
269
324
|
|
|
270
|
-
// src/shared/
|
|
325
|
+
// src/shared/prompts/index.ts
|
|
271
326
|
var import_prompts3 = require("@clack/prompts");
|
|
327
|
+
var checkIfCanceled = (value) => {
|
|
328
|
+
if ((0, import_prompts3.isCancel)(value)) {
|
|
329
|
+
(0, import_prompts3.cancel)();
|
|
330
|
+
process.exit(0);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
var error = (error2, exit = true) => {
|
|
334
|
+
import_prompts3.log.error(
|
|
335
|
+
fmt.red(
|
|
336
|
+
typeof error2 === "string" ? error2 : error2?.shortMessage || error2?.message || JSON.stringify(error2)
|
|
337
|
+
)
|
|
338
|
+
);
|
|
339
|
+
exit && process.exit(-1);
|
|
340
|
+
};
|
|
341
|
+
var select = async (opts) => {
|
|
342
|
+
const value = await (0, import_prompts3.select)(opts);
|
|
343
|
+
checkIfCanceled(value);
|
|
344
|
+
return value;
|
|
345
|
+
};
|
|
346
|
+
var text = async (opts) => {
|
|
347
|
+
const value = await (0, import_prompts3.text)(opts);
|
|
348
|
+
checkIfCanceled(value);
|
|
349
|
+
return value;
|
|
350
|
+
};
|
|
351
|
+
var textList = async (opts) => {
|
|
352
|
+
opts.message = `${opts.message} ${fmt.gray('(separated with ",")')}`;
|
|
353
|
+
const value = await (0, import_prompts3.text)(opts);
|
|
354
|
+
checkIfCanceled(value);
|
|
355
|
+
opts.validate?.(value);
|
|
356
|
+
return value.split(",");
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// src/migrations/utils.ts
|
|
360
|
+
function getApplicableMigrations(schemaVersion = 0) {
|
|
361
|
+
if (schemaVersion > migrationsList.length) {
|
|
362
|
+
error(
|
|
363
|
+
normalizeLogMessage(
|
|
364
|
+
`Used blox schema version: ${schemaVersion} is newer than cli version: ${migrationsList.length},
|
|
365
|
+
please update cli first or use older version`
|
|
366
|
+
)
|
|
367
|
+
);
|
|
368
|
+
return [];
|
|
369
|
+
}
|
|
370
|
+
return migrationsList.slice(schemaVersion);
|
|
371
|
+
}
|
|
372
|
+
async function migrate(name, { getPath = (f) => f, read, write }) {
|
|
373
|
+
let remoteBloxFile;
|
|
374
|
+
try {
|
|
375
|
+
remoteBloxFile = await read(getPath(".blox"));
|
|
376
|
+
} catch {
|
|
377
|
+
remoteBloxFile = {};
|
|
378
|
+
}
|
|
379
|
+
const migrations = getApplicableMigrations(remoteBloxFile.schemaVersion);
|
|
380
|
+
if (migrations.length) {
|
|
381
|
+
import_prompts2.log.warn(fmt.yellow(`Migrations found for ${name}, will migrate to latest schema`));
|
|
382
|
+
}
|
|
383
|
+
const currentRev = {
|
|
384
|
+
".blox": remoteBloxFile
|
|
385
|
+
};
|
|
386
|
+
for (const migration of migrations) {
|
|
387
|
+
const deps = {};
|
|
388
|
+
for (const [name2, { location, defaultValue = {} }] of Object.entries(
|
|
389
|
+
migration.dependencies
|
|
390
|
+
)) {
|
|
391
|
+
try {
|
|
392
|
+
const filePath = getPath(location);
|
|
393
|
+
if (currentRev[filePath] !== void 0) {
|
|
394
|
+
deps[name2] = currentRev[filePath];
|
|
395
|
+
} else {
|
|
396
|
+
deps[name2] = await read(filePath);
|
|
397
|
+
}
|
|
398
|
+
} catch {
|
|
399
|
+
}
|
|
400
|
+
if (deps[name2] === void 0) {
|
|
401
|
+
deps[name2] = defaultValue;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const output = await migration.run(deps);
|
|
405
|
+
for (const [name2, value] of Object.entries(output || {})) {
|
|
406
|
+
const { location } = migration.dependencies[name2] || {};
|
|
407
|
+
location && (currentRev[location] = value);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (write) {
|
|
411
|
+
for (const [location, value] of Object.entries(currentRev)) {
|
|
412
|
+
await write(getPath(location), JSON.stringify(value, null, 2));
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return { output: currentRev, migrationsRan: migrations.length };
|
|
416
|
+
}
|
|
417
|
+
function migrateOnLocal(name, cwd = ".") {
|
|
418
|
+
return migrate(name, {
|
|
419
|
+
getPath: (file) => import_node_path3.default.join(cwd, file),
|
|
420
|
+
read: import_fs_extra2.default.readJson,
|
|
421
|
+
write: import_fs_extra2.default.writeFile
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
function migrateOnRemote(name, commit2) {
|
|
425
|
+
return migrate(name, {
|
|
426
|
+
read: (file) => git.readJsonFromCommit(commit2, file)
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// src/migrations/index.ts
|
|
431
|
+
async function migrateAll(autoCommit) {
|
|
432
|
+
let migrationsRan = (await migrateOnLocal("app")).migrationsRan;
|
|
433
|
+
const blocks = import_fs_extra3.default.existsSync(blocksDir) ? import_fs_extra3.default.readdirSync(blocksDir) : [];
|
|
434
|
+
for (const block of blocks) {
|
|
435
|
+
migrationsRan += (await migrateOnLocal(block, import_node_path4.default.join(blocksDir, block))).migrationsRan;
|
|
436
|
+
}
|
|
437
|
+
if (autoCommit && migrationsRan) {
|
|
438
|
+
import_prompts2.log.warn(fmt.yellow(`Migrations done and will be commited`));
|
|
439
|
+
await git.add(".");
|
|
440
|
+
await git.commit("chore: migrate blox schema");
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// src/services/registry.ts
|
|
445
|
+
var import_node_path6 = __toESM(require("path"));
|
|
446
|
+
var import_fs_extra5 = __toESM(require("fs-extra"));
|
|
447
|
+
var import_klaw = __toESM(require("klaw"));
|
|
448
|
+
var import_merge = __toESM(require("lodash/merge"));
|
|
449
|
+
|
|
450
|
+
// src/shared/spinner.ts
|
|
451
|
+
var import_prompts5 = require("@clack/prompts");
|
|
272
452
|
var spinner = () => {
|
|
273
|
-
const s = (0,
|
|
453
|
+
const s = (0, import_prompts5.spinner)();
|
|
274
454
|
const minimal = isMinimalMode() || isDebugMode();
|
|
275
455
|
return {
|
|
276
456
|
start: (msg) => {
|
|
277
457
|
minimal ? import_prompts2.log.step(`${msg || "start"}...`) : s.start(msg);
|
|
278
458
|
},
|
|
279
|
-
stop: (msg, code) => {
|
|
280
|
-
minimal ? code ? import_prompts2.log.error(msg
|
|
459
|
+
stop: (msg = "error", code) => {
|
|
460
|
+
minimal ? code ? import_prompts2.log.error(msg) : import_prompts2.log.step(msg || "success") : s.stop(code ? fmt.red(msg) : fmt.green(msg), code);
|
|
281
461
|
},
|
|
282
462
|
message: (msg) => {
|
|
283
463
|
minimal ? import_prompts2.log.message(msg) : s.message(msg);
|
|
@@ -293,70 +473,53 @@ async function runTask(message, task) {
|
|
|
293
473
|
await (typeof task === "function" ? task() : task);
|
|
294
474
|
taskSpinner.stop(endMessage);
|
|
295
475
|
} catch (e) {
|
|
296
|
-
taskSpinner.stop(`Failed: ${
|
|
476
|
+
taskSpinner.stop(`Failed: ${startMessage}`, 1);
|
|
297
477
|
throw e;
|
|
298
478
|
}
|
|
299
479
|
}
|
|
300
480
|
|
|
301
481
|
// src/services/config.ts
|
|
302
|
-
var
|
|
482
|
+
var import_node_path5 = __toESM(require("path"));
|
|
303
483
|
var import_env_paths = __toESM(require("env-paths"));
|
|
304
|
-
var
|
|
484
|
+
var import_fs_extra4 = __toESM(require("fs-extra"));
|
|
305
485
|
var appPaths = (0, import_env_paths.default)("blox", { suffix: "" });
|
|
306
|
-
var configFilePath =
|
|
486
|
+
var configFilePath = import_node_path5.default.join(appPaths.config, "config.json");
|
|
307
487
|
var getLocalConfig = () => {
|
|
308
488
|
try {
|
|
309
|
-
return
|
|
489
|
+
return import_fs_extra4.default.readJSONSync(configFilePath);
|
|
310
490
|
} catch {
|
|
311
491
|
return defaultConfig;
|
|
312
492
|
}
|
|
313
493
|
};
|
|
314
494
|
var updateLocalConfig = (config) => {
|
|
315
495
|
debug("Update local config");
|
|
316
|
-
|
|
496
|
+
import_fs_extra4.default.ensureFileSync(configFilePath);
|
|
317
497
|
const current = getLocalConfig();
|
|
318
|
-
|
|
498
|
+
import_fs_extra4.default.writeJsonSync(configFilePath, { ...current, ...config });
|
|
319
499
|
};
|
|
320
500
|
|
|
321
501
|
// src/services/registry.ts
|
|
322
|
-
var registriesDataPath =
|
|
323
|
-
var
|
|
324
|
-
var syncRegistries = async (force, logProgress = true) => {
|
|
502
|
+
var registriesDataPath = import_node_path6.default.join(appPaths.data, "registries");
|
|
503
|
+
var syncRegistries = async () => {
|
|
325
504
|
debug("Sync registries");
|
|
326
|
-
|
|
327
|
-
import_fs_extra4.default.ensureDirSync(registriesDataPath);
|
|
505
|
+
import_fs_extra5.default.ensureDirSync(registriesDataPath);
|
|
328
506
|
const config = getLocalConfig();
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
try {
|
|
507
|
+
return await runTask("Update registries", async () => {
|
|
508
|
+
await Promise.all(
|
|
509
|
+
config.registries.map(async (registry) => {
|
|
510
|
+
debug(`update registry: ${registry}`);
|
|
334
511
|
const repoName = registry.split("/").pop() || "";
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
if (!registriesStat[repoName]) {
|
|
338
|
-
registriesStat[repoName] = {};
|
|
339
|
-
}
|
|
340
|
-
const now = (0, import_dayjs.default)();
|
|
341
|
-
const lastUpdated = registriesStat[repoName].lastUpdated;
|
|
342
|
-
const shouldUpdate = !lastUpdated || now.isAfter((0, import_dayjs.default)(lastUpdated).add((0, import_ms.default)(config.syncFrequency)));
|
|
343
|
-
if (!import_fs_extra4.default.existsSync(repoDir)) {
|
|
512
|
+
const repoDir = import_node_path6.default.join(registriesDataPath, repoName);
|
|
513
|
+
if (!import_fs_extra5.default.existsSync(repoDir)) {
|
|
344
514
|
await git.clone(registry, "--depth 1", { cwd: registriesDataPath });
|
|
345
|
-
|
|
346
|
-
} else if (force || shouldUpdate) {
|
|
515
|
+
} else if (import_fs_extra5.default.existsSync(import_node_path6.default.join(repoDir, ".git"))) {
|
|
347
516
|
await git.update({ cwd: repoDir });
|
|
348
|
-
registriesStat[repoName].lastUpdated = now.format();
|
|
349
517
|
} else {
|
|
350
|
-
|
|
518
|
+
debug(`local registry`);
|
|
351
519
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
task.stop(`Updating registry: ${registry} failed`, 1);
|
|
356
|
-
throw e;
|
|
357
|
-
}
|
|
358
|
-
})
|
|
359
|
-
);
|
|
520
|
+
})
|
|
521
|
+
);
|
|
522
|
+
});
|
|
360
523
|
};
|
|
361
524
|
var getAvailableTemplates = async () => {
|
|
362
525
|
debug("Get available templates");
|
|
@@ -365,25 +528,31 @@ var getAvailableTemplates = async () => {
|
|
|
365
528
|
filter: (item) => !item.includes(".git")
|
|
366
529
|
})) {
|
|
367
530
|
if (file.path.endsWith(".json")) {
|
|
368
|
-
const data =
|
|
369
|
-
Object.
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
if (templates[templ]?.source === source) {
|
|
373
|
-
templates[templ] = (0, import_merge.default)(templates[templ], template);
|
|
374
|
-
} else {
|
|
375
|
-
templates[templ] = template;
|
|
531
|
+
const data = import_fs_extra5.default.readJSONSync(file.path);
|
|
532
|
+
Object.entries(data.templates || {}).forEach(([templateName, template]) => {
|
|
533
|
+
if (!template.source) {
|
|
534
|
+
template.source = `/template-${templateName}`;
|
|
376
535
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
536
|
+
if (template.source.startsWith("/")) {
|
|
537
|
+
template.source = data.baseUrl + template.source;
|
|
538
|
+
}
|
|
539
|
+
Object.entries(template.blocks || {}).forEach(([blockName, block]) => {
|
|
540
|
+
if (!block.source) {
|
|
541
|
+
block.source = `/block-${templateName}-${blockName}`;
|
|
542
|
+
}
|
|
543
|
+
if (block.source.startsWith("/")) {
|
|
544
|
+
block.source = data.baseUrl + block.source;
|
|
381
545
|
}
|
|
382
546
|
});
|
|
547
|
+
if (templates[templateName]) {
|
|
548
|
+
templates[templateName] = (0, import_merge.default)(templates[templateName], template);
|
|
549
|
+
} else {
|
|
550
|
+
templates[templateName] = template;
|
|
551
|
+
}
|
|
383
552
|
});
|
|
384
553
|
}
|
|
385
554
|
}
|
|
386
|
-
debug(templates);
|
|
555
|
+
debug({ templates });
|
|
387
556
|
return templates;
|
|
388
557
|
};
|
|
389
558
|
var getAvailableBlocks = async (templateInfo) => {
|
|
@@ -395,60 +564,80 @@ var getAvailableBlocks = async (templateInfo) => {
|
|
|
395
564
|
const baseTemplate = templates[base.name];
|
|
396
565
|
Object.entries(baseTemplate?.blocks || {}).forEach(([k, v]) => blocks[k] = v);
|
|
397
566
|
}
|
|
398
|
-
debug(blocks);
|
|
567
|
+
debug({ blocks });
|
|
399
568
|
return blocks;
|
|
400
569
|
};
|
|
570
|
+
var getAvailableVersions = async (id) => {
|
|
571
|
+
const registries = import_fs_extra5.default.readdirSync(registriesDataPath);
|
|
572
|
+
for (const registry of registries) {
|
|
573
|
+
const versionsFile = import_node_path6.default.join(registriesDataPath, registry, "versions", `${id}.json`);
|
|
574
|
+
if (import_fs_extra5.default.existsSync(versionsFile)) {
|
|
575
|
+
return import_fs_extra5.default.readJSONSync(versionsFile);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
};
|
|
401
579
|
|
|
402
580
|
// src/services/template.ts
|
|
403
|
-
var
|
|
581
|
+
var import_node_path7 = __toESM(require("path"));
|
|
404
582
|
var import_ejs = __toESM(require("ejs"));
|
|
405
|
-
var
|
|
583
|
+
var import_fs_extra6 = __toESM(require("fs-extra"));
|
|
406
584
|
var import_klaw2 = __toESM(require("klaw"));
|
|
407
585
|
async function replacePlaceholders(dir, context) {
|
|
408
586
|
debug("Replace placeholders");
|
|
409
587
|
for await (const file of (0, import_klaw2.default)(dir)) {
|
|
410
588
|
if (file.stats.isFile()) {
|
|
411
|
-
const template =
|
|
589
|
+
const template = import_fs_extra6.default.readFileSync(file.path, "utf-8");
|
|
412
590
|
const rendered = import_ejs.default.render(template, context);
|
|
413
|
-
|
|
591
|
+
import_fs_extra6.default.writeFileSync(file.path, rendered, "utf-8");
|
|
414
592
|
}
|
|
415
593
|
}
|
|
416
594
|
}
|
|
417
|
-
function
|
|
418
|
-
|
|
595
|
+
function getTemplateDef(cwd = ".") {
|
|
596
|
+
debug("Get template def");
|
|
597
|
+
const def = getBloxFile(cwd).definition;
|
|
598
|
+
debug(def);
|
|
599
|
+
return def;
|
|
419
600
|
}
|
|
420
|
-
function
|
|
421
|
-
debug("Get
|
|
422
|
-
const
|
|
423
|
-
debug(
|
|
424
|
-
return
|
|
601
|
+
function getBlockDef(cwd = ".") {
|
|
602
|
+
debug("Get block def");
|
|
603
|
+
const def = getBloxFile(cwd).definition;
|
|
604
|
+
def && debug(def);
|
|
605
|
+
return def;
|
|
425
606
|
}
|
|
426
|
-
function
|
|
607
|
+
function getBloxFile(cwd = ".") {
|
|
427
608
|
try {
|
|
428
|
-
|
|
609
|
+
const file = import_fs_extra6.default.readJSONSync(import_node_path7.default.join(cwd, bloxFilePath));
|
|
610
|
+
if (!file.checkpoints) {
|
|
611
|
+
file.checkpoints = {};
|
|
612
|
+
}
|
|
613
|
+
return file;
|
|
429
614
|
} catch {
|
|
430
615
|
return {};
|
|
431
616
|
}
|
|
432
617
|
}
|
|
433
|
-
function
|
|
618
|
+
function saveBloxFile(info, cwd = ".") {
|
|
434
619
|
debug("Save info");
|
|
435
|
-
|
|
620
|
+
import_fs_extra6.default.writeFileSync(import_node_path7.default.join(cwd, bloxFilePath), JSON.stringify(info, null, 2));
|
|
436
621
|
}
|
|
437
|
-
function
|
|
438
|
-
const info =
|
|
439
|
-
|
|
622
|
+
function updateCheckpoint(name, checkpoint, cwd = ".") {
|
|
623
|
+
const info = getBloxFile(cwd);
|
|
624
|
+
if (!info.checkpoints) {
|
|
625
|
+
info.checkpoints = {};
|
|
626
|
+
}
|
|
627
|
+
info.checkpoints[name] = checkpoint;
|
|
628
|
+
saveBloxFile(info, cwd);
|
|
440
629
|
}
|
|
441
630
|
function getUsedBlocks() {
|
|
442
631
|
debug("Get used blocks");
|
|
443
632
|
const blocks = [];
|
|
444
|
-
if (
|
|
445
|
-
|
|
633
|
+
if (import_fs_extra6.default.existsSync(blocksDir)) {
|
|
634
|
+
import_fs_extra6.default.readdirSync(blocksDir, { withFileTypes: true }).forEach((file) => {
|
|
446
635
|
if (!file.isDirectory()) {
|
|
447
636
|
return;
|
|
448
637
|
}
|
|
449
|
-
const
|
|
450
|
-
if (
|
|
451
|
-
blocks.push(
|
|
638
|
+
const blockDef = getBlockDef(import_node_path7.default.join(blocksDir, file.name));
|
|
639
|
+
if (blockDef) {
|
|
640
|
+
blocks.push(blockDef);
|
|
452
641
|
}
|
|
453
642
|
});
|
|
454
643
|
}
|
|
@@ -456,176 +645,129 @@ function getUsedBlocks() {
|
|
|
456
645
|
return blocks;
|
|
457
646
|
}
|
|
458
647
|
|
|
459
|
-
// src/services/
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
};
|
|
488
|
-
var textList = async (opts) => {
|
|
489
|
-
opts.message = `${opts.message} ${fmt.gray('(separated with ",")')}`;
|
|
490
|
-
const value = await (0, import_prompts4.text)(opts);
|
|
491
|
-
checkIfCanceled(value);
|
|
492
|
-
opts.validate?.(value);
|
|
493
|
-
return value.split(",");
|
|
494
|
-
};
|
|
495
|
-
|
|
496
|
-
// src/services/updater.ts
|
|
497
|
-
async function updateFromRemote(name, source = "", ignore = [], destPath = ".") {
|
|
498
|
-
debug("Updating from remote");
|
|
499
|
-
const basedOnInfo = getBasedOnInfo(destPath);
|
|
500
|
-
const remoteBloxFile = await git.readJsonFromRemote(name, ".blox");
|
|
501
|
-
for (const [baseName, baseInfo] of Object.entries(remoteBloxFile.bases || {})) {
|
|
502
|
-
const baseFrom = basedOnInfo.bases?.[baseName]?.commit;
|
|
503
|
-
const updateBase = () => {
|
|
504
|
-
if (!basedOnInfo.bases) {
|
|
505
|
-
basedOnInfo.bases = {};
|
|
506
|
-
}
|
|
507
|
-
basedOnInfo.bases = (0, import_merge2.default)(basedOnInfo.bases, { [baseName]: baseInfo });
|
|
508
|
-
};
|
|
509
|
-
if (!baseFrom) {
|
|
510
|
-
updateBase();
|
|
511
|
-
continue;
|
|
512
|
-
}
|
|
513
|
-
const baseTo = baseInfo.commit;
|
|
514
|
-
if (await git.isCommitOlder(baseFrom, baseTo)) {
|
|
515
|
-
updateBase();
|
|
516
|
-
} else {
|
|
517
|
-
logWarningSection(
|
|
518
|
-
`${baseName} is newer in project than template, make sure no breaking changes`
|
|
519
|
-
);
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
const fromBasedCommit = basedOnInfo.bases?.[name]?.commit;
|
|
523
|
-
const fromCommit = fromBasedCommit || basedOnInfo.commit;
|
|
524
|
-
if (!fromCommit) {
|
|
525
|
-
debug("Skip update, no fromCommit found");
|
|
526
|
-
return;
|
|
527
|
-
}
|
|
528
|
-
await git.updateRemote(name, source);
|
|
529
|
-
const toCommit = await git.getLatestCommitFromRemote(name);
|
|
530
|
-
debug({ fromCommit, toCommit });
|
|
531
|
-
if (fromCommit == toCommit) {
|
|
532
|
-
debug("No new version");
|
|
533
|
-
return;
|
|
648
|
+
// src/services/versions/checkCompatibility.ts
|
|
649
|
+
function processDependencyVersion(dep) {
|
|
650
|
+
if (!dep.version) dep.version = 0;
|
|
651
|
+
return dep;
|
|
652
|
+
}
|
|
653
|
+
function processTemplate(def) {
|
|
654
|
+
if (!def.version) def.version = 0;
|
|
655
|
+
Object.values(def.bases || {}).forEach(processDependencyVersion);
|
|
656
|
+
return def;
|
|
657
|
+
}
|
|
658
|
+
function processBases(info) {
|
|
659
|
+
if (!info || !info.checkpoints) return {};
|
|
660
|
+
Object.values(info.checkpoints).forEach((base) => {
|
|
661
|
+
if (!base.version) base.version = 0;
|
|
662
|
+
});
|
|
663
|
+
return info.checkpoints;
|
|
664
|
+
}
|
|
665
|
+
function processBlocks(defs) {
|
|
666
|
+
defs.forEach((def) => {
|
|
667
|
+
if (!def.version) def.version = 0;
|
|
668
|
+
def.dependencies?.forEach(processDependencyVersion);
|
|
669
|
+
def.templates?.forEach(processDependencyVersion);
|
|
670
|
+
});
|
|
671
|
+
return defs;
|
|
672
|
+
}
|
|
673
|
+
function ensureVersionOverlap(version2 = 0, range = 0, mismatchMessage) {
|
|
674
|
+
if (typeof range === "number") {
|
|
675
|
+
range = [range, range];
|
|
534
676
|
}
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
try {
|
|
546
|
-
await git.applyPatch(patchFilePath, destPath);
|
|
547
|
-
} catch (e) {
|
|
548
|
-
const errorMessage = `${e}`;
|
|
549
|
-
if (errorMessage.includes("error:")) {
|
|
550
|
-
await deletePatchFile();
|
|
551
|
-
console.log(`${e}`.replace("error:", fmt.red("error:")));
|
|
552
|
-
error("Failed to apply changes.");
|
|
553
|
-
}
|
|
677
|
+
const match = version2 >= range[0] && version2 <= range[1];
|
|
678
|
+
if (!match) {
|
|
679
|
+
const requiredVersionText = range[0] === range[1] ? `${range[0]}` : `min: ${range[0]}, max: ${range[1]}`;
|
|
680
|
+
throw new Error(
|
|
681
|
+
normalizeLogMessage(`
|
|
682
|
+
${mismatchMessage}
|
|
683
|
+
current version: ${version2}
|
|
684
|
+
required version: ${requiredVersionText}
|
|
685
|
+
`)
|
|
686
|
+
);
|
|
554
687
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
688
|
+
}
|
|
689
|
+
async function _checkVersionsCompatibility(template, usedBlocks, basesVersion) {
|
|
690
|
+
for (const templateBaseDep of template.bases || []) {
|
|
691
|
+
const currentVersion = basesVersion[templateBaseDep.name]?.version;
|
|
692
|
+
ensureVersionOverlap(
|
|
693
|
+
currentVersion,
|
|
694
|
+
templateBaseDep.version,
|
|
695
|
+
`Template "${template.name}" version: ${template.version} is not compatible with base "${templateBaseDep.name}"`
|
|
559
696
|
);
|
|
560
697
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
698
|
+
for (const block of usedBlocks) {
|
|
699
|
+
for (const blockTemplateDep of block.templates || []) {
|
|
700
|
+
const currentTemplate = basesVersion[blockTemplateDep.name];
|
|
701
|
+
if (!currentTemplate) {
|
|
702
|
+
throw new Error(
|
|
703
|
+
`Block "${block.name}" version: ${block.version} depends on missing template "${blockTemplateDep.name}" `
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
ensureVersionOverlap(
|
|
707
|
+
currentTemplate?.version,
|
|
708
|
+
blockTemplateDep.version,
|
|
709
|
+
`Block "${block.name}" version: ${block.version} is not compatible with template "${blockTemplateDep.name}" `
|
|
710
|
+
);
|
|
565
711
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
712
|
+
for (const blockDep of block.dependencies || []) {
|
|
713
|
+
const blockDepDef = usedBlocks.find((b) => b.name === blockDep.name);
|
|
714
|
+
if (!blockDepDef) {
|
|
715
|
+
throw new Error(
|
|
716
|
+
`Block "${block.name}" version: ${block.version} depends on missing block "${blockDep.name}" `
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
ensureVersionOverlap(
|
|
720
|
+
blockDepDef?.version,
|
|
721
|
+
blockDep.version,
|
|
722
|
+
`Block "${block.name}" version: ${block.version} is not compatible with block "${blockDep.name}" `
|
|
723
|
+
);
|
|
570
724
|
}
|
|
571
|
-
} else {
|
|
572
|
-
basedOnInfo.commit = toCommit;
|
|
573
725
|
}
|
|
574
|
-
|
|
726
|
+
}
|
|
727
|
+
async function checkVersionsCompatibility() {
|
|
728
|
+
const template = processTemplate(getTemplateDef());
|
|
729
|
+
const usedBlocks = processBlocks(getUsedBlocks());
|
|
730
|
+
const basesVersion = processBases(getBloxFile());
|
|
731
|
+
basesVersion[template.name] = { version: template.version, commit: "" };
|
|
732
|
+
await _checkVersionsCompatibility(template, usedBlocks, basesVersion);
|
|
575
733
|
}
|
|
576
734
|
|
|
577
|
-
// src/
|
|
578
|
-
async function
|
|
579
|
-
const
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
735
|
+
// src/services/versions/select.ts
|
|
736
|
+
async function selectVersion(remote, fromCommit) {
|
|
737
|
+
const versionsData = await getAvailableVersions(remote);
|
|
738
|
+
if (!versionsData) return;
|
|
739
|
+
const versions = [];
|
|
740
|
+
for (const [v, { lastCommit }] of Object.entries(versionsData)) {
|
|
741
|
+
if (!fromCommit) {
|
|
742
|
+
versions.push(v);
|
|
743
|
+
continue;
|
|
744
|
+
}
|
|
745
|
+
if (fromCommit === lastCommit) continue;
|
|
746
|
+
if (!lastCommit || await git.isCommitOlder(fromCommit, lastCommit)) {
|
|
747
|
+
versions.push(v);
|
|
585
748
|
}
|
|
586
|
-
await updateFromRemote(templateInfo.name, template.source, templateInfo.ignore);
|
|
587
|
-
});
|
|
588
|
-
for (const base of templateInfo.bases || []) {
|
|
589
|
-
await runTask(
|
|
590
|
-
{ start: `Update App base: ${base.name}`, end: `Updated App base: ${base.name}` },
|
|
591
|
-
async () => {
|
|
592
|
-
const template = templates[base.name];
|
|
593
|
-
if (!template) {
|
|
594
|
-
error("Selected template not available, try to sync registries");
|
|
595
|
-
return;
|
|
596
|
-
}
|
|
597
|
-
await updateFromRemote(base.name, template.source, []);
|
|
598
|
-
}
|
|
599
|
-
);
|
|
600
749
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
async function updateBlock(block, blocks) {
|
|
604
|
-
if (updatedBlocks.has(block)) {
|
|
605
|
-
debug(`${block} already updated`);
|
|
750
|
+
if (!versions.length) {
|
|
751
|
+
debug("No versions found, will download latest commit");
|
|
606
752
|
return;
|
|
607
753
|
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
await runTask({ start: `Update block: ${block}`, end: `Updated block: ${block}` }, async () => {
|
|
611
|
-
if (!blockDef) {
|
|
612
|
-
error("Selected block not available, try to sync registries", false);
|
|
613
|
-
return;
|
|
614
|
-
}
|
|
615
|
-
await updateFromRemote(block, blockDef.source, blockDef.ignore, blockPath);
|
|
616
|
-
updatedBlocks.add(block);
|
|
617
|
-
});
|
|
618
|
-
const blockInfo = getTemplateInfo(blockPath);
|
|
619
|
-
for (const dep of blockInfo?.dependencies || []) {
|
|
620
|
-
debug(`${block} has dependency on ${dep.name}, will be updated`);
|
|
621
|
-
await updateBlock(dep.name, blocks);
|
|
754
|
+
if (versions.length === 1) {
|
|
755
|
+
return versionsData[versions[0] || ""]?.lastCommit;
|
|
622
756
|
}
|
|
757
|
+
const version2 = await select({
|
|
758
|
+
message: "Select version to be used",
|
|
759
|
+
options: versions.map((v) => ({
|
|
760
|
+
value: v,
|
|
761
|
+
label: v
|
|
762
|
+
}))
|
|
763
|
+
});
|
|
764
|
+
return versionsData[version2]?.lastCommit;
|
|
623
765
|
}
|
|
624
766
|
|
|
625
767
|
// src/commands/init/utils/index.ts
|
|
626
768
|
function runInitScript(cwd) {
|
|
627
769
|
debug(`Run init script in: ${cwd}`);
|
|
628
|
-
const info =
|
|
770
|
+
const info = getTemplateDef(cwd);
|
|
629
771
|
if (info.init) {
|
|
630
772
|
return $(info.init, [], { cwd });
|
|
631
773
|
}
|
|
@@ -634,33 +776,39 @@ function runInitScript(cwd) {
|
|
|
634
776
|
// src/commands/add/index.ts
|
|
635
777
|
var addBlock = async (block, blocks) => {
|
|
636
778
|
const blockPath = `blocks/${block}`;
|
|
637
|
-
const
|
|
779
|
+
const blockRegistryData = blocks[block];
|
|
638
780
|
if (!import_fs_extra7.default.existsSync(blockPath)) {
|
|
639
781
|
await runTask(
|
|
640
782
|
{ start: `Fetching block: ${block}`, end: `Added block: ${block}` },
|
|
641
783
|
async () => {
|
|
642
|
-
await git.clone(
|
|
643
|
-
|
|
784
|
+
await git.clone(blockRegistryData?.source || "", blockPath);
|
|
785
|
+
let commit2 = await selectVersion(block);
|
|
786
|
+
if (commit2) {
|
|
787
|
+
await git.resetToCommit(commit2, blockPath);
|
|
788
|
+
} else {
|
|
789
|
+
commit2 = await git.getLatestCommitFromPath(blockPath);
|
|
790
|
+
}
|
|
644
791
|
await git.unInit(blockPath);
|
|
645
792
|
await replacePlaceholders(blockPath, {});
|
|
646
|
-
|
|
793
|
+
await migrateAll();
|
|
794
|
+
updateCheckpoint(block, { commit: commit2 }, blockPath);
|
|
647
795
|
}
|
|
648
796
|
);
|
|
649
797
|
await runTask("Run init script", async () => runInitScript(blockPath));
|
|
650
798
|
} else {
|
|
651
|
-
debug(`${block} already exists
|
|
652
|
-
await updateBlock(block, blocks);
|
|
799
|
+
debug(`${block} already exists`);
|
|
653
800
|
}
|
|
654
|
-
const
|
|
655
|
-
for (const dep of
|
|
801
|
+
const blockDef = getBlockDef(blockPath);
|
|
802
|
+
for (const dep of blockDef?.dependencies || []) {
|
|
656
803
|
await addBlock(dep.name, blocks);
|
|
657
804
|
}
|
|
658
805
|
};
|
|
659
|
-
var
|
|
660
|
-
await
|
|
806
|
+
var add2 = command("add").description("Add block to app").alias("a").argument("[block]", "Block to be added").action(async (block) => {
|
|
807
|
+
await migrateAll();
|
|
808
|
+
await syncRegistries();
|
|
661
809
|
try {
|
|
662
|
-
const
|
|
663
|
-
const blocks = await getAvailableBlocks(
|
|
810
|
+
const templateDef = getTemplateDef();
|
|
811
|
+
const blocks = await getAvailableBlocks(templateDef);
|
|
664
812
|
if (!block) {
|
|
665
813
|
block = await select({
|
|
666
814
|
message: "Select template to be used",
|
|
@@ -672,10 +820,15 @@ var add = command("add").description("Add block to app").alias("a").argument("[b
|
|
|
672
820
|
});
|
|
673
821
|
}
|
|
674
822
|
if (!blocks[block]) {
|
|
675
|
-
error("Selected block not available
|
|
823
|
+
error("Selected block not available in registries");
|
|
676
824
|
}
|
|
677
825
|
await addBlock(block, blocks);
|
|
826
|
+
await checkVersionsCompatibility();
|
|
678
827
|
} catch (e) {
|
|
828
|
+
if (!isDebugMode()) {
|
|
829
|
+
import_prompts2.log.warn("Error happened, Will rollback changes");
|
|
830
|
+
await git.rollback();
|
|
831
|
+
}
|
|
679
832
|
error(e);
|
|
680
833
|
}
|
|
681
834
|
});
|
|
@@ -688,14 +841,7 @@ var ensureRequired = (name) => (value) => {
|
|
|
688
841
|
};
|
|
689
842
|
|
|
690
843
|
// src/commands/conf/index.ts
|
|
691
|
-
var conf = command("conf").description("Set blox global config").alias("c").option("-r, --registries <registries...>", "Remote registries urls").
|
|
692
|
-
if (!syncFrequency) {
|
|
693
|
-
syncFrequency = await text({
|
|
694
|
-
message: "Sync frequency",
|
|
695
|
-
placeholder: "(1m, 1h,1d,...etc)",
|
|
696
|
-
defaultValue: defaultConfig.syncFrequency
|
|
697
|
-
});
|
|
698
|
-
}
|
|
844
|
+
var conf = command("conf").description("Set blox global config").alias("c").option("-r, --registries <registries...>", "Remote registries urls").action(async ({ registries }) => {
|
|
699
845
|
if (!registries?.length) {
|
|
700
846
|
registries = await textList({
|
|
701
847
|
message: "Registries urls",
|
|
@@ -703,7 +849,7 @@ var conf = command("conf").description("Set blox global config").alias("c").opti
|
|
|
703
849
|
});
|
|
704
850
|
}
|
|
705
851
|
try {
|
|
706
|
-
updateLocalConfig({ registries
|
|
852
|
+
updateLocalConfig({ registries });
|
|
707
853
|
import_prompts2.log.success("local config updated successfully.");
|
|
708
854
|
} catch (e) {
|
|
709
855
|
error(e);
|
|
@@ -711,8 +857,9 @@ var conf = command("conf").description("Set blox global config").alias("c").opti
|
|
|
711
857
|
});
|
|
712
858
|
|
|
713
859
|
// src/commands/init/index.ts
|
|
860
|
+
var import_fs_extra8 = __toESM(require("fs-extra"));
|
|
714
861
|
var init2 = command("init").description("Init new app").alias("i").option("-n, --name <name>", "App name").option("-t, --template <template>", "Template to use").action(async ({ name, template }) => {
|
|
715
|
-
await syncRegistries(
|
|
862
|
+
await syncRegistries();
|
|
716
863
|
try {
|
|
717
864
|
if (!name) {
|
|
718
865
|
name = await text({ message: "Select app name" });
|
|
@@ -729,65 +876,210 @@ var init2 = command("init").description("Init new app").alias("i").option("-n, -
|
|
|
729
876
|
});
|
|
730
877
|
}
|
|
731
878
|
if (!templates[template]) {
|
|
732
|
-
error("Selected template not available
|
|
879
|
+
error("Selected template not available in registries");
|
|
733
880
|
}
|
|
734
881
|
const task = spinner();
|
|
735
882
|
task.start(`Fetch template`);
|
|
736
883
|
await git.clone(templates[template]?.source || "", name);
|
|
737
|
-
|
|
884
|
+
let commit2 = await selectVersion(template);
|
|
885
|
+
if (commit2) {
|
|
886
|
+
await git.resetToCommit(commit2, name);
|
|
887
|
+
} else {
|
|
888
|
+
commit2 = await git.getLatestCommitFromPath(name);
|
|
889
|
+
}
|
|
738
890
|
await git.unInit(name);
|
|
891
|
+
await migrateAll();
|
|
739
892
|
await replacePlaceholders(name, { app: { name } });
|
|
740
|
-
|
|
893
|
+
updateCheckpoint(name, { commit: commit2 }, name);
|
|
741
894
|
await git.init(name);
|
|
742
895
|
task.stop(`App initialized in ${name}`);
|
|
743
896
|
await runTask("Run init script", async () => runInitScript(name || ""));
|
|
744
897
|
} catch (e) {
|
|
898
|
+
name && import_fs_extra8.default.existsSync(name) && import_fs_extra8.default.rmdirSync(name);
|
|
745
899
|
error(e);
|
|
746
900
|
}
|
|
747
901
|
});
|
|
748
902
|
|
|
749
|
-
// src/commands/
|
|
750
|
-
var
|
|
903
|
+
// src/commands/migrate/index.ts
|
|
904
|
+
var migrateSchema = command("migrate").description("Migrate blox schema").action(async () => {
|
|
751
905
|
try {
|
|
752
|
-
await
|
|
753
|
-
import_prompts2.log.success("Local registries synced successfully.");
|
|
906
|
+
await migrateAll();
|
|
754
907
|
} catch (e) {
|
|
908
|
+
if (!isDebugMode()) {
|
|
909
|
+
import_prompts2.log.warn("Error happened, Will rollback changes");
|
|
910
|
+
await git.rollback();
|
|
911
|
+
}
|
|
755
912
|
error(e);
|
|
756
913
|
}
|
|
757
914
|
});
|
|
758
915
|
|
|
916
|
+
// src/commands/update/index.ts
|
|
917
|
+
var import_prompts13 = require("@clack/prompts");
|
|
918
|
+
|
|
919
|
+
// src/commands/update/utils.ts
|
|
920
|
+
var import_node_path9 = __toESM(require("path"));
|
|
921
|
+
|
|
922
|
+
// src/services/updater.ts
|
|
923
|
+
var import_node_path8 = __toESM(require("path"));
|
|
924
|
+
var import_fs_extra9 = __toESM(require("fs-extra"));
|
|
925
|
+
async function updateFromRemote(name, source = "", ignore = [], destPath = ".") {
|
|
926
|
+
debug("Updating from remote");
|
|
927
|
+
await git.updateRemote(name, source);
|
|
928
|
+
const bloxFile = getBloxFile(destPath);
|
|
929
|
+
const fromCommit = bloxFile.checkpoints[name]?.commit;
|
|
930
|
+
if (!fromCommit) {
|
|
931
|
+
debug("Skip update, no fromCommit found");
|
|
932
|
+
return;
|
|
933
|
+
}
|
|
934
|
+
let toCommit = await selectVersion(name, fromCommit);
|
|
935
|
+
if (!toCommit) {
|
|
936
|
+
toCommit = await git.getLatestCommitFromRemote(name);
|
|
937
|
+
}
|
|
938
|
+
debug({ fromCommit, toCommit });
|
|
939
|
+
if (fromCommit == toCommit) {
|
|
940
|
+
debug("No new version");
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
const remoteBloxFile = (await migrateOnRemote(name, toCommit)).output[".blox"];
|
|
944
|
+
for (const [baseName, baseInfo] of Object.entries(remoteBloxFile.checkpoints || {})) {
|
|
945
|
+
const baseFrom = bloxFile.checkpoints[baseName]?.commit;
|
|
946
|
+
if (!baseFrom) {
|
|
947
|
+
bloxFile.checkpoints[baseName] = baseInfo;
|
|
948
|
+
continue;
|
|
949
|
+
}
|
|
950
|
+
const baseTo = baseInfo.commit;
|
|
951
|
+
if (await git.isCommitOlder(baseFrom, baseTo)) {
|
|
952
|
+
bloxFile.checkpoints[baseName] = baseInfo;
|
|
953
|
+
} else {
|
|
954
|
+
logWarningSection(
|
|
955
|
+
`${baseName} is newer in project than template, make sure no breaking changes`
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
const patchFilePath = import_node_path8.default.join(destPath, "diff.patch");
|
|
960
|
+
ignore.push("./.blox");
|
|
961
|
+
await git.createPatch(fromCommit, toCommit, patchFilePath, ignore);
|
|
962
|
+
const changes = await git.getChangesLog(fromCommit, toCommit);
|
|
963
|
+
const deletePatchFile = async () => {
|
|
964
|
+
if (!isDebugMode()) {
|
|
965
|
+
await import_fs_extra9.default.unlink(patchFilePath);
|
|
966
|
+
}
|
|
967
|
+
debug("make sure to delete the patch file after you're done with it.");
|
|
968
|
+
};
|
|
969
|
+
try {
|
|
970
|
+
await git.applyPatch(patchFilePath, destPath);
|
|
971
|
+
} catch (e) {
|
|
972
|
+
const errorMessage = `${e}`;
|
|
973
|
+
if (errorMessage.includes("error:")) {
|
|
974
|
+
await deletePatchFile();
|
|
975
|
+
console.log(`${e}`.replace("error:", fmt.red("error:")));
|
|
976
|
+
throw new Error("Failed to apply changes.");
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
if (changes.trim() !== "") {
|
|
980
|
+
logWarningSection(
|
|
981
|
+
"These is the changes from last update, make sure to review for breaking changes",
|
|
982
|
+
changes.replace(/BREAKING CHANGE/gi, fmt.red("BREAKING CHANGE"))
|
|
983
|
+
);
|
|
984
|
+
}
|
|
985
|
+
await deletePatchFile();
|
|
986
|
+
bloxFile.checkpoints[name] = { commit: toCommit };
|
|
987
|
+
if (bloxFile.definition.name === name) {
|
|
988
|
+
bloxFile.definition = remoteBloxFile.definition;
|
|
989
|
+
} else {
|
|
990
|
+
bloxFile.checkpoints[name].version = remoteBloxFile.definition.version;
|
|
991
|
+
}
|
|
992
|
+
saveBloxFile(bloxFile, destPath);
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
// src/commands/update/utils.ts
|
|
996
|
+
async function updateApp(templateInfo, templates) {
|
|
997
|
+
await runTask({ start: "Update App", end: "Updated App" }, async () => {
|
|
998
|
+
const template = templates[templateInfo.name];
|
|
999
|
+
if (!template) {
|
|
1000
|
+
error(`Selected template: ${templateInfo.name} not available in registries`);
|
|
1001
|
+
return;
|
|
1002
|
+
}
|
|
1003
|
+
await updateFromRemote(templateInfo.name, template.source, templateInfo.ignore);
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
async function updateAppBases(templateInfo, templates) {
|
|
1007
|
+
for (const base of templateInfo.bases || []) {
|
|
1008
|
+
await runTask(
|
|
1009
|
+
{ start: `Update App base: ${base.name}`, end: `Updated App base: ${base.name}` },
|
|
1010
|
+
async () => {
|
|
1011
|
+
const template = templates[base.name];
|
|
1012
|
+
if (!template) {
|
|
1013
|
+
error("Selected template not available in registries");
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
await updateFromRemote(base.name, template.source, []);
|
|
1017
|
+
}
|
|
1018
|
+
);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
async function updateBlock(block, blocks) {
|
|
1022
|
+
const blockDef = blocks[block];
|
|
1023
|
+
const blockPath = import_node_path9.default.join(blocksDir, block);
|
|
1024
|
+
await runTask({ start: `Update block: ${block}`, end: `Updated block: ${block}` }, async () => {
|
|
1025
|
+
if (!blockDef) {
|
|
1026
|
+
error(`Selected block: ${block} not available in registries`, false);
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
await updateFromRemote(block, blockDef.source, blockDef.ignore, blockPath);
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
|
|
759
1033
|
// src/commands/update/index.ts
|
|
760
1034
|
var update2 = command("update").description("Update app/block").alias("u").addOption(
|
|
761
|
-
option("-s, --scope <scope>", "Scope of update").default("all").choices(["app", "block", "all"])
|
|
762
|
-
).option("-b, --block <name>", "Block to update, valid only with scope block").
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
|
|
1035
|
+
option("-s, --scope <scope>", "Scope of update").default("all").choices(["app", "bases", "block", "all"])
|
|
1036
|
+
).option("-b, --block <name>", "Block to update, valid only with scope block").action(async (args) => {
|
|
1037
|
+
if (await git.hasUncommittedChanges()) {
|
|
1038
|
+
import_prompts13.log.warn("Make sure to commit any changes first");
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
await migrateAll(true);
|
|
1042
|
+
await syncRegistries();
|
|
1043
|
+
const templateDef = getTemplateDef();
|
|
1044
|
+
const availableBlocks = await getAvailableBlocks(templateDef);
|
|
1045
|
+
const templates = await getAvailableTemplates();
|
|
769
1046
|
const usedBlocks = getUsedBlocks();
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
await
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
if (args.scope === "block") {
|
|
776
|
-
if (!args.block) {
|
|
777
|
-
args.block = await select({
|
|
778
|
-
message: "Select block to be updated",
|
|
779
|
-
options: usedBlocks.map((block2) => ({
|
|
780
|
-
value: block2.name,
|
|
781
|
-
label: block2.name
|
|
782
|
-
}))
|
|
783
|
-
});
|
|
1047
|
+
try {
|
|
1048
|
+
if (["app", "all"].includes(args.scope)) {
|
|
1049
|
+
await updateApp(templateDef, templates);
|
|
784
1050
|
}
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
1051
|
+
if (["bases", "all"].includes(args.scope)) {
|
|
1052
|
+
await updateAppBases(templateDef, templates);
|
|
1053
|
+
}
|
|
1054
|
+
if (args.scope === "all") {
|
|
1055
|
+
for (const block of usedBlocks) {
|
|
1056
|
+
await updateBlock(block.name, availableBlocks);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
if (args.scope === "block") {
|
|
1060
|
+
if (!args.block) {
|
|
1061
|
+
args.block = await select({
|
|
1062
|
+
message: "Select block to be updated",
|
|
1063
|
+
options: usedBlocks.map((block) => ({
|
|
1064
|
+
value: block.name,
|
|
1065
|
+
label: block.name
|
|
1066
|
+
}))
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
const blockDef = usedBlocks.find((block) => block.name === args.block);
|
|
1070
|
+
if (!blockDef) {
|
|
1071
|
+
error("Selected block not used in app");
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
await updateBlock(blockDef.name, availableBlocks);
|
|
789
1075
|
}
|
|
790
|
-
await
|
|
1076
|
+
await checkVersionsCompatibility();
|
|
1077
|
+
} catch (e) {
|
|
1078
|
+
if (!isDebugMode()) {
|
|
1079
|
+
import_prompts13.log.warn("Error happened, Will rollback changes");
|
|
1080
|
+
await git.rollback();
|
|
1081
|
+
}
|
|
1082
|
+
error(e);
|
|
791
1083
|
}
|
|
792
1084
|
});
|
|
793
1085
|
|
|
@@ -802,11 +1094,14 @@ var upgrade = command("upgrade").description("Upgrade blox cli to latest version
|
|
|
802
1094
|
});
|
|
803
1095
|
|
|
804
1096
|
// src/commands/index.ts
|
|
805
|
-
var commands = [init2,
|
|
1097
|
+
var commands = [init2, add2, update2, conf, upgrade, migrateSchema];
|
|
806
1098
|
|
|
807
1099
|
// bin/run.ts
|
|
808
|
-
|
|
809
|
-
import_extra_typings2.program.
|
|
810
|
-
import_extra_typings2.program.option("-
|
|
811
|
-
|
|
812
|
-
import_extra_typings2.program.
|
|
1100
|
+
async function main() {
|
|
1101
|
+
import_extra_typings2.program.name("blox").version(version);
|
|
1102
|
+
import_extra_typings2.program.option("-d, --debug", "work in debug mode");
|
|
1103
|
+
import_extra_typings2.program.option("-m, --minimal", "work in minimal mode");
|
|
1104
|
+
commands.forEach((cmd) => import_extra_typings2.program.addCommand(cmd));
|
|
1105
|
+
import_extra_typings2.program.parse();
|
|
1106
|
+
}
|
|
1107
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naseej/blox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "blox cli",
|
|
5
5
|
"bin": {
|
|
6
6
|
"blox": "./bin/run"
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"@types/lodash": "4.17.20",
|
|
45
45
|
"eslint": "^8.57.0",
|
|
46
46
|
"vitest": "2.1.8",
|
|
47
|
+
"vite-tsconfig-paths": "^5.1.4",
|
|
47
48
|
"prettier": "^3.2.5",
|
|
48
49
|
"tsx": "4.19.2",
|
|
49
50
|
"tsup": "^8.0.2",
|
package/readme.md
CHANGED
|
@@ -32,7 +32,6 @@ Commands:
|
|
|
32
32
|
blox init Init new app [alias: i]
|
|
33
33
|
blox add Add block to app [alias: a]
|
|
34
34
|
blox update Update app/block [alias: u]
|
|
35
|
-
blox sync Sync local registry with remote updates [alias: s]
|
|
36
35
|
blox conf Set blox global config [alias: c]
|
|
37
36
|
|
|
38
37
|
Options:
|
|
@@ -49,9 +48,9 @@ Options:
|
|
|
49
48
|
* [init](#init)
|
|
50
49
|
* [add](#add)
|
|
51
50
|
* [update](#update)
|
|
52
|
-
* [sync](#sync)
|
|
53
51
|
* [conf](#conf)
|
|
54
52
|
* [upgrade](#upgrade)
|
|
53
|
+
* [migrate](#migrate)
|
|
55
54
|
|
|
56
55
|
#### init
|
|
57
56
|
|
|
@@ -96,28 +95,12 @@ blox update
|
|
|
96
95
|
Update app/block
|
|
97
96
|
|
|
98
97
|
Options:
|
|
99
|
-
-
|
|
100
|
-
-s, --scope Scope of update [choices: "app", "block", "all"] [default: "all"]
|
|
98
|
+
-s, --scope Scope of update [choices: "app", "bases", "block", "all"] [default: "all"]
|
|
101
99
|
-b, --block Block to update, valid only with scope block
|
|
102
100
|
```
|
|
103
101
|
|
|
104
102
|
* Note: you may need to resolve merge conflicts if found
|
|
105
103
|
|
|
106
|
-
#### sync
|
|
107
|
-
|
|
108
|
-
```sh
|
|
109
|
-
blox sync --help
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
```
|
|
113
|
-
blox sync
|
|
114
|
-
|
|
115
|
-
Sync local registry with remote updates
|
|
116
|
-
|
|
117
|
-
Options:
|
|
118
|
-
-f, --force Force update [boolean] [default: "false"]
|
|
119
|
-
```
|
|
120
|
-
|
|
121
104
|
#### conf
|
|
122
105
|
|
|
123
106
|
```sh
|
|
@@ -144,6 +127,17 @@ blox upgrade
|
|
|
144
127
|
|
|
145
128
|
Upgrade blox cli to latest version
|
|
146
129
|
```
|
|
130
|
+
#### migrate
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
blox migrate --help
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
blox migrate
|
|
138
|
+
|
|
139
|
+
Migrate blox schema
|
|
140
|
+
```
|
|
147
141
|
|
|
148
142
|
### Docs
|
|
149
143
|
|