@naseej/blox 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/run.js +113 -35
  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.0";
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,64 @@ 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 updateAppBlocks({
1052
+ toAppCommit,
1053
+ usedBlocks,
1054
+ availableBlocks
1055
+ }) {
1056
+ for (const block of usedBlocks) {
1057
+ const blockRegistryData = availableBlocks[block.name];
1058
+ if (!blockRegistryData) return;
1059
+ await git.updateRemote(block.name, blockRegistryData.source);
1060
+ const blockPath = import_node_path10.default.join(blocksDir, block.name);
1061
+ const currentBloxFile = getBloxFile(blockPath);
1062
+ const remoteBloxFile = (await migrateOnRemote(block.name, toAppCommit, blockPath)).output[".blox"];
1063
+ const blockFromCommit = currentBloxFile.checkpoints[block.name]?.commit;
1064
+ const blockToCommit = remoteBloxFile.checkpoints[block.name]?.commit;
1065
+ if (!blockFromCommit || !blockToCommit) continue;
1066
+ if (blockFromCommit === blockToCommit) continue;
1067
+ if (await git.isCommitOlder(blockFromCommit, blockToCommit)) {
1068
+ saveBloxFile(remoteBloxFile, blockPath);
1069
+ } else {
1070
+ logWarningSection(
1071
+ `${block.name} is newer in project than template, make sure no breaking changes`
1072
+ );
1073
+ }
1074
+ }
1075
+ }
1076
+ async function updateApp({
1077
+ templateDef,
1078
+ templates,
1079
+ usedBlocks,
1080
+ availableBlocks
1081
+ }) {
1025
1082
  await runTask({ start: "Update App", end: "Updated App" }, async () => {
1026
- const template = templates[templateInfo.name];
1083
+ const template = templates[templateDef.name];
1027
1084
  if (!template) {
1028
- error(`Selected template: ${templateInfo.name} not available in registries`);
1085
+ error(`Selected template: ${templateDef.name} not available in registries`);
1029
1086
  return;
1030
1087
  }
1031
- await updateFromRemote(templateInfo.name, template.source, templateInfo.ignore);
1088
+ const toAppCommit = await updateFromRemote({
1089
+ name: templateDef.name,
1090
+ source: template.source,
1091
+ ignore: templateDef.ignore
1092
+ });
1093
+ if (!toAppCommit) return;
1094
+ await updateAppBlocks({ toAppCommit, usedBlocks, availableBlocks });
1032
1095
  });
1033
1096
  }
1034
- async function updateAppBases(templateInfo, templates) {
1035
- for (const base of templateInfo.bases || []) {
1097
+ async function updateAppBases({
1098
+ templateDef,
1099
+ templates,
1100
+ usedBlocks,
1101
+ availableBlocks
1102
+ }) {
1103
+ for (const base of templateDef.bases || []) {
1036
1104
  await runTask(
1037
1105
  { start: `Update App base: ${base.name}`, end: `Updated App base: ${base.name}` },
1038
1106
  async () => {
@@ -1041,7 +1109,12 @@ async function updateAppBases(templateInfo, templates) {
1041
1109
  error("Selected template not available in registries");
1042
1110
  return;
1043
1111
  }
1044
- await updateFromRemote(base.name, template.source, []);
1112
+ const toAppCommit = await updateFromRemote({
1113
+ name: base.name,
1114
+ source: template.source
1115
+ });
1116
+ if (!toAppCommit) return;
1117
+ await updateAppBlocks({ toAppCommit, usedBlocks, availableBlocks });
1045
1118
  }
1046
1119
  );
1047
1120
  }
@@ -1055,7 +1128,12 @@ async function updateBlock(block, blocks) {
1055
1128
  error(`Selected block: ${block} not available in registries`, false);
1056
1129
  return;
1057
1130
  }
1058
- await updateFromRemote(block, blockRegistryData.source, blockDef.ignore, blockPath);
1131
+ await updateFromRemote({
1132
+ name: block,
1133
+ source: blockRegistryData.source,
1134
+ ignore: blockDef.ignore,
1135
+ dir: blockPath
1136
+ });
1059
1137
  });
1060
1138
  }
1061
1139
 
@@ -1064,7 +1142,7 @@ var update2 = command("update").description("Update app/block").alias("u").addOp
1064
1142
  option("-s, --scope <scope>", "Scope of update").default("all").choices(["app", "bases", "block", "all"])
1065
1143
  ).option("-b, --block <name>", "Block to update, valid only with scope block").action(async (args) => {
1066
1144
  if (await git.hasUncommittedChanges()) {
1067
- import_prompts13.log.warn(fmt.yellow("Make sure to commit any changes first"));
1145
+ import_prompts14.log.warn(fmt.yellow("Make sure to commit any changes first"));
1068
1146
  return;
1069
1147
  }
1070
1148
  await migrateAll(true);
@@ -1075,10 +1153,10 @@ var update2 = command("update").description("Update app/block").alias("u").addOp
1075
1153
  const usedBlocks = getUsedBlocks();
1076
1154
  try {
1077
1155
  if (["app", "all"].includes(args.scope)) {
1078
- await updateApp(templateDef, templates);
1156
+ await updateApp({ templateDef, templates, usedBlocks, availableBlocks });
1079
1157
  }
1080
1158
  if (["bases", "all"].includes(args.scope)) {
1081
- await updateAppBases(templateDef, templates);
1159
+ await updateAppBases({ templateDef, templates, usedBlocks, availableBlocks });
1082
1160
  }
1083
1161
  if (args.scope === "all") {
1084
1162
  for (const block of usedBlocks) {
@@ -1105,7 +1183,7 @@ var update2 = command("update").description("Update app/block").alias("u").addOp
1105
1183
  await checkVersionsCompatibility();
1106
1184
  } catch (e) {
1107
1185
  if (!isDebugMode()) {
1108
- import_prompts13.log.warn("Error happened, Will rollback changes");
1186
+ import_prompts14.log.warn("Error happened, Will rollback changes");
1109
1187
  await git.rollback();
1110
1188
  }
1111
1189
  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.0",
4
4
  "description": "blox cli",
5
5
  "bin": {
6
6
  "blox": "./bin/run"