@cmssy/codemod 7.0.1 → 8.0.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/index.js +84 -12
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -162,9 +162,63 @@ function transform2(source) {
162
162
  return { code, changed };
163
163
  }
164
164
 
165
+ // src/v8.ts
166
+ var HAND_TYPED_CONTENT = /content\s*:\s*(Record<string,\s*unknown>|\{[^}]*\}|[A-Z]\w*(Content|Props))/;
167
+ function stripTypeArguments(source) {
168
+ const marker = "defineBlock<";
169
+ let code = source;
170
+ let changed = false;
171
+ for (let start = code.indexOf(marker); start !== -1; ) {
172
+ const open = start + marker.length - 1;
173
+ let depth = 0;
174
+ let end = -1;
175
+ for (let i = open; i < code.length; i++) {
176
+ const char = code[i];
177
+ if (char === "<") depth++;
178
+ else if (char === ">") {
179
+ depth--;
180
+ if (depth === 0) {
181
+ end = i;
182
+ break;
183
+ }
184
+ } else if (char === "(" || char === ";" || char === "\n") {
185
+ break;
186
+ }
187
+ }
188
+ if (end === -1) {
189
+ start = code.indexOf(marker, start + marker.length);
190
+ continue;
191
+ }
192
+ code = code.slice(0, open) + code.slice(end + 1);
193
+ changed = true;
194
+ start = code.indexOf(marker);
195
+ }
196
+ return { code, changed };
197
+ }
198
+ var INLINE_SCHEMA = /props\s*:\s*\{/;
199
+ function transform3(source) {
200
+ const { code, changed } = stripTypeArguments(source);
201
+ const notes = [];
202
+ const usesBlockProps = /\bBlockProps\s*</.test(code);
203
+ if (/\bdefineBlock\s*\(/.test(code) && INLINE_SCHEMA.test(code)) {
204
+ notes.push(
205
+ "the schema is inline - export it, and type the component with BlockProps<typeof props>"
206
+ );
207
+ } else if (HAND_TYPED_CONTENT.test(code) && !usesBlockProps) {
208
+ notes.push(
209
+ "types a block's content by hand - derive it from the schema instead"
210
+ );
211
+ }
212
+ return notes.length > 0 ? { code, changed, notes } : { code, changed };
213
+ }
214
+
165
215
  // src/index.ts
166
- var TRANSFORMS = { v5: transform, v7: transform2 };
167
- var PREVIOUS_MAJOR = { v5: "4.x", v7: "6.x" };
216
+ var TRANSFORMS = { v5: transform, v7: transform2, v8: transform3 };
217
+ var PREVIOUS_MAJOR = {
218
+ v5: "4.x",
219
+ v7: "6.x",
220
+ v8: "7.x"
221
+ };
168
222
  var SKIP = /* @__PURE__ */ new Set(["node_modules", "dist", "build", "out", "coverage"]);
169
223
  var EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs"];
170
224
  function skipDirectory(name) {
@@ -186,9 +240,9 @@ async function sourceFiles(dir) {
186
240
  async function main() {
187
241
  const args = process.argv.slice(2);
188
242
  const version = args[0];
189
- const transform3 = TRANSFORMS[version];
190
- if (!transform3) {
191
- console.error("usage: cmssy-codemod v5|v7 [path] [--dry]");
243
+ const transform4 = TRANSFORMS[version];
244
+ if (!transform4) {
245
+ console.error("usage: cmssy-codemod v5|v7|v8 [path] [--dry]");
192
246
  process.exitCode = 1;
193
247
  return;
194
248
  }
@@ -198,26 +252,44 @@ async function main() {
198
252
  );
199
253
  const files = await sourceFiles(target);
200
254
  const touched = [];
255
+ const manual = [];
201
256
  let needsCore = false;
202
257
  for (const file of files) {
203
258
  const source = await readFile(file, "utf8");
204
- const { code, changed } = transform3(source);
259
+ const { code, changed, notes } = transform4(source);
260
+ if (notes && notes.length > 0) manual.push({ file, notes });
205
261
  if (!changed) continue;
206
262
  touched.push(file);
207
263
  if (code.includes('from "@cmssy/core"')) needsCore = true;
208
264
  if (!dry) await writeFile(file, code);
209
265
  }
210
- if (touched.length === 0) {
266
+ const report = (file) => file.slice(target.length + 1);
267
+ if (touched.length === 0 && manual.length === 0) {
211
268
  console.log(
212
269
  `cmssy: nothing to migrate - no ${PREVIOUS_MAJOR[version]} imports found.`
213
270
  );
214
271
  return;
215
272
  }
216
- console.log(
217
- `cmssy: ${dry ? "would rewrite" : "rewrote"} ${touched.length} file(s):`
218
- );
219
- for (const file of touched) {
220
- console.log(` ${file.slice(target.length + 1)}`);
273
+ if (touched.length > 0) {
274
+ console.log(
275
+ `cmssy: ${dry ? "would rewrite" : "rewrote"} ${touched.length} file(s):`
276
+ );
277
+ for (const file of touched) console.log(` ${report(file)}`);
278
+ }
279
+ if (manual.length > 0) {
280
+ console.log(
281
+ `
282
+ cmssy: ${manual.length} file(s) need a human - a block's content must be
283
+ derived from its schema, and only you know which fields it means to read:
284
+ `
285
+ );
286
+ for (const { file, notes } of manual) {
287
+ console.log(` ${report(file)}
288
+ ${notes.join("\n ")}`);
289
+ }
290
+ console.log(
291
+ "\n https://github.com/cmssy-io/cmssy-sdk/blob/main/docs/migrations/v7-to-v8.md"
292
+ );
221
293
  }
222
294
  if (needsCore && !await dependsOnCore(target)) {
223
295
  console.log(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmssy/codemod",
3
- "version": "7.0.1",
3
+ "version": "8.0.1",
4
4
  "description": "Rewrites cmssy imports across major versions, so a breaking release costs minutes instead of an afternoon.",
5
5
  "keywords": [
6
6
  "cmssy",