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

This commit is contained in:
鲁树人 2023-09-16 15:11:11 +01:00
parent e003eba10c
commit 1c3b43c419
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", "moduleResolution": "bundler",
"allowSyntheticDefaultImports": true "allowSyntheticDefaultImports": true
}, },
"include": [ "include": ["vite.config.ts", "support/**/*.ts"]
"vite.config.ts"
]
} }

View File

@ -1,4 +1,3 @@
import cp from 'node:child_process';
import url from 'node:url'; import url from 'node:url';
import path from 'node:path'; import path from 'node:path';
import fs from 'node:fs'; import fs from 'node:fs';
@ -10,15 +9,13 @@ import replace from '@rollup/plugin-replace';
import topLevelAwait from 'vite-plugin-top-level-await'; import topLevelAwait from 'vite-plugin-top-level-await';
import { VitePWA } from 'vite-plugin-pwa'; import { VitePWA } from 'vite-plugin-pwa';
const gitRoot = url.fileURLToPath(new URL('.', import.meta.url)); import { tryCommand } from './support/command';
const pkg = JSON.parse(fs.readFileSync(gitRoot + '/package.json', 'utf-8'));
function command(cmd, dir = '') { const projectRoot = url.fileURLToPath(new URL('.', import.meta.url));
return cp.execSync(cmd, { cwd: path.join(gitRoot, dir), encoding: 'utf-8' }).trim(); const pkg = JSON.parse(fs.readFileSync(projectRoot + '/package.json', 'utf-8'));
}
const COMMAND_GIT_VERSION = 'git describe --long --dirty --tags --always'; 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}`; const version = `${pkg.version}-${shortCommit}`;
// https://vitejs.dev/config/ // https://vitejs.dev/config/