Files

28 lines
1.5 KiB
JavaScript

import {spawnSync} from 'node:child_process';
import {appendFileSync, existsSync, mkdirSync, readFileSync} from 'node:fs';
import {resolve} from 'node:path';
const source = resolve('fluxer-src');
function git(...args) {
const result = spawnSync('git', args, {encoding: 'utf8', stdio: ['ignore', 'pipe', 'inherit']});
if (result.error) throw result.error;
if (result.status !== 0) throw new Error(`git ${args[0]} failed (${result.status})`);
if (result.stdout) process.stdout.write(result.stdout);
return result.stdout.trim();
}
// checkout cleans the job workspace; never reuse a source tree from a previous run.
if (existsSync(source)) throw new Error(`Source directory already exists: ${source}`);
mkdirSync(source);
git('-C', source, 'init');
git('-C', source, 'config', 'core.autocrlf', 'false');
git('-C', source, 'remote', 'add', 'origin', 'https://github.com/fluxerapp/fluxer.git');
const ref = process.env.UPSTREAM_SHA || 'main';
if (ref !== 'main' && !/^[0-9a-f]{40}$/.test(ref)) throw new Error('UPSTREAM_SHA must be a full commit SHA');
git('-C', source, 'fetch', '--depth=1', 'origin', ref);
git('-C', source, 'checkout', '--detach', 'FETCH_HEAD');
const sha = git('-C', source, 'rev-parse', 'HEAD');
const packageManager = JSON.parse(readFileSync(resolve(source, 'package.json'), 'utf8')).packageManager;
console.log(`Fluxer revision: ${sha}; package manager: ${packageManager}`);
if (process.env.GITHUB_OUTPUT) appendFileSync(process.env.GITHUB_OUTPUT, `sha=${sha}\n`);
if (process.env.GITHUB_ENV) appendFileSync(process.env.GITHUB_ENV, `WORKDIR=${source}\n`);