From f247d0d6d1664ab08157d755a0455e480ff307c8 Mon Sep 17 00:00:00 2001 From: Kipras Melnikovas Date: Mon, 1 Nov 2021 15:35:04 +0200 Subject: [PATCH 1/4] feat: temp `run.js` script to quickly run local codemods Signed-off-by: Kipras Melnikovas --- run.js | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ shorthands.json | 3 ++ 2 files changed, 130 insertions(+) create mode 100755 run.js create mode 100644 shorthands.json diff --git a/run.js b/run.js new file mode 100755 index 000000000..4fe483a58 --- /dev/null +++ b/run.js @@ -0,0 +1,127 @@ +#!/usr/bin/env node + +/** + * a temporary utility to run local codemods quickly + * before we improve stuff upstream. + */ + +const helpMsg = ` +run.js parser=flow codemodsToRun filesOrDirectoriesToModify 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: eatNextArg() || '', + filesOrDirectoriesToModify: eatNextArg() || '', + extensions: eatNextArg() || 'js,jsx,ts,tsx', + } +); + +run(); + +function run() { + let { + parser, // + transformsToRun, + filesOrDirectoriesToModify, + extensions, + } = parseArgv(); + + const shorthands = require(path.join(__dirname, './shorthands.json')); + console.log({ shorthands }); + + transformsToRun = parseArrayFromCsv(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); + return t; + } + }) + .flat(); + + filesOrDirectoriesToModify = parseArrayFromCsv(filesOrDirectoriesToModify); + + const cliPath = path.join(__dirname, './packages/cli/bin/codeshift-cli.js'); + + const cmdToExec = `${cliPath} --parser ${parser} -e ${extensions} -t ${transformsToRun} ${filesOrDirectoriesToModify}`; + console.log({ cmdToExec }); + + cp.exec(cmdToExec, (err, stdout, stderr) => { + if (err) { + console.error(stderr); + return process.exit(1); + } + + console.log(stdout); + + console.log('succ'); + process.exit(0); + }); +} + +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`); + 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"] +} From 61cc9496a6c59bac1c8969cb20e7702c8e99e3ca Mon Sep 17 00:00:00 2001 From: Kipras Melnikovas Date: Mon, 1 Nov 2021 18:04:16 +0200 Subject: [PATCH 2/4] fix: we only support a single path for applying codemods (will fix later in main cli) Signed-off-by: Kipras Melnikovas --- run.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/run.js b/run.js index 4fe483a58..288bee00d 100755 --- a/run.js +++ b/run.js @@ -6,7 +6,7 @@ */ const helpMsg = ` -run.js parser=flow codemodsToRun filesOrDirectoriesToModify extensions="js,jsx,ts,tsx" +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. @@ -38,8 +38,8 @@ const parseArgv = () => ( shouldPrintHelp() && process.exit(1), { parser: eatNextArg() || 'flow', - transformsToRun: eatNextArg() || '', - filesOrDirectoriesToModify: eatNextArg() || '', + transformsToRun: parseArrayFromCsv(eatNextArg() || ''), + fileOrDirectoryToModify: eatNextArg() || '', extensions: eatNextArg() || 'js,jsx,ts,tsx', } ); @@ -50,14 +50,14 @@ function run() { let { parser, // transformsToRun, - filesOrDirectoriesToModify, + fileOrDirectoryToModify, extensions, } = parseArgv(); const shorthands = require(path.join(__dirname, './shorthands.json')); console.log({ shorthands }); - transformsToRun = parseArrayFromCsv(transformsToRun) + transformsToRun = transformsToRun .map(t => { if (t in shorthands) { return resolveTransformsFromShorthand(shorthands[t]); @@ -71,11 +71,9 @@ function run() { }) .flat(); - filesOrDirectoriesToModify = parseArrayFromCsv(filesOrDirectoriesToModify); - const cliPath = path.join(__dirname, './packages/cli/bin/codeshift-cli.js'); - const cmdToExec = `${cliPath} --parser ${parser} -e ${extensions} -t ${transformsToRun} ${filesOrDirectoriesToModify}`; + const cmdToExec = `${cliPath} --parser ${parser} -e ${extensions} -t ${transformsToRun} ${fileOrDirectoryToModify}`; console.log({ cmdToExec }); cp.exec(cmdToExec, (err, stdout, stderr) => { From f4b83ef2302f40a9f730a8c9d7df9c46367d4816 Mon Sep 17 00:00:00 2001 From: Kipras Melnikovas Date: Wed, 3 Nov 2021 19:08:39 +0200 Subject: [PATCH 3/4] fix: join transforms with ',' depends on: https://github.com/pipedrive/CodeshiftCommunity/pull/5 Signed-off-by: Kipras Melnikovas --- run.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/run.js b/run.js index 288bee00d..24767cc4c 100755 --- a/run.js +++ b/run.js @@ -69,7 +69,8 @@ function run() { return t; } }) - .flat(); + .flat() + .join(','); const cliPath = path.join(__dirname, './packages/cli/bin/codeshift-cli.js'); From d53171e599b8ea286b9a0f349fb0063d9e0cc502 Mon Sep 17 00:00:00 2001 From: Kipras Melnikovas Date: Wed, 3 Nov 2021 20:46:05 +0200 Subject: [PATCH 4/4] feat: pipe stdio! Signed-off-by: Kipras Melnikovas --- run.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/run.js b/run.js index 24767cc4c..927ed5a67 100755 --- a/run.js +++ b/run.js @@ -44,6 +44,21 @@ const parseArgv = () => ( } ); +/** + * 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() { @@ -65,7 +80,7 @@ function run() { const dir = path.dirname(t); const cmd = `yarn --cwd ${dir} build`; console.log('transform to run, build cmd', { cmd }); - cp.execSync(cmd); + cp.execSync(cmd, { ...pipeStdioOpts() }); return t; } }) @@ -77,17 +92,7 @@ function run() { const cmdToExec = `${cliPath} --parser ${parser} -e ${extensions} -t ${transformsToRun} ${fileOrDirectoryToModify}`; console.log({ cmdToExec }); - cp.exec(cmdToExec, (err, stdout, stderr) => { - if (err) { - console.error(stderr); - return process.exit(1); - } - - console.log(stdout); - - console.log('succ'); - process.exit(0); - }); + cp.execSync(cmdToExec, { ...pipeStdioOpts() }); } function parseArrayFromCsv(csv = '') { @@ -99,7 +104,7 @@ function parseArrayFromCsv(csv = '') { } function resolveTransformsFromShorthand([pathToCodemodPkg, transformVersion]) { - cp.execSync(`yarn --cwd ${pathToCodemodPkg} build`); + cp.execSync(`yarn --cwd ${pathToCodemodPkg} build`, { ...pipeStdioOpts() }); console.log('built'); const pathToCodemodConfig = path.join(