Skip to content

Commit ab76ecc

Browse files
author
Robert Jackson
committed
Add --codemod-dir command line flag
1 parent ea2dada commit ab76ecc

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

commands/local/generate/codemod.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ module.exports.builder = function builder(yargs) {
66
.positional('codemod-name', {
77
describe: 'the name of the codemod to generate',
88
})
9+
.option('codemod-dir', {
10+
type: 'string',
11+
describe: 'the path to the transform directory',
12+
default: `./transforms/`,
13+
})
914
.option('type', {
1015
alias: 't',
1116
describe: 'choose the transform type',
@@ -16,13 +21,14 @@ module.exports.builder = function builder(yargs) {
1621

1722
function jsHandler(options) {
1823
const fs = require('fs-extra');
24+
const path = require('path');
1925
const { stripIndent } = require('common-tags');
2026
const importCwd = require('import-cwd');
2127
const generateFixture = require('./fixture').handler;
2228

2329
let { codemodName } = options;
2430
let projectName = importCwd('./package.json').name;
25-
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
31+
let codemodDir = path.join(options.codemodDir, codemodName);
2632

2733
fs.outputFileSync(
2834
`${codemodDir}/index.js`,
@@ -94,7 +100,12 @@ function jsHandler(options) {
94100
'utf8'
95101
);
96102

97-
generateFixture({ codemodName, fixtureName: 'basic', type: options.type });
103+
generateFixture({
104+
codemodName,
105+
codemodDir: options.codemodDir,
106+
fixtureName: 'basic',
107+
type: options.type,
108+
});
98109
}
99110

100111
function hbsHandler(options) {
@@ -173,7 +184,12 @@ function hbsHandler(options) {
173184
'utf8'
174185
);
175186

176-
generateFixture({ codemodName, fixtureName: 'basic', type: options.type });
187+
generateFixture({
188+
codemodName,
189+
codemodDir: options.codemodDir,
190+
fixtureName: 'basic',
191+
type: options.type,
192+
});
177193
}
178194

179195
module.exports.handler = function handler(options) {

commands/local/generate/fixture.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ module.exports.builder = function builder(yargs) {
88
})
99
.positional('fixture-name', {
1010
describe: 'the name of the fixture to generate',
11+
})
12+
.option('codemod-dir', {
13+
type: 'string',
14+
describe: 'the path to the transform directory',
15+
default: `./transforms/`,
1116
});
1217
};
1318

1419
module.exports.handler = function handler(options) {
1520
const fs = require('fs-extra');
21+
const path = require('path');
1622
const { getTransformType } = require('../../../src/transform-support');
1723

1824
let { codemodName, fixtureName } = options;
19-
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
25+
let codemodDir = path.resolve(process.cwd(), path.join(options.codemodDir, codemodName));
2026
let fixturePath = `${codemodDir}/__testfixtures__/${fixtureName}`;
2127

2228
let transformType = getTransformType(codemodDir);

tests/cli-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ QUnit.module('codemod-cli', function (hooks) {
158158
]);
159159
});
160160

161+
QUnit.test('should generate a codemod in a custom directory', async function(assert) {
162+
let result = await execa(EXECUTABLE_PATH, [
163+
'generate',
164+
'codemod',
165+
'main',
166+
'--codemod-dir',
167+
'other-dir',
168+
]);
169+
170+
assert.equal(result.exitCode, 0, 'exited with zero');
171+
assert.deepEqual(walkSync(codemodProject.path('other-dir')), [
172+
'main/',
173+
'main/README.md',
174+
'main/__testfixtures__/',
175+
'main/__testfixtures__/basic.input.js',
176+
'main/__testfixtures__/basic.output.js',
177+
'main/index.js',
178+
'main/test.js',
179+
]);
180+
});
181+
161182
QUnit.test('should generate a hbs codemod', async function (assert) {
162183
let result = await execa(EXECUTABLE_PATH, ['generate', 'codemod', 'main', '--type', 'hbs']);
163184

0 commit comments

Comments
 (0)