forked from denoland/deno_bindgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
62 lines (51 loc) · 1.37 KB
/
cli.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
// Copyright 2020-2021 the Deno authors. All rights reserved. MIT license.
import { ensureDir } from "https://deno.land/std@0.105.0/fs/ensure_dir.ts";
import { parse } from "https://deno.land/std@0.105.0/flags/mod.ts";
import { codegen } from "./codegen.ts";
const flags = parse(Deno.args, { "--": true });
const release = !!flags.release;
const fetchPrefix = typeof flags.release == "string"
? flags.release
: "../target/" + (release ? "release" : "debug");
async function build() {
const cmd = ["cargo", "build"];
if (release) cmd.push("--release");
cmd.push(...flags["--"]);
const proc = Deno.run({ cmd });
await proc.status();
}
let source = null;
async function generate() {
let conf;
try {
conf = JSON.parse(await Deno.readTextFile("bindings.json"));
} catch (_) {
// Nothing to update.
return;
}
const pkgName = conf.name;
source = "// Auto-generated with deno_bindgen\n";
source += codegen(
fetchPrefix,
pkgName,
conf.typeDefs,
conf.tsTypes,
conf.symbols,
{
le: conf.littleEndian,
release,
},
);
await Deno.remove("bindings.json");
}
try {
await Deno.remove("bindings.json");
} catch (e) {
// no op
}
await build().catch((_) => Deno.removeSync("bindings.json"));
await generate();
if (source != null) {
await ensureDir("bindings");
await Deno.writeTextFile("bindings/bindings.ts", source);
}