-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild-env.ts
46 lines (35 loc) · 1.11 KB
/
build-env.ts
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { defineNuxtModule, useLogger } from '@nuxt/kit';
import colors from 'picocolors';
import { exec } from 'tinyexec';
import { version } from '../package.json';
import { createKey, KeyPrefix } from '../shared/utils/keys';
export default defineNuxtModule({
meta: {
name: 'build-env',
},
async setup(_options, nuxt) {
if (nuxt.options._prepare) {
return;
}
const logger = useLogger('build-env');
const commit = await getCommitSha();
const uniqueBuildId = createKey(KeyPrefix.Build);
nuxt.options.runtimeConfig.build = {
id: uniqueBuildId,
};
nuxt.options.runtimeConfig.public.build = {
time: Date.now(),
commit,
version: `v${version}`,
};
logger.info(`Unique build.id: ${colors.bold(colors.cyan(uniqueBuildId))}`);
},
});
async function getCommitSha(): Promise<string> {
const SOURCE_COMMIT = process.env.SOURCE_COMMIT;
if (typeof SOURCE_COMMIT === 'string' && SOURCE_COMMIT.length > 8) {
return SOURCE_COMMIT.substring(0, 8);
}
const { stdout } = await exec('git', ['rev-parse', '--short', 'HEAD']);
return stdout.trimEnd();
}