Skip to content

Commit 5828779

Browse files
committed
Add separate functions to read JSON
1 parent 37c8e3d commit 5828779

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

packages/compass-e2e-tests/smoke-test.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env npx ts-node
2-
import { createWriteStream, existsSync, promises as fs } from 'fs';
3-
import path from 'path';
2+
import assert from 'node:assert/strict';
3+
import { createWriteStream, existsSync, promises as fs } from 'node:fs';
4+
import path from 'node:path';
5+
46
import yargs from 'yargs';
57
import { hideBin } from 'yargs/helpers';
68
import https from 'https';
@@ -88,6 +90,23 @@ const argv = yargs(hideBin(process.argv))
8890

8991
type SmokeTestsContext = ReturnType<typeof argv['parseSync']>;
9092

93+
async function readJson<T extends object>(...segments: string[]): Promise<T> {
94+
const result = JSON.parse(
95+
await fs.readFile(path.join(...segments), 'utf8')
96+
) as unknown;
97+
assert(typeof result === 'object' && result !== null, 'Expected an object');
98+
return result as T;
99+
}
100+
101+
async function readPackageVersion(packagePath: string) {
102+
const pkg = await readJson(packagePath, 'package.json');
103+
assert(
104+
'version' in pkg && typeof pkg.version === 'string',
105+
'Expected a package version'
106+
);
107+
return pkg.version;
108+
}
109+
91110
async function run() {
92111
const parsedArgs = argv.parseSync();
93112

@@ -111,11 +130,7 @@ async function run() {
111130

112131
const compassDir = path.resolve(__dirname, '..', '..', 'packages', 'compass');
113132
// use the specified DEV_VERSION_IDENTIFIER if set or load version from packages/compass/package.json
114-
const version = context.devVersion
115-
? context.devVersion
116-
: (JSON.parse(
117-
await fs.readFile(path.join(compassDir, 'package.json'), 'utf8')
118-
).version as string);
133+
const version = context.devVersion ?? (await readPackageVersion(compassDir));
119134
const platform = platformFromContext(context);
120135
const outPath = path.resolve(__dirname, 'hadron-build-info.json');
121136

@@ -130,7 +145,7 @@ async function run() {
130145
};
131146
console.log('infoArgs', infoArgs);
132147
writeBuildInfo(infoArgs);
133-
const buildInfo = JSON.parse(await fs.readFile(infoArgs.out, 'utf8'));
148+
const buildInfo = await readJson(infoArgs.out);
134149

135150
if (!buildInfoIsCommon(buildInfo)) {
136151
throw new Error('buildInfo is missing');

0 commit comments

Comments
 (0)