-
Notifications
You must be signed in to change notification settings - Fork 12.6k
/
ior.ts
125 lines (116 loc) · 4.6 KB
/
ior.ts
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/// <reference types="node"/>
import fs = require('fs');
import path = require('path');
interface IOLog {
filesRead: {
path: string;
result: { contents: string; };
}[];
arguments: string[];
}
module Commands {
export function dir(obj: IOLog) {
obj.filesRead.filter(f => f.result !== undefined).forEach(f => {
console.log(f.path);
});
}
dir['description'] = ': displays a list of files';
export function find(obj: IOLog, str: string) {
obj.filesRead.filter(f => f.result !== undefined).forEach(f => {
var lines = f.result.contents.split('\n');
var printedHeader = false;
lines.forEach(line => {
if (line.indexOf(str) >= 0) {
if (!printedHeader) {
console.log(' === ' + f.path + ' ===');
printedHeader = true;
}
console.log(line);
}
});
});
}
find['description'] = ' string: finds text in files';
export function grab(obj: IOLog, filename: string) {
obj.filesRead.filter(f => f.result !== undefined).forEach(f => {
if (path.basename(f.path) === filename) {
fs.writeFile(filename, f.result.contents);
}
});
}
grab['description'] = ' filename.ts: writes out the specified file to disk';
export function extract(obj: IOLog, outputFolder: string) {
var directorySeparator = "/";
function directoryExists(path: string): boolean {
return fs.existsSync(path) && fs.statSync(path).isDirectory();
}
function getDirectoryPath(path: string) {
return path.substr(0, Math.max(getRootLength(path), path.lastIndexOf(directorySeparator)));
}
function getRootLength(path: string): number {
if (path.charAt(0) === directorySeparator) {
if (path.charAt(1) !== directorySeparator) return 1;
var p1 = path.indexOf(directorySeparator, 2);
if (p1 < 0) return 2;
var p2 = path.indexOf(directorySeparator, p1 + 1);
if (p2 < 0) return p1 + 1;
return p2 + 1;
}
if (path.charAt(1) === ":") {
if (path.charAt(2) === directorySeparator) return 3;
}
return 0;
}
function ensureDirectoriesExist(directoryPath: string) {
if (directoryPath.length > getRootLength(directoryPath) && !directoryExists(directoryPath)) {
var parentDirectory = getDirectoryPath(directoryPath);
ensureDirectoriesExist(parentDirectory);
console.log("creating directory: " + directoryPath);
fs.mkdirSync(directoryPath);
}
}
function normalizeSlashes(path: string): string {
return path.replace(/\\/g, "/");
}
function transalatePath(outputFolder:string, path: string): string {
return normalizeSlashes(outputFolder + directorySeparator + path.replace(":", ""));
}
function fileExists(path: string): boolean {
return fs.existsSync(path);
}
obj.filesRead.forEach(f => {
var filename = transalatePath(outputFolder, f.path);
ensureDirectoriesExist(getDirectoryPath(filename));
console.log("writing filename: " + filename);
fs.writeFileSync(filename, f.result.contents);
});
console.log("Command: tsc ");
obj.arguments.forEach(a => {
if (getRootLength(a) > 0) {
console.log(transalatePath(outputFolder, a));
}
else {
console.log(a);
}
console.log(" ");
});
}
extract['description'] = ' outputFolder: extract all input files to <outputFolder>';
}
var args = process.argv.slice(2);
if (args.length < 2) {
console.log('Usage: node ior.js path_to_file.json [command]');
console.log('List of commands: ');
Object.keys(Commands).forEach(k => console.log(' ' + k + Commands[k]['description']));
} else {
var cmd: Function = Commands[args[1]];
if (cmd === undefined) {
console.log('Unknown command ' + args[1]);
} else {
fs.readFile(args[0], 'utf-8', (err, data) => {
if (err) throw err;
var json = JSON.parse(data);
cmd.apply(undefined, [json].concat(args.slice(2)));
});
}
}