-
Notifications
You must be signed in to change notification settings - Fork 2
/
svg2elm.ts
68 lines (58 loc) · 1.81 KB
/
svg2elm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import path from "path";
import fs from "fs";
const { Elm } = require("./Main.elm");
type Ok = { type: "Ok"; elm: string };
type Err = { type: "Err"; error: string };
type Result = Ok | Err;
/**
* Generates an Elm function from an SVG.
*
* @param name - The name of the Elm function to generate. This name will be camelCased.
* @param code - The source code of the SVG file
*/
export function generateSvgFunction(
name: string,
code: string
): Promise<string> {
return new Promise((resolve, reject) => {
const app = Elm.Main.init({ flags: { name, code } });
app.ports.compiled.subscribe((result: Result) => {
if (result.type === "Ok") {
resolve(result.elm);
} else {
reject(result.error);
}
});
});
}
/**
* Given a module name, generates the module declaration and required imports.
*
* @param moduleName - The name of the Elm module to generate.
*/
export function generateModuleHeader(moduleName: string) {
return `module ${moduleName} exposing (..)
import Svg
import VirtualDom exposing (Attribute, attribute)
`;
}
/**
* Generates an Elm Module with a function per SVG file.
*
* @param moduleName - The name of the Elm module to generate.
* @param filePaths - An array of file paths to SVG files
*/
export async function generateModule(
moduleName: string,
filePaths: string[]
): Promise<string> {
const functions = await Promise.all(
filePaths.map(async filePath => {
const basename = path.basename(filePath, ".svg");
const content = await fs.promises.readFile(filePath);
return generateSvgFunction(basename, content.toString());
})
);
const header = generateModuleHeader(moduleName);
return `${header}\n\n${functions.join("\n\n")}`;
}