-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.js
137 lines (82 loc) · 3.55 KB
/
task.js
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
126
127
128
129
130
131
132
133
134
135
136
137
let ts = require('typescript');
const fs = require('fs-extra');
const path = require('path');
const _ = require('lodash');
const glob = require('glob');
module.exports = (logger, dirname, config) => {
return () => {
return new Promise((resolve, reject) => {
// Load local TypeScript
if ( config.useLocal ) {
try {
ts = require(path.join(dirname, 'node_modules', 'typescript'));
if ( ! ts ) throw new Error();
logger('Using local TypeScript...');
}
catch (error) {
logger('Could not find local TypeScript! Using the shipped version instead...');
}
}
// Validate config
if ( (! config.outDir || ! config.rootDir) && ! config.tsconfig ) return reject(new Error('TypeScript plugin misconfiguration! Either outDir and rootDir or tsconfig must be present.'));
let finalOptions = _.cloneDeep(config);
let tsConfigExclude;
delete finalOptions.cleanOutDir;
// Load tsconfig.json if necessary
if ( config.tsconfig ) {
try {
let tsconfig = fs.readJsonSync(path.resolve(dirname, config.tsconfig));
finalOptions = tsconfig.compilerOptions;
tsConfigExclude = (tsconfig.exclude || []).map(filename => path.resolve(dirname, path.dirname(config.tsconfig), filename));
tsConfigExclude = tsConfigExclude.map(filename => {
try {
let stat = fs.lstatSync(filename);
return stat.isDirectory() ? path.join(filename, '**', '*.ts') : filename;
}
catch (error) {
return null;
}
})
.filter(filename => filename !== null);
}
catch (error) {
return reject(new Error(`Could not load "${path.resolve(dirname, config.tsconfig)}"!\n${error}`));
}
}
// Resolve ourDir and rootDir
finalOptions.outDir = path.join(path.dirname(config.tsconfig), finalOptions.outDir);
finalOptions.rootDir = path.join(path.dirname(config.tsconfig), finalOptions.rootDir);
// Empty outDir if necessary
if ( config.cleanOutDir ) {
fs.emptyDirSync(path.resolve(dirname, finalOptions.outDir));
}
// Sanitize lib
if ( finalOptions.lib && finalOptions.lib.map ) {
finalOptions.lib = finalOptions.lib.map(lib => `lib.${lib}.d.ts`);
}
// Search `config.rootDir` for `.ts` files
glob(path.resolve(dirname, finalOptions.rootDir, '**/*.ts'), {
ignore: tsConfigExclude
}, (error, files) => {
if ( error ) return reject(error);
logger(`Transpiling ${files.length} files...`);
// Create program
const program = ts.createProgram(files.map(file => path.resolve(dirname, finalOptions.rootDir, file)), finalOptions);
let emitResult = program.emit();
let diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
diagnostics.forEach(diagnostic => {
if ( diagnostic.file ) {
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
logger(`TypeScript: ${diagnostic.file.fileName} (${position.line + 1},${position.character + 1}): ${message}`);
}
else {
logger(`TypeScript: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`);
}
});
logger(`${files.length} files were transpiled.`);
resolve();
});
});
};
};