chore: don't fail the build when building from source tarball (#40)

This commit is contained in:
Jixun Wu 2023-09-16 15:11:11 +01:00
parent 0bd52157c4
commit f17c2f195c
3 changed files with 22 additions and 10 deletions

17
support/command.ts Normal file
View File

@ -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;
}
}

View File

@ -6,7 +6,5 @@
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": [
"vite.config.ts"
]
"include": ["vite.config.ts", "support/**/*.ts"]
}

View File

@ -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/