Skip to content

Commit a788dfc

Browse files
committed
export utility function for plugin use
1 parent fb05308 commit a788dfc

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

cli/literator.ts

+57-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { outputFileSync, removeSync, readFileSync, existsSync } from "fs-extra";
1+
import { outputFileSync, removeSync, readFileSync, existsSync, readFile, pathExists } from "fs-extra";
22
import * as traverse from "filewalker";
33
import * as watch from "node-watch";
44
import { SvelteTranscriber } from "../src";
55
import { config } from "./config";
66
import { Transcriber } from "typedraft";
7-
import { transformSync } from "@babel/core";
7+
import { transformSync, transformAsync } from "@babel/core";
88
import * as TypescriptPreset from "@babel/preset-typescript";
99

1010
function TraverseDirectory(path: string, callback: (name: string, path: string) => void)
@@ -91,15 +91,34 @@ export function ComposeFile(source: string)
9191
{
9292
if (source.endsWith(".svelte.tsx"))
9393
{
94-
TranscribeSvelteDraft(source);
94+
const component = TranscribeSvelteDraftSync(source);
95+
outputFileSync(source.replace(".tsx", ""), component, "utf8");
9596
}
9697
else if (source.endsWith(".js.tsx"))
9798
{
98-
TranscribeTypeDraft(source);
99+
const code = TranscribeTypeDraftSync(source);
100+
outputFileSync(source.replace(".tsx", ""), code, "utf8");
99101
}
100102
}
101103

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)
103122
{
104123
const code = readFileSync(source, "utf8");
105124
const transcriber = new Transcriber(code);
@@ -113,10 +132,39 @@ function TranscribeTypeDraft(source: string)
113132
presets: [[TypescriptPreset, { jsxPragma: "preserve", isTSX: true, allExtensions: true }]]
114133
}).code;
115134

116-
outputFileSync(source.replace(".tsx", ""), js_code, "utf8");
135+
return js_code;
117136
}
118137

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)
120168
{
121169
const code = readFileSync(source, "utf8");
122170
const transcriber = new SvelteTranscriber(code);
@@ -141,5 +189,6 @@ function TranscribeSvelteDraft(source: string)
141189
style_section,
142190
"</style>"
143191
].join("\n");
144-
outputFileSync(source.replace(".tsx", ""), component, "utf8");
192+
193+
return component;
145194
}

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-draft",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Develop svelte app in typedraft",
55
"author": "mistlog",
66
"license": "MIT",

0 commit comments

Comments
 (0)