-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathtsconfig.ts
843 lines (744 loc) · 30.6 KB
/
tsconfig.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
import * as fsu from "../utils/fsUtil";
import simpleValidator = require('./simpleValidator');
var types = simpleValidator.types;
// Most compiler options come from require('typescript').CompilerOptions, but
// 'module' and 'target' cannot use the same enum as that interface since we
// do not want to force users to put magic numbers in their tsconfig files
// Possible: Use require('typescript').parseConfigFile in TS1.5
// NOTE: see the changes in `commandLineParser.ts` in the TypeScript sources to see what needs updating
/**
* When adding you need to
* 0 Add in this interface
* 1 Add to the validation
* 2 If its an enum : Update `typescriptEnumMap`
* 3 If its a path : Update the `make relative` code
*/
interface CompilerOptions {
allowNonTsExtensions?: boolean;
allowUnreachableCode?: boolean;
allowUnusedLabels?: boolean;
charset?: string;
codepage?: number;
declaration?: boolean;
diagnostics?: boolean;
emitBOM?: boolean;
experimentalAsyncFunctions?: boolean;
experimentalDecorators?: boolean; // Experimental. Needed for the next option `emitDecoratorMetadata` see : https://github.com/Microsoft/TypeScript/pull/3330
emitDecoratorMetadata?: boolean; // Experimental. Emits addition type information for this reflection API https://github.com/rbuckton/ReflectDecorators
help?: boolean;
isolatedModules?: boolean;
inlineSourceMap?: boolean;
inlineSources?: boolean;
jsx?: string;
locale?: string;
listFiles?: boolean;
mapRoot?: string; // Optionally Specifies the location where debugger should locate map files after deployment
module?: string;
moduleResolution?: string;
newLine?: string;
noEmit?: boolean;
noEmitHelpers?: boolean;
noEmitOnError?: boolean;
noErrorTruncation?: boolean;
noFallthroughCasesInSwitch?: boolean;
noImplicitAny?: boolean; // Error on inferred `any` type
noImplicitReturns?: boolean;
noLib?: boolean;
noLibCheck?: boolean;
noResolve?: boolean;
out?: string;
outFile?: string; // new name for out
outDir?: string; // Redirect output structure to this directory
preserveConstEnums?: boolean;
removeComments?: boolean; // Do not emit comments in output
rootDir?: string;
sourceMap?: boolean; // Generates SourceMaps (.map files)
sourceRoot?: string; // Optionally specifies the location where debugger should locate TypeScript source files after deployment
stripInternal?: boolean;
suppressExcessPropertyErrors?: boolean; // Optionally disable strict object literal assignment checking
suppressImplicitAnyIndexErrors?: boolean;
target?: string; // 'es3'|'es5' (default)|'es6'
version?: boolean;
watch?: boolean;
}
var compilerOptionsValidation: simpleValidator.ValidationInfo = {
allowNonTsExtensions: { type: types.boolean },
allowUnreachableCode: { type: types.boolean },
allowUnusedLabels: { type: types.boolean },
charset: { type: types.string },
codepage: { type: types.number },
declaration: { type: types.boolean },
diagnostics: { type: types.boolean },
emitBOM: { type: types.boolean },
experimentalAsyncFunctions: { type: types.boolean },
experimentalDecorators: { type: types.boolean },
emitDecoratorMetadata: { type: types.boolean },
help: { type: types.boolean },
inlineSourceMap: { type: types.boolean },
inlineSources: { type: types.boolean },
isolatedModules: { type: types.boolean },
jsx: { type: types.string, validValues: ['preserve', 'react'] },
locals: { type: types.string },
listFiles: { type: types.boolean },
mapRoot: { type: types.string },
module: { type: types.string, validValues: ['commonjs', 'amd', 'system', 'umd', 'es6', 'es2015'] },
moduleResolution: { type: types.string, validValues: ['classic', 'node'] },
newLine: { type: types.string },
noEmit: { type: types.boolean },
noEmitHelpers: { type: types.boolean },
noEmitOnError: { type: types.boolean },
noErrorTruncation: { type: types.boolean },
noFallthroughCasesInSwitch: { type: types.boolean },
noImplicitAny: { type: types.boolean },
noImplicitReturns: { type: types.boolean },
noLib: { type: types.boolean },
noLibCheck: { type: types.boolean },
noResolve: { type: types.boolean },
out: { type: types.string },
outFile: { type: types.string },
outDir: { type: types.string },
preserveConstEnums: { type: types.boolean },
removeComments: { type: types.boolean },
rootDir: { type: types.string },
sourceMap: { type: types.boolean },
sourceRoot: { type: types.string },
stripInternal: { type: types.boolean },
suppressExcessPropertyErrors: { type: types.boolean },
suppressImplicitAnyIndexErrors: { type: types.boolean },
target: { type: types.string, validValues: ['es3', 'es5', 'es6'] },
version: { type: types.boolean },
watch: { type: types.boolean },
}
var validator = new simpleValidator.SimpleValidator(compilerOptionsValidation);
interface UsefulFromPackageJson {
/** We need this as this is the name the user is going to import **/
name: string;
/** we need this to figure out the basePath (will depend on how `outDir` is relative to this directory) */
directory: string;
/** This is going to be typescript.definition */
definition: string;
main: string;
}
/**
* This is the JSON.parse result of a tsconfig.json
*/
interface TypeScriptProjectRawSpecification {
compilerOptions?: CompilerOptions;
exclude?: string[]; // optional: An array of 'glob / minimatch / RegExp' patterns to specify directories / files to exclude
files?: string[]; // optional: paths to files
filesGlob?: string[]; // optional: An array of 'glob / minimatch / RegExp' patterns to specify source files
formatCodeOptions?: formatting.FormatCodeOptions; // optional: formatting options
compileOnSave?: boolean; // optional: compile on save. Ignored to build tools. Used by IDEs
buildOnSave?: boolean;
externalTranspiler?: string | { name: string; options?: any };
scripts?: { postbuild?: string };
atom?: { rewriteTsconfig?: boolean };
}
/**
* This is `TypeScriptProjectRawSpecification` parsed further
* Designed for use throughout out code base
*/
export interface TypeScriptProjectSpecification {
compilerOptions: ts.CompilerOptions;
files: string[];
typings: string[]; // These are considered externs for .d.ts. Note : duplicated in files
filesGlob?: string[];
formatCodeOptions: ts.FormatCodeOptions;
compileOnSave: boolean;
buildOnSave: boolean;
package?: UsefulFromPackageJson;
externalTranspiler?: string | { name: string; options?: any };
scripts: { postbuild?: string };
atom: { rewriteTsconfig: boolean };
}
///////// FOR USE WITH THE API /////////////
export interface TypeScriptProjectFileDetails {
/** The path to the project file. This acts as the baseDIR */
projectFileDirectory: string;
/** The actual path of the project file (including tsconfig.json) */
projectFilePath: string;
project: TypeScriptProjectSpecification;
inMemory: boolean;
}
//////////////////////////////////////////////////////////////////////
export var errors = {
GET_PROJECT_INVALID_PATH: 'The path used to query for tsconfig.json does not exist',
GET_PROJECT_NO_PROJECT_FOUND: 'No Project Found',
GET_PROJECT_FAILED_TO_OPEN_PROJECT_FILE: 'Failed to fs.readFileSync the project file',
GET_PROJECT_JSON_PARSE_FAILED: 'Failed to JSON.parse the project file',
GET_PROJECT_GLOB_EXPAND_FAILED: 'Failed to expand filesGlob in the project file',
GET_PROJECT_PROJECT_FILE_INVALID_OPTIONS: 'Project file contains invalid options',
CREATE_FILE_MUST_EXIST: 'The Typescript file must exist on disk in order to create a project',
CREATE_PROJECT_ALREADY_EXISTS: 'Project file already exists',
};
export interface GET_PROJECT_JSON_PARSE_FAILED_Details {
projectFilePath: string;
error: Error;
}
export interface GET_PROJECT_PROJECT_FILE_INVALID_OPTIONS_Details {
projectFilePath: string;
errorMessage: string;
}
export interface GET_PROJECT_GLOB_EXPAND_FAILED_Details {
glob: string[];
projectFilePath: string;
errorMessage: string;
}
export interface GET_PROJECT_NO_PROJECT_FOUND_Details {
projectFilePath: string;
errorMessage: string;
}
function errorWithDetails<T>(error: Error, details: T): Error {
error.details = details;
return error;
}
import fs = require('fs');
import path = require('path');
import tsconfig = require('tsconfig');
import os = require('os');
import detectIndent = require('detect-indent');
import extend = require('xtend');
import formatting = require('./formatting');
var projectFileName = 'tsconfig.json';
/**
* This is what we write to new files
*/
var defaultFilesGlob = [
"**/*.ts",
"**/*.tsx",
"!node_modules/**",
];
/**
* This is what we use when the user doens't specify a files / filesGlob
*/
var invisibleFilesGlob = '{**/*.ts,**/*.tsx}';
export var defaults: ts.CompilerOptions = {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
isolatedModules: false,
jsx: ts.JsxEmit.React,
experimentalDecorators: true,
emitDecoratorMetadata: true,
declaration: false,
noImplicitAny: false,
removeComments: true,
noLib: false,
preserveConstEnums: true,
suppressImplicitAnyIndexErrors: true
};
var typescriptEnumMap = {
target: {
'es3': ts.ScriptTarget.ES3,
'es5': ts.ScriptTarget.ES5,
'es6': ts.ScriptTarget.ES6,
'latest': ts.ScriptTarget.Latest
},
module: {
'none': ts.ModuleKind.None,
'commonjs': ts.ModuleKind.CommonJS,
'amd': ts.ModuleKind.AMD,
'umd': ts.ModuleKind.UMD,
'system': ts.ModuleKind.System,
'es6': ts.ModuleKind.ES6,
'es2015': ts.ModuleKind.ES2015,
},
moduleResolution: {
'node': ts.ModuleResolutionKind.NodeJs,
'classic': ts.ModuleResolutionKind.Classic
},
jsx: {
'preserve': ts.JsxEmit.Preserve,
'react': ts.JsxEmit.React
},
newLine: {
'CRLF': ts.NewLineKind.CarriageReturnLineFeed,
'LF': ts.NewLineKind.LineFeed
}
};
var jsonEnumMap: any = {};
Object.keys(typescriptEnumMap).forEach(name => {
jsonEnumMap[name] = reverseKeysAndValues(typescriptEnumMap[name]);
});
function mixin(target: any, source: any): any {
for (var key in source) {
target[key] = source[key];
}
return target;
}
function rawToTsCompilerOptions(jsonOptions: CompilerOptions, projectDir: string): ts.CompilerOptions {
// Cannot use Object.create because the compiler checks hasOwnProperty
var compilerOptions = <ts.CompilerOptions> mixin({}, defaults);
for (var key in jsonOptions) {
if (typescriptEnumMap[key]) {
compilerOptions[key] = typescriptEnumMap[key][jsonOptions[key].toLowerCase()];
}
else {
compilerOptions[key] = jsonOptions[key];
}
}
if (compilerOptions.outDir !== undefined) {
compilerOptions.outDir = path.resolve(projectDir, compilerOptions.outDir);
}
if (compilerOptions.rootDir !== undefined) {
compilerOptions.rootDir = path.resolve(projectDir, compilerOptions.rootDir);
}
if (compilerOptions.out !== undefined) {
compilerOptions.out = path.resolve(projectDir, compilerOptions.out);
}
if (compilerOptions.outFile !== undefined) {
// Till out is removed. Support outFile by just copying it to `out`
compilerOptions.out = path.resolve(projectDir, compilerOptions.outFile);
}
return compilerOptions;
}
function tsToRawCompilerOptions(compilerOptions: ts.CompilerOptions): CompilerOptions {
// Cannot use Object.create because JSON.stringify will only serialize own properties
var jsonOptions = <CompilerOptions> mixin({}, compilerOptions);
Object.keys(compilerOptions).forEach((key) => {
if (jsonEnumMap[key] && compilerOptions[key]) {
var value = <string>compilerOptions[key];
jsonOptions[key] = jsonEnumMap[key][value];
}
});
return jsonOptions;
}
export function getDefaultInMemoryProject(srcFile: string): TypeScriptProjectFileDetails {
var dir = fs.lstatSync(srcFile).isDirectory() ? srcFile : path.dirname(srcFile);
var files = [srcFile];
var typings = getDefinitionsForNodeModules(dir, files);
files = increaseProjectForReferenceAndImports(files);
files = uniq(files.map(fsu.consistentPath));
let project: TypeScriptProjectSpecification = {
compilerOptions: defaults,
files,
typings: typings.ours.concat(typings.implicit),
formatCodeOptions: formatting.defaultFormatCodeOptions(),
compileOnSave: true,
buildOnSave: false,
scripts: {},
atom: { rewriteTsconfig: true },
};
return {
projectFileDirectory: dir,
projectFilePath: dir + '/' + projectFileName,
project: project,
inMemory: true
};
}
/** Given an src (source file or directory) goes up the directory tree to find the project specifications.
* Use this to bootstrap the UI for what project the user might want to work on.
* Note: Definition files (.d.ts) are considered thier own project
*/
export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDetails {
if (!fs.existsSync(pathOrSrcFile)) {
throw new Error(errors.GET_PROJECT_INVALID_PATH);
}
var dir = fs.lstatSync(pathOrSrcFile).isDirectory() ? pathOrSrcFile : path.dirname(pathOrSrcFile);
var projectFile = tsconfig.resolveSync(dir);
if (!projectFile) {
throw errorWithDetails<GET_PROJECT_NO_PROJECT_FOUND_Details>(
new Error(errors.GET_PROJECT_NO_PROJECT_FOUND), { projectFilePath: fsu.consistentPath(pathOrSrcFile), errorMessage: 'not found' });
}
var projectFileDirectory = path.dirname(projectFile) + path.sep;
// We now have a valid projectFile. Parse it:
var projectSpec: TypeScriptProjectRawSpecification;
var projectFileTextContent: string;
try {
projectFileTextContent = fs.readFileSync(projectFile, 'utf8');
} catch (ex) {
throw new Error(errors.GET_PROJECT_FAILED_TO_OPEN_PROJECT_FILE);
}
try {
projectSpec = tsconfig.parseFileSync(projectFileTextContent, projectFile, { resolvePaths: false });
} catch (ex) {
throw errorWithDetails<GET_PROJECT_JSON_PARSE_FAILED_Details>(
new Error(errors.GET_PROJECT_JSON_PARSE_FAILED), { projectFilePath: fsu.consistentPath(projectFile), error: ex.message });
}
/** Setup defaults for atom key */
if (!projectSpec.atom) {
projectSpec.atom = {
rewriteTsconfig: true,
}
}
if (projectSpec.filesGlob) { // for filesGlob we keep the files in sync
var prettyJSONProjectSpec = prettyJSON(projectSpec, detectIndent(projectFileTextContent).indent);
if (prettyJSONProjectSpec !== projectFileTextContent && projectSpec.atom.rewriteTsconfig) {
fs.writeFileSync(projectFile, prettyJSONProjectSpec);
}
}
var pkg: UsefulFromPackageJson = null;
try {
var packagePath = travelUpTheDirectoryTreeTillYouFind(projectFileDirectory, 'package.json');
if (packagePath) {
let packageJSONPath = getPotentiallyRelativeFile(projectFileDirectory, packagePath);
let parsedPackage = JSON.parse(fs.readFileSync(packageJSONPath).toString());
pkg = {
main: parsedPackage.main,
name: parsedPackage.name,
directory: path.dirname(packageJSONPath),
definition: parsedPackage.typescript && parsedPackage.typescript.definition
};
}
}
catch (ex) {
// console.error('no package.json found', projectFileDirectory, ex.message);
}
var project: TypeScriptProjectSpecification = {
compilerOptions: {},
files: projectSpec.files.map(x => path.resolve(projectFileDirectory, x)),
filesGlob: projectSpec.filesGlob,
formatCodeOptions: formatting.makeFormatCodeOptions(projectSpec.formatCodeOptions),
compileOnSave: projectSpec.compileOnSave == undefined ? true : projectSpec.compileOnSave,
package: pkg,
typings: [],
externalTranspiler: projectSpec.externalTranspiler == undefined ? undefined : projectSpec.externalTranspiler,
scripts: projectSpec.scripts || {},
buildOnSave: !!projectSpec.buildOnSave,
atom: { rewriteTsconfig: true }
};
// Validate the raw compiler options before converting them to TS compiler options
var validationResult = validator.validate(projectSpec.compilerOptions);
if (validationResult.errorMessage) {
throw errorWithDetails<GET_PROJECT_PROJECT_FILE_INVALID_OPTIONS_Details>(
new Error(errors.GET_PROJECT_PROJECT_FILE_INVALID_OPTIONS),
{ projectFilePath: fsu.consistentPath(projectFile), errorMessage: validationResult.errorMessage }
);
}
// Convert the raw options to TS options
project.compilerOptions = rawToTsCompilerOptions(projectSpec.compilerOptions, projectFileDirectory);
// Expand files to include references
project.files = increaseProjectForReferenceAndImports(project.files);
// Expand files to include node_modules / package.json / typescript.definition
var typings = getDefinitionsForNodeModules(dir, project.files);
project.files = project.files.concat(typings.implicit);
project.typings = typings.ours.concat(typings.implicit);
project.files = project.files.concat(typings.packagejson);
// Normalize to "/" for all files
// And take the uniq values
project.files = uniq(project.files.map(fsu.consistentPath));
projectFileDirectory = removeTrailingSlash(fsu.consistentPath(projectFileDirectory));
return {
projectFileDirectory: projectFileDirectory,
projectFilePath: projectFileDirectory + '/' + projectFileName,
project: project,
inMemory: false
};
}
/** Creates a project by source file location. Defaults are assumed unless overriden by the optional spec. */
export function createProjectRootSync(srcFile: string, defaultOptions?: ts.CompilerOptions) {
if (!fs.existsSync(srcFile)) {
throw new Error(errors.CREATE_FILE_MUST_EXIST);
}
// Get directory
var dir = fs.lstatSync(srcFile).isDirectory() ? srcFile : path.dirname(srcFile);
var projectFilePath = path.normalize(dir + '/' + projectFileName);
if (fs.existsSync(projectFilePath))
throw new Error(errors.CREATE_PROJECT_ALREADY_EXISTS);
// We need to write the raw spec
var projectSpec: TypeScriptProjectRawSpecification = {};
projectSpec.compilerOptions = tsToRawCompilerOptions(defaultOptions || defaults);
projectSpec.filesGlob = defaultFilesGlob;
projectSpec.compileOnSave = true;
projectSpec.buildOnSave = false;
fs.writeFileSync(projectFilePath, prettyJSON(projectSpec));
return getProjectSync(srcFile);
}
/////////////////////////////////////////////
/////////////// UTILITIES ///////////////////
/////////////////////////////////////////////
function increaseProjectForReferenceAndImports(files: string[]): string[] {
var filesMap = simpleValidator.createMap(files);
var willNeedMoreAnalysis = (file: string) => {
if (!filesMap[file]) {
filesMap[file] = true;
files.push(file);
return true;
} else {
return false;
}
}
var getReferencedOrImportedFiles = (files: string[]): string[]=> {
var referenced: string[][] = [];
files.forEach(file => {
try {
var content = fs.readFileSync(file).toString();
}
catch (ex) {
// if we cannot read a file for whatever reason just quit
return;
}
var preProcessedFileInfo = ts.preProcessFile(content, true),
dir = path.dirname(file);
let extensions = ['.ts', '.d.ts', '.tsx'];
function getIfExists(filePathNoExt: string) {
for (let ext of extensions) {
if (fs.existsSync(filePathNoExt + ext)) {
return filePathNoExt + ext;
}
}
}
referenced.push(
preProcessedFileInfo.referencedFiles.map(fileReference => {
// We assume reference paths are always relative
var file = path.resolve(dir, fsu.consistentPath(fileReference.fileName));
// Try by itself then with extensions
if (fs.existsSync(file)) {
return file;
}
return getIfExists(file);
}).filter(file=> !!file)
.concat(
preProcessedFileInfo.importedFiles
.filter((fileReference) => pathIsRelative(fileReference.fileName))
.map(fileReference => {
let fileNoExt = path.resolve(dir, fileReference.fileName);
let file = getIfExists(fileNoExt);
if (!file) {
file = getIfExists(`${file}/index`);
}
return file;
}).filter(file=> !!file)
)
);
});
return selectMany(referenced);
}
var more = getReferencedOrImportedFiles(files)
.filter(willNeedMoreAnalysis);
while (more.length) {
more = getReferencedOrImportedFiles(files)
.filter(willNeedMoreAnalysis);
}
return files;
}
/** There can be only one typing by name */
interface Typings {
[name: string]: {
filePath: string;
/** Right now its just INF as we don't do version checks. First one wins! */
version: number; // (Simple : maj * 1000000 + min). Don't care about patch
};
}
/**
* Spec
* We will expand on files making sure that we don't have a `typing` with the same name
* Also if two node_modules reference a similar sub project (and also recursively) then the one with latest `version` field wins
*/
function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ours: string[]; implicit: string[], packagejson: string[] } {
let packagejson = [];
/** TODO use later when we care about versions */
function versionStringToNumber(version: string): number {
var [maj, min, patch] = version.split('.');
return parseInt(maj) * 1000000 + parseInt(min);
}
var typings: Typings = {};
// Find our `typings` (anything in a typings folder with extension `.d.ts` is considered a typing)
// These are INF powerful
var ourTypings = files
.filter(f=> path.basename(path.dirname(f)) == 'typings' && endsWith(f, '.d.ts')
|| path.basename(path.dirname(path.dirname(f))) == 'typings' && endsWith(f, '.d.ts'));
ourTypings.forEach(f=> typings[path.basename(f)] = { filePath: f, version: Infinity });
var existing = createMap(files.map(fsu.consistentPath));
function addAllReferencedFilesWithMaxVersion(file: string) {
var dir = path.dirname(file);
try {
var content = fs.readFileSync(file).toString();
}
catch (ex) {
// if we cannot read a file for whatever reason just quit
return;
}
var preProcessedFileInfo = ts.preProcessFile(content, true);
var files = preProcessedFileInfo.referencedFiles.map(fileReference => {
// We assume reference paths are always relative
var file = path.resolve(dir, fileReference.fileName);
// Try by itself, .d.ts
if (fs.existsSync(file)) {
return file;
}
if (fs.existsSync(file + '.d.ts')) {
return file + '.d.ts';
}
}).filter(f=> !!f);
// Only ones we don't have by name yet
// TODO: replace INF with an actual version
files = files
.filter(f => !typings[path.basename(f)] || typings[path.basename(f)].version > Infinity);
// Add these
files.forEach(f => typings[path.basename(f)] = { filePath: f, version: Infinity });
// Keep expanding
files.forEach(f=> addAllReferencedFilesWithMaxVersion(f));
}
// Keep going up till we find node_modules
// at that point read the `package.json` for each file in node_modules
// And then if that package.json has `typescript.definition` we import that file
try {
var node_modules = travelUpTheDirectoryTreeTillYouFind(projectDir, 'node_modules', true);
// For each sub directory of node_modules look at package.json and then `typescript.definition`
var moduleDirs = getDirs(node_modules);
for (let moduleDir of moduleDirs) {
try {
var package_json = JSON.parse(fs.readFileSync(`${moduleDir}/package.json`).toString());
packagejson.push(`${moduleDir}/package.json`);
}
catch (ex) {
// Can't read package.json ... no worries ... move on to other modules
continue;
}
if (package_json.typescript && package_json.typescript.definition) {
let file = path.resolve(moduleDir, './', package_json.typescript.definition);
typings[path.basename(file)] = {
filePath: file,
version: Infinity
};
// Also add any files that this `.d.ts` references as long as they don't conflict with what we have
addAllReferencedFilesWithMaxVersion(file);
}
}
}
catch (ex) {
if (ex.message == "not found") {
// Sure we didn't find node_modules
// Thats cool
}
// this is best effort only at the moment
else {
console.error('Failed to read package.json from node_modules due to error:', ex, ex.stack);
}
}
var all = Object.keys(typings)
.map(typing => typings[typing].filePath)
.map(x=> fsu.consistentPath(x));
var implicit = all
.filter(x=> !existing[x]);
var ours = all
.filter(x=> existing[x]);
return { implicit, ours, packagejson };
}
export function prettyJSON(object: any, indent: string | number = 4): string {
var cache = [];
var value = JSON.stringify(
object,
// fixup circular reference
function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
},
indent
);
value = value.split('\n').join(os.EOL) + os.EOL;
cache = null;
return value;
}
// Not particularly awesome e.g. '/..foo' will be not relative
export function pathIsRelative(str: string) {
if (!str.length) return false;
return str[0] == '.' || str.substring(0, 2) == "./" || str.substring(0, 3) == "../";
}
// Not optimized
function selectMany<T>(arr: T[][]): T[] {
var result = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
result.push(arr[i][j]);
}
}
return result;
}
export function endsWith(str: string, suffix: string): boolean {
return str && str.indexOf(suffix, str.length - suffix.length) !== -1;
}
function uniq(arr: string[]): string[] {
var map = simpleValidator.createMap(arr);
return Object.keys(map);
}
/**
* Converts "C:\boo" , "C:\boo\foo.ts" => "./foo.ts"; Works on unix as well.
*/
export function makeRelativePath(relativeFolder: string, filePath: string) {
var relativePath = path.relative(relativeFolder, filePath).split('\\').join('/');
if (relativePath[0] !== '.') {
relativePath = './' + relativePath;
}
return relativePath;
}
export function removeExt(filePath: string) {
return filePath.substr(0, filePath.lastIndexOf('.'));
}
export function removeTrailingSlash(filePath: string) {
if (!filePath) return filePath;
if (endsWith(filePath, '/')) return filePath.substr(0, filePath.length - 1);
return filePath;
}
/**
* returns the path if found
* @throws an error "not found" if not found */
export function travelUpTheDirectoryTreeTillYouFind(dir: string, fileOrDirectory: string,
/** This is useful if we don't want to file `node_modules from inside node_modules` */
abortIfInside = false): string {
while (fs.existsSync(dir)) { // while directory exists
var potentialFile = dir + '/' + fileOrDirectory;
/** This means that we were *just* in this directory */
if (before == potentialFile) {
if (abortIfInside) {
throw new Error("not found")
}
}
if (fs.existsSync(potentialFile)) { // found it
return potentialFile;
}
else { // go up
var before = dir;
dir = path.dirname(dir);
// At root:
if (dir == before) throw new Error("not found");
}
}
}
export function getPotentiallyRelativeFile(basePath: string, filePath: string) {
if (pathIsRelative(filePath)) {
return fsu.consistentPath(path.resolve(basePath, filePath));
}
return fsu.consistentPath(filePath);
}
function getDirs(rootDir: string): string[] {
var files = fs.readdirSync(rootDir)
var dirs = []
for (var file of files) {
if (file[0] != '.') {
var filePath = `${rootDir}/${file}`
var stat = fs.statSync(filePath);
if (stat.isDirectory()) {
dirs.push(filePath)
}
}
}
return dirs
}
/**
* Create a quick lookup map from list
*/
export function createMap(arr: string[]): { [string: string]: boolean } {
return arr.reduce((result: { [string: string]: boolean }, key: string) => {
result[key] = true;
return result;
}, <{ [string: string]: boolean }>{});
}
/**
* Turns keys into values and values into keys
*/
function reverseKeysAndValues(obj) {
var toret = {};
Object.keys(obj).forEach(function(key) {
toret[obj[key]] = key;
});
return toret;
}