File: perfsys.ts

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (104 lines) | stat: -rw-r--r-- 2,938 bytes parent folder | download | duplicates (3)
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
/// <reference path="..\src\compiler\sys.ts"/>
/// <reference path="..\src\compiler\types.ts"/>

namespace perftest {
    interface IOLog {
        resolvePath: ts.Map<string>;
        fileNames: string[];
    }

    export interface IO {
        getOut(): string;
    }

    export const readFile = ts.sys.readFile;
    const writeFile = ts.sys.writeFile;
    export const write = ts.sys.write;
    const resolvePath = ts.sys.resolvePath;
    export const getExecutingFilePath = ts.sys.getExecutingFilePath;
    export const getCurrentDirectory = ts.sys.getCurrentDirectory;
    // const exit = ts.sys.exit;

    const args = ts.sys.args;

    // augment sys so first ts.executeCommandLine call will be finish silently
    ts.sys.write = (s: string) => { };
    ts.sys.exit = (code: number) => { };
    ts.sys.args = [];

    export function restoreSys() {
        ts.sys.args = args;
        ts.sys.write = write;
    }

    export function hasLogIOFlag() {
        return args.length > 2 && args[0] === "--logio";
    }

    export function getArgsWithoutLogIOFlag() {
        return args.slice(2);
    }

    export function getArgsWithoutIOLogFile() {
        return args.slice(1);
    }

    const resolvePathLog: ts.Map<string> = {};

    export function interceptIO() {
        ts.sys.resolvePath = (s) => {
            const result = resolvePath(s);
            resolvePathLog[s] = result;
            return result;
        };
    }

    export function writeIOLog(fileNames: string[]) {
        const path = args[1];
        const log: IOLog = {
            fileNames: fileNames,
            resolvePath: resolvePathLog
        };

        writeFile(path, JSON.stringify(log));
    }

    export function prepare(): IO {
        const log = <IOLog>JSON.parse(readFile(args[0]));

        const files: ts.Map<string> = {};
        log.fileNames.forEach(f => { files[f] = readFile(f); });

        ts.sys.createDirectory = (s: string) => { };
        ts.sys.directoryExists = (s: string) => true;
        ts.sys.fileExists = (s: string) => true;

        const currentDirectory = ts.sys.getCurrentDirectory();
        ts.sys.getCurrentDirectory = () => currentDirectory;

        const executingFilePath = ts.sys.getExecutingFilePath();
        ts.sys.getExecutingFilePath = () => executingFilePath;

        ts.sys.readFile = (s: string) => {
            return files[s];
        };

        ts.sys.resolvePath = (s: string) => {
            const path = log.resolvePath[s];
            if (!path) {
                throw new Error("Unexpected path '" + s + "'");
            }
            return path;
        };

        ts.sys.writeFile = (path: string, data: string) => { };

        let out = "";

        ts.sys.write = (s: string) => { out += s; };

        return {
            getOut: () => out,
        };
    }
}