108 lines
3.4 KiB
JavaScript
108 lines
3.4 KiB
JavaScript
import { spawnSync } from 'node:child_process'
|
|
import { existsSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import {
|
|
assertArtifactDirectoryReady,
|
|
collectSourceInputs,
|
|
createArtifactManifest,
|
|
gitStatusIsDirty,
|
|
parseArtifactOutDir,
|
|
resolveArtifactOutDir,
|
|
writeArtifactManifest,
|
|
} from './artifact-manifest.mjs'
|
|
|
|
const scriptPath = fileURLToPath(import.meta.url)
|
|
const frontendRoot = path.resolve(path.dirname(scriptPath), '..')
|
|
// The frontend is an independent Git repository after the workspace split.
|
|
const repoRoot = frontendRoot
|
|
|
|
function commandOutput(command, args, cwd) {
|
|
const result = spawnSync(command, args, {
|
|
cwd,
|
|
encoding: 'utf8',
|
|
windowsHide: true,
|
|
})
|
|
if (result.status !== 0) {
|
|
const detail = result.stderr?.trim() || result.error?.message || `${command} exited with status ${result.status}`
|
|
throw new Error(detail)
|
|
}
|
|
return result.stdout.trim()
|
|
}
|
|
|
|
function resolveSourceRevision() {
|
|
const configuredRevision = process.env.RELEASE_SOURCE_REVISION?.trim()
|
|
return configuredRevision || commandOutput('git', ['rev-parse', 'HEAD'], repoRoot)
|
|
}
|
|
|
|
function resolveNpmVersion() {
|
|
const userAgentMatch = process.env.npm_config_user_agent?.match(/(?:^|\s)npm\/([^\s]+)/)
|
|
if (userAgentMatch) {
|
|
return userAgentMatch[1]
|
|
}
|
|
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'
|
|
return commandOutput(npmCommand, ['--version'], frontendRoot)
|
|
}
|
|
|
|
function runViteBuild(outDir) {
|
|
const viteEntry = path.join(frontendRoot, 'node_modules', 'vite', 'bin', 'vite.js')
|
|
if (!existsSync(viteEntry)) {
|
|
throw new Error('Vite is not installed; run npm ci before build:artifact')
|
|
}
|
|
|
|
const result = spawnSync(
|
|
process.execPath,
|
|
[viteEntry, 'build', '--outDir', outDir],
|
|
{
|
|
cwd: frontendRoot,
|
|
env: { ...process.env, NODE_ENV: 'production' },
|
|
stdio: 'inherit',
|
|
windowsHide: true,
|
|
},
|
|
)
|
|
if (result.status !== 0) {
|
|
throw new Error(result.error?.message || `Vite build exited with status ${result.status}`)
|
|
}
|
|
}
|
|
|
|
export function buildArtifact(argv = process.argv.slice(2)) {
|
|
const rawOutDir = parseArtifactOutDir(argv)
|
|
const outDir = resolveArtifactOutDir(rawOutDir, {
|
|
cwd: process.cwd(),
|
|
frontendRoot,
|
|
repoRoot,
|
|
ciArtifactRoot: process.env.CI_ARTIFACT_DIR,
|
|
})
|
|
assertArtifactDirectoryReady(outDir)
|
|
|
|
const sourceRevision = resolveSourceRevision()
|
|
const sourceDirty = gitStatusIsDirty(commandOutput('git', ['status', '--porcelain=v1', '--untracked-files=all'], repoRoot))
|
|
const sourceFiles = collectSourceInputs(frontendRoot)
|
|
const npmVersion = resolveNpmVersion()
|
|
runViteBuild(outDir)
|
|
|
|
const manifest = createArtifactManifest({
|
|
outDir,
|
|
packageLockPath: path.join(frontendRoot, 'package-lock.json'),
|
|
sourceRevision,
|
|
buildUtc: new Date().toISOString(),
|
|
nodeVersion: process.version,
|
|
npmVersion,
|
|
sourceDirty,
|
|
sourceFiles,
|
|
})
|
|
const manifestPath = writeArtifactManifest(outDir, manifest)
|
|
process.stdout.write(`[build:artifact] artifact: ${outDir}\n`)
|
|
process.stdout.write(`[build:artifact] manifest: ${manifestPath}\n`)
|
|
}
|
|
|
|
if (process.argv[1] && path.resolve(process.argv[1]) === scriptPath) {
|
|
try {
|
|
buildArtifact()
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error)
|
|
process.stderr.write(`[build:artifact] ${message}\n`)
|
|
process.exitCode = 1
|
|
}
|
|
}
|