fix(source): wait for output write to finish
This commit is contained in:
parent
ab40330f61
commit
72bedc87a9
|
@ -1,23 +1,37 @@
|
||||||
import { promisify } from 'util';
|
|
||||||
import { readFile, writeFile } from 'fs';
|
import { readFile, writeFile } from 'fs';
|
||||||
|
import { isNil } from 'lodash';
|
||||||
|
import { promisify } from 'util';
|
||||||
|
|
||||||
|
export const FILE_ENCODING = 'utf-8';
|
||||||
export const readFileSync = promisify(readFile);
|
export const readFileSync = promisify(readFile);
|
||||||
export const writeFileSync = promisify(writeFile);
|
export const writeFileSync = promisify(writeFile);
|
||||||
|
|
||||||
export async function loadSource(path: string): Promise<string> {
|
export async function loadSource(path: string): Promise<string> {
|
||||||
if (path === '-') {
|
if (path === '-') {
|
||||||
return readFileSync(0, 'utf-8');
|
return readFileSync(0, {
|
||||||
|
encoding: FILE_ENCODING,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
return readFileSync(path, 'utf-8');
|
return readFileSync(path, {
|
||||||
|
encoding: FILE_ENCODING,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writeSource(path: string, data: string): Promise<void> {
|
export async function writeSource(path: string, data: string): Promise<void> {
|
||||||
if (path === '-') {
|
if (path === '-') {
|
||||||
process.stdout.write(data);
|
return new Promise((res, rej) => {
|
||||||
|
process.stdout.write(data, (err: Error | null | undefined) => {
|
||||||
|
if (isNil(err)) {
|
||||||
|
res();
|
||||||
|
} else {
|
||||||
|
rej(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
return writeFileSync(path, data, {
|
return writeFileSync(path, data, {
|
||||||
encoding: 'utf-8',
|
encoding: FILE_ENCODING,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue