1
1
import * as fsu from "../utils/fsUtil" ;
2
2
3
3
import simpleValidator = require( './simpleValidator' ) ;
4
- import stripBom = require( 'strip-bom' ) ;
5
4
var types = simpleValidator . types ;
6
5
7
6
// Most compiler options come from require('typescript').CompilerOptions, but
@@ -196,11 +195,14 @@ function errorWithDetails<T>(error: Error, details: T): Error {
196
195
197
196
import fs = require( 'fs' ) ;
198
197
import path = require( 'path' ) ;
199
- import glob = require( 'glob ' ) ;
198
+ import tsconfig = require( 'tsconfig ' ) ;
200
199
import os = require( 'os' ) ;
200
+ import detectIndent = require( 'detect-indent' ) ;
201
+ import extend = require( 'xtend' ) ;
201
202
import formatting = require( './formatting' ) ;
202
203
203
204
var projectFileName = 'tsconfig.json' ;
205
+
204
206
/**
205
207
* This is what we write to new files
206
208
*/
@@ -352,33 +354,28 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
352
354
throw new Error ( errors . GET_PROJECT_INVALID_PATH ) ;
353
355
}
354
356
355
- // Get the path directory
356
357
var dir = fs . lstatSync ( pathOrSrcFile ) . isDirectory ( ) ? pathOrSrcFile : path . dirname ( pathOrSrcFile ) ;
358
+ var projectFile = tsconfig . resolveSync ( dir ) ;
357
359
358
- // Keep going up till we find the project file
359
- var projectFile = '' ;
360
- try {
361
- projectFile = travelUpTheDirectoryTreeTillYouFind ( dir , projectFileName ) ;
360
+ if ( ! projectFile ) {
361
+ throw errorWithDetails < GET_PROJECT_NO_PROJECT_FOUND_Details > (
362
+ new Error ( errors . GET_PROJECT_NO_PROJECT_FOUND ) , { projectFilePath : fsu . consistentPath ( pathOrSrcFile ) , errorMessage : 'not found' } ) ;
362
363
}
363
- catch ( e ) {
364
- let err : Error = e ;
365
- if ( err . message == "not found" ) {
366
- throw errorWithDetails < GET_PROJECT_NO_PROJECT_FOUND_Details > (
367
- new Error ( errors . GET_PROJECT_NO_PROJECT_FOUND ) , { projectFilePath : fsu . consistentPath ( pathOrSrcFile ) , errorMessage : err . message } ) ;
368
- }
369
- }
370
- projectFile = path . normalize ( projectFile ) ;
364
+
371
365
var projectFileDirectory = path . dirname ( projectFile ) + path . sep ;
372
366
373
367
// We now have a valid projectFile. Parse it:
374
368
var projectSpec : TypeScriptProjectRawSpecification ;
369
+ var projectFileTextContent : string ;
370
+
375
371
try {
376
- var projectFileTextContent = fs . readFileSync ( projectFile , 'utf8' ) ;
372
+ projectFileTextContent = fs . readFileSync ( projectFile , 'utf8' ) ;
377
373
} catch ( ex ) {
378
374
throw new Error ( errors . GET_PROJECT_FAILED_TO_OPEN_PROJECT_FILE ) ;
379
375
}
376
+
380
377
try {
381
- projectSpec = JSON . parse ( stripBom ( projectFileTextContent ) ) ;
378
+ projectSpec = tsconfig . parseFileSync ( projectFileTextContent , projectFile ) ;
382
379
} catch ( ex ) {
383
380
throw errorWithDetails < GET_PROJECT_JSON_PARSE_FAILED_Details > (
384
381
new Error ( errors . GET_PROJECT_JSON_PARSE_FAILED ) , { projectFilePath : fsu . consistentPath ( projectFile ) , error : ex . message } ) ;
@@ -387,42 +384,19 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
387
384
// Setup default project options
388
385
if ( ! projectSpec . compilerOptions ) projectSpec . compilerOptions = { } ;
389
386
390
- // Our customizations for "tsconfig.json"
391
- // Use grunt.file.expand type of logic
392
- var cwdPath = path . relative ( process . cwd ( ) , path . dirname ( projectFile ) ) ;
393
- var filesGlob = invisibleFilesGlob ;
394
- var ignore = [ ] ;
387
+ if ( projectSpec . filesGlob ) { // for filesGlob we keep the files in sync
388
+ var relativeProjectSpec = extend ( projectSpec , {
389
+ files : projectSpec . files . map ( x => fsu . consistentPath ( path . relative ( projectFileDirectory , x ) ) ) ,
390
+ exclude : projectSpec . exclude . map ( x => fsu . consistentPath ( path . relative ( projectFileDirectory , x ) ) )
391
+ } ) ;
395
392
396
- if ( Array . isArray ( projectSpec . filesGlob ) ) {
397
- filesGlob = projectSpec . filesGlob . length === 1 ? projectSpec . filesGlob [ 0 ] : `{${ projectSpec . filesGlob . join ( ',' ) } }` ;
398
- } else if ( projectSpec . exclude ) {
399
- ignore = projectSpec . exclude . map ( path => `${ path } /**` )
400
- }
393
+ var prettyJSONProjectSpec = prettyJSON ( relativeProjectSpec , detectIndent ( projectFileTextContent ) . indent ) ;
401
394
402
- if ( filesGlob ) { // Expand whatever needs expanding
403
- try {
404
- projectSpec . files = glob . sync ( filesGlob , {
405
- cwd : cwdPath ,
406
- ignore : ignore ,
407
- nodir : true
408
- } ) ;
409
- }
410
- catch ( ex ) {
411
- throw errorWithDetails < GET_PROJECT_GLOB_EXPAND_FAILED_Details > (
412
- new Error ( errors . GET_PROJECT_GLOB_EXPAND_FAILED ) ,
413
- { glob : projectSpec . filesGlob , projectFilePath : fsu . consistentPath ( projectFile ) , errorMessage : ex . message } ) ;
414
- }
415
- }
416
- if ( projectSpec . filesGlob ) { // for filesGlob we keep the files in sync
417
- var prettyJSONProjectSpec = prettyJSON ( projectSpec ) ;
418
395
if ( prettyJSONProjectSpec !== projectFileTextContent ) {
419
- fs . writeFileSync ( projectFile , prettyJSON ( projectSpec ) ) ;
396
+ fs . writeFileSync ( projectFile , prettyJSONProjectSpec ) ;
420
397
}
421
398
}
422
399
423
- // Remove all relativeness
424
- projectSpec . files = projectSpec . files . map ( ( file ) => path . resolve ( projectFileDirectory , file ) ) ;
425
-
426
400
var pkg : UsefulFromPackageJson = null ;
427
401
try {
428
402
var packagePath = travelUpTheDirectoryTreeTillYouFind ( projectFileDirectory , 'package.json' ) ;
@@ -703,9 +677,10 @@ function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ou
703
677
return { implicit, ours, packagejson } ;
704
678
}
705
679
706
- export function prettyJSON ( object : any ) : string {
680
+ export function prettyJSON ( object : any , indent : string | number = 4 ) : string {
707
681
var cache = [ ] ;
708
- var value = JSON . stringify ( object ,
682
+ var value = JSON . stringify (
683
+ object ,
709
684
// fixup circular reference
710
685
function ( key , value ) {
711
686
if ( typeof value === 'object' && value !== null ) {
@@ -718,8 +693,8 @@ export function prettyJSON(object: any): string {
718
693
}
719
694
return value ;
720
695
} ,
721
- // indent 4 spaces
722
- 4 ) ;
696
+ indent
697
+ ) ;
723
698
value = value . split ( '\n' ) . join ( os . EOL ) + os . EOL ;
724
699
cache = null ;
725
700
return value ;
0 commit comments