95 lines
4.7 KiB
JavaScript
95 lines
4.7 KiB
JavaScript
// Paths and browser flags for the kit. Everything resolves from this folder, so the skill runs wherever it is installed.
|
|
// Override any of them with an environment variable.
|
|
|
|
import {createRequire} from 'node:module';
|
|
import path from 'node:path';
|
|
import {fileURLToPath} from 'node:url';
|
|
import {spawnSync} from 'node:child_process';
|
|
import {existsSync, readdirSync} from 'node:fs';
|
|
|
|
const ROOT = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
// Resolve a tool once: $FFMPEG etc, then PATH (where.exe), then winget's Links dir,
|
|
// then common install targets, then <skill>/bin (what `node film.mjs --setup` fills in).
|
|
const findTool = (name, extra = []) => {
|
|
if (process.env[name]) return process.env[name];
|
|
const bin = name.toLowerCase();
|
|
try {
|
|
const w = spawnSync('where.exe', [bin], {encoding: 'utf8'});
|
|
if (w.status === 0) {
|
|
const hit = w.stdout.split(/\r?\n/).map((s) => s.trim()).filter(Boolean).find((p) => existsSync(p));
|
|
if (hit) return hit;
|
|
}
|
|
} catch {}
|
|
const la = process.env.LOCALAPPDATA;
|
|
const exe = process.platform === 'win32' ? bin + '.exe' : bin;
|
|
const cands = [
|
|
la && path.join(la, 'Microsoft', 'WinGet', 'Links', exe),
|
|
...extra.map((d) => path.join(d, exe)),
|
|
path.join(ROOT, 'bin', exe),
|
|
].filter(Boolean);
|
|
for (const c of cands) if (existsSync(c)) return c;
|
|
return bin;
|
|
};
|
|
|
|
export const FFMPEG = findTool('FFMPEG', ['C:\\ffmpeg\\bin']);
|
|
export const FFPROBE = findTool('FFPROBE', ['C:\\ffmpeg\\bin']);
|
|
export const PYTHON = process.env.PYTHON || (process.platform === 'win32' ? 'python' : 'python3');
|
|
export const SKILL_DIR = ROOT;
|
|
|
|
// three, esbuild, fonts and playwright: `npm install` in this folder puts them here.
|
|
export const DEPS = process.env.CUTAWAY_DEPS || path.join(ROOT, 'node_modules');
|
|
// Browser: $CHROMIUM, else playwright's downloaded Chromium if installed, else the system Edge/Chrome.
|
|
// Both run the same WebGL (the capture verified ANGLE/D3D11 on the discrete GPU through msedge), so a stalled
|
|
// `npx playwright install chromium` never blocks a render. Set CHROMIUM= to force playwright's bundled default.
|
|
const findBrowser = () => {
|
|
if (process.env.CHROMIUM !== undefined) return process.env.CHROMIUM || undefined;
|
|
const dir =
|
|
process.platform === 'win32'
|
|
? path.join(process.env.LOCALAPPDATA || '', 'ms-playwright')
|
|
: process.platform === 'darwin'
|
|
? path.join(process.env.HOME || '', 'Library/Caches/ms-playwright')
|
|
: path.join(process.env.HOME || '', '.cache/ms-playwright');
|
|
try {
|
|
for (const d of readdirSync(dir).sort().reverse()) {
|
|
if (!/^chromium-/.test(d)) continue;
|
|
for (const sub of process.platform === 'win32'
|
|
? ['chrome-win/chrome.exe', 'chrome-win64/chrome.exe']
|
|
: process.platform === 'darwin'
|
|
? ['Chromium.app/Contents/MacOS/Chromium', 'chrome-mac/Chromium.app/Contents/MacOS/Chromium']
|
|
: ['chrome-linux/chrome', 'chrome-linux64/chrome']) {
|
|
const p = path.join(dir, d, sub);
|
|
if (existsSync(p)) return p;
|
|
}
|
|
}
|
|
} catch {}
|
|
const pf = process.env['PROGRAMFILES(X86)'] || 'C:\\Program Files (x86)';
|
|
const pf64 = process.env['PROGRAMFILES'] || 'C:\\Program Files';
|
|
for (const p of [
|
|
process.platform === 'win32' && path.join(pf, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
|
|
process.platform === 'win32' && path.join(pf64, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
|
|
path.join(pf64, 'Google', 'Chrome', 'Application', 'chrome.exe'),
|
|
path.join(pf, 'Google', 'Chrome', 'Application', 'chrome.exe'),
|
|
process.platform === 'darwin' && '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
process.platform === 'darwin' && '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
process.platform === 'linux' && '/usr/bin/google-chrome',
|
|
process.platform === 'linux' && '/usr/bin/microsoft-edge',
|
|
process.platform === 'linux' && '/usr/bin/chromium',
|
|
]) if (p && existsSync(p)) return p;
|
|
return undefined;
|
|
};
|
|
export const CHROMIUM = findBrowser();
|
|
// Real GPU WebGL instead of a software rasteriser. On Windows d3d11 plus --force_high_performance_gpu keeps ANGLE off
|
|
// an integrated GPU when a discrete one exists. capture.mjs prints the GPU it got on every run.
|
|
export const CHROMIUM_ARGS = [
|
|
...(process.platform === 'win32' ? ['--use-angle=d3d11'] : []),
|
|
'--force_high_performance_gpu', '--ignore-gpu-blocklist', '--enable-gpu-rasterization', '--enable-webgl', '--hide-scrollbars',
|
|
];
|
|
|
|
// Builds and renders land in ./cutaway-out of the folder you run from.
|
|
export const OUT = path.resolve(process.env.CUTAWAY_OUT || 'cutaway-out');
|
|
export const DIST = path.join(OUT, 'dist');
|
|
|
|
export const fromDeps = (id) => createRequire(path.join(DEPS, 'x.js'))(id);
|
|
export const playwright = () => fromDeps('playwright');
|