@naseej/blox 1.1.0 → 1.2.1

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 (2) hide show
  1. package/dist/run.js +99 -34
  2. package/package.json +1 -1
package/dist/run.js CHANGED
@@ -31,7 +31,7 @@ var option = (flags, description) => {
31
31
  };
32
32
 
33
33
  // package.json
34
- var version = "1.1.0";
34
+ var version = "1.2.1";
35
35
 
36
36
  // src/commands/add/index.ts
37
37
  var import_fs_extra8 = __toESM(require("fs-extra"));
@@ -66,6 +66,9 @@ var v1 = {
66
66
  }
67
67
  },
68
68
  run: async ({ packageJson, bloxFile: oldBloxFile }) => {
69
+ if (!packageJson.blox) {
70
+ return {};
71
+ }
69
72
  const bloxFile = {
70
73
  schemaVersion: 1,
71
74
  definition: packageJson.blox,
@@ -348,6 +351,11 @@ var text = async (opts) => {
348
351
  checkIfCanceled(value);
349
352
  return value;
350
353
  };
354
+ var confirm = async (opts) => {
355
+ const value = await (0, import_prompts3.confirm)(opts);
356
+ checkIfCanceled(value);
357
+ return value;
358
+ };
351
359
  var textList = async (opts) => {
352
360
  opts.message = `${opts.message} ${fmt.gray('(separated with ",")')}`;
353
361
  const value = await (0, import_prompts3.text)(opts);
@@ -370,19 +378,17 @@ function getApplicableMigrations(schemaVersion = 0) {
370
378
  return migrationsList.slice(schemaVersion);
371
379
  }
372
380
  async function migrate(name, { getPath = (f) => f, read, write }) {
373
- let remoteBloxFile;
381
+ let originalBloxFile;
374
382
  try {
375
- remoteBloxFile = await read(getPath(".blox"));
383
+ originalBloxFile = await read(getPath(".blox"));
376
384
  } 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`));
385
+ originalBloxFile = {};
382
386
  }
383
- const currentRev = {
384
- ".blox": remoteBloxFile
387
+ const migrations = getApplicableMigrations(originalBloxFile.schemaVersion);
388
+ let originalRev = {
389
+ ".blox": originalBloxFile
385
390
  };
391
+ const currentRev = { ...originalRev };
386
392
  for (const migration of migrations) {
387
393
  const deps = {};
388
394
  for (const [name2, { location, defaultValue = {} }] of Object.entries(
@@ -401,18 +407,25 @@ async function migrate(name, { getPath = (f) => f, read, write }) {
401
407
  deps[name2] = defaultValue;
402
408
  }
403
409
  }
410
+ originalRev = { ...deps, ...originalRev };
404
411
  const output = await migration.run(deps);
405
412
  for (const [name2, value] of Object.entries(output || {})) {
406
413
  const { location } = migration.dependencies[name2] || {};
407
414
  location && (currentRev[location] = value);
408
415
  }
409
416
  }
417
+ let filesChanged = false;
410
418
  if (write) {
411
419
  for (const [location, value] of Object.entries(currentRev)) {
420
+ if (currentRev[location] === originalRev[location]) continue;
421
+ filesChanged = true;
412
422
  await write(getPath(location), JSON.stringify(value, null, 2));
413
423
  }
414
424
  }
415
- return { output: currentRev, migrationsRan: migrations.length };
425
+ if (filesChanged) {
426
+ import_prompts2.log.warn(fmt.yellow(`Migrations found for ${name}, will migrate to latest schema`));
427
+ }
428
+ return { output: currentRev, filesChanged };
416
429
  }
417
430
  function migrateOnLocal(name, cwd = ".") {
418
431
  return migrate(name, {
@@ -421,20 +434,22 @@ function migrateOnLocal(name, cwd = ".") {
421
434
  write: import_fs_extra2.default.writeFile
422
435
  });
423
436
  }
424
- function migrateOnRemote(name, commit2) {
437
+ function migrateOnRemote(name, commit2, cwd) {
425
438
  return migrate(name, {
426
- read: (file) => git.readJsonFromCommit(commit2, file)
439
+ read: (file) => git.readJsonFromCommit(commit2, file),
440
+ getPath: cwd ? (file) => import_node_path3.default.join(cwd, file) : void 0
427
441
  });
428
442
  }
429
443
 
430
444
  // src/migrations/index.ts
431
445
  async function migrateAll(autoCommit) {
432
- let migrationsRan = (await migrateOnLocal("app")).migrationsRan;
446
+ let filesChanged = (await migrateOnLocal("app")).filesChanged;
433
447
  const blocks = import_fs_extra3.default.existsSync(blocksDir) ? import_fs_extra3.default.readdirSync(blocksDir) : [];
434
448
  for (const block of blocks) {
435
- migrationsRan += (await migrateOnLocal(block, import_node_path4.default.join(blocksDir, block))).migrationsRan;
449
+ const res = await migrateOnLocal(block, import_node_path4.default.join(blocksDir, block));
450
+ filesChanged = filesChanged || res.filesChanged;
436
451
  }
437
- if (autoCommit && migrationsRan) {
452
+ if (autoCommit && filesChanged && await git.hasUncommittedChanges()) {
438
453
  import_prompts2.log.warn(fmt.yellow(`Migrations done and will be commited`));
439
454
  await git.add(".");
440
455
  await git.commit("chore: migrate blox schema");
@@ -942,7 +957,7 @@ var migrateSchema = command("migrate").description("Migrate blox schema").action
942
957
  });
943
958
 
944
959
  // src/commands/update/index.ts
945
- var import_prompts13 = require("@clack/prompts");
960
+ var import_prompts14 = require("@clack/prompts");
946
961
 
947
962
  // src/commands/update/utils.ts
948
963
  var import_node_path10 = __toESM(require("path"));
@@ -950,10 +965,10 @@ var import_node_path10 = __toESM(require("path"));
950
965
  // src/services/updater.ts
951
966
  var import_node_path9 = __toESM(require("path"));
952
967
  var import_fs_extra10 = __toESM(require("fs-extra"));
953
- async function updateFromRemote(name, source = "", ignore2 = [], destPath = ".") {
968
+ async function updateFromRemote({ name, source = "", ignore: ignore2 = [], dir = "." }) {
954
969
  debug("Updating from remote");
955
970
  await git.updateRemote(name, source);
956
- const bloxFile = getBloxFile(destPath);
971
+ const bloxFile = getBloxFile(dir);
957
972
  const fromCommit = bloxFile.checkpoints[name]?.commit;
958
973
  if (!fromCommit) {
959
974
  debug("Skip update, no fromCommit found");
@@ -984,8 +999,8 @@ async function updateFromRemote(name, source = "", ignore2 = [], destPath = ".")
984
999
  );
985
1000
  }
986
1001
  }
987
- const patchFilePath = import_node_path9.default.join(destPath, "diff.patch");
988
- ignore2.push("./.blox");
1002
+ const patchFilePath = import_node_path9.default.join(dir, "diff.patch");
1003
+ ignore2.push(".blox", "**/.blox");
989
1004
  await git.createPatch(fromCommit, toCommit, patchFilePath, ignore2);
990
1005
  const changes = await git.getChangesLog(fromCommit, toCommit);
991
1006
  const deletePatchFile = async () => {
@@ -994,8 +1009,9 @@ async function updateFromRemote(name, source = "", ignore2 = [], destPath = ".")
994
1009
  }
995
1010
  debug("make sure to delete the patch file after you're done with it.");
996
1011
  };
1012
+ let hasMergeConflicts = false;
997
1013
  try {
998
- await git.applyPatch(patchFilePath, destPath);
1014
+ await git.applyPatch(patchFilePath, dir);
999
1015
  } catch (e) {
1000
1016
  const errorMessage = `${e}`;
1001
1017
  if (errorMessage.includes("error:")) {
@@ -1003,6 +1019,16 @@ async function updateFromRemote(name, source = "", ignore2 = [], destPath = ".")
1003
1019
  console.log(`${e}`.replace("error:", fmt.red("error:")));
1004
1020
  throw new Error("Failed to apply changes.");
1005
1021
  }
1022
+ hasMergeConflicts = errorMessage.includes("with conflicts");
1023
+ }
1024
+ if (hasMergeConflicts) {
1025
+ const shouldContinue = await confirm({
1026
+ message: "Please resolve conflicts and press yes to continue"
1027
+ });
1028
+ if (!shouldContinue) {
1029
+ await deletePatchFile();
1030
+ throw new Error("Failed to solve conflicts");
1031
+ }
1006
1032
  }
1007
1033
  if (changes.trim() !== "") {
1008
1034
  logWarningSection(
@@ -1017,22 +1043,52 @@ async function updateFromRemote(name, source = "", ignore2 = [], destPath = ".")
1017
1043
  } else {
1018
1044
  bloxFile.checkpoints[name].version = remoteBloxFile.definition.version;
1019
1045
  }
1020
- saveBloxFile(bloxFile, destPath);
1046
+ saveBloxFile(bloxFile, dir);
1047
+ return toCommit;
1021
1048
  }
1022
1049
 
1023
1050
  // src/commands/update/utils.ts
1024
- async function updateApp(templateInfo, templates) {
1051
+ async function updateApp({
1052
+ templateDef,
1053
+ templates,
1054
+ usedBlocks,
1055
+ availableBlocks
1056
+ }) {
1025
1057
  await runTask({ start: "Update App", end: "Updated App" }, async () => {
1026
- const template = templates[templateInfo.name];
1058
+ const template = templates[templateDef.name];
1027
1059
  if (!template) {
1028
- error(`Selected template: ${templateInfo.name} not available in registries`);
1060
+ error(`Selected template: ${templateDef.name} not available in registries`);
1029
1061
  return;
1030
1062
  }
1031
- await updateFromRemote(templateInfo.name, template.source, templateInfo.ignore);
1063
+ const toAppCommit = await updateFromRemote({
1064
+ name: templateDef.name,
1065
+ source: template.source,
1066
+ ignore: templateDef.ignore
1067
+ });
1068
+ if (!toAppCommit) return;
1069
+ for (const block of usedBlocks) {
1070
+ const blockRegistryData = availableBlocks[block.name];
1071
+ if (!blockRegistryData) return;
1072
+ await git.updateRemote(block.name, blockRegistryData.source);
1073
+ const blockPath = import_node_path10.default.join(blocksDir, block.name);
1074
+ const currentBloxFile = getBloxFile(blockPath);
1075
+ const remoteBloxFile = (await migrateOnRemote(block.name, toAppCommit, blockPath)).output[".blox"];
1076
+ const blockFromCommit = currentBloxFile.checkpoints[block.name]?.commit;
1077
+ const blockToCommit = remoteBloxFile.checkpoints[block.name]?.commit;
1078
+ if (!blockFromCommit || !blockToCommit) continue;
1079
+ if (blockFromCommit === blockToCommit) continue;
1080
+ if (await git.isCommitOlder(blockFromCommit, blockToCommit)) {
1081
+ saveBloxFile(remoteBloxFile, blockPath);
1082
+ } else {
1083
+ logWarningSection(
1084
+ `${block.name} is newer in project than template, make sure no breaking changes`
1085
+ );
1086
+ }
1087
+ }
1032
1088
  });
1033
1089
  }
1034
- async function updateAppBases(templateInfo, templates) {
1035
- for (const base of templateInfo.bases || []) {
1090
+ async function updateAppBases(templateDef, templates) {
1091
+ for (const base of templateDef.bases || []) {
1036
1092
  await runTask(
1037
1093
  { start: `Update App base: ${base.name}`, end: `Updated App base: ${base.name}` },
1038
1094
  async () => {
@@ -1041,7 +1097,11 @@ async function updateAppBases(templateInfo, templates) {
1041
1097
  error("Selected template not available in registries");
1042
1098
  return;
1043
1099
  }
1044
- await updateFromRemote(base.name, template.source, []);
1100
+ await updateFromRemote({
1101
+ name: base.name,
1102
+ source: template.source,
1103
+ ignore: templateDef.ignore
1104
+ });
1045
1105
  }
1046
1106
  );
1047
1107
  }
@@ -1055,7 +1115,12 @@ async function updateBlock(block, blocks) {
1055
1115
  error(`Selected block: ${block} not available in registries`, false);
1056
1116
  return;
1057
1117
  }
1058
- await updateFromRemote(block, blockRegistryData.source, blockDef.ignore, blockPath);
1118
+ await updateFromRemote({
1119
+ name: block,
1120
+ source: blockRegistryData.source,
1121
+ ignore: blockDef.ignore,
1122
+ dir: blockPath
1123
+ });
1059
1124
  });
1060
1125
  }
1061
1126
 
@@ -1064,7 +1129,7 @@ var update2 = command("update").description("Update app/block").alias("u").addOp
1064
1129
  option("-s, --scope <scope>", "Scope of update").default("all").choices(["app", "bases", "block", "all"])
1065
1130
  ).option("-b, --block <name>", "Block to update, valid only with scope block").action(async (args) => {
1066
1131
  if (await git.hasUncommittedChanges()) {
1067
- import_prompts13.log.warn(fmt.yellow("Make sure to commit any changes first"));
1132
+ import_prompts14.log.warn(fmt.yellow("Make sure to commit any changes first"));
1068
1133
  return;
1069
1134
  }
1070
1135
  await migrateAll(true);
@@ -1075,7 +1140,7 @@ var update2 = command("update").description("Update app/block").alias("u").addOp
1075
1140
  const usedBlocks = getUsedBlocks();
1076
1141
  try {
1077
1142
  if (["app", "all"].includes(args.scope)) {
1078
- await updateApp(templateDef, templates);
1143
+ await updateApp({ templateDef, templates, usedBlocks, availableBlocks });
1079
1144
  }
1080
1145
  if (["bases", "all"].includes(args.scope)) {
1081
1146
  await updateAppBases(templateDef, templates);
@@ -1105,7 +1170,7 @@ var update2 = command("update").description("Update app/block").alias("u").addOp
1105
1170
  await checkVersionsCompatibility();
1106
1171
  } catch (e) {
1107
1172
  if (!isDebugMode()) {
1108
- import_prompts13.log.warn("Error happened, Will rollback changes");
1173
+ import_prompts14.log.warn("Error happened, Will rollback changes");
1109
1174
  await git.rollback();
1110
1175
  }
1111
1176
  error(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naseej/blox",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "blox cli",
5
5
  "bin": {
6
6
  "blox": "./bin/run"