From f17c2f195ca32e157823284587eb7cd238b9fe94 Mon Sep 17 00:00:00 2001 From: Jixun Wu Date: Sat, 16 Sep 2023 15:11:11 +0100 Subject: [PATCH] chore: don't fail the build when building from source tarball (#40) --- support/command.ts | 17 +++++++++++++++++ tsconfig.node.json | 4 +--- vite.config.ts | 11 ++++------- 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 support/command.ts diff --git a/support/command.ts b/support/command.ts new file mode 100644 index 0000000..3a5e8a1 --- /dev/null +++ b/support/command.ts @@ -0,0 +1,17 @@ +import cp from 'node:child_process'; +import path from 'node:path'; +import url from 'node:url'; + +const projectRoot = url.fileURLToPath(new URL('../', import.meta.url)); + +export function command(cmd: string, dir = '') { + return cp.execSync(cmd, { cwd: path.join(projectRoot, dir), encoding: 'utf-8' }).trim(); +} + +export function tryCommand(cmd: string, dir = '', fallback = '') { + try { + return command(cmd, dir); + } catch (e) { + return fallback; + } +} diff --git a/tsconfig.node.json b/tsconfig.node.json index b5a3431..427345e 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -6,7 +6,5 @@ "moduleResolution": "bundler", "allowSyntheticDefaultImports": true }, - "include": [ - "vite.config.ts" - ] + "include": ["vite.config.ts", "support/**/*.ts"] } diff --git a/vite.config.ts b/vite.config.ts index 5769c19..f64deca 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,3 @@ -import cp from 'node:child_process'; import url from 'node:url'; import path from 'node:path'; import fs from 'node:fs'; @@ -10,15 +9,13 @@ import replace from '@rollup/plugin-replace'; import topLevelAwait from 'vite-plugin-top-level-await'; import { VitePWA } from 'vite-plugin-pwa'; -const gitRoot = url.fileURLToPath(new URL('.', import.meta.url)); -const pkg = JSON.parse(fs.readFileSync(gitRoot + '/package.json', 'utf-8')); +import { tryCommand } from './support/command'; -function command(cmd, dir = '') { - return cp.execSync(cmd, { cwd: path.join(gitRoot, dir), encoding: 'utf-8' }).trim(); -} +const projectRoot = url.fileURLToPath(new URL('.', import.meta.url)); +const pkg = JSON.parse(fs.readFileSync(projectRoot + '/package.json', 'utf-8')); const COMMAND_GIT_VERSION = 'git describe --long --dirty --tags --always'; -const shortCommit = command(COMMAND_GIT_VERSION); +const shortCommit = tryCommand(COMMAND_GIT_VERSION, __dirname, 'unknown'); const version = `${pkg.version}-${shortCommit}`; // https://vitejs.dev/config/