@cmssy/codemod 5.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.
- package/dist/index.js +174 -0
- package/package.json +38 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { readFile, readdir, writeFile } from "fs/promises";
|
|
5
|
+
import { join, resolve } from "path";
|
|
6
|
+
|
|
7
|
+
// src/v5.ts
|
|
8
|
+
var RENAMES = {
|
|
9
|
+
CmssyNextConfig: "CmssyConfig",
|
|
10
|
+
clearCartWorkspaceIdCache: "clearWorkspaceIdCache"
|
|
11
|
+
};
|
|
12
|
+
var SERVER_SYMBOLS = /* @__PURE__ */ new Set([
|
|
13
|
+
"createCmssyPage",
|
|
14
|
+
"createCmssyEditPage",
|
|
15
|
+
"createCmssyNotFound",
|
|
16
|
+
"CmssyChrome",
|
|
17
|
+
"CmssyChromeProps",
|
|
18
|
+
"CreateCmssyNotFoundOptions",
|
|
19
|
+
"buildCmssyMetadata",
|
|
20
|
+
"BuildCmssyMetadataOptions",
|
|
21
|
+
"createCmssyRobots",
|
|
22
|
+
"CreateCmssyRobotsOptions",
|
|
23
|
+
"createCmssySitemap",
|
|
24
|
+
"CreateCmssySitemapOptions",
|
|
25
|
+
"CmssySitemapContext",
|
|
26
|
+
"createCmssyAuthRoute",
|
|
27
|
+
"CmssyAuthRouteHandlers",
|
|
28
|
+
"createCmssyCartRoute",
|
|
29
|
+
"CmssyCartRouteHandlers",
|
|
30
|
+
"CMSSY_CART_COOKIE",
|
|
31
|
+
"createCmssyOrdersRoute",
|
|
32
|
+
"CmssyOrdersRouteHandlers",
|
|
33
|
+
"createDraftRoute",
|
|
34
|
+
"CmssyDraftRouteConfig",
|
|
35
|
+
"getCmssyUser",
|
|
36
|
+
"getCmssyAccessToken",
|
|
37
|
+
"getCmssyLocale",
|
|
38
|
+
"isCmssyEditMode",
|
|
39
|
+
"fetchProducts",
|
|
40
|
+
"fetchProduct"
|
|
41
|
+
]);
|
|
42
|
+
var MIDDLEWARE_SYMBOLS = /* @__PURE__ */ new Set([
|
|
43
|
+
"createCmssyProxy",
|
|
44
|
+
"cmssyProxyMatcher",
|
|
45
|
+
"CmssyProxyOptions",
|
|
46
|
+
"cmssyEditRewrite",
|
|
47
|
+
"createCmssyEditMiddleware",
|
|
48
|
+
"CMSSY_EDIT_PATH_PREFIX",
|
|
49
|
+
"createCmssyLocaleMiddleware",
|
|
50
|
+
"resolveLocaleFromPathname",
|
|
51
|
+
"createCmssyAuthMiddleware",
|
|
52
|
+
"CmssyAuthMiddleware",
|
|
53
|
+
"isCmssyEditRequest",
|
|
54
|
+
"applyCmssyCsp",
|
|
55
|
+
"cmssyCspHeaders",
|
|
56
|
+
"CmssyCspOptions",
|
|
57
|
+
"localeForPathname"
|
|
58
|
+
]);
|
|
59
|
+
var CLIENT_SYMBOLS = /* @__PURE__ */ new Set([
|
|
60
|
+
"CmssyLink",
|
|
61
|
+
"CmssyLinkProps",
|
|
62
|
+
"CmssyLocaleProvider",
|
|
63
|
+
"CmssyLocaleProviderProps",
|
|
64
|
+
"useCmssyLocale"
|
|
65
|
+
]);
|
|
66
|
+
var ENTRY_FOR = (symbol) => {
|
|
67
|
+
if (SERVER_SYMBOLS.has(symbol)) return "@cmssy/next/server";
|
|
68
|
+
if (MIDDLEWARE_SYMBOLS.has(symbol)) return "@cmssy/next/middleware";
|
|
69
|
+
if (CLIENT_SYMBOLS.has(symbol)) return "@cmssy/next/client";
|
|
70
|
+
return "@cmssy/next";
|
|
71
|
+
};
|
|
72
|
+
var IMPORT = /import\s+(type\s+)?\{([^}]*)\}\s+from\s+["']@cmssy\/next(?:\/preset)?["'];?/g;
|
|
73
|
+
function parseSpecifiers(body) {
|
|
74
|
+
return body.split(",").map((part) => part.trim()).filter(Boolean).map((raw) => {
|
|
75
|
+
const withoutType = raw.replace(/^type\s+/, "");
|
|
76
|
+
const name = (withoutType.split(/\s+as\s+/)[0] ?? withoutType).trim();
|
|
77
|
+
return { raw, name };
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function applyRenames(specifier) {
|
|
81
|
+
const replacement = RENAMES[specifier.name];
|
|
82
|
+
if (!replacement) return specifier;
|
|
83
|
+
return {
|
|
84
|
+
name: replacement,
|
|
85
|
+
raw: specifier.raw.replace(specifier.name, replacement)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function transform(source) {
|
|
89
|
+
let changed = false;
|
|
90
|
+
let code = source.replace(IMPORT, (match, typeOnly, body) => {
|
|
91
|
+
const specifiers = parseSpecifiers(body).map(applyRenames);
|
|
92
|
+
if (specifiers.length === 0) return match;
|
|
93
|
+
const byEntry = /* @__PURE__ */ new Map();
|
|
94
|
+
for (const specifier of specifiers) {
|
|
95
|
+
const entry = ENTRY_FOR(specifier.name);
|
|
96
|
+
const bucket = byEntry.get(entry) ?? [];
|
|
97
|
+
bucket.push(specifier.raw);
|
|
98
|
+
byEntry.set(entry, bucket);
|
|
99
|
+
}
|
|
100
|
+
const prefix = typeOnly ? "import type " : "import ";
|
|
101
|
+
const rewritten = [...byEntry].map(
|
|
102
|
+
([entry, names]) => `${prefix}{ ${names.join(", ")} } from "${entry}";`
|
|
103
|
+
).join("\n");
|
|
104
|
+
if (rewritten !== match) changed = true;
|
|
105
|
+
return rewritten;
|
|
106
|
+
});
|
|
107
|
+
for (const [from, to] of Object.entries(RENAMES)) {
|
|
108
|
+
const pattern = new RegExp(`\\b${from}\\b`, "g");
|
|
109
|
+
if (pattern.test(code)) {
|
|
110
|
+
code = code.replace(pattern, to);
|
|
111
|
+
changed = true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { code, changed };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/index.ts
|
|
118
|
+
var SKIP = /* @__PURE__ */ new Set(["node_modules", "dist", "build", "out", "coverage"]);
|
|
119
|
+
var EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs"];
|
|
120
|
+
function skipDirectory(name) {
|
|
121
|
+
return name.startsWith(".") || SKIP.has(name);
|
|
122
|
+
}
|
|
123
|
+
async function sourceFiles(dir) {
|
|
124
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
125
|
+
const files = await Promise.all(
|
|
126
|
+
entries.map(async (entry) => {
|
|
127
|
+
const path = join(dir, entry.name);
|
|
128
|
+
if (entry.isDirectory()) {
|
|
129
|
+
return skipDirectory(entry.name) ? [] : sourceFiles(path);
|
|
130
|
+
}
|
|
131
|
+
return EXTENSIONS.some((ext) => entry.name.endsWith(ext)) ? [path] : [];
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
return files.flat();
|
|
135
|
+
}
|
|
136
|
+
async function main() {
|
|
137
|
+
const args = process.argv.slice(2);
|
|
138
|
+
const version = args[0];
|
|
139
|
+
if (version !== "v5") {
|
|
140
|
+
console.error("usage: cmssy-codemod v5 [path] [--dry]");
|
|
141
|
+
process.exitCode = 1;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const dry = args.includes("--dry");
|
|
145
|
+
const target = resolve(
|
|
146
|
+
args.find((a) => !a.startsWith("-") && a !== "v5") ?? "."
|
|
147
|
+
);
|
|
148
|
+
const files = await sourceFiles(target);
|
|
149
|
+
const touched = [];
|
|
150
|
+
for (const file of files) {
|
|
151
|
+
const source = await readFile(file, "utf8");
|
|
152
|
+
const { code, changed } = transform(source);
|
|
153
|
+
if (!changed) continue;
|
|
154
|
+
touched.push(file);
|
|
155
|
+
if (!dry) await writeFile(file, code);
|
|
156
|
+
}
|
|
157
|
+
if (touched.length === 0) {
|
|
158
|
+
console.log("cmssy: nothing to migrate - no 4.x imports found.");
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
console.log(
|
|
162
|
+
`cmssy: ${dry ? "would rewrite" : "rewrote"} ${touched.length} file(s):`
|
|
163
|
+
);
|
|
164
|
+
for (const file of touched) {
|
|
165
|
+
console.log(` ${file.slice(target.length + 1)}`);
|
|
166
|
+
}
|
|
167
|
+
console.log(
|
|
168
|
+
"\nThe imports moved; the wiring did not. Run your build, then the editor\nsmoke test - a site whose editor is dead still builds:\n https://github.com/cmssy-io/cmssy-sdk/blob/main/docs/testing.md"
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
main().catch((error) => {
|
|
172
|
+
console.error(error);
|
|
173
|
+
process.exitCode = 1;
|
|
174
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cmssy/codemod",
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"description": "Rewrites cmssy imports across major versions, so a breaking release costs minutes instead of an afternoon.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cmssy",
|
|
7
|
+
"codemod",
|
|
8
|
+
"migration"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://cmssy.com",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/cmssy-io/cmssy-sdk.git",
|
|
14
|
+
"directory": "packages/codemod"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"bin": {
|
|
22
|
+
"cmssy-codemod": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.0.0",
|
|
29
|
+
"tsup": "^8.3.0",
|
|
30
|
+
"typescript": "^5.6.0",
|
|
31
|
+
"vitest": "^2.1.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"typecheck": "tsc --noEmit"
|
|
37
|
+
}
|
|
38
|
+
}
|