1
- import ejs from "ejs" ;
2
- import fs from 'fs-extra'
3
- import path from "path" ;
4
- import { format , resolveConfig } from "prettier" ;
1
+ import { render } from "ejs" ;
2
+ import { readFile , outputFile , remove } from 'fs-extra'
3
+ import { resolve , extname , parse } from "path" ;
4
+ import { format as prettierFormatter } from "prettier/standalone"
5
+ import parserBabel from "prettier/parser-babel" ;
6
+ import parserEstree from "prettier/plugins/estree" ;
5
7
import options from '../core/utils/vue/options'
6
- import { fileURLToPath } from "node:url" ;
7
- import { dirname } from "path" ;
8
8
9
9
// formatting the code
10
10
11
11
export async function ejsRender ( filePath : string , name : string ) : Promise < void > {
12
12
try {
13
- const __filename = fileURLToPath ( import . meta. url ) ;
14
- const __dirname = dirname ( __filename ) ;
15
13
let prettierCode : string = '' ;
16
14
17
- const language = options . useTypeScript ? 'vue-ts' : 'vue-js' ;
15
+ const file = parse ( filePath ) ;
18
16
19
- const templatePath = path . resolve ( __dirname , `../template/ ${ language } ` )
17
+ const dest = resolve ( process . cwd ( ) , name )
20
18
21
- const file = path . parse ( filePath ) ;
19
+ const readFilePath = resolve ( dest , file . dir , ` ${ file . name } .ejs` )
22
20
23
- const dest = path . resolve ( process . cwd ( ) , name )
21
+ const outputFilePath = resolve ( dest , filePath )
24
22
25
- const readFilePath = path . resolve ( dest , file . dir , ` ${ file . name } .ejs` )
23
+ const templateCode = await readFile ( readFilePath )
26
24
27
- const outputFilePath = path . resolve ( dest , filePath )
25
+ const code = render ( templateCode . toString ( ) , options ) ;
28
26
29
- const templateCode = await fs . readFile ( readFilePath )
30
-
31
- const code = ejs . render ( templateCode . toString ( ) , options ) ;
32
-
33
- const extname = path . extname ( filePath ) . replace ( / [ . ] / g, '' )
34
- const opts = await resolveConfig ( templatePath )
27
+ const extensionName = extname ( filePath ) . replace ( / [ . ] / g, '' )
35
28
36
29
try {
37
- switch ( extname ) {
30
+ switch ( extensionName ) {
38
31
case 'ts' :
39
32
case 'tsx' :
40
33
case 'jsx' :
41
34
case 'js' :
42
- prettierCode = await format ( code , {
35
+ prettierCode = await prettierFormatter ( code , {
43
36
parser : 'babel' ,
44
- ... opts
37
+ plugins : [ parserBabel , parserEstree ]
45
38
} ) ;
46
39
break ;
47
40
case 'json' :
48
- prettierCode = await format ( code , {
41
+ prettierCode = await prettierFormatter ( code , {
49
42
parser : "json" ,
50
- ... opts
43
+ plugins : [ parserBabel , parserEstree ]
51
44
} ) ;
52
45
break ;
53
46
case 'cjs' :
54
- prettierCode = await format ( code , {
47
+ prettierCode = await prettierFormatter ( code , {
55
48
parser : "babel" ,
56
- ... opts
49
+ plugins : [ parserBabel , parserEstree ]
57
50
} ) ;
58
51
break ;
59
52
case 'toml' :
@@ -63,14 +56,14 @@ export async function ejsRender(filePath: string, name: string): Promise<void> {
63
56
prettierCode = code
64
57
break
65
58
default :
66
- prettierCode = await format ( code , { parser : extname } )
59
+ prettierCode = await prettierFormatter ( code , { parser : extname } )
67
60
break
68
61
}
69
62
} catch ( err ) {
70
63
console . log ( err )
71
64
}
72
- await fs . outputFile ( outputFilePath , prettierCode )
73
- await fs . remove ( readFilePath )
65
+ await outputFile ( outputFilePath , prettierCode )
66
+ await remove ( readFilePath )
74
67
} catch ( error ) {
75
68
console . log ( error )
76
69
}
0 commit comments