@cmssy/codemod 7.0.0 → 8.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.
Files changed (2) hide show
  1. package/dist/index.js +86 -12
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -162,8 +162,62 @@ 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
+ function transform3(source) {
199
+ const { code, changed } = stripTypeArguments(source);
200
+ const notes = [];
201
+ const usesBlockProps = /\bBlockProps\s*</.test(code);
202
+ if (/\bdefineBlock\s*[<(]/.test(code) && !usesBlockProps) {
203
+ notes.push(
204
+ "declares a block - type its component as BlockProps<typeof props>"
205
+ );
206
+ } else if (HAND_TYPED_CONTENT.test(code) && !usesBlockProps) {
207
+ notes.push(
208
+ "types a block's content by hand - derive it from the schema instead"
209
+ );
210
+ }
211
+ return notes.length > 0 ? { code, changed, notes } : { code, changed };
212
+ }
213
+
165
214
  // src/index.ts
166
- var TRANSFORMS = { v5: transform, v7: transform2 };
215
+ var TRANSFORMS = { v5: transform, v7: transform2, v8: transform3 };
216
+ var PREVIOUS_MAJOR = {
217
+ v5: "4.x",
218
+ v7: "6.x",
219
+ v8: "7.x"
220
+ };
167
221
  var SKIP = /* @__PURE__ */ new Set(["node_modules", "dist", "build", "out", "coverage"]);
168
222
  var EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs"];
169
223
  function skipDirectory(name) {
@@ -185,9 +239,9 @@ async function sourceFiles(dir) {
185
239
  async function main() {
186
240
  const args = process.argv.slice(2);
187
241
  const version = args[0];
188
- const transform3 = TRANSFORMS[version];
189
- if (!transform3) {
190
- console.error("usage: cmssy-codemod v5|v7 [path] [--dry]");
242
+ const transform4 = TRANSFORMS[version];
243
+ if (!transform4) {
244
+ console.error("usage: cmssy-codemod v5|v7|v8 [path] [--dry]");
191
245
  process.exitCode = 1;
192
246
  return;
193
247
  }
@@ -197,24 +251,44 @@ async function main() {
197
251
  );
198
252
  const files = await sourceFiles(target);
199
253
  const touched = [];
254
+ const manual = [];
200
255
  let needsCore = false;
201
256
  for (const file of files) {
202
257
  const source = await readFile(file, "utf8");
203
- const { code, changed } = transform3(source);
258
+ const { code, changed, notes } = transform4(source);
259
+ if (notes && notes.length > 0) manual.push({ file, notes });
204
260
  if (!changed) continue;
205
261
  touched.push(file);
206
262
  if (code.includes('from "@cmssy/core"')) needsCore = true;
207
263
  if (!dry) await writeFile(file, code);
208
264
  }
209
- if (touched.length === 0) {
210
- console.log("cmssy: nothing to migrate - no 4.x imports found.");
265
+ const report = (file) => file.slice(target.length + 1);
266
+ if (touched.length === 0 && manual.length === 0) {
267
+ console.log(
268
+ `cmssy: nothing to migrate - no ${PREVIOUS_MAJOR[version]} imports found.`
269
+ );
211
270
  return;
212
271
  }
213
- console.log(
214
- `cmssy: ${dry ? "would rewrite" : "rewrote"} ${touched.length} file(s):`
215
- );
216
- for (const file of touched) {
217
- console.log(` ${file.slice(target.length + 1)}`);
272
+ if (touched.length > 0) {
273
+ console.log(
274
+ `cmssy: ${dry ? "would rewrite" : "rewrote"} ${touched.length} file(s):`
275
+ );
276
+ for (const file of touched) console.log(` ${report(file)}`);
277
+ }
278
+ if (manual.length > 0) {
279
+ console.log(
280
+ `
281
+ cmssy: ${manual.length} file(s) need a human - a block's content must be
282
+ derived from its schema, and only you know which fields it means to read:
283
+ `
284
+ );
285
+ for (const { file, notes } of manual) {
286
+ console.log(` ${report(file)}
287
+ ${notes.join("\n ")}`);
288
+ }
289
+ console.log(
290
+ "\n https://github.com/cmssy-io/cmssy-sdk/blob/main/docs/migrations/v7-to-v8.md"
291
+ );
218
292
  }
219
293
  if (needsCore && !await dependsOnCore(target)) {
220
294
  console.log(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmssy/codemod",
3
- "version": "7.0.0",
3
+ "version": "8.0.0",
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",