flecks/packages/core/build/testing.js

47 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2024-02-09 23:59:11 -06:00
import {randomBytes} from 'crypto';
import {mkdir} from 'fs/promises';
import {tmpdir} from 'os';
2024-02-11 21:03:06 -06:00
import {basename, join} from 'path';
2024-02-09 23:59:11 -06:00
import {rimraf} from 'rimraf';
export function id() {
return new Promise((resolve, reject) => {
randomBytes(16, (error, bytes) => (error ? reject(error) : resolve(bytes.toString('hex'))));
});
}
export async function createWorkspace() {
2024-02-11 21:03:06 -06:00
let workspace = join(tmpdir(), '@flecks', 'core', 'testing', await id());
try {
throw new Error();
}
catch (error) {
workspace += `-${basename(
error.stack
.split('\n').slice(-1)[0]
.split('at ')[1]
.match(/\((.*)\)$/)[1]
.split(':').slice(-3, -2)[0],
)}`;
}
2024-02-09 23:59:11 -06:00
await mkdir(workspace, {recursive: true});
2024-02-11 21:03:06 -06:00
process.on('exit', () => {
rimraf.sync(workspace);
});
2024-02-09 23:59:11 -06:00
// sheeeeesh
process.prependListener('message', async (message) => {
if ('__workerpool-terminate__' === message) {
rimraf.sync(workspace);
}
});
return workspace;
}
2024-02-10 14:17:12 -06:00
export function heavySetup(fn) {
return function heavySetup() {
this.timeout(0);
return fn();
};
}