og-image/file.ts

19 lines
595 B
TypeScript
Raw Normal View History

2019-01-21 12:31:16 -08:00
import { writeFile } from 'fs';
import { join } from 'path';
import { promisify } from 'util';
import { tmpdir } from 'os';
import { URL } from 'url';
2019-01-01 16:19:25 -08:00
const writeFileAsync = promisify(writeFile);
2019-01-21 12:31:16 -08:00
export async function writeTempFile(name: string, contents: string) {
2019-01-01 16:19:25 -08:00
const randomPath = join(tmpdir(), `${name}.html`);
console.log('Writing file to ' + randomPath);
await writeFileAsync(randomPath, contents);
return randomPath;
}
2019-01-21 12:31:16 -08:00
export function pathToFileURL(path: string) {
2019-01-01 16:19:25 -08:00
const { href } = new URL(path, 'file:');
console.log('File url is ' + href);
return href;
2019-01-21 12:31:16 -08:00
}