Skip to content

Commit d0d5e29

Browse files
committed
self review
1 parent ad5d12d commit d0d5e29

File tree

5 files changed

+32
-45
lines changed

5 files changed

+32
-45
lines changed

lib/commands/build_command.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import type { Path } from "@david/path";
1515
export async function runBuildCommand(args: BuildCommand) {
1616
const output = await runPreBuild(args);
1717

18-
await args.outDir.ensureDir();
19-
await writeSnippets();
18+
args.outDir.ensureDirSync();
19+
writeSnippets();
2020

2121
const files = args.inline
2222
? await inlinePreBuild(output, args)
@@ -25,17 +25,17 @@ export async function runBuildCommand(args: BuildCommand) {
2525
for (const file of files) {
2626
console.log(` write ${colors.yellow(file.path.toString())}`);
2727
if (typeof file.data === "string") {
28-
await file.path.writeText(file.data);
28+
file.path.writeTextSync(file.data);
2929
} else {
30-
await file.path.write(file.data);
30+
file.path.writeSync(file.data);
3131
}
3232
}
3333

3434
console.log(
3535
`${colors.bold(colors.green("Finished"))} WebAssembly output`,
3636
);
3737

38-
async function writeSnippets() {
38+
function writeSnippets() {
3939
const localModules = Array.from(output.bindgen.localModules);
4040
const snippets = Array.from(output.bindgen.snippets);
4141

@@ -46,25 +46,25 @@ export async function runBuildCommand(args: BuildCommand) {
4646
const snippetsDest = args.outDir.join("snippets");
4747
// start with a fresh directory in order to clear out any previously
4848
// created snippets which might have a different name
49-
await snippetsDest.emptyDir();
49+
snippetsDest.emptyDirSync();
5050

5151
for (const [name, text] of localModules) {
5252
const filePath = snippetsDest.join(name);
5353
const dirPath = filePath.parentOrThrow();
54-
await dirPath.mkdir({ recursive: true });
55-
await filePath.writeText(text);
54+
dirPath.mkdirSync({ recursive: true });
55+
filePath.writeTextSync(text);
5656
}
5757

5858
for (const [identifier, list] of snippets) {
5959
if (list.length === 0) {
6060
continue;
6161
}
6262
const dirPath = snippetsDest.join(identifier);
63-
await dirPath.mkdir({ recursive: true });
63+
dirPath.mkdirSync({ recursive: true });
6464
for (const [i, text] of list.entries()) {
6565
const name = `inline${i}.js`;
6666
const filePath = dirPath.join(name);
67-
await filePath.writeText(text);
67+
filePath.writeTextSync(text);
6868
}
6969
}
7070
}
@@ -85,8 +85,8 @@ async function handleWasmModuleOutput(
8585
),
8686
data: await getFormattedText(`${generatedHeader}
8787
// @ts-self-types="./${output.bindingDts.path.basename()}"
88-
// source-hash: ${output.sourceHash}
8988
89+
// source-hash: ${output.sourceHash}
9090
import * as wasm from "./${output.wasmFileName}";
9191
export * from "./${output.bindingJsBg.path.basename()}";
9292
import { __wbg_set_wasm } from "./${output.bindingJsBg.path.basename()}";
@@ -116,18 +116,8 @@ async function inlinePreBuild(
116116
),
117117
data: await getFormattedText(`${generatedHeader}
118118
// @ts-self-types="./${output.bindingDts.path.basename()}"
119-
// source-hash: ${output.sourceHash}
120-
121-
function base64decode(b64) {
122-
const binString = atob(b64);
123-
const size = binString.length;
124-
const bytes = new Uint8Array(size);
125-
for (let i = 0; i < size; i++) {
126-
bytes[i] = binString.charCodeAt(i);
127-
}
128-
return bytes;
129-
}
130119
120+
// source-hash: ${output.sourceHash}
131121
import * as imports from "./${output.bindingJsBg.path.basename()}";
132122
const bytes = base64decode("\\\n${
133123
base64.encodeBase64(wasmBytes).replace(/.{78}/g, "$&\\\n")
@@ -140,6 +130,16 @@ const wasm = new WebAssembly.Instance(wasmModule, {
140130
export * from "./${output.bindingJsBg.path.basename()}";
141131
import { __wbg_set_wasm } from "./${output.bindingJsBg.path.basename()}";
142132
__wbg_set_wasm(wasm.exports);
133+
134+
function base64decode(b64) {
135+
const binString = atob(b64);
136+
const size = binString.length;
137+
const bytes = new Uint8Array(size);
138+
for (let i = 0; i < size; i++) {
139+
bytes[i] = binString.charCodeAt(i);
140+
}
141+
return bytes;
142+
}
143143
`),
144144
}, {
145145
path: output.bindingJsBg.path,

lib/commands/check_command.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { runPreBuild } from "../pre_build.ts";
66

77
export async function runCheckCommand(args: CheckCommand) {
88
const output = await runPreBuild(args);
9-
const originalHash = await getOriginalSourceHash();
9+
const originalHash = getOriginalSourceHash();
1010
if (originalHash === output.sourceHash) {
1111
console.log(
1212
`${colors.bold(colors.green("Success"))} ` +
@@ -20,14 +20,12 @@ export async function runCheckCommand(args: CheckCommand) {
2020
Deno.exit(1);
2121
}
2222

23-
async function getOriginalSourceHash() {
23+
function getOriginalSourceHash() {
2424
const filePath = args.outDir.join(
2525
`${output.crateName}.${args.bindingJsFileExt}`,
2626
);
2727
try {
28-
return getSourceHashFromText(
29-
await filePath.readText(),
30-
);
28+
return getSourceHashFromText(filePath.readTextSync());
3129
} catch (err) {
3230
if (err instanceof Deno.errors.NotFound) {
3331
console.warn(`${colors.yellow("Warning")} could not find ${filePath}`);

lib/commands/new_command.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ edition = "2021"
4747
`,
4848
);
4949

50-
let gitIgnoreText = await getFileTextIfExists("./.gitignore") ?? "";
50+
const gitIgnoreFile = rootDir.join(".gitignore");
51+
let gitIgnoreText = gitIgnoreFile.readMaybeTextSync() ?? "";
5152
if (!/^\/target$/m.test(gitIgnoreText)) {
5253
gitIgnoreText = gitIgnoreText.trim();
5354
if (gitIgnoreText.length > 0) {
5455
gitIgnoreText = gitIgnoreText + "\n";
5556
}
5657
gitIgnoreText += "/target\n";
57-
await Deno.writeTextFile("./.gitignore", gitIgnoreText);
58+
gitIgnoreFile.writeTextSync(gitIgnoreText);
5859
}
5960

6061
const srcDir = rsLibDir.join("src");
@@ -143,18 +144,6 @@ function writeIfNotExists(path: Path, text: string) {
143144
path.writeTextSync(text);
144145
}
145146

146-
async function getFileTextIfExists(path: string) {
147-
try {
148-
return await Deno.readTextFile(path);
149-
} catch (err) {
150-
if (err instanceof Deno.errors.NotFound) {
151-
return undefined;
152-
} else {
153-
throw err;
154-
}
155-
}
156-
}
157-
158147
async function checkIfRequiredToolsExist() {
159148
const requiredTools = ["deno", "cargo"];
160149
const notInstalled: string[] = [];

lib/manifest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class WasmCrate {
162162
paths.sort();
163163
const hasher = new Sha1();
164164
for (const path of paths) {
165-
const fileText = await Deno.readTextFile(path);
165+
const fileText = path.readTextSync();
166166
// standardize file paths so this is not subject to
167167
// however git is configured to checkout files
168168
hasher.update(fileText.replace(/\r?\n/g, "\n"));
@@ -179,7 +179,7 @@ export class WasmCrate {
179179
})
180180
) {
181181
if (entry.isFile) {
182-
paths.push(entry.path);
182+
paths.push(new Path(entry.path));
183183
}
184184
}
185185
return paths;

tests/cli_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const rootFolder = new Path(import.meta.dirname!).parentOrThrow();
55

66
Deno.test("should create a new wasmbuild project, build it, and run it", async () => {
77
using tempDir = createTempDirSync();
8-
await tempDir.join("deno.json").writeText(
8+
tempDir.join("deno.json").writeTextSync(
99
`{ "tasks": { "wasmbuild": "${
1010
Deno.execPath().replace(/\\/g, "\\\\")
1111
} run -A ${
@@ -14,7 +14,7 @@ Deno.test("should create a new wasmbuild project, build it, and run it", async (
1414
);
1515
await runCommand("deno", "task", "wasmbuild", "new");
1616
await runCommand("deno", "task", "wasmbuild");
17-
await tempDir.join("test.ts").writeText(`
17+
tempDir.join("test.ts").writeTextSync(`
1818
import { add } from "./lib/rs_lib.js";
1919
2020
Deno.test("should add values", async () => {

0 commit comments

Comments
 (0)