-
Notifications
You must be signed in to change notification settings - Fork 34
Import gist #75
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
base: master
Are you sure you want to change the base?
Import gist #75
Changes from 6 commits
b4cd0b5
1024984
24f155d
e645135
7744e3f
654f435
b3e8539
7aa0ea4
922a429
90659a5
15a19f0
797cc1d
121a53a
5658ea5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
*.trace | ||
.env |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,26 @@ module.exports.command = 'new <project-name>'; | |
module.exports.desc = 'Generate a new codemod project'; | ||
|
||
module.exports.builder = function builder(yargs) { | ||
yargs.positional('project-name', { | ||
describe: 'The name of the project to generate', | ||
}); | ||
yargs | ||
.positional('project-name', { | ||
describe: 'The name of the project to generate', | ||
}) | ||
.option('url', { | ||
alias: 'u', | ||
demandOption: false, | ||
describe: 'ast-explorer gist url to import from', | ||
type: 'string', | ||
}) | ||
.option('codemod', { | ||
alias: 'c', | ||
demandOption: false, | ||
describe: 'name of the codemod to generate', | ||
type: 'string', | ||
}); | ||
}; | ||
|
||
module.exports.handler = async function handler(options) { | ||
let { projectName } = options; | ||
let { projectName, url, codemod: codemodName } = options; | ||
|
||
const fs = require('fs-extra'); | ||
const { stripIndent } = require('common-tags'); | ||
|
@@ -256,4 +269,75 @@ module.exports.handler = async function handler(options) { | |
); | ||
fs.outputFileSync(projectName + '/.gitignore', '/node_modules\n/.eslintcache'); | ||
fs.ensureFileSync(projectName + '/transforms/.gitkeep'); | ||
|
||
// If import options [ url && codemod] are present | ||
if (url && codemodName) { | ||
let regex = /https:\/\/astexplorer\.net\/#\/gist\/(\w+)\/(\w+)/; | ||
let matches = regex.exec(url); | ||
|
||
let [, gist_id] = matches; | ||
|
||
require('dotenv').config(); | ||
const Octokit = require('@octokit/rest'); | ||
const octokit = new Octokit({ auth: process.env.CODEMOD_CLI_API_KEY }); | ||
let codemodDir = `${process.cwd()}/${projectName}/transforms/${codemodName}`; | ||
|
||
octokit.gists | ||
.get({ | ||
gist_id, | ||
}) | ||
.then(({ data }) => { | ||
fs.outputFileSync(`${codemodDir}/index.js`, data.files['transform.js'].content, 'utf8'); | ||
}) | ||
.catch(err => { | ||
console.log('Error: ', err); | ||
}); | ||
|
||
fs.outputFileSync( | ||
`${codemodDir}/test.js`, | ||
stripIndent` | ||
'use strict'; | ||
|
||
const { runTransformTest } = require('codemod-cli'); | ||
|
||
runTransformTest({ | ||
type: 'jscodeshift', | ||
name: '${codemodName}', | ||
}); | ||
`, | ||
'utf8' | ||
); | ||
fs.outputFileSync( | ||
`${codemodDir}/README.md`, | ||
stripIndent` | ||
# ${codemodName}\n | ||
|
||
## Usage | ||
|
||
\`\`\` | ||
npx ${projectName} ${codemodName} path/of/files/ or/some**/*glob.js | ||
|
||
# or | ||
|
||
yarn global add ${projectName} | ||
${projectName} ${codemodName} path/of/files/ or/some**/*glob.js | ||
\`\`\` | ||
|
||
## Input / Output | ||
|
||
<!--FIXTURES_TOC_START--> | ||
<!--FIXTURES_TOC_END--> | ||
|
||
<!--FIXTURES_CONTENT_START--> | ||
<!--FIXTURES_CONTENT_END--> | ||
`, | ||
'utf8' | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you abstract this so we don't slowly diverge the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, working on it. |
||
|
||
// Generate basic test fixtures | ||
let fixturePath = `${codemodDir}/__testfixtures__/basic`; | ||
|
||
fs.outputFileSync(`${fixturePath}.input.js`, ''); | ||
fs.outputFileSync(`${fixturePath}.output.js`, ''); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module.exports.command = 'export <codemod-name>'; | ||
module.exports.desc = 'Export the current transform to ast-explorer'; | ||
|
||
module.exports.builder = function builder(yargs) { | ||
yargs.positional('codemod-name', { | ||
describe: 'the name of the codemod to export', | ||
}); | ||
}; | ||
|
||
module.exports.handler = function handler(options) { | ||
const fs = require('fs-extra'); | ||
|
||
let { codemodName } = options; | ||
|
||
const Octokit = require('@octokit/rest'); | ||
const octokit = new Octokit({ auth: 'acabfddbefb244cb3e674fa84046a907c78f2294' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 - Seems odd to hard code this here? Is this a mistake? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, my bad, will remove that. |
||
|
||
let files = { | ||
'astexplorer.json': { | ||
content: `{ | ||
"v": 2, | ||
"parserID": "recast", | ||
"toolID": "jscodeshift", | ||
"settings": { | ||
"recast": null, | ||
}, | ||
"versions": { | ||
"recast": "0.18.2", | ||
"jscodeshift": "0.6.4" | ||
} | ||
}`, | ||
}, | ||
'source.js': { | ||
content: 'console.log("hello world");', | ||
}, | ||
'tranform.js': { | ||
content: fs.readFileSync(`${process.cwd()}/transforms/${codemodName}/index.js`, 'utf-8'), | ||
}, | ||
}; | ||
|
||
octokit.gists | ||
.create({ | ||
files, | ||
}) | ||
.then(({ data }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can use async/await here instead of manual chaining. |
||
// https://api.github.com/gists/de5cff0a12c2aaf129f94b775306af6f | ||
console.log(data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just notating for removal in the future There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted. |
||
|
||
// Getting the revision url and replace it with astexplorer format | ||
//let url = data.history[0].url.replace('api.github.com/gists', 'astexplorer.net/#/gist'); | ||
let gistId = data.id; | ||
let url = `https://astexplorer.net/#/gist/${gistId}`; | ||
const exec = require('child_process').exec; | ||
exec(`open ${url}`); | ||
}) | ||
.catch(err => { | ||
console.log('Error: ', err); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module.exports.command = 'import <gist-url> <codemod-name>'; | ||
module.exports.desc = 'Generate a new codemod file from ast-explorer gist'; | ||
|
||
module.exports.builder = function builder(yargs) { | ||
yargs.positional('codemod-name', { | ||
describe: 'the name of the codemod to generate', | ||
}); | ||
yargs.positional('gist-url', { | ||
describe: 'the url of the ast-explorer gist', | ||
}); | ||
}; | ||
|
||
module.exports.handler = function handler(options) { | ||
const fs = require('fs-extra'); | ||
const { stripIndent } = require('common-tags'); | ||
const importCwd = require('import-cwd'); | ||
const generateFixture = require('./generate/fixture').handler; | ||
|
||
let { codemodName, gistUrl } = options; | ||
// https://astexplorer.net/#/gist/cb7d2e7ce49741966e5e96a4b2eadc4d/d6b902bf639adc2bc6d31b35ba38aa45910b2413 | ||
let regex = /https:\/\/astexplorer\.net\/#\/gist\/(\w+)\/(\w+)/; | ||
let matches = regex.exec(gistUrl); | ||
|
||
let [, gist_id] = matches; | ||
|
||
require('dotenv').config(); | ||
const Octokit = require('@octokit/rest'); | ||
const octokit = new Octokit({ auth: process.env.CODEMOD_CLI_API_KEY }); | ||
let projectName = importCwd('./package.json').name; | ||
let codemodDir = `${process.cwd()}/transforms/${codemodName}`; | ||
|
||
octokit.gists | ||
.get({ | ||
gist_id, | ||
}) | ||
.then(({ data }) => { | ||
// TODO: handle error if transform.js is not present | ||
console.log(data.files); | ||
fs.outputFileSync(`${codemodDir}/index.js`, data.files['transform.js'].content, 'utf8'); | ||
}) | ||
.catch(err => { | ||
console.log('Error: ', err); | ||
}); | ||
|
||
fs.outputFileSync( | ||
`${codemodDir}/test.js`, | ||
stripIndent` | ||
'use strict'; | ||
|
||
const { runTransformTest } = require('codemod-cli'); | ||
|
||
runTransformTest({ | ||
type: 'jscodeshift', | ||
name: '${codemodName}', | ||
}); | ||
`, | ||
'utf8' | ||
); | ||
fs.outputFileSync( | ||
`${codemodDir}/README.md`, | ||
stripIndent` | ||
# ${codemodName}\n | ||
|
||
## Usage | ||
|
||
\`\`\` | ||
npx ${projectName} ${codemodName} path/of/files/ or/some**/*glob.js | ||
|
||
# or | ||
|
||
yarn global add ${projectName} | ||
${projectName} ${codemodName} path/of/files/ or/some**/*glob.js | ||
\`\`\` | ||
|
||
## Input / Output | ||
|
||
<!--FIXTURES_TOC_START--> | ||
<!--FIXTURES_TOC_END--> | ||
|
||
<!--FIXTURES_CONTENT_START--> | ||
<!--FIXTURES_CONTENT_END--> | ||
`, | ||
'utf8' | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets use a shared function to generate these README.md's, I don't want them to slowly get out of sync due to the duplication. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed |
||
|
||
generateFixture({ codemodName, fixtureName: 'basic' }); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this? Can we download the actual file directly (without needing an API key)?
I'm thinking like, from:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think yes, but in case of export we need an api to create gists, so I thought we can use this. What do you think?