-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from Shopify/transform_cli
Scripts to create/rename transforms
- Loading branch information
Showing
9 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
packages/shopify-codemod/transform-cli/create-transform.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
packages/shopify-codemod/transform-cli/rename-transform.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
8 changes: 8 additions & 0 deletions
8
packages/shopify-codemod/transform-cli/templates/test-suite.js.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/shopify-codemod/transform-cli/templates/transform.js.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/shopify-codemod/transform-cli/transform-management-utils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |