@icebreakers/monorepo 2.0.8 → 2.0.10

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.
@@ -44,11 +44,12 @@
44
44
  "@changesets/cli": "^2.29.7",
45
45
  "@commitlint/cli": "^20.1.0",
46
46
  "@icebreakers/commitlint-config": "^1.2.1",
47
- "@icebreakers/eslint-config": "^1.5.0",
47
+ "@icebreakers/eslint-config": "^1.5.2",
48
48
  "@icebreakers/monorepo": "workspace:*",
49
49
  "@icebreakers/stylelint-config": "^1.2.1",
50
50
  "@types/fs-extra": "^11.0.4",
51
51
  "@types/node": "^24.7.2",
52
+ "@types/semver": "^7.7.1",
52
53
  "@vitest/coverage-v8": "~3.2.4",
53
54
  "ci-info": "^4.3.1",
54
55
  "cross-env": "^10.1.0",
@@ -578,7 +578,7 @@ async function cleanProjects(cwd) {
578
578
 
579
579
  // package.json
580
580
  var name = "@icebreakers/monorepo";
581
- var version = "2.0.8";
581
+ var version = "2.0.10";
582
582
 
583
583
  // src/constants.ts
584
584
  init_esm_shims();
@@ -1005,15 +1005,104 @@ function isMatch(str, arr) {
1005
1005
 
1006
1006
  // src/commands/upgrade/index.ts
1007
1007
  init_esm_shims();
1008
- import { Buffer as Buffer2 } from "buffer";
1009
- import process2 from "process";
1010
- import checkbox2 from "@inquirer/checkbox";
1011
- import confirm from "@inquirer/confirm";
1012
- import fs7 from "fs-extra";
1013
1008
  var import_set_value6 = __toESM(require_set_value(), 1);
1009
+ import process2 from "process";
1010
+ import checkbox3 from "@inquirer/checkbox";
1011
+ import fs8 from "fs-extra";
1014
1012
  import klaw from "klaw";
1015
1013
  import path13 from "pathe";
1014
+
1015
+ // src/commands/upgrade/overwrite.ts
1016
+ init_esm_shims();
1017
+ import { Buffer as Buffer2 } from "buffer";
1018
+ import checkbox2 from "@inquirer/checkbox";
1019
+ import fs7 from "fs-extra";
1016
1020
  import pc3 from "picocolors";
1021
+ function asBuffer(data) {
1022
+ return typeof data === "string" ? Buffer2.from(data) : data;
1023
+ }
1024
+ async function evaluateWriteIntent(targetPath, options) {
1025
+ const { skipOverwrite, source } = options;
1026
+ const exists = await fs7.pathExists(targetPath);
1027
+ if (!exists) {
1028
+ return {
1029
+ type: "write",
1030
+ reason: "missing"
1031
+ };
1032
+ }
1033
+ if (skipOverwrite) {
1034
+ return {
1035
+ type: "skip",
1036
+ reason: "skipOverwrite"
1037
+ };
1038
+ }
1039
+ const src = asBuffer(source);
1040
+ let destSize = 0;
1041
+ try {
1042
+ const stat = await fs7.stat(targetPath);
1043
+ destSize = stat.size;
1044
+ } catch {
1045
+ return {
1046
+ type: "write",
1047
+ reason: "missing"
1048
+ };
1049
+ }
1050
+ if (destSize !== src.length) {
1051
+ return {
1052
+ type: "prompt",
1053
+ reason: "changed"
1054
+ };
1055
+ }
1056
+ const dest = await fs7.readFile(targetPath);
1057
+ if (!isFileChanged(src, dest)) {
1058
+ return {
1059
+ type: "skip",
1060
+ reason: "identical"
1061
+ };
1062
+ }
1063
+ return {
1064
+ type: "prompt",
1065
+ reason: "changed"
1066
+ };
1067
+ }
1068
+ async function scheduleOverwrite(intent, options) {
1069
+ const { relPath, targetPath, action, pending } = options;
1070
+ if (intent.type === "write") {
1071
+ await action();
1072
+ return;
1073
+ }
1074
+ if (intent.type === "prompt") {
1075
+ pending.push({
1076
+ relPath,
1077
+ targetPath,
1078
+ action
1079
+ });
1080
+ }
1081
+ }
1082
+ async function flushPendingOverwrites(pending) {
1083
+ if (!pending.length) {
1084
+ return;
1085
+ }
1086
+ const selected = await checkbox2({
1087
+ message: "\u68C0\u6D4B\u5230\u4EE5\u4E0B\u6587\u4EF6\u5185\u5BB9\u4E0E\u5F53\u524D\u4ED3\u5E93\u4E0D\u540C\uFF0C\u9009\u62E9\u9700\u8981\u8986\u76D6\u7684\u6587\u4EF6",
1088
+ choices: pending.map((item) => ({
1089
+ name: pc3.greenBright(item.relPath),
1090
+ value: item.targetPath,
1091
+ checked: false
1092
+ })),
1093
+ loop: false
1094
+ });
1095
+ const selectedSet = new Set(selected);
1096
+ for (const item of pending) {
1097
+ if (selectedSet.has(item.targetPath)) {
1098
+ await item.action();
1099
+ }
1100
+ }
1101
+ }
1102
+
1103
+ // src/commands/upgrade/pkg-json.ts
1104
+ init_esm_shims();
1105
+ import { coerce, gte, minVersion } from "semver";
1017
1106
 
1018
1107
  // src/commands/upgrade/scripts.ts
1019
1108
  init_esm_shims();
@@ -1026,6 +1115,90 @@ var scripts = {
1026
1115
  };
1027
1116
  var scriptsEntries = Object.entries(scripts);
1028
1117
 
1118
+ // src/commands/upgrade/pkg-json.ts
1119
+ function isWorkspace(version2) {
1120
+ if (typeof version2 === "string") {
1121
+ return version2.startsWith("workspace:");
1122
+ }
1123
+ return false;
1124
+ }
1125
+ function parseVersion(input) {
1126
+ if (typeof input !== "string" || input.trim().length === 0) {
1127
+ return null;
1128
+ }
1129
+ return minVersion(input) ?? coerce(input);
1130
+ }
1131
+ function shouldAssignVersion(currentVersion, nextVersion) {
1132
+ if (typeof currentVersion !== "string" || currentVersion.trim().length === 0) {
1133
+ return true;
1134
+ }
1135
+ if (currentVersion === nextVersion) {
1136
+ return false;
1137
+ }
1138
+ const current = parseVersion(currentVersion);
1139
+ const next = parseVersion(nextVersion);
1140
+ if (!current || !next) {
1141
+ return true;
1142
+ }
1143
+ return !gte(current, next);
1144
+ }
1145
+ function setPkgJson(sourcePkgJson, targetPkgJson, options) {
1146
+ const packageManager = sourcePkgJson.packageManager ?? "";
1147
+ const sourceDeps = sourcePkgJson.dependencies ?? {};
1148
+ const sourceDevDeps = sourcePkgJson.devDependencies ?? {};
1149
+ const targetDeps = { ...targetPkgJson.dependencies ?? {} };
1150
+ const targetDevDeps = { ...targetPkgJson.devDependencies ?? {} };
1151
+ if (packageManager) {
1152
+ targetPkgJson.packageManager = packageManager;
1153
+ }
1154
+ for (const [depName, depVersion] of Object.entries(sourceDeps)) {
1155
+ if (typeof depVersion !== "string") {
1156
+ continue;
1157
+ }
1158
+ const targetVersion = targetDeps[depName];
1159
+ if (isWorkspace(targetVersion)) {
1160
+ continue;
1161
+ }
1162
+ if (shouldAssignVersion(targetVersion, depVersion)) {
1163
+ targetDeps[depName] = depVersion;
1164
+ }
1165
+ }
1166
+ if (Object.keys(targetDeps).length) {
1167
+ targetPkgJson.dependencies = targetDeps;
1168
+ }
1169
+ for (const [depName, depVersion] of Object.entries(sourceDevDeps)) {
1170
+ if (typeof depVersion !== "string") {
1171
+ continue;
1172
+ }
1173
+ if (depName === name) {
1174
+ const nextVersion = `^${version}`;
1175
+ const targetVersion = targetDevDeps[depName];
1176
+ if (!isWorkspace(targetVersion) && shouldAssignVersion(targetVersion, nextVersion)) {
1177
+ targetDevDeps[depName] = nextVersion;
1178
+ }
1179
+ } else {
1180
+ const targetVersion = targetDevDeps[depName];
1181
+ if (isWorkspace(targetVersion)) {
1182
+ continue;
1183
+ }
1184
+ if (shouldAssignVersion(targetVersion, depVersion)) {
1185
+ targetDevDeps[depName] = depVersion;
1186
+ }
1187
+ }
1188
+ }
1189
+ if (Object.keys(targetDevDeps).length) {
1190
+ targetPkgJson.devDependencies = targetDevDeps;
1191
+ }
1192
+ const scriptPairs = (options == null ? void 0 : options.scripts) ? Object.entries(options.scripts) : scriptsEntries;
1193
+ if (scriptPairs.length) {
1194
+ const scripts2 = { ...targetPkgJson.scripts ?? {} };
1195
+ for (const [scriptName, scriptCmd] of scriptPairs) {
1196
+ scripts2[scriptName] = scriptCmd;
1197
+ }
1198
+ targetPkgJson.scripts = scripts2;
1199
+ }
1200
+ }
1201
+
1029
1202
  // src/commands/upgrade/targets.ts
1030
1203
  init_esm_shims();
1031
1204
  function getAssetTargets(raw) {
@@ -1064,80 +1237,6 @@ function getAssetTargets(raw) {
1064
1237
  }
1065
1238
 
1066
1239
  // src/commands/upgrade/index.ts
1067
- function isWorkspace(version2) {
1068
- if (typeof version2 === "string") {
1069
- return version2.startsWith("workspace:");
1070
- }
1071
- return false;
1072
- }
1073
- function setPkgJson(sourcePkgJson, targetPkgJson, options) {
1074
- const packageManager = index_default(sourcePkgJson, "packageManager", { default: "" });
1075
- const sourceDeps = index_default(sourcePkgJson, "dependencies", { default: {} });
1076
- const sourceDevDeps = index_default(sourcePkgJson, "devDependencies", { default: {} });
1077
- const targetDeps = { ...index_default(targetPkgJson, "dependencies", { default: {} }) };
1078
- const targetDevDeps = { ...index_default(targetPkgJson, "devDependencies", { default: {} }) };
1079
- if (packageManager) {
1080
- targetPkgJson.packageManager = packageManager;
1081
- }
1082
- for (const [depName, depVersion] of Object.entries(sourceDeps)) {
1083
- if (!isWorkspace(targetDeps[depName])) {
1084
- targetDeps[depName] = depVersion;
1085
- }
1086
- }
1087
- if (Object.keys(targetDeps).length) {
1088
- targetPkgJson.dependencies = targetDeps;
1089
- }
1090
- for (const [depName, depVersion] of Object.entries(sourceDevDeps)) {
1091
- if (depName === name) {
1092
- targetDevDeps[depName] = `^${version}`;
1093
- } else if (!isWorkspace(targetDevDeps[depName])) {
1094
- targetDevDeps[depName] = depVersion;
1095
- }
1096
- }
1097
- if (Object.keys(targetDevDeps).length) {
1098
- targetPkgJson.devDependencies = targetDevDeps;
1099
- }
1100
- const scriptPairs = (options == null ? void 0 : options.scripts) ? Object.entries(options.scripts) : scriptsEntries;
1101
- if (scriptPairs.length) {
1102
- const scripts2 = { ...targetPkgJson.scripts ?? {} };
1103
- for (const [scriptName, scriptCmd] of scriptPairs) {
1104
- scripts2[scriptName] = scriptCmd;
1105
- }
1106
- targetPkgJson.scripts = scripts2;
1107
- }
1108
- }
1109
- function confirmOverwrite(filename) {
1110
- return confirm({ message: `${pc3.greenBright(filename)} \u6587\u4EF6\u5185\u5BB9\u53D1\u751F\u6539\u53D8,\u662F\u5426\u8986\u76D6?`, default: true });
1111
- }
1112
- function asBuffer(data) {
1113
- return typeof data === "string" ? Buffer2.from(data) : data;
1114
- }
1115
- async function shouldWriteFile(targetPath, options) {
1116
- const { skipOverwrite, source, promptLabel } = options;
1117
- const exists = await fs7.pathExists(targetPath);
1118
- if (!exists) {
1119
- return true;
1120
- }
1121
- if (skipOverwrite) {
1122
- return false;
1123
- }
1124
- const src = asBuffer(source);
1125
- let destSize = 0;
1126
- try {
1127
- const stat = await fs7.stat(targetPath);
1128
- destSize = stat.size;
1129
- } catch {
1130
- return true;
1131
- }
1132
- if (destSize !== src.length) {
1133
- return confirmOverwrite(promptLabel);
1134
- }
1135
- const dest = await fs7.readFile(targetPath);
1136
- if (!isFileChanged(src, dest)) {
1137
- return false;
1138
- }
1139
- return confirmOverwrite(promptLabel);
1140
- }
1141
1240
  async function upgradeMonorepo(opts) {
1142
1241
  const cwd = opts.cwd ?? process2.cwd();
1143
1242
  const upgradeConfig = await resolveCommandConfig("upgrade", cwd);
@@ -1158,7 +1257,7 @@ async function upgradeMonorepo(opts) {
1158
1257
  const mergeTargets = upgradeConfig == null ? void 0 : upgradeConfig.mergeTargets;
1159
1258
  let targets = configTargets.length ? mergeTargets === false ? [...configTargets] : Array.from(/* @__PURE__ */ new Set([...baseTargets, ...configTargets])) : baseTargets;
1160
1259
  if (merged.interactive) {
1161
- targets = await checkbox2({
1260
+ targets = await checkbox3({
1162
1261
  message: "\u9009\u62E9\u4F60\u9700\u8981\u7684\u6587\u4EF6",
1163
1262
  choices: targets.map((x) => {
1164
1263
  return {
@@ -1174,6 +1273,7 @@ async function upgradeMonorepo(opts) {
1174
1273
  const skipChangesetMarkdown = (upgradeConfig == null ? void 0 : upgradeConfig.skipChangesetMarkdown) ?? true;
1175
1274
  const scriptOverrides = upgradeConfig == null ? void 0 : upgradeConfig.scripts;
1176
1275
  const skipOverwrite = merged.skipOverwrite;
1276
+ const pendingOverwrites = [];
1177
1277
  for await (const file of klaw(assetsDir, {
1178
1278
  filter(p) {
1179
1279
  const str = path13.relative(assetsDir, p);
@@ -1193,43 +1293,72 @@ async function upgradeMonorepo(opts) {
1193
1293
  const targetPath = path13.resolve(absOutDir, relPath);
1194
1294
  try {
1195
1295
  if (relPath === "package.json") {
1196
- if (!await fs7.pathExists(targetPath)) {
1296
+ if (!await fs8.pathExists(targetPath)) {
1197
1297
  continue;
1198
1298
  }
1199
- const sourcePkgJson = await fs7.readJson(file.path);
1200
- const targetPkgJson = await fs7.readJson(targetPath);
1299
+ const sourcePkgJson = await fs8.readJson(file.path);
1300
+ const targetPkgJson = await fs8.readJson(targetPath);
1201
1301
  setPkgJson(sourcePkgJson, targetPkgJson, { scripts: scriptOverrides });
1202
1302
  const data = `${JSON.stringify(targetPkgJson, void 0, 2)}
1203
1303
  `;
1204
- if (await shouldWriteFile(targetPath, { skipOverwrite, source: data, promptLabel: relPath })) {
1205
- await fs7.outputFile(targetPath, data, "utf8");
1304
+ const intent2 = await evaluateWriteIntent(targetPath, { skipOverwrite, source: data });
1305
+ const action2 = async () => {
1306
+ await fs8.outputFile(targetPath, data, "utf8");
1206
1307
  logger.success(targetPath);
1207
- }
1308
+ };
1309
+ await scheduleOverwrite(intent2, {
1310
+ relPath,
1311
+ targetPath,
1312
+ action: action2,
1313
+ pending: pendingOverwrites
1314
+ });
1208
1315
  continue;
1209
1316
  }
1210
1317
  if (relPath === ".changeset/config.json" && repoName) {
1211
- const changesetJson = await fs7.readJson(file.path);
1318
+ const changesetJson = await fs8.readJson(file.path);
1212
1319
  (0, import_set_value6.default)(changesetJson, "changelog.1.repo", repoName);
1213
1320
  const data = `${JSON.stringify(changesetJson, void 0, 2)}
1214
1321
  `;
1215
- if (await shouldWriteFile(targetPath, { skipOverwrite, source: data, promptLabel: relPath })) {
1216
- await fs7.outputFile(targetPath, data, "utf8");
1322
+ const intent2 = await evaluateWriteIntent(targetPath, { skipOverwrite, source: data });
1323
+ const action2 = async () => {
1324
+ await fs8.outputFile(targetPath, data, "utf8");
1217
1325
  logger.success(targetPath);
1218
- }
1326
+ };
1327
+ await scheduleOverwrite(intent2, {
1328
+ relPath,
1329
+ targetPath,
1330
+ action: action2,
1331
+ pending: pendingOverwrites
1332
+ });
1219
1333
  continue;
1220
1334
  }
1221
1335
  if (relPath === "LICENSE") {
1222
- const source = await fs7.readFile(file.path);
1223
- if (await shouldWriteFile(targetPath, { skipOverwrite: true, source, promptLabel: relPath })) {
1224
- await fs7.copy(file.path, targetPath);
1336
+ const source2 = await fs8.readFile(file.path);
1337
+ const intent2 = await evaluateWriteIntent(targetPath, { skipOverwrite: true, source: source2 });
1338
+ const action2 = async () => {
1339
+ await fs8.outputFile(targetPath, source2);
1225
1340
  logger.success(targetPath);
1226
- }
1341
+ };
1342
+ await scheduleOverwrite(intent2, {
1343
+ relPath,
1344
+ targetPath,
1345
+ action: action2,
1346
+ pending: pendingOverwrites
1347
+ });
1227
1348
  continue;
1228
1349
  }
1229
- if (await shouldWriteFile(targetPath, { skipOverwrite, source: await fs7.readFile(file.path), promptLabel: relPath })) {
1230
- await fs7.copy(file.path, targetPath);
1350
+ const source = await fs8.readFile(file.path);
1351
+ const intent = await evaluateWriteIntent(targetPath, { skipOverwrite, source });
1352
+ const action = async () => {
1353
+ await fs8.outputFile(targetPath, source);
1231
1354
  logger.success(targetPath);
1232
- }
1355
+ };
1356
+ await scheduleOverwrite(intent, {
1357
+ relPath,
1358
+ targetPath,
1359
+ action,
1360
+ pending: pendingOverwrites
1361
+ });
1233
1362
  } catch (error) {
1234
1363
  if (isIgnorableFsError(error)) {
1235
1364
  continue;
@@ -1237,6 +1366,7 @@ async function upgradeMonorepo(opts) {
1237
1366
  throw error;
1238
1367
  }
1239
1368
  }
1369
+ await flushPendingOverwrites(pendingOverwrites);
1240
1370
  }
1241
1371
 
1242
1372
  // src/commands/index.ts