aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/config/fetchPackageMetadata.ts
blob: 02e16d57609a20b01d1654542ba89842b1533758 (plain) (blame)
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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { readFile } from 'node:fs/promises';
import path from 'node:path';

import z from 'zod';

const PackageInfo = z.object({
  name: z.string().min(1),
  version: z.string().min(1),
});

export default async function fetchPackageMetadata(
  thisDir: string,
): Promise<void> {
  const contents = await readFile(path.join(thisDir, 'package.json'), 'utf8');
  const { name: packageName, version: packageVersion } = PackageInfo.parse(
    JSON.parse(contents),
  );
  process.env['VITE_PACKAGE_NAME'] ??= packageName;
  process.env['VITE_PACKAGE_VERSION'] ??= packageVersion;
}