From 0564d36cd9e8a4b415dd8bf75df4c221d3778da9 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Thu, 3 Apr 2025 14:17:59 +0200 Subject: [PATCH] refactor: extract utils read package json --- src/services/build/build.rust.services.ts | 5 ++--- src/services/eject/eject.javascript.services.ts | 7 +++---- src/utils/pkg.utils.ts | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/utils/pkg.utils.ts diff --git a/src/services/build/build.rust.services.ts b/src/services/build/build.rust.services.ts index caa7780c..c6cc1464 100644 --- a/src/services/build/build.rust.services.ts +++ b/src/services/build/build.rust.services.ts @@ -17,6 +17,7 @@ import type {BuildArgs} from '../../types/build'; import {readSatelliteDid} from '../../utils/did.utils'; import {checkCargoBinInstalled, checkIcWasmVersion, checkRustVersion} from '../../utils/env.utils'; import {formatBytes, formatTime} from '../../utils/format.utils'; +import {readPackageJson} from '../../utils/pkg.utils'; import {confirmAndExit} from '../../utils/prompt.utils'; const CARGO_RELEASE_DIR = join(process.cwd(), 'target', 'wasm32-unknown-unknown', 'release'); @@ -193,9 +194,7 @@ const api = async () => { const readCoreLib = async (): Promise<'core' | 'core-standalone'> => { try { - const packageJson = await readFile(join(process.cwd(), 'package.json'), 'utf-8'); - // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion - const {dependencies} = JSON.parse(packageJson) as {dependencies?: Record}; + const {dependencies} = await readPackageJson(); return Object.keys(dependencies ?? {}).includes('@junobuild/core-standalone') ? 'core-standalone' : 'core'; diff --git a/src/services/eject/eject.javascript.services.ts b/src/services/eject/eject.javascript.services.ts index a254eb13..96e50bb9 100644 --- a/src/services/eject/eject.javascript.services.ts +++ b/src/services/eject/eject.javascript.services.ts @@ -1,6 +1,6 @@ import {execute} from '@junobuild/cli-tools'; import {magenta} from 'kleur'; -import {mkdir, readFile} from 'node:fs/promises'; +import {mkdir} from 'node:fs/promises'; import {join} from 'node:path'; import { DEVELOPER_PROJECT_SATELLITE_PATH, @@ -10,6 +10,7 @@ import { TS_TEMPLATE_PATH } from '../../constants/dev.constants'; import {copyTemplateFile} from '../../utils/fs.utils'; +import {readPackageJson} from '../../utils/pkg.utils'; import {detectPackageManager} from '../../utils/pm.utils'; import {confirmAndExit} from '../../utils/prompt.utils'; @@ -77,9 +78,7 @@ const installFunctionsLib = async () => { const hasFunctionsLib = async (): Promise => { try { - const packageJson = await readFile(join(process.cwd(), 'package.json'), 'utf-8'); - // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion - const {dependencies} = JSON.parse(packageJson) as {dependencies?: Record}; + const {dependencies} = await readPackageJson(); return Object.keys(dependencies ?? {}).includes('@junobuild/functions'); } catch (_err: unknown) { // This should not block the developer therefore we fallback to asking for installing the library. diff --git a/src/utils/pkg.utils.ts b/src/utils/pkg.utils.ts new file mode 100644 index 00000000..0bb8686a --- /dev/null +++ b/src/utils/pkg.utils.ts @@ -0,0 +1,17 @@ +import {readFile} from 'node:fs/promises'; +import {join} from 'node:path'; + +export interface PackageJson { + dependencies?: Record; +} + +export const readPackageJson = async (): Promise => { + const packageJson = await readFile(join(process.cwd(), 'package.json'), 'utf-8'); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion + const {dependencies} = JSON.parse(packageJson) as {dependencies?: Record}; + + return { + dependencies + }; +};