Skip to content

Commit 25c6234

Browse files
committed
Replace resolve logic with tsconfig
Closes #651. References #646.
1 parent 5d21618 commit 25c6234

File tree

5 files changed

+59
-104
lines changed

5 files changed

+59
-104
lines changed

dist/main/tsconfig/tsconfig.js

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var fsu = require("../utils/fsUtil");
22
var simpleValidator = require('./simpleValidator');
3-
var stripBom = require('strip-bom');
43
var types = simpleValidator.types;
54
var compilerOptionsValidation = {
65
allowNonTsExtensions: { type: simpleValidator.types.boolean },
@@ -62,8 +61,10 @@ function errorWithDetails(error, details) {
6261
}
6362
var fs = require('fs');
6463
var path = require('path');
65-
var glob = require('glob');
64+
var tsconfig = require('tsconfig');
6665
var os = require('os');
66+
var detectIndent = require('detect-indent');
67+
var extend = require('xtend');
6768
var formatting = require('./formatting');
6869
var projectFileName = 'tsconfig.json';
6970
var defaultFilesGlob = [
@@ -186,61 +187,37 @@ function getProjectSync(pathOrSrcFile) {
186187
throw new Error(exports.errors.GET_PROJECT_INVALID_PATH);
187188
}
188189
var dir = fs.lstatSync(pathOrSrcFile).isDirectory() ? pathOrSrcFile : path.dirname(pathOrSrcFile);
189-
var projectFile = '';
190-
try {
191-
projectFile = travelUpTheDirectoryTreeTillYouFind(dir, projectFileName);
192-
}
193-
catch (e) {
194-
var err = e;
195-
if (err.message == "not found") {
196-
throw errorWithDetails(new Error(exports.errors.GET_PROJECT_NO_PROJECT_FOUND), { projectFilePath: fsu.consistentPath(pathOrSrcFile), errorMessage: err.message });
197-
}
190+
var projectFile = tsconfig.resolveSync(dir);
191+
if (!projectFile) {
192+
throw errorWithDetails(new Error(exports.errors.GET_PROJECT_NO_PROJECT_FOUND), { projectFilePath: fsu.consistentPath(pathOrSrcFile), errorMessage: 'not found' });
198193
}
199-
projectFile = path.normalize(projectFile);
200194
var projectFileDirectory = path.dirname(projectFile) + path.sep;
201195
var projectSpec;
196+
var projectFileTextContent;
202197
try {
203-
var projectFileTextContent = fs.readFileSync(projectFile, 'utf8');
198+
projectFileTextContent = fs.readFileSync(projectFile, 'utf8');
204199
}
205200
catch (ex) {
206201
throw new Error(exports.errors.GET_PROJECT_FAILED_TO_OPEN_PROJECT_FILE);
207202
}
208203
try {
209-
projectSpec = JSON.parse(stripBom(projectFileTextContent));
204+
projectSpec = tsconfig.parseFileSync(projectFileTextContent, projectFile);
210205
}
211206
catch (ex) {
212207
throw errorWithDetails(new Error(exports.errors.GET_PROJECT_JSON_PARSE_FAILED), { projectFilePath: fsu.consistentPath(projectFile), error: ex.message });
213208
}
214209
if (!projectSpec.compilerOptions)
215210
projectSpec.compilerOptions = {};
216-
var cwdPath = path.relative(process.cwd(), path.dirname(projectFile));
217-
var filesGlob = invisibleFilesGlob;
218-
var ignore = [];
219-
if (Array.isArray(projectSpec.filesGlob)) {
220-
filesGlob = projectSpec.filesGlob.length === 1 ? projectSpec.filesGlob[0] : "{" + projectSpec.filesGlob.join(',') + "}";
221-
}
222-
else if (projectSpec.exclude) {
223-
ignore = projectSpec.exclude.map(function (path) { return (path + "/**"); });
224-
}
225-
if (filesGlob) {
226-
try {
227-
projectSpec.files = glob.sync(filesGlob, {
228-
cwd: cwdPath,
229-
ignore: ignore,
230-
nodir: true
231-
});
232-
}
233-
catch (ex) {
234-
throw errorWithDetails(new Error(exports.errors.GET_PROJECT_GLOB_EXPAND_FAILED), { glob: projectSpec.filesGlob, projectFilePath: fsu.consistentPath(projectFile), errorMessage: ex.message });
235-
}
236-
}
237211
if (projectSpec.filesGlob) {
238-
var prettyJSONProjectSpec = prettyJSON(projectSpec);
212+
var relativeProjectSpec = extend(projectSpec, {
213+
files: projectSpec.files.map(function (x) { return fsu.consistentPath(path.relative(projectFileDirectory, x)); }),
214+
exclude: projectSpec.exclude.map(function (x) { return fsu.consistentPath(path.relative(projectFileDirectory, x)); })
215+
});
216+
var prettyJSONProjectSpec = prettyJSON(relativeProjectSpec, detectIndent(projectFileTextContent).indent);
239217
if (prettyJSONProjectSpec !== projectFileTextContent) {
240-
fs.writeFileSync(projectFile, prettyJSON(projectSpec));
218+
fs.writeFileSync(projectFile, prettyJSONProjectSpec);
241219
}
242220
}
243-
projectSpec.files = projectSpec.files.map(function (file) { return path.resolve(projectFileDirectory, file); });
244221
var pkg = null;
245222
try {
246223
var packagePath = travelUpTheDirectoryTreeTillYouFind(projectFileDirectory, 'package.json');
@@ -432,7 +409,8 @@ function getDefinitionsForNodeModules(projectDir, files) {
432409
.filter(function (x) { return existing[x]; });
433410
return { implicit: implicit, ours: ours, packagejson: packagejson };
434411
}
435-
function prettyJSON(object) {
412+
function prettyJSON(object, indent) {
413+
if (indent === void 0) { indent = 4; }
436414
var cache = [];
437415
var value = JSON.stringify(object, function (key, value) {
438416
if (typeof value === 'object' && value !== null) {
@@ -442,7 +420,7 @@ function prettyJSON(object) {
442420
cache.push(value);
443421
}
444422
return value;
445-
}, 4);
423+
}, indent);
446424
value = value.split('\n').join(os.EOL) + os.EOL;
447425
cache = null;
448426
return value;

lib/globals.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ declare module 'escape-html' {
3232
export = escape;
3333
}
3434

35-
// courtesy @blakeembrey
36-
declare module 'strip-bom' {
37-
import Buffer = require('buffer')
38-
39-
function stripBom(value: string): string
40-
function stripBom(value: Buffer): Buffer
35+
declare module 'detect-indent' {
36+
function detectIndent (string: string): { amount: number; type?: string; indent: string };
37+
export = detectIndent;
38+
}
4139

42-
export = stripBom
40+
declare module 'xtend' {
41+
function extend <T, U> (dest: T, src: U): T & U;
42+
export = extend;
4343
}
4444

4545
declare module 'atom-space-pen-views' {

lib/main/tsconfig/tsconfig.ts

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as fsu from "../utils/fsUtil";
22

33
import simpleValidator = require('./simpleValidator');
4-
import stripBom = require('strip-bom');
54
var 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

197196
import fs = require('fs');
198197
import path = require('path');
199-
import glob = require('glob');
198+
import tsconfig = require('tsconfig');
200199
import os = require('os');
200+
import detectIndent = require('detect-indent');
201+
import extend = require('xtend');
201202
import formatting = require('./formatting');
202203

203204
var 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;

lib/tsconfig.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
"./main/atom/views/plainMessageView.ts",
5454
"./main/atom/views/projectSymbolsView.ts",
5555
"./main/atom/views/renameView.ts",
56-
"./main/atom/views/rView.tsx",
57-
"./main/atom/views/semanticView.tsx",
5856
"./main/atom/views/semanticViewGlobals.d.ts",
5957
"./main/atom/views/simpleOverlaySelectionView.ts",
6058
"./main/atom/views/simpleSelectionView.ts",
@@ -67,20 +65,20 @@
6765
"./main/lang/core/project.ts",
6866
"./main/lang/fixmyts/astUtils.ts",
6967
"./main/lang/fixmyts/quickFix.ts",
68+
"./main/lang/fixmyts/quickFixRegistry.ts",
7069
"./main/lang/fixmyts/quickFixes/addClassMember.ts",
7170
"./main/lang/fixmyts/quickFixes/addClassMethod.ts",
7271
"./main/lang/fixmyts/quickFixes/addImportStatement.ts",
7372
"./main/lang/fixmyts/quickFixes/equalsToEquals.ts",
7473
"./main/lang/fixmyts/quickFixes/extractVariable.ts",
7574
"./main/lang/fixmyts/quickFixes/implementInterface.ts",
76-
"./main/lang/fixmyts/quickFixes/quotesToQuotes.ts",
7775
"./main/lang/fixmyts/quickFixes/quoteToTemplate.ts",
76+
"./main/lang/fixmyts/quickFixes/quotesToQuotes.ts",
7877
"./main/lang/fixmyts/quickFixes/singleLineCommentToJsdoc.ts",
7978
"./main/lang/fixmyts/quickFixes/stringConcatToTemplate.ts",
8079
"./main/lang/fixmyts/quickFixes/typeAssertPropertyAccessToAny.ts",
8180
"./main/lang/fixmyts/quickFixes/typeAssertPropertyAccessToType.ts",
8281
"./main/lang/fixmyts/quickFixes/wrapInProperty.ts",
83-
"./main/lang/fixmyts/quickFixRegistry.ts",
8482
"./main/lang/modules/astToText.ts",
8583
"./main/lang/modules/building.ts",
8684
"./main/lang/modules/formatting.ts",
@@ -128,6 +126,8 @@
128126
"./worker/debug.ts",
129127
"./worker/lib/workerLib.ts",
130128
"./worker/parent.ts",
131-
"./worker/queryParent.ts"
129+
"./worker/queryParent.ts",
130+
"./main/atom/views/rView.tsx",
131+
"./main/atom/views/semanticView.tsx"
132132
]
133133
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"babel": "^5.6.23",
4646
"basarat-text-buffer": "6.0.0",
4747
"d3": "^3.5.5",
48+
"detect-indent": "^4.0.0",
4849
"emissary": "^1.3.3",
4950
"escape-html": "^1.0.1",
5051
"fuzzaldrin": "^2.1.0",
@@ -56,7 +57,8 @@
5657
"ntypescript": "1.201509200205.1",
5758
"react": "^0.13.3",
5859
"season": "^5.1.4",
59-
"strip-bom": "^2.0.0"
60+
"tsconfig": "^1.1.0",
61+
"xtend": "^4.0.0"
6062
},
6163
"devDependencies": {},
6264
"package-deps": [

0 commit comments

Comments
 (0)