@@ -2,12 +2,19 @@ module.exports.command = 'codemod <codemod-name>';
22module . exports . desc = 'Generate a new codemod file' ;
33
44module . exports . builder = function builder ( yargs ) {
5- yargs . positional ( 'codemod-name' , {
6- describe : 'the name of the codemod to generate' ,
7- } ) ;
5+ yargs
6+ . positional ( 'codemod-name' , {
7+ describe : 'the name of the codemod to generate' ,
8+ } )
9+ . option ( 'type' , {
10+ alias : 't' ,
11+ describe : 'choose the transform type' ,
12+ choices : [ 'js' , 'hbs' ] ,
13+ default : 'js' ,
14+ } ) ;
815} ;
916
10- module . exports . handler = function handler ( options ) {
17+ function jsHandler ( options ) {
1118 const fs = require ( 'fs-extra' ) ;
1219 const { stripIndent } = require ( 'common-tags' ) ;
1320 const importCwd = require ( 'import-cwd' ) ;
@@ -36,7 +43,9 @@ module.exports.handler = function handler(options) {
3643 .join('');
3744 })
3845 .toSource();
39- }
46+ };
47+
48+ module.exports.type = 'js';
4049 ` ,
4150 'utf8'
4251 ) ;
@@ -47,8 +56,7 @@ module.exports.handler = function handler(options) {
4756
4857 const { runTransformTest } = require('codemod-cli');
4958
50- runTransformTest({
51- type: 'jscodeshift',
59+ runTransformTest({
5260 name: '${ codemodName } ',
5361 });
5462 ` ,
@@ -86,5 +94,95 @@ module.exports.handler = function handler(options) {
8694 'utf8'
8795 ) ;
8896
89- generateFixture ( { codemodName, fixtureName : 'basic' } ) ;
97+ generateFixture ( { codemodName, fixtureName : 'basic' , type : options . type } ) ;
98+ }
99+
100+ function hbsHandler ( options ) {
101+ const fs = require ( 'fs-extra' ) ;
102+ const { stripIndent } = require ( 'common-tags' ) ;
103+ const importCwd = require ( 'import-cwd' ) ;
104+ const generateFixture = require ( './fixture' ) . handler ;
105+
106+ let { codemodName } = options ;
107+ let projectName = importCwd ( './package.json' ) . name ;
108+ let codemodDir = `${ process . cwd ( ) } /transforms/${ codemodName } ` ;
109+
110+ fs . outputFileSync (
111+ `${ codemodDir } /index.js` ,
112+ stripIndent `
113+ module.exports = function ({ source /*, path*/ }, { parse, visit }) {
114+ const ast = parse(source);
115+
116+ return visit(ast, (env) => {
117+ let { builders: b } = env.syntax;
118+
119+ return {
120+ MustacheStatement() {
121+ return b.mustache(b.path('wat-wat'));
122+ },
123+ };
124+ });
125+ };
126+
127+ module.exports.type = 'hbs';
128+ ` ,
129+ 'utf8'
130+ ) ;
131+ fs . outputFileSync (
132+ `${ codemodDir } /test.js` ,
133+ stripIndent `
134+ 'use strict';
135+
136+ const { runTransformTest } = require('codemod-cli');
137+
138+ runTransformTest({
139+ name: '${ codemodName } ',
140+ });
141+ ` ,
142+ 'utf8'
143+ ) ;
144+ fs . outputFileSync (
145+ `${ codemodDir } /README.md` ,
146+ stripIndent `
147+ # ${ codemodName } \n
148+
149+ ## Usage
150+
151+ \`\`\`
152+ npx ${ projectName } ${ codemodName } path/of/files/ or/some**/*glob.hbs
153+
154+ # or
155+
156+ yarn global add ${ projectName }
157+ ${ projectName } ${ codemodName } path/of/files/ or/some**/*glob.hbs
158+ \`\`\`
159+
160+ ## Local Usage
161+ \`\`\`
162+ node ./bin/cli.js ${ codemodName } path/of/files/ or/some**/*glob.hbs
163+ \`\`\`
164+
165+ ## Input / Output
166+
167+ <!--FIXTURES_TOC_START-->
168+ <!--FIXTURES_TOC_END-->
169+
170+ <!--FIXTURES_CONTENT_START-->
171+ <!--FIXTURES_CONTENT_END-->
172+ ` ,
173+ 'utf8'
174+ ) ;
175+
176+ generateFixture ( { codemodName, fixtureName : 'basic' , type : options . type } ) ;
177+ }
178+
179+ module . exports . handler = function handler ( options ) {
180+ switch ( options . type ) {
181+ case 'js' :
182+ return jsHandler ( options ) ;
183+ case 'hbs' :
184+ return hbsHandler ( options ) ;
185+ default :
186+ throw new Error ( `Unknown type: "${ options . type } "` ) ;
187+ }
90188} ;
0 commit comments