Skip to content

Commit

Permalink
Scripts to generate transform boilerplate / rename transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodForOneFare committed May 24, 2016
1 parent 01a1b7c commit 0757297
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/shopify-codemod/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
test
coverage
.eslintignore
bin/create-transform
bin/rename-transform
coverage
test
transform-cli
6 changes: 6 additions & 0 deletions packages/shopify-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,9 @@ foo = () => {
## Contributing

All code is written in ES2015+ in the `transforms/` directory. Make sure to add tests for all new transforms and features. A custom `transforms(fixtureName)` assertion is provided which checks that the passed transformer converts the fixture found at `test/fixtures/{{fixtureName}}.input.js` to the one found at `test/fixtures/{{fixtureName}}.output.js`. You can run `npm test` to run all tests, or `npm run test:watch` to have Mocha watch for changes and re-run the tests.


### Development helpers
`bin/create-transform your-transform-name-here` creates a transform file, a test file, and some empty test fixtures.

`bin/rename-transform old-transform-name new-transform-name` renames the transforms file, test file, and fixture directory.
7 changes: 7 additions & 0 deletions packages/shopify-codemod/bin/create-transform
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node --harmony_destructuring

/* eslint-disable node/shebang */
const create = require('../transform-cli/create-transform');

const [,, dasherizedName] = process.argv;
create(dasherizedName);
7 changes: 7 additions & 0 deletions packages/shopify-codemod/bin/rename-transform
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node --harmony_destructuring

/* eslint-disable node/shebang */
const rename = require('../transform-cli/rename-transform');

const [,, oldName, newName] = process.argv;
rename(oldName, newName);
41 changes: 41 additions & 0 deletions packages/shopify-codemod/transform-cli/create-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable no-sync */
const fs = require('fs');
const path = require('path');
const utils = require('./transform-management-utils');

function createFileSync(filePath, content) {
fs.writeFileSync(filePath, content);
}

function readTemplateSync(name) {
return fs.readFileSync(path.join('transform-cli', 'templates', name), 'utf8');
}

function generateTransformJS({camelizedName}) {
return readTemplateSync('transform.js.template')
.replace(/__CAMELIZED_NAME__/g, camelizedName);
}

function generateTestSuiteJS({dasherizedName, camelizedName}) {
return readTemplateSync('test-suite.js.template')
.replace(/__DASHERIZED_NAME__/g, dasherizedName)
.replace(/__CAMELIZED_NAME__/g, camelizedName);
}

module.exports = function createTransform(dasherizedName) {
if (!dasherizedName) {
throw new Error('Specify the transform\'s dasherized name as an argument.');
}

utils.validateTransformName(dasherizedName);
const transformInfo = utils.transformNameInfo(dasherizedName);
const {transformFilePath, testSuiteFilePath, fixtureDir} = transformInfo;

fs.writeFileSync(transformFilePath, generateTransformJS(transformInfo));
fs.writeFileSync(testSuiteFilePath, generateTestSuiteJS(transformInfo));

fs.mkdirSync(fixtureDir);

createFileSync(path.join(fixtureDir, 'basic.input.js'), '');
createFileSync(path.join(fixtureDir, 'basic.output.js'), '');
};
33 changes: 33 additions & 0 deletions packages/shopify-codemod/transform-cli/rename-transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable no-sync */
const fs = require('fs');
const utils = require('./transform-management-utils');

module.exports = function renameTransform(oldName, newName) {
if (!oldName) {
throw new Error('Specify the transform\'s current dasherized name as an argument.');
}

if (!newName) {
throw new Error('Specify the transform\'s new dasherized name as an argument.');
}

utils.validateTransformName(newName);

const oldInfo = utils.transformNameInfo(oldName);
const newInfo = utils.transformNameInfo(newName);

fs.renameSync(oldInfo.transformFilePath, newInfo.transformFilePath);
fs.renameSync(oldInfo.testSuiteFilePath, newInfo.testSuiteFilePath);
fs.renameSync(oldInfo.fixtureDir, newInfo.fixtureDir);

const transformJS = fs
.readFileSync(newInfo.transformFilePath, 'utf8')
.replace(new RegExp(oldInfo.camelizedName, 'g'), newInfo.camelizedName);
fs.writeFileSync(newInfo.transformFilePath, transformJS);

const testSuiteJS = fs
.readFileSync(newInfo.testSuiteFilePath, 'utf8')
.replace(new RegExp(oldInfo.dasherizedName, 'g'), newInfo.dasherizedName)
.replace(new RegExp(oldInfo.camelizedName, 'g'), newInfo.camelizedName);
fs.writeFileSync(newInfo.testSuiteFilePath, testSuiteJS);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'test-helper';
import transform from '__DASHERIZED_NAME__';

describe('__CAMELIZED_NAME__', () => {
it('CHANGE THIS', () => {
expect(transform).to.transform('__DASHERIZED_NAME__/basic');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function __CAMELIZED_NAME__({source}, {jscodeshift: j}, {printOptions = {quote: 'single'}}) {
return j(source)
.find(j.Identifier, {
name: '',
})
.replaceWith(({node}) => {
return
})
.toSource(printOptions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable no-sync */
const path = require('path');

function camelize(str) {
return str.replace(/-[a-z]/g, (dashWithLetter) => dashWithLetter[1].toUpperCase());
}

module.exports.validateTransformName = function(dasherizedName) {
if (/[A-Z_0-9]/.test(dasherizedName)) {
throw new Error('Transform names must contain only lower-case letters and hyphens.');
}
};

module.exports.transformNameInfo = function(dasherizedName) {
const transformFilePath = path.join('transforms', `${dasherizedName}.js`);
const testSuiteFilePath = path.join('test', 'transforms', `${dasherizedName}.test.js`);
const fixtureDir = path.join('test', 'fixtures', dasherizedName);

return {
dasherizedName,
camelizedName: camelize(dasherizedName),
transformFilePath,
testSuiteFilePath,
fixtureDir,
};
};

0 comments on commit 0757297

Please sign in to comment.