11import * as fsu from "../utils/fsUtil" ;
22
33import simpleValidator = require( './simpleValidator' ) ;
4- import stripBom = require( 'strip-bom' ) ;
54var types = simpleValidator . types ;
65
76// Most compiler options come from require('typescript').CompilerOptions, but
@@ -196,11 +195,14 @@ function errorWithDetails<T>(error: Error, details: T): Error {
196195
197196import fs = require( 'fs' ) ;
198197import path = require( 'path' ) ;
199- import glob = require( 'glob ' ) ;
198+ import tsconfig = require( 'tsconfig ' ) ;
200199import os = require( 'os' ) ;
200+ import detectIndent = require( 'detect-indent' ) ;
201+ import extend = require( 'xtend' ) ;
201202import formatting = require( './formatting' ) ;
202203
203204var projectFileName = 'tsconfig.json' ;
205+
204206/**
205207 * This is what we write to new files
206208 */
@@ -352,33 +354,28 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
352354 throw new Error ( errors . GET_PROJECT_INVALID_PATH ) ;
353355 }
354356
355- // Get the path directory
356357 var dir = fs . lstatSync ( pathOrSrcFile ) . isDirectory ( ) ? pathOrSrcFile : path . dirname ( pathOrSrcFile ) ;
358+ var projectFile = tsconfig . resolveSync ( dir ) ;
357359
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' } ) ;
362363 }
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+
371365 var projectFileDirectory = path . dirname ( projectFile ) + path . sep ;
372366
373367 // We now have a valid projectFile. Parse it:
374368 var projectSpec : TypeScriptProjectRawSpecification ;
369+ var projectFileTextContent : string ;
370+
375371 try {
376- var projectFileTextContent = fs . readFileSync ( projectFile , 'utf8' ) ;
372+ projectFileTextContent = fs . readFileSync ( projectFile , 'utf8' ) ;
377373 } catch ( ex ) {
378374 throw new Error ( errors . GET_PROJECT_FAILED_TO_OPEN_PROJECT_FILE ) ;
379375 }
376+
380377 try {
381- projectSpec = JSON . parse ( stripBom ( projectFileTextContent ) ) ;
378+ projectSpec = tsconfig . parseFileSync ( projectFileTextContent , projectFile ) ;
382379 } catch ( ex ) {
383380 throw errorWithDetails < GET_PROJECT_JSON_PARSE_FAILED_Details > (
384381 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
387384 // Setup default project options
388385 if ( ! projectSpec . compilerOptions ) projectSpec . compilerOptions = { } ;
389386
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+ } ) ;
395392
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 ) ;
401394
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 ) ;
418395 if ( prettyJSONProjectSpec !== projectFileTextContent ) {
419- fs . writeFileSync ( projectFile , prettyJSON ( projectSpec ) ) ;
396+ fs . writeFileSync ( projectFile , prettyJSONProjectSpec ) ;
420397 }
421398 }
422399
423- // Remove all relativeness
424- projectSpec . files = projectSpec . files . map ( ( file ) => path . resolve ( projectFileDirectory , file ) ) ;
425-
426400 var pkg : UsefulFromPackageJson = null ;
427401 try {
428402 var packagePath = travelUpTheDirectoryTreeTillYouFind ( projectFileDirectory , 'package.json' ) ;
@@ -703,9 +677,10 @@ function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ou
703677 return { implicit, ours, packagejson } ;
704678}
705679
706- export function prettyJSON ( object : any ) : string {
680+ export function prettyJSON ( object : any , indent : string | number = 4 ) : string {
707681 var cache = [ ] ;
708- var value = JSON . stringify ( object ,
682+ var value = JSON . stringify (
683+ object ,
709684 // fixup circular reference
710685 function ( key , value ) {
711686 if ( typeof value === 'object' && value !== null ) {
@@ -718,8 +693,8 @@ export function prettyJSON(object: any): string {
718693 }
719694 return value ;
720695 } ,
721- // indent 4 spaces
722- 4 ) ;
696+ indent
697+ ) ;
723698 value = value . split ( '\n' ) . join ( os . EOL ) + os . EOL ;
724699 cache = null ;
725700 return value ;
0 commit comments