Skip to content

Commit 3c655d8

Browse files
author
Robert Jackson
committed
Allow runTransform to accept path to transform to run.
1 parent 18c37e0 commit 3c655d8

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/bin-support.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
const DEFAULT_JS_EXTENSIONS = 'js,ts';
44

5-
async function runJsTransform(root, transformName, args, extensions = DEFAULT_JS_EXTENSIONS) {
5+
async function runJsTransform(transformPath, args, extensions = DEFAULT_JS_EXTENSIONS) {
66
const globby = require('globby');
77
const execa = require('execa');
88
const chalk = require('chalk');
99
const path = require('path');
1010
const { parseTransformArgs } = require('./options-support');
11-
const { getTransformPath } = require('./transform-support');
1211

1312
let { paths, options, transformerOptions } = parseTransformArgs(args);
1413

1514
try {
1615
let foundPaths = await globby(paths, {
1716
expandDirectories: { extensions: extensions.split(',') },
1817
});
19-
let transformPath = getTransformPath(root, transformName);
2018

2119
let jscodeshiftPkg = require('jscodeshift/package');
2220
let jscodeshiftPath = path.dirname(require.resolve('jscodeshift/package'));
@@ -45,17 +43,15 @@ async function runJsTransform(root, transformName, args, extensions = DEFAULT_JS
4543
}
4644
}
4745

48-
async function runTemplateTransform(root, transformName, args) {
46+
async function runTemplateTransform(transformPath, args) {
4947
const execa = require('execa');
5048
const chalk = require('chalk');
5149
const path = require('path');
5250
const { parseTransformArgs } = require('./options-support');
53-
const { getTransformPath } = require('./transform-support');
5451

5552
let { paths, options } = parseTransformArgs(args);
5653

5754
try {
58-
let transformPath = getTransformPath(root, transformName);
5955
let binOptions = ['-t', transformPath, ...paths];
6056
let templateRecastDir = path.dirname(require.resolve('ember-template-recast/package.json'));
6157
let templateRecastPkg = require('ember-template-recast/package');
@@ -85,9 +81,9 @@ async function runTransform(binRoot, transformName, args, extensions) {
8581

8682
switch (type) {
8783
case 'js':
88-
return runJsTransform(root, transformName, args, extensions);
84+
return runJsTransform(transformPath, args, extensions);
8985
case 'hbs':
90-
return runTemplateTransform(root, transformName, args);
86+
return runTemplateTransform(transformPath, args);
9187
default:
9288
throw new Error(`Unknown type passed to runTransform: "${type}"`);
9389
}

src/transform-support.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
function getTransformPath(root, transformName) {
44
const path = require('path');
55

6+
// transformName **IS** a valid path, no need to resolve manually
7+
if (transformName.startsWith('.') || transformName.startsWith('/')) {
8+
return transformName;
9+
}
10+
611
return require.resolve(path.join(root, 'transforms', transformName));
712
}
813

tests/cli-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,25 @@ QUnit.module('codemod-cli', function (hooks) {
444444
},
445445
});
446446
});
447+
448+
QUnit.test('works with custom codemod directory', async function (assert) {
449+
userProject.write({
450+
foo: { 'something.js': 'let blah = bar', 'other.js': 'let blah = bar' },
451+
});
452+
453+
await execa(EXECUTABLE_PATH, ['generate', 'codemod', 'secondary', '--codemod-dir', 'other-dir'], {
454+
cwd: codemodProject.path(),
455+
});
456+
457+
await execa(codemodProject.path('bin/cli.js'), [codemodProject.path('./other-dir/secondary/index.js'), 'foo/*thing.js']);
458+
459+
assert.deepEqual(userProject.read(), {
460+
foo: {
461+
'something.js': 'let halb = rab',
462+
'other.js': 'let blah = bar',
463+
},
464+
});
465+
});
447466
});
448467

449468
QUnit.module('programmatic API', function (hooks) {

0 commit comments

Comments
 (0)