@sps-woodland/codemods 0.0.2 → 0.0.3
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/transform.js +1 -0
- package/main.js +1 -1
- package/package.json +3 -2
- package/vite.config.js +14 -0
- package/changeList.js +0 -3
- package/mods/imports.js +0 -46
- package/mods/jsx.js +0 -32
- package/sps-woodland-codemods-0.0.1.tgz +0 -0
- package/transform.js +0 -46
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function P(s,e,t){const o=s.find(e.ImportDeclaration),r=new Map;for(const c of Object.keys(t.byOldPackage)){const{oldNames:l,newPackages:n}=t.byOldPackage[c];o.filter(d=>d.node.source.value===c).forEach(d=>{const k=e(d).replaceWith(e.importDeclaration(d.node.specifiers.filter(a=>!l.has(a.imported.name)),d.node.source));for(const a of Object.keys(n)){const p=n[a];if(r.has(a)){const i=r.get(a),m=e.importDeclaration([...i.node.specifiers,...p.map(w=>e.importSpecifier(e.identifier(w)))],i.node.source);r.set(a,m),e(i).replaceWith(m)}else{const i=e.importDeclaration(p.map(m=>e.importSpecifier(e.identifier(m))),e.literal(a));r.set(a,i),k.insertAfter(i)}}})}}function u(s,e,t){const o=s.find(e.JSXOpeningElement),r=s.find(e.JSXClosingElement);for(const{oldName:c,newName:l}of t.list)o.filter(n=>n.node.name.name===c).forEach(n=>{e(n).replaceWith(e.jsxOpeningElement(e.jsxIdentifier(l),n.node.attributes,n.node.selfClosing))}),r.filter(n=>n.node.name.name===c).forEach(n=>{e(n).replaceWith(e.jsxClosingElement(e.jsxIdentifier(l),n.node.attributes,n.node.selfClosing))})}const g=[{oldName:"SpsCardV2",newName:"Card",newPackage:"@woodland/cards",oldPackage:"@spscommerce/ds-react"}],N=g.reduce((s,e)=>{let t=s[e.oldPackage];t||(s[e.oldPackage]=t={oldNames:new Set,newPackages:{}}),t.oldNames.add(e.oldName);let o=t.newPackages[e.newPackage];return o||(t.newPackages[e.newPackage]=o=[]),o.push(e.newName),s},{}),f={list:g,byOldPackage:N};function h(s,{jscodeshift:e},t){const o=e(s.source);return P(o,e,f),u(o,e,f),o.toSource()}module.exports=h;
|
package/main.js
CHANGED
|
@@ -14,7 +14,7 @@ const yargv = yargs
|
|
|
14
14
|
|
|
15
15
|
console.log(yargv);
|
|
16
16
|
|
|
17
|
-
run("transform.js", ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"], {
|
|
17
|
+
run("node_modules/@sps-woodland/codemods/dist/transform.js", ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"], {
|
|
18
18
|
dry: true,
|
|
19
19
|
print: true,
|
|
20
20
|
verbose: 1,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sps-woodland/codemods",
|
|
3
3
|
"description": "SPS Woodland Design System codemods",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"author": "SPS Commerce",
|
|
6
6
|
"license": "UNLICENSED",
|
|
7
7
|
"repository": "https://github.com/spscommerce/woodland/tree/master/packages/@sps-woodland/codemods",
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
"yargs": "^12.0.2"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
|
+
"build": "vite build",
|
|
23
24
|
"clean": "git clean -fdX",
|
|
24
|
-
"pub": "pnpm pack &&
|
|
25
|
+
"pub": "pnpm pack && npm publish"
|
|
25
26
|
}
|
|
26
27
|
}
|
package/vite.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { defineConfig } from "vite";
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
build: {
|
|
6
|
+
lib: {
|
|
7
|
+
entry: path.resolve(__dirname, "src/transform.js"),
|
|
8
|
+
formats: ["cjs"],
|
|
9
|
+
fileName: "transform",
|
|
10
|
+
},
|
|
11
|
+
outDir: path.resolve(__dirname, "./dist"),
|
|
12
|
+
emptyOutDir: true,
|
|
13
|
+
},
|
|
14
|
+
});
|
package/changeList.js
DELETED
package/mods/imports.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
export function modImports(source, j, changes) {
|
|
2
|
-
// Find all nodes that match a type of `ImportDeclaration`
|
|
3
|
-
const imports = source.find(j.ImportDeclaration);
|
|
4
|
-
const newPackageImports = new Map();
|
|
5
|
-
|
|
6
|
-
for (const oldPackage of Object.keys(changes.byOldPackage)) {
|
|
7
|
-
const { oldNames, newPackages } = changes.byOldPackage[oldPackage];
|
|
8
|
-
imports
|
|
9
|
-
.filter(path => path.node.source.value === oldPackage)
|
|
10
|
-
.forEach((oldImport) => {
|
|
11
|
-
const jImport = j(oldImport)
|
|
12
|
-
// Replace the existing node with a new one
|
|
13
|
-
.replaceWith(
|
|
14
|
-
// Build a new import declaration node based on the existing one for stuff still coming from the old package
|
|
15
|
-
j.importDeclaration(
|
|
16
|
-
// copy over the existing import specificers (things being imported) not in the list
|
|
17
|
-
oldImport.node.specifiers.filter(specifier => !oldNames.has(specifier.imported.name)),
|
|
18
|
-
// Keep source (where it's being imported from)
|
|
19
|
-
oldImport.node.source,
|
|
20
|
-
),
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
for (const newPackage of Object.keys(newPackages)) {
|
|
24
|
-
const newNames = newPackages[newPackage];
|
|
25
|
-
// if there's already an import created for this package, we need to update it (via replacement, as above ^)
|
|
26
|
-
// otherwise we create one
|
|
27
|
-
if (newPackageImports.has(newPackage)) {
|
|
28
|
-
const newImport = newPackageImports.get(newPackage);
|
|
29
|
-
const newNewImport = j.importDeclaration(
|
|
30
|
-
[...newImport.node.specifiers, ...newNames.map((name) => j.importSpecifier(j.identifier(name)))],
|
|
31
|
-
newImport.node.source,
|
|
32
|
-
);
|
|
33
|
-
newPackageImports.set(newPackage, newNewImport);
|
|
34
|
-
j(newImport).replaceWith(newNewImport);
|
|
35
|
-
} else {
|
|
36
|
-
const newImport = j.importDeclaration(
|
|
37
|
-
newNames.map((name) => j.importSpecifier(j.identifier(name))),
|
|
38
|
-
j.literal(newPackage),
|
|
39
|
-
);
|
|
40
|
-
newPackageImports.set(newPackage, newImport);
|
|
41
|
-
jImport.insertAfter(newImport);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
}
|
package/mods/jsx.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export function modJsx(source, j, changes) {
|
|
2
|
-
const jsxOpeningElements = source.find(j.JSXOpeningElement);
|
|
3
|
-
const jsxClosingElements = source.find(j.JSXClosingElement);
|
|
4
|
-
|
|
5
|
-
for (const { oldName, newName } of changes.list) {
|
|
6
|
-
jsxOpeningElements
|
|
7
|
-
.filter(element => element.node.name.name === oldName)
|
|
8
|
-
.forEach(jsxElement => {
|
|
9
|
-
j(jsxElement)
|
|
10
|
-
.replaceWith(
|
|
11
|
-
j.jsxOpeningElement(
|
|
12
|
-
j.jsxIdentifier(newName),
|
|
13
|
-
jsxElement.node.attributes,
|
|
14
|
-
jsxElement.node.selfClosing,
|
|
15
|
-
)
|
|
16
|
-
)
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
jsxClosingElements
|
|
20
|
-
.filter(element => element.node.name.name === oldName)
|
|
21
|
-
.forEach(jsxElement => {
|
|
22
|
-
j(jsxElement)
|
|
23
|
-
.replaceWith(
|
|
24
|
-
j.jsxClosingElement(
|
|
25
|
-
j.jsxIdentifier(newName),
|
|
26
|
-
jsxElement.node.attributes,
|
|
27
|
-
jsxElement.node.selfClosing,
|
|
28
|
-
)
|
|
29
|
-
)
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
Binary file
|
package/transform.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { modImports } from "./mods/imports";
|
|
2
|
-
import { modJsx } from "./mods/jsx";
|
|
3
|
-
import { changeList } from "./changeList";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* changesByPackage example result:
|
|
7
|
-
* {
|
|
8
|
-
* "@spscommerce/ds-react": {
|
|
9
|
-
* oldNames: new Set(["SpsCardV2", "SpsCardHeaderV2", "SpsTabsV2"]),
|
|
10
|
-
* newPackages: {
|
|
11
|
-
* "@woodland/cards": ["Card", "CardHeader"],
|
|
12
|
-
* "@woodland/tabs": ["Tabs"]
|
|
13
|
-
* }
|
|
14
|
-
* }
|
|
15
|
-
* }
|
|
16
|
-
*/
|
|
17
|
-
const changesByPackage = changeList.reduce((acc, changeInfo) => {
|
|
18
|
-
let entryForThisOldPkg = acc[changeInfo.oldPackage];
|
|
19
|
-
if (!entryForThisOldPkg) {
|
|
20
|
-
acc[changeInfo.oldPackage] = entryForThisOldPkg = {
|
|
21
|
-
oldNames: new Set(),
|
|
22
|
-
newPackages: {}
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
entryForThisOldPkg.oldNames.add(changeInfo.oldName);
|
|
26
|
-
let newPkgEntry = entryForThisOldPkg.newPackages[changeInfo.newPackage];
|
|
27
|
-
if (!newPkgEntry) {
|
|
28
|
-
entryForThisOldPkg.newPackages[changeInfo.newPackage] = newPkgEntry = [];
|
|
29
|
-
}
|
|
30
|
-
newPkgEntry.push(changeInfo.newName);
|
|
31
|
-
return acc;
|
|
32
|
-
}, {});
|
|
33
|
-
|
|
34
|
-
const changes = {
|
|
35
|
-
list: changeList,
|
|
36
|
-
byOldPackage: changesByPackage,
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export default function transformer(file, { jscodeshift: j }, options) {
|
|
40
|
-
const source = j(file.source);
|
|
41
|
-
|
|
42
|
-
modImports(source, j, changes);
|
|
43
|
-
modJsx(source, j, changes);
|
|
44
|
-
|
|
45
|
-
return source.toSource();
|
|
46
|
-
}
|