@sps-woodland/codemods 0.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.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [@sps-woodland/codemods](https://github.com/SPSCommerce/woodland/tree/master/packages/@sps-woodland/codemods#readme)
2
+
3
+ SPS Woodland Design System codemods
package/changeList.js ADDED
@@ -0,0 +1,3 @@
1
+ export const changeList = [
2
+ { oldName: "SpsCardV2", newName: "Card", newPackage: "@woodland/cards", oldPackage: "@spscommerce/ds-react" }
3
+ ];
package/main.js ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env node
2
+ const yargs = require("yargs");
3
+ const { run } = require("jscodeshift/src/Runner")
4
+
5
+ const yargv = yargs
6
+ .usage("Usage: $0 [options]")
7
+ .option("verbose", {
8
+ alias: "v",
9
+ type: "boolean",
10
+ description: "Print additional details to console"
11
+ })
12
+ .help("help")
13
+ .alias("help", "h").argv;
14
+
15
+ console.log(yargv);
16
+
17
+ const res = await run("transform.js", ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"], {
18
+ dry: true,
19
+ print: true,
20
+ verbose: 1,
21
+ });
22
+ console.log(res);
@@ -0,0 +1,46 @@
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 ADDED
@@ -0,0 +1,32 @@
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
+ }
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@sps-woodland/codemods",
3
+ "description": "SPS Woodland Design System codemods",
4
+ "version": "0.0.1",
5
+ "author": "SPS Commerce",
6
+ "license": "UNLICENSED",
7
+ "repository": "https://github.com/spscommerce/woodland/tree/master/packages/@sps-woodland/codemods",
8
+ "homepage": "https://github.com/spscommerce/woodland/tree/master/packages/@sps-woodland/codemods#readme",
9
+ "main": "./main.js",
10
+ "bin": "./main.js",
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "dependencies": {
15
+ "@babel/core": ">=7.13.0 <8.0.0",
16
+ "@babel/preset-env": "^7.1.6",
17
+ "chalk": "^2.4.1",
18
+ "concat-stream": "^1.6.2",
19
+ "jscodeshift": "^0.13.1",
20
+ "yargs": "^12.0.2"
21
+ },
22
+ "scripts": {
23
+ "clean": "git clean -fdX",
24
+ "pub": "pnpm pack && node ../../../scripts/publish-package.js"
25
+ }
26
+ }
Binary file
package/transform.js ADDED
@@ -0,0 +1,46 @@
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
+ }