1+ #!/usr/bin/env node
2+
3+ const yargs = require ( "yargs" ) ;
4+ const figlet = require ( "figlet" ) ;
5+ const fs = require ( "fs" ) ;
6+ const util = require ( "util" ) ;
7+ const child_process = require ( "child_process" ) ;
8+ const exec = util . promisify ( child_process . exec ) ;
9+ const fsExtra = require ( 'fs-extra' ) ;
10+ const path = require ( "path" ) ;
11+ const srcPath = path . join ( process . cwd ( ) , "src" ) ;
12+ if ( ! fs . existsSync ( srcPath ) ) { fs . mkdirSync ( srcPath ) ; }
13+
14+ let pathObj = { } ;
15+ // 展示log
16+ let showLog = false ;
17+
18+ console . log ( figlet . textSync ( "mybatis-cli" , {
19+ horizontalLayout : "full"
20+ } ) ) ;
21+
22+ /**
23+ * 拷贝文件夹下的文件
24+ * @param filePath 文件路径
25+ */
26+ function copyDirFile ( filePath ) {
27+ //根据文件路径读取文件,返回文件列表
28+ let files = fs . readdirSync ( filePath ) . filter ( item => item !== '.DS_Store' ) ;
29+ //遍历读取到的文件列表
30+ for ( const fileName of files ) {
31+ //获取当前文件的绝对路径
32+ const filedir = path . join ( filePath , fileName ) ;
33+ //根据文件路径获取文件信息,返回一个fs.Stats对象
34+ const stats = fs . statSync ( filedir ) ;
35+ const isFile = stats . isFile ( ) ; //是文件
36+ const isDir = stats . isDirectory ( ) ; //是文件夹
37+ if ( isFile ) {
38+ const _dirName = path . dirname ( filedir ) ;
39+ const _fileName = path . basename ( filedir ) ;
40+ const fileNameArray = _fileName . split ( "." ) ;
41+ const filePrefix = fileNameArray [ 0 ] ;
42+ const fileSuffix = `.${ fileNameArray [ 1 ] } ` ;
43+ // 排除掉 .DS_Store 这个文件
44+ if ( fileSuffix !== ".DS_Store" ) {
45+ const destPath = `${ _dirName } /${ filePrefix } Copy${ fileSuffix } ` ;
46+ fs . copyFile ( filedir , destPath , ( err ) => { } ) ;
47+ pathObj [ filedir ] = destPath ;
48+ }
49+ }
50+ if ( isDir ) {
51+ copyDirFile ( filedir ) ; //递归,如果是文件夹,就继续遍历该文件夹下面的文件
52+ }
53+ }
54+ }
55+
56+ /**
57+ * 更新文件夹下的文件
58+ * @param filePath 文件路径
59+ */
60+ function updateDirFile ( filePath ) {
61+ showLog && console . log ( 'updateDirFile' )
62+ //根据文件路径读取文件,返回文件列表
63+ const files = fs . readdirSync ( filePath ) . filter ( item => item !== '.DS_Store' ) ;
64+ //遍历读取到的文件列表
65+ for ( const fileName of files ) {
66+ //获取当前文件的绝对路径
67+ const filedir = path . join ( filePath , fileName ) ;
68+ //根据文件路径获取文件信息,返回一个fs.Stats对象
69+ const stats = fs . statSync ( filedir ) ;
70+ const isFile = stats . isFile ( ) ; //是文件
71+ const isDir = stats . isDirectory ( ) ; //是文件夹
72+ if ( isFile ) {
73+ _fileName = path . basename ( filedir ) ;
74+ const fileNameArray = _fileName . split ( "." ) ;
75+ fileSuffix = `.${ fileNameArray [ 1 ] } ` ;
76+ let splitStr ;
77+ if ( fileSuffix === ".java" ) {
78+ splitStr = "/** The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment. */" ;
79+ } else if ( fileSuffix === ".xml" ) {
80+ splitStr = "<!-- The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment. -->" ;
81+ } else {
82+ splitStr = "The above part of the comment is auto generated, the following part is written by the user, please do not delete this comment." ;
83+ }
84+ if ( pathObj [ filedir ] && fs . existsSync ( pathObj [ filedir ] ) ) {
85+ let data1 = fs . readFileSync ( filedir , "utf-8" ) ;
86+ let str1 = data1 . toString ( ) ;
87+ const _data1 = str1 . split ( splitStr ) ;
88+ updateFileStr = _data1 [ 0 ] ;
89+
90+ let data2 = fs . readFileSync ( pathObj [ filedir ] , "utf-8" ) ;
91+ let str2 = data2 . toString ( ) ;
92+ const _data2 = str2 . split ( splitStr ) ;
93+ copyFileStr = _data2 [ 1 ] ;
94+ resultStr = updateFileStr + splitStr + copyFileStr ;
95+ // 写入文件
96+ fs . writeFileSync ( filedir , resultStr , function ( err ) {
97+ if ( err ) return console . log ( err ) ;
98+ } ) ;
99+ }
100+ }
101+ if ( isDir ) {
102+ updateDirFile ( filedir ) ; //递归,如果是文件夹,就继续遍历该文件夹下面的文件
103+ }
104+ }
105+ }
106+
107+ /**
108+ * 删除文件
109+ * @param delPath 文件路径
110+ */
111+ function deleteFile ( delPath ) {
112+ try {
113+ // 判断文件或文件夹是否存在
114+ if ( fs . existsSync ( delPath ) ) {
115+ fs . unlinkSync ( delPath ) ;
116+ } else {
117+ console . log ( "inexistence path:" , delPath ) ;
118+ }
119+ } catch ( error ) {
120+ console . log ( "del error" , error ) ;
121+ }
122+ }
123+
124+ /**
125+ * 删除文件夹下的文件
126+ */
127+ function deleteDirFile ( ) {
128+ for ( const filePath in pathObj ) {
129+ //根据文件路径获取文件信息,返回一个fs.Stats对象
130+ const stats = fs . statSync ( filePath ) ;
131+ const isFile = stats . isFile ( ) ; //是文件
132+ if ( isFile && pathObj [ filePath ] && fs . existsSync ( pathObj [ filePath ] ) ) {
133+ deleteFile ( pathObj [ filePath ] ) ;
134+ }
135+ }
136+ }
137+
138+
139+ /**
140+ * init 初始化
141+ */
142+ async function init ( ) {
143+ try {
144+ const globalNodeModulesPathObj = await exec ( 'npm root -g' ) || { } ;
145+ const globalNodeModulesPath = globalNodeModulesPathObj . stdout . replace ( '\n' , '' ) ;
146+ const globalJasmineTemplatePath = path . join ( globalNodeModulesPath , 'mybatis-cli/src/template' ) ;
147+ // Async with promises:
148+ fsExtra . copy ( globalJasmineTemplatePath , `${ process . cwd ( ) } ` )
149+ . then ( ( ) => console . log ( 'mybatis-cli init success' ) )
150+ . catch ( ( err ) => console . error ( err ) ) ;
151+ } catch ( error ) {
152+ console . log ( "mybatis-cli init error: " + error ) ;
153+ }
154+ }
155+
156+ /**
157+ * generate 生成mybatis entity、mapper、xml
158+ */
159+ async function generate ( ) {
160+ try {
161+ const globalNodeModulesPathObj = await exec ( 'npm root -g' ) || { } ;
162+ const globalNodeModulesPath = globalNodeModulesPathObj . stdout . replace ( '\n' , '' ) ;
163+ const globalJasmineBinPath = path . join ( globalNodeModulesPath , 'mybatis-cli/src/jasmine-bin/bin/jasmine' ) ;
164+ // 创建文件副本
165+ copyDirFile ( srcPath ) ;
166+ // 生成mybatis相关文件
167+ await exec ( `${ globalJasmineBinPath } ${ process . cwd ( ) } /jasmine.properties` ) ;
168+ // 更新文件
169+ updateDirFile ( srcPath ) ;
170+ // 删除文件副本
171+ deleteDirFile ( ) ;
172+ console . log ( 'mybatis-cli generate success' ) ;
173+ } catch ( error ) {
174+ console . log ( "mybatis-cli generate error: " + error ) ;
175+ }
176+ }
177+
178+ // 输出支持的命令
179+ yargs
180+ . scriptName ( 'mybatis-cli' )
181+ . usage ( '$0 <cmd> [args]' )
182+ . command (
183+ 'i' ,
184+ '初始化生成mybatis-cli配置文件' ,
185+ ( yargs ) => { } ,
186+ function ( argv ) {
187+ init ( ) ;
188+ }
189+ )
190+ . command (
191+ 'g' ,
192+ '生成mybatis' ,
193+ ( yargs ) => { } ,
194+ function ( argv ) {
195+ generate ( ) ;
196+ }
197+ )
198+ . help ( ) . argv ;
0 commit comments