Skip to content

Commit ae0a1ef

Browse files
authored
Merge pull request #25 from rwjblue/add-bin
Add bin script to generated project.
2 parents 2e5037f + f2d71d7 commit ae0a1ef

7 files changed

Lines changed: 288 additions & 99 deletions

File tree

commands/global/new.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ module.exports.handler = function handler(options) {
2525
scripts: {
2626
test: 'codemod-cli test',
2727
},
28+
bin: './bin/cli.js',
2829
keywords: ['codemod-cli'],
29-
devDependencies: {
30+
dependencies: {
3031
'codemod-cli': `^${pkg.version}`,
32+
},
33+
devDependencies: {
3134
jest: pkg.devDependencies.jest,
3235
},
3336
},
@@ -60,5 +63,22 @@ module.exports.handler = function handler(options) {
6063
- yarn test
6164
`
6265
);
66+
fs.outputFileSync(
67+
projectName + '/bin/cli.js',
68+
stripIndent`
69+
#!/usr/bin/env node
70+
'use strict';
71+
72+
require('codemod-cli').runTransform(
73+
__dirname,
74+
process.argv[2] /* transform name */,
75+
process.argv.slice(2) /* paths or globs */
76+
)
77+
`,
78+
{
79+
encoding: 'utf8',
80+
mode: 0o755 /* -rwxr-xr-x */,
81+
}
82+
);
6383
fs.ensureFileSync(projectName + '/transforms/.gitkeep');
6484
};

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010
"lint:js": "eslint .",
1111
"test": "qunit tests/**/*-test.js"
1212
},
13+
"files": [
14+
"src",
15+
"bin",
16+
"commands"
17+
],
1318
"dependencies": {
19+
"chalk": "^2.4.1",
1420
"common-tags": "^1.8.0",
21+
"execa": "^0.10.0",
1522
"fs-extra": "^6.0.1",
23+
"globby": "^8.0.1",
1624
"import-cwd": "^2.1.0",
1725
"import-local": "^1.0.0",
1826
"jscodeshift": "^0.5.1",
@@ -26,7 +34,6 @@
2634
"eslint-config-prettier": "^2.9.0",
2735
"eslint-plugin-node": "^6.0.1",
2836
"eslint-plugin-prettier": "^2.6.0",
29-
"execa": "^0.10.0",
3037
"jest": "^23.1.0",
3138
"lerna-changelog": "^0.8.0",
3239
"prettier": "^1.13.5",

src/bin-support.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
function runTransform(binRoot, transformName, paths) {
4+
const globby = require('globby');
5+
const execa = require('execa');
6+
const chalk = require('chalk');
7+
const path = require('path');
8+
9+
return globby(paths)
10+
.then(paths => {
11+
let transformPath = path.join(binRoot, '..', 'transforms', transformName, 'index.js');
12+
13+
return execa('jscodeshift', ['-t', transformPath, '--extensions', 'js,ts', ...paths], {
14+
stdio: 'inherit',
15+
});
16+
})
17+
.catch(error => {
18+
console.error(chalk.red(error.stack)); // eslint-disable-line no-console
19+
process.exitCode = 1;
20+
21+
throw error;
22+
});
23+
}
24+
25+
module.exports = {
26+
runTransform,
27+
};

src/index.js

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,9 @@
11
'use strict';
22

3-
/* global it, describe */
4-
5-
const { runInlineTest } = require('jscodeshift/dist/testUtils');
6-
const fs = require('fs-extra');
7-
const path = require('path');
8-
9-
function transformDetails(options) {
10-
let root = process.cwd() + `/transforms/${options.name}/`;
11-
12-
return {
13-
name: options.name,
14-
root,
15-
transformPath: root + 'index.js',
16-
fixtureDir: root + '__testfixtures__/',
17-
};
18-
}
19-
20-
function jscodeshiftTest(options) {
21-
let details = transformDetails(options);
22-
23-
let transform = require(details.transformPath);
24-
25-
describe(details.name, function() {
26-
fs.readdirSync(details.fixtureDir)
27-
.filter(filename => /\.input$/.test(path.basename(filename, path.extname(filename))))
28-
.forEach(filename => {
29-
let extension = path.extname(filename);
30-
let testName = filename.replace(`.input${extension}`, '');
31-
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
32-
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
33-
34-
describe(testName, function() {
35-
it('transforms correctly', function() {
36-
runInlineTest(
37-
transform,
38-
{},
39-
{ source: fs.readFileSync(inputPath, 'utf8') },
40-
fs.readFileSync(outputPath, 'utf8')
41-
);
42-
});
43-
44-
it('is idempotent', function() {
45-
runInlineTest(
46-
transform,
47-
{},
48-
{ source: fs.readFileSync(outputPath, 'utf8') },
49-
fs.readFileSync(outputPath, 'utf8')
50-
);
51-
});
52-
});
53-
});
54-
});
55-
}
56-
57-
function runTransformTest(options) {
58-
switch (options.type) {
59-
case 'jscodeshift':
60-
return jscodeshiftTest(options);
61-
}
62-
}
3+
const TestSupport = require('./test-support');
4+
const BinSupport = require('./bin-support');
635

646
module.exports = {
65-
runTransformTest,
7+
runTransformTest: TestSupport.runTransformTest,
8+
runTransform: BinSupport.runTransform,
669
};

src/test-support.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
/* global it, describe */
4+
5+
const { runInlineTest } = require('jscodeshift/dist/testUtils');
6+
const fs = require('fs-extra');
7+
const path = require('path');
8+
9+
function transformDetails(options) {
10+
let root = process.cwd() + `/transforms/${options.name}/`;
11+
12+
return {
13+
name: options.name,
14+
root,
15+
transformPath: root + 'index.js',
16+
fixtureDir: root + '__testfixtures__/',
17+
};
18+
}
19+
20+
function jscodeshiftTest(options) {
21+
let details = transformDetails(options);
22+
23+
let transform = require(details.transformPath);
24+
25+
describe(details.name, function() {
26+
fs.readdirSync(details.fixtureDir)
27+
.filter(filename => /\.input$/.test(path.basename(filename, path.extname(filename))))
28+
.forEach(filename => {
29+
let extension = path.extname(filename);
30+
let testName = filename.replace(`.input${extension}`, '');
31+
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
32+
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
33+
34+
describe(testName, function() {
35+
it('transforms correctly', function() {
36+
runInlineTest(
37+
transform,
38+
{},
39+
{ source: fs.readFileSync(inputPath, 'utf8') },
40+
fs.readFileSync(outputPath, 'utf8')
41+
);
42+
});
43+
44+
it('is idempotent', function() {
45+
runInlineTest(
46+
transform,
47+
{},
48+
{ source: fs.readFileSync(outputPath, 'utf8') },
49+
fs.readFileSync(outputPath, 'utf8')
50+
);
51+
});
52+
});
53+
});
54+
});
55+
}
56+
57+
function runTransformTest(options) {
58+
switch (options.type) {
59+
case 'jscodeshift':
60+
return jscodeshiftTest(options);
61+
}
62+
}
63+
64+
module.exports = {
65+
runTransformTest,
66+
};

0 commit comments

Comments
 (0)