Skip to content

Commit 7360f3f

Browse files
committed
Clean up files (combine help, file checks)
1 parent 836ba1b commit 7360f3f

File tree

9 files changed

+55
-68
lines changed

9 files changed

+55
-68
lines changed

src/cli/js-compute-runtime-cli.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env node
22

33
import { parseInputs } from '../parseInputs.js';
4-
import { printVersion } from '../printVersion.js';
5-
import { printHelp } from '../printHelp.js';
4+
import { printHelp, printVersion } from '../printHelp.js';
65
import { addSdkMetadataField } from '../addSdkMetadataField.js';
76

87
const parsedInputs = await parseInputs(process.argv.slice(2));

src/compileApplicationToWasm.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import { rmSync } from 'node:fs';
1212
import weval from '@bytecodealliance/weval';
1313
import wizer from '@bytecodealliance/wizer';
1414

15-
import { isFile } from './isFile.js';
16-
import { isFileOrDoesNotExist } from './isFileOrDoesNotExist.js';
15+
import { isDirectory, isFile } from './isFile.js';
1716
import { postbundle } from './postbundle.js';
1817
import { bundle } from './bundle.js';
1918
import { composeSourcemaps, ExcludePattern } from './composeSourcemaps.js';
@@ -102,30 +101,29 @@ export async function compileApplicationToWasm(params: CompileApplicationToWasmP
102101
);
103102
process.exit(1);
104103
}
105-
try {
106-
await mkdir(dirname(output), {
107-
recursive: true,
108-
});
109-
} catch (maybeError: unknown) {
110-
const error = maybeError instanceof Error ? maybeError : new Error(String(maybeError));
111-
console.error(
112-
`Error: Failed to create the \`output\` (${output}) directory`,
113-
error.message,
114-
);
115-
process.exit(1);
116-
}
117104

105+
// If output exists already, make sure it's not a directory
106+
// (we'll try to overwrite it if it's a file)
118107
try {
119-
if (!(await isFileOrDoesNotExist(output))) {
108+
if (await isDirectory(output)) {
120109
console.error(
121-
`Error: The \`output\` path does not point to a file: ${output}`,
110+
`Error: The \`output\` path points to a directory: ${output}`,
122111
);
123112
process.exit(1);
124113
}
114+
} catch {
115+
// Output doesn't exist
116+
}
117+
118+
try {
119+
await mkdir(dirname(output), {
120+
recursive: true,
121+
});
125122
} catch (maybeError: unknown) {
126-
const error = maybeError instanceof Error ? maybeError : new Error(String(maybeError));
123+
const error =
124+
maybeError instanceof Error ? maybeError : new Error(String(maybeError));
127125
console.error(
128-
`Error: Failed to check whether the \`output\` (${output}) is a file path`,
126+
`Error: Failed to create the \`output\` (${dirname(output)}) directory: ${output}`,
129127
error.message,
130128
);
131129
process.exit(1);

src/isFile.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ export async function isFile(path: string) {
44
const stats = await stat(path);
55
return stats.isFile();
66
}
7+
8+
export async function isDirectory(path: string) {
9+
const stats = await stat(path);
10+
return stats.isDirectory();
11+
}

src/isFileOrDoesNotExist.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/parseInputs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { fileURLToPath } from 'node:url';
22
import { dirname, join, isAbsolute } from 'node:path';
3-
import { unknownArgument } from './unknownArgument.js';
4-
import { tooManyEngines } from './tooManyEngines.js';
3+
import { tooManyEngines, unknownArgument } from './printHelp.js';
54
import { EnvParser } from './env.js';
65

76
export type ParsedInputs =

src/printHelp.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { basename } from 'node:path';
1+
import { readFile } from 'node:fs/promises';
2+
import { basename, dirname, join } from 'node:path';
23
import { argv } from 'node:process';
3-
import { printVersion } from './printVersion.js';
4+
import { fileURLToPath } from 'node:url';
5+
const __dirname = dirname(fileURLToPath(import.meta.url));
46

57
export async function printHelp() {
68
await printVersion();
@@ -34,3 +36,31 @@ ARGS:
3436
<output> The file path to write the output Wasm module to [default: bin/main.wasm]
3537
`);
3638
}
39+
40+
export async function printVersion() {
41+
const packageJson = await readFile(join(__dirname, '../package.json'), {
42+
encoding: 'utf-8',
43+
});
44+
const version = (JSON.parse(packageJson) as { version: string }).version;
45+
console.log(`${basename(argv[1])} ${version}`);
46+
}
47+
48+
export function tooManyEngines() {
49+
console.error(`error: The argument '--engine-wasm <engine-wasm>' was provided more than once, but cannot be used multiple times
50+
51+
USAGE:
52+
js-compute-runtime --engine-wasm <engine-wasm>
53+
54+
For more information try --help`);
55+
process.exit(1);
56+
}
57+
58+
export function unknownArgument(cliInput: string) {
59+
console.error(`error: Found argument '${cliInput}' which wasn't expected, or isn't valid in this context
60+
61+
USAGE:
62+
js-compute-runtime [FLAGS] [OPTIONS] [ARGS]
63+
64+
For more information try --help`);
65+
process.exit(1);
66+
}

src/printVersion.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/tooManyEngines.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/unknownArgument.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)