1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import * as fs from 'fs';
import { ExecOptions } from 'shelljs';
import { exec } from 'child_process';
import * as path from 'path';
export const command_line = (command: string, fromDirectory?: string) => {
return new Promise<string>((resolve, reject) => {
let options: ExecOptions = {};
if (fromDirectory) {
options.cwd = path.join(__dirname, fromDirectory);
}
exec(command, options, (err, stdout) => err ? reject(err) : resolve(stdout));
});
};
export const read_text_file = (relativePath: string) => {
let target = path.join(__dirname, relativePath);
return new Promise<string>((resolve, reject) => {
fs.readFile(target, (err, data) => err ? reject(err) : resolve(data.toString()));
});
};
export const write_text_file = (relativePath: string, contents: any) => {
let target = path.join(__dirname, relativePath);
return new Promise<void>((resolve, reject) => {
fs.writeFile(target, contents, (err) => err ? reject(err) : resolve());
});
};
|