2019-11-02 12:35:20 +00:00
|
|
|
import { readFile as readBack, writeFile as writeBack } from 'fs';
|
2019-06-16 21:35:43 +00:00
|
|
|
import { isNil } from 'lodash';
|
|
|
|
import { promisify } from 'util';
|
2019-06-15 22:38:05 +00:00
|
|
|
|
2019-06-16 21:35:43 +00:00
|
|
|
export const FILE_ENCODING = 'utf-8';
|
2019-11-02 12:35:20 +00:00
|
|
|
export const readFile = promisify(readBack);
|
|
|
|
export const writeFile = promisify(writeBack);
|
2019-06-15 22:38:05 +00:00
|
|
|
|
|
|
|
export async function loadSource(path: string): Promise<string> {
|
|
|
|
if (path === '-') {
|
2019-11-02 12:35:20 +00:00
|
|
|
return readFile(0, {
|
2019-06-16 21:35:43 +00:00
|
|
|
encoding: FILE_ENCODING,
|
|
|
|
});
|
2019-06-15 22:38:05 +00:00
|
|
|
} else {
|
2019-11-02 12:35:20 +00:00
|
|
|
return readFile(path, {
|
2019-06-16 21:35:43 +00:00
|
|
|
encoding: FILE_ENCODING,
|
|
|
|
});
|
2019-06-15 22:38:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-16 01:21:11 +00:00
|
|
|
|
|
|
|
export async function writeSource(path: string, data: string): Promise<void> {
|
|
|
|
if (path === '-') {
|
2019-06-16 21:35:43 +00:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
process.stdout.write(data, (err: Error | null | undefined) => {
|
|
|
|
if (isNil(err)) {
|
|
|
|
res();
|
|
|
|
} else {
|
|
|
|
rej(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-06-16 01:21:11 +00:00
|
|
|
} else {
|
2019-11-02 12:35:20 +00:00
|
|
|
return writeFile(path, data, {
|
2019-06-16 21:35:43 +00:00
|
|
|
encoding: FILE_ENCODING,
|
2019-06-16 01:21:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|