diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 000000000..f06235c46 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +node_modules +dist diff --git a/community/@pipedrive__convention-ui-react/.gitignore b/community/@pipedrive__convention-ui-react/.gitignore new file mode 100644 index 000000000..3c3629e64 --- /dev/null +++ b/community/@pipedrive__convention-ui-react/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/community/@pipedrive__convention-ui-react/.npmignore b/community/@pipedrive__convention-ui-react/.npmignore new file mode 100644 index 000000000..3ffc71d94 --- /dev/null +++ b/community/@pipedrive__convention-ui-react/.npmignore @@ -0,0 +1,5 @@ +src/ +**/__test__ +**/*.spec.(ts|js) +.vscode +jest.config.js diff --git a/community/@pipedrive__convention-ui-react/.prettierrc.js b/community/@pipedrive__convention-ui-react/.prettierrc.js new file mode 100644 index 000000000..edeb32b59 --- /dev/null +++ b/community/@pipedrive__convention-ui-react/.prettierrc.js @@ -0,0 +1,7 @@ +module.exports = { + semi: true, + trailingComma: "all", + singleQuote: false, + printWidth: 120, + tabWidth: 4, +}; diff --git a/community/@pipedrive__convention-ui-react/jest.config.js b/community/@pipedrive__convention-ui-react/jest.config.js new file mode 100644 index 000000000..c10b374d3 --- /dev/null +++ b/community/@pipedrive__convention-ui-react/jest.config.js @@ -0,0 +1,21 @@ +module.exports = { + transform: { + "^.+\\.ts$": "ts-jest", // + }, + moduleFileExtensions: [ + "ts", // + "tsx", + "js", + ], + testRegex: "^.+\\.spec\\.(tsx|ts|js)$", + globals: { + // + "ts-jest": { + tsconfig: "tsconfig.json", + }, + }, + testPathIgnorePatterns: [ + "/node_modules/", // + "dist/", + ], +}; diff --git a/community/@pipedrive__convention-ui-react/package.json b/community/@pipedrive__convention-ui-react/package.json new file mode 100644 index 000000000..0e364c979 --- /dev/null +++ b/community/@pipedrive__convention-ui-react/package.json @@ -0,0 +1,24 @@ +{ + "private": true, + "name": "@pipedrive/convention-ui-react", + "version": "0.0.0", + "license": "MIT", + "main": "dist/codeshift.config.js", + "scripts": { + "build": "tsc --build", + "test": "jest" + }, + "dependencies": {}, + "devDependencies": { + "@codeshift/test-utils": "*", + "@codeshift/utils": "*", + "@types/jest": "^26.0.15", + "@types/node": "^16.11.0", + "jest": "^26.6.0", + "jscodeshift": "^0.12.0", + "prettier": "^1.16.4", + "ts-jest": "^26.4.4", + "typescript": "^4.3.5" + }, + "repository": "https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/master/community/@pipedrive__convention-ui-react" +} diff --git a/community/@pipedrive__convention-ui-react/src/5.0.0/transform.spec.ts b/community/@pipedrive__convention-ui-react/src/5.0.0/transform.spec.ts new file mode 100644 index 000000000..5f9b24d3b --- /dev/null +++ b/community/@pipedrive__convention-ui-react/src/5.0.0/transform.spec.ts @@ -0,0 +1,20 @@ +// import { applyTransform } from "@codeshift/test-utils"; +// import * as transformer from "./transform"; + +// describe("@pipedrive/convention-ui-react@5.0.0 transform", () => { +// it("should transform correctly", () => { +// const result = applyTransform( +// transformer, +// ` +// import foo from '<% packageName %>'; +// console.log(foo); +// `, +// { parser: "tsx" }, +// ); + +// expect(result).toMatchInlineSnapshot(` +// "import foo from '<% packageName %>'; +// console.log(foo);" +// `); +// }); +// }); diff --git a/community/@pipedrive__convention-ui-react/src/5.0.0/transform.ts b/community/@pipedrive__convention-ui-react/src/5.0.0/transform.ts new file mode 100644 index 000000000..982997186 --- /dev/null +++ b/community/@pipedrive__convention-ui-react/src/5.0.0/transform.ts @@ -0,0 +1,7 @@ +import { API, FileInfo, Options } from "jscodeshift"; + +export default function transformer(file: FileInfo, { jscodeshift: j }: API, options: Options) { + const source = j(file.source); + + return source.toSource(options.printOptions); +} diff --git a/community/@pipedrive__convention-ui-react/src/codeshift.config.js b/community/@pipedrive__convention-ui-react/src/codeshift.config.js new file mode 100644 index 000000000..350960681 --- /dev/null +++ b/community/@pipedrive__convention-ui-react/src/codeshift.config.js @@ -0,0 +1,9 @@ +module.exports = { + maintainers: ["kiprasmel"], + target: [], + description: "Codemods for @pipedrive/convention-ui-react", + transforms: { + "5.0.0": require.resolve("./5.0.0/transform"), // + }, + presets: {}, +}; diff --git a/community/@pipedrive__convention-ui-react/tsconfig.json b/community/@pipedrive__convention-ui-react/tsconfig.json new file mode 100644 index 000000000..d76f1bfb9 --- /dev/null +++ b/community/@pipedrive__convention-ui-react/tsconfig.json @@ -0,0 +1,36 @@ +{ + "compilerOptions": { + "allowJs": true, + "target": "es5", // + "module": "commonjs", + "lib": [ + "es2017" // + ], + "skipLibCheck": true, + "sourceMap": true, + "outDir": "./dist", + "moduleResolution": "node", + "removeComments": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "resolveJsonModule": true, + "baseUrl": "." + }, + "exclude": [ + "node_modules" // + ], + "include": [ + "./src/**/*.ts", // + "./src/**/*.js" + ] +} diff --git a/package.json b/package.json index ec3817f28..ab788aa5e 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "test:watch": "jest --watch", "test:coverage": "jest --coverage", "validate": "yarn build && yarn lint && yarn monorepo:check && preconstruct validate", - "lint": "eslint --config .eslintrc.js --ext tsx,ts ./packages/**/src ./community/**/*", + "lint": "yarn eslint --config .eslintrc.js --ext tsx,ts ./packages/**/src ./community/**/*", "lint:fix": "yarn lint -- --fix", "lint:file-structure": "npx @ls-lint/ls-lint", "types:check": "tsc --noEmit --skipLibCheck", @@ -61,7 +61,7 @@ "ts-loader": "^6.2.1", "ts-node": "^9.1.1", "tslib": "^2.2.0", - "typescript": "^4.1.3" + "typescript": "^4.3.5" }, "workspaces": [ "packages/*", diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index b5ee325f2..08fc7ea69 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -16,7 +16,7 @@ program .usage('[global options] ...') .option( '-t, --transform ', - 'The transform to run, will prompt for a transform if not provided and no module is passed', + 'The transform(s) to run, will prompt for a transform if not provided and no module is passed\nTo provide multiple transforms, separate them with commas (e.g. "-t t1,t2,t3")', ) .option( '--packages ', diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts index 5ae27236b..7f8e3447d 100644 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/main.ts @@ -1,3 +1,4 @@ +import path from 'path'; import semver from 'semver'; import chalk from 'chalk'; import { PluginManager } from 'live-plugin-manager'; @@ -23,7 +24,11 @@ export default async function main(paths: string[], flags: Flags) { } if (flags.transform) { - transforms.push(flags.transform); + if (flags.transform.includes(',')) { + flags.transform.split(',').forEach(t => transforms.push(t.trim())); + } else { + transforms.push(flags.transform); + } } const packageManager = new PluginManager(); @@ -126,9 +131,10 @@ export default async function main(paths: string[], flags: Flags) { ); for (const transform of transforms) { - console.log(chalk.green('Running transform:'), transform); + const resolvedTransformPath = path.resolve(transform); + console.log(chalk.green('Running transform:'), resolvedTransformPath); - await jscodeshift.run(transform, paths, { + await jscodeshift.run(resolvedTransformPath, paths, { verbose: 0, dry: flags.dry, print: true, diff --git a/run.js b/run.js new file mode 100755 index 000000000..927ed5a67 --- /dev/null +++ b/run.js @@ -0,0 +1,131 @@ +#!/usr/bin/env node + +/** + * a temporary utility to run local codemods quickly + * before we improve stuff upstream. + */ + +const helpMsg = ` +run.js parser=flow codemodsToRun fileOrDirectoryToModify extensions="js,jsx,ts,tsx" + +codemodsToRun - paths to codemods which have a codeshift.config.js file. + separated by spaces. + can also just provide keys from the "shorthands.json" file + +examples: + +./run.js flow ./community/@pipedrive__convention-ui-react/src/5.0.0/transform.ts ~/projects/some-project-which-uses-cui4/src/ +./run.js flow cui5 ~/projects/some-project-which-uses-cui4/src/ # cui5 is from shorthands.json + +`; + +const path = require('path'); +const cp = require('child_process'); + +const peekNextArg = () => process.argv[0]; +const eatNextArg = () => process.argv.shift(); + +const shouldPrintHelp = () => ( + (should = + !process.argv.length || + ['-h', '--help', '-help', 'help'].includes(peekNextArg())), + should && console.log(helpMsg), + should +); + +const parseArgv = () => ( + process.argv.splice(0, 2), + shouldPrintHelp() && process.exit(1), + { + parser: eatNextArg() || 'flow', + transformsToRun: parseArrayFromCsv(eatNextArg() || ''), + fileOrDirectoryToModify: eatNextArg() || '', + extensions: eatNextArg() || 'js,jsx,ts,tsx', + } +); + +/** + * will pipe stdio as if we were running the command ourselves! + * + * see https://stackoverflow.com/a/47338488/9285308 + * + * usage: + * + * ```js + * const command = "ls -la"; + * require("child_process").execSync(command, { ...pipeStdioOpts() }); + * ``` + * + */ +const pipeStdioOpts = (cwd = process.cwd()) => ({ cwd, stdio: 'inherit' }); + +run(); + +function run() { + let { + parser, // + transformsToRun, + fileOrDirectoryToModify, + extensions, + } = parseArgv(); + + const shorthands = require(path.join(__dirname, './shorthands.json')); + console.log({ shorthands }); + + transformsToRun = transformsToRun + .map(t => { + if (t in shorthands) { + return resolveTransformsFromShorthand(shorthands[t]); + } else { + const dir = path.dirname(t); + const cmd = `yarn --cwd ${dir} build`; + console.log('transform to run, build cmd', { cmd }); + cp.execSync(cmd, { ...pipeStdioOpts() }); + return t; + } + }) + .flat() + .join(','); + + const cliPath = path.join(__dirname, './packages/cli/bin/codeshift-cli.js'); + + const cmdToExec = `${cliPath} --parser ${parser} -e ${extensions} -t ${transformsToRun} ${fileOrDirectoryToModify}`; + console.log({ cmdToExec }); + + cp.execSync(cmdToExec, { ...pipeStdioOpts() }); +} + +function parseArrayFromCsv(csv = '') { + return csv + .split(',') + .filter(c => !!c) + .map(c => c.trim()) + .filter(c => !!c); +} + +function resolveTransformsFromShorthand([pathToCodemodPkg, transformVersion]) { + cp.execSync(`yarn --cwd ${pathToCodemodPkg} build`, { ...pipeStdioOpts() }); + console.log('built'); + + const pathToCodemodConfig = path.join( + pathToCodemodPkg, + 'dist', + 'codeshift.config.js', + ); + console.log({ pathToCodemodConfig }); + + const codemodCfg = require(path.join(__dirname, pathToCodemodConfig)); + + const { transforms } = codemodCfg; + + const transformsApplicable = Object.entries(transforms) + .map(([version, relPathToTransform]) => { + if (version === transformVersion) { + return relPathToTransform; + // return path.join(pathToCodemodPkg, 'dist', relPathToTransform); // TODO must ensure it's compiled / run with ts-node / require from 'dist' + } + }) + .filter(x => !!x); + + return transformsApplicable; +} diff --git a/shorthands.json b/shorthands.json new file mode 100644 index 000000000..fffb4a391 --- /dev/null +++ b/shorthands.json @@ -0,0 +1,3 @@ +{ + "cui5": ["./community/@pipedrive__convention-ui-react", "5.0.0"] +} diff --git a/x b/x new file mode 100755 index 000000000..3b5937d83 --- /dev/null +++ b/x @@ -0,0 +1,222 @@ +#!/usr/bin/env bash + +set -e + +mkdir -p .tmp/ + +for i in {1..1}; do cat > ./.tmp/$i.js < + lol haha + +); + +const a2 = + + nice badge bro + + +const a3 = + + this one too damn + + +const a4 =