UNPKG

2.19 kBPlain TextView Raw
1import * as ts from 'typescript';
2import * as fs from 'fs';
3import * as path from 'path';
4import * as util from 'util';
5
6import { logger } from './logger';
7
8export function d(node) {
9 console.log(util.inspect(node, { showHidden: true, depth: 10 }));
10}
11
12const carriageReturnLineFeed = '\r\n';
13const lineFeed = '\n';
14
15// get default new line break
16export function getNewLineCharacter(options: ts.CompilerOptions): string {
17 if (options.newLine === ts.NewLineKind.CarriageReturnLineFeed) {
18 return carriageReturnLineFeed;
19 }
20 else if (options.newLine === ts.NewLineKind.LineFeed) {
21 return lineFeed;
22 }
23 return carriageReturnLineFeed;
24}
25
26
27// Create a compilerHost object to allow the compiler to read and write files
28export function compilerHost(transpileOptions: any): ts.CompilerHost {
29
30 const inputFileName = transpileOptions.fileName || (transpileOptions.jsx ? 'module.tsx' : 'module.ts');
31
32 const compilerHost: ts.CompilerHost = {
33 getSourceFile: (fileName) => {
34 if (fileName.lastIndexOf('.ts') !== -1) {
35 if (fileName === 'lib.d.ts') {
36 return undefined;
37 }
38
39 if (path.isAbsolute(fileName) === false) {
40 fileName = path.join(transpileOptions.tsconfigDirectory, fileName);
41 }
42
43 let libSource = '';
44
45 try {
46 libSource = fs.readFileSync(fileName).toString();
47 }
48 catch(e) {
49 logger.trace(e, fileName);
50 }
51
52 return ts.createSourceFile(fileName, libSource, transpileOptions.target, false);
53 }
54 return undefined;
55 },
56 writeFile: (name, text) => {},
57 getDefaultLibFileName: () => 'lib.d.ts',
58 useCaseSensitiveFileNames: () => false,
59 getCanonicalFileName: fileName => fileName,
60 getCurrentDirectory: () => '',
61 getNewLine: () => '\n',
62 fileExists: (fileName): boolean => fileName === inputFileName,
63 readFile: () => '',
64 directoryExists: () => true,
65 getDirectories: () => []
66 };
67 return compilerHost;
68}
\No newline at end of file