Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

temp: x script for quickly running & seeing the diff #71

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
1 change: 1 addition & 0 deletions community/@pipedrive__convention-ui-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
5 changes: 5 additions & 0 deletions community/@pipedrive__convention-ui-react/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src/
**/__test__
**/*.spec.(ts|js)
.vscode
jest.config.js
7 changes: 7 additions & 0 deletions community/@pipedrive__convention-ui-react/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: false,
printWidth: 120,
tabWidth: 4,
};
21 changes: 21 additions & 0 deletions community/@pipedrive__convention-ui-react/jest.config.js
Original file line number Diff line number Diff line change
@@ -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/",
],
};
24 changes: 24 additions & 0 deletions community/@pipedrive__convention-ui-react/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// import { applyTransform } from "@codeshift/test-utils";
// import * as transformer from "./transform";

// describe("@pipedrive/[email protected] 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);"
// `);
// });
// });
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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: {},
};
36 changes: 36 additions & 0 deletions community/@pipedrive__convention-ui-react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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/*",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ program
.usage('[global options] <file-paths>...')
.option(
'-t, --transform <value>',
'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 <value>',
Expand Down
12 changes: 9 additions & 3 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import semver from 'semver';
import chalk from 'chalk';
import { PluginManager } from 'live-plugin-manager';
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
131 changes: 131 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions shorthands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cui5": ["./community/@pipedrive__convention-ui-react", "5.0.0"]
}
Loading