Skip to content

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.trace
.env
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ codemod-cli new <project-name>
This will create a small project structure (`README.md`, `package.json`, etc) which is
ready to help you manage your codemods.

You can also import from ast-explorer to create a codemod project via:

```
codemod-cli new <project-name> --url <ast-explorer-url> --codemod <codemod-name>
```

Once you have a project, you can generate a new codemod:

```
Expand Down
92 changes: 88 additions & 4 deletions commands/global/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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}`;
Copy link
Owner

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:

https://gist.githubusercontent.com/astexplorer/6f3bf899542dbe21e0474a2a74b2ffc9/raw/c23b658b268c2e5b644fed7f1a360433748e7f71/transform.js

Copy link
Contributor Author

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?


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'
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you abstract this so we don't slowly diverge the README.md contents?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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`, '');
}
};
59 changes: 59 additions & 0 deletions commands/local/export.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' });
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 - Seems odd to hard code this here? Is this a mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 }) => {
Copy link
Owner

Choose a reason for hiding this comment

The 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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just notating for removal in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
});
};
87 changes: 87 additions & 0 deletions commands/local/import.js
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'
);
Copy link
Owner

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed


generateFixture({ codemodName, fixtureName: 'basic' });
};
Loading