interlinked/source/driftingnebula.ts

71 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-03-07 15:33:44 +00:00
import fsp from 'node:fs/promises';
import path from 'node:path';
import {performance} from 'node:perf_hooks';
2022-03-07 21:42:15 +00:00
import process from 'node:process';
2022-03-07 15:33:44 +00:00
import {execa} from 'execa';
import {Crop} from './gegl/exports.js';
2022-03-07 16:33:04 +00:00
import Project from './project.js';
2022-03-07 15:33:44 +00:00
2022-03-07 16:37:45 +00:00
import d2022_03_06 from './2022-03-06.js';
2022-03-07 18:49:54 +00:00
import d2022_03_07 from './2022-03-07.js';
2022-03-07 16:37:45 +00:00
2022-03-07 15:33:44 +00:00
async function main(): Promise<void> {
2022-03-07 21:42:15 +00:00
const noRender = process.argv.includes('--no-render');
2022-03-07 21:46:09 +00:00
const includeDefaults = process.argv.includes('--include-defaults');
2022-03-07 18:49:54 +00:00
const projects: Project[] = [d2022_03_06, d2022_03_07];
2022-03-07 15:33:44 +00:00
for (const {name, operations, resolution} of projects) {
const dataStart = performance.now();
const {width, height} = resolution;
const baseDir = path.resolve(`./output/${name}`);
await fsp.mkdir(baseDir, {recursive: true});
console.log(`# ${name}`);
console.log(`* ${width}x${height}`);
console.log(`* ${operations.length} operations`);
const graph = operations.flatMap((operation) => {
2022-03-07 21:46:09 +00:00
const graph = operation.graph(includeDefaults);
2022-03-07 15:33:44 +00:00
if (operation.appendCrop) {
2022-03-07 21:46:09 +00:00
graph.push(...new Crop({height, width}).graph(includeDefaults));
2022-03-07 15:33:44 +00:00
}
return graph;
});
const prettyGraph = graph.map((operation) =>
operation.startsWith('gegl:') ? `\n${operation}\n` : ` ${operation}\n`,
);
const graphFile = `${name}.txt`;
const outputFile = `${name}.png`;
console.log(`* Writing ${graphFile}`);
await fsp.writeFile(
path.join(baseDir, graphFile),
prettyGraph.join('').trimStart(),
);
2022-03-07 21:42:15 +00:00
if (noRender) {
console.log(`* Skipped ${outputFile}`);
} else {
console.log(`* Writing ${outputFile}`);
await execa('gegl', [
'-o',
path.join(baseDir, outputFile),
'--',
...graph,
]);
}
2022-03-07 15:33:44 +00:00
const time = (performance.now() - dataStart).toFixed(2);
console.log(`* Generated in ${time}ms`);
}
}
void main();