1
- import { outputFileSync , removeSync , readFileSync , existsSync } from "fs-extra" ;
1
+ import { outputFileSync , removeSync , readFileSync , existsSync , readFile , pathExists } from "fs-extra" ;
2
2
import * as traverse from "filewalker" ;
3
3
import * as watch from "node-watch" ;
4
4
import { SvelteTranscriber } from "../src" ;
5
5
import { config } from "./config" ;
6
6
import { Transcriber } from "typedraft" ;
7
- import { transformSync } from "@babel/core" ;
7
+ import { transformSync , transformAsync } from "@babel/core" ;
8
8
import * as TypescriptPreset from "@babel/preset-typescript" ;
9
9
10
10
function TraverseDirectory ( path : string , callback : ( name : string , path : string ) => void )
@@ -91,15 +91,34 @@ export function ComposeFile(source: string)
91
91
{
92
92
if ( source . endsWith ( ".svelte.tsx" ) )
93
93
{
94
- TranscribeSvelteDraft ( source ) ;
94
+ const component = TranscribeSvelteDraftSync ( source ) ;
95
+ outputFileSync ( source . replace ( ".tsx" , "" ) , component , "utf8" ) ;
95
96
}
96
97
else if ( source . endsWith ( ".js.tsx" ) )
97
98
{
98
- TranscribeTypeDraft ( source ) ;
99
+ const code = TranscribeTypeDraftSync ( source ) ;
100
+ outputFileSync ( source . replace ( ".tsx" , "" ) , code , "utf8" ) ;
99
101
}
100
102
}
101
103
102
- function TranscribeTypeDraft ( source : string )
104
+ export async function TranscribeTypeDraftAsync ( source : string )
105
+ {
106
+ const code = await readFile ( source , "utf8" ) ;
107
+ const transcriber = new Transcriber ( code ) ;
108
+ config . dsls . forEach ( dsl => transcriber . AddDSL ( dsl . name , dsl . dsl ) ) ;
109
+
110
+ const ts_code = transcriber . Transcribe ( ) ;
111
+
112
+ const js_code = await transformAsync ( ts_code , {
113
+ filename : "script.tsx" ,
114
+ ast : true ,
115
+ presets : [ [ TypescriptPreset , { jsxPragma : "preserve" , isTSX : true , allExtensions : true } ] ]
116
+ } ) ;
117
+
118
+ return js_code . code ;
119
+ }
120
+
121
+ export function TranscribeTypeDraftSync ( source : string )
103
122
{
104
123
const code = readFileSync ( source , "utf8" ) ;
105
124
const transcriber = new Transcriber ( code ) ;
@@ -113,10 +132,39 @@ function TranscribeTypeDraft(source: string)
113
132
presets : [ [ TypescriptPreset , { jsxPragma : "preserve" , isTSX : true , allExtensions : true } ] ]
114
133
} ) . code ;
115
134
116
- outputFileSync ( source . replace ( ".tsx" , "" ) , js_code , "utf8" ) ;
135
+ return js_code ;
117
136
}
118
137
119
- function TranscribeSvelteDraft ( source : string )
138
+ export async function TranscribeSvelteDraftAsync ( source : string )
139
+ {
140
+ const code = await readFile ( source , "utf8" ) ;
141
+ const transcriber = new SvelteTranscriber ( code ) ;
142
+ config . dsls . forEach ( dsl => transcriber . AddDSL ( dsl . name , dsl . dsl ) ) ;
143
+ const { import_section, script_section, template_section } = transcriber . TranscribeToSections ( ) ;
144
+
145
+ //
146
+ const style = source . replace ( ".svelte.tsx" , ".css" ) ;
147
+ const style_section = await pathExists ( style ) ? await readFile ( style , "utf8" ) : "" ;
148
+
149
+ //
150
+ const component = [
151
+ "<script>" ,
152
+ import_section ,
153
+ "\n" ,
154
+ script_section ,
155
+ "</script>" ,
156
+ "\n" ,
157
+ template_section ,
158
+ "\n" ,
159
+ "<style>" ,
160
+ style_section ,
161
+ "</style>"
162
+ ] . join ( "\n" ) ;
163
+
164
+ return component ;
165
+ }
166
+
167
+ export function TranscribeSvelteDraftSync ( source : string )
120
168
{
121
169
const code = readFileSync ( source , "utf8" ) ;
122
170
const transcriber = new SvelteTranscriber ( code ) ;
@@ -141,5 +189,6 @@ function TranscribeSvelteDraft(source: string)
141
189
style_section ,
142
190
"</style>"
143
191
] . join ( "\n" ) ;
144
- outputFileSync ( source . replace ( ".tsx" , "" ) , component , "utf8" ) ;
192
+
193
+ return component ;
145
194
}
0 commit comments