Skip to content

Replace resolve logic with tsconfig #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 18 additions & 40 deletions dist/main/tsconfig/tsconfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var fsu = require("../utils/fsUtil");
var simpleValidator = require('./simpleValidator');
var stripBom = require('strip-bom');
var types = simpleValidator.types;
var compilerOptionsValidation = {
allowNonTsExtensions: { type: simpleValidator.types.boolean },
Expand Down Expand Up @@ -62,8 +61,10 @@ function errorWithDetails(error, details) {
}
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var tsconfig = require('tsconfig');
var os = require('os');
var detectIndent = require('detect-indent');
var extend = require('xtend');
var formatting = require('./formatting');
var projectFileName = 'tsconfig.json';
var defaultFilesGlob = [
Expand Down Expand Up @@ -186,61 +187,37 @@ function getProjectSync(pathOrSrcFile) {
throw new Error(exports.errors.GET_PROJECT_INVALID_PATH);
}
var dir = fs.lstatSync(pathOrSrcFile).isDirectory() ? pathOrSrcFile : path.dirname(pathOrSrcFile);
var projectFile = '';
try {
projectFile = travelUpTheDirectoryTreeTillYouFind(dir, projectFileName);
}
catch (e) {
var err = e;
if (err.message == "not found") {
throw errorWithDetails(new Error(exports.errors.GET_PROJECT_NO_PROJECT_FOUND), { projectFilePath: fsu.consistentPath(pathOrSrcFile), errorMessage: err.message });
}
var projectFile = tsconfig.resolveSync(dir);
if (!projectFile) {
throw errorWithDetails(new Error(exports.errors.GET_PROJECT_NO_PROJECT_FOUND), { projectFilePath: fsu.consistentPath(pathOrSrcFile), errorMessage: 'not found' });
}
projectFile = path.normalize(projectFile);
var projectFileDirectory = path.dirname(projectFile) + path.sep;
var projectSpec;
var projectFileTextContent;
try {
var projectFileTextContent = fs.readFileSync(projectFile, 'utf8');
projectFileTextContent = fs.readFileSync(projectFile, 'utf8');
}
catch (ex) {
throw new Error(exports.errors.GET_PROJECT_FAILED_TO_OPEN_PROJECT_FILE);
}
try {
projectSpec = JSON.parse(stripBom(projectFileTextContent));
projectSpec = tsconfig.parseFileSync(projectFileTextContent, projectFile);
}
catch (ex) {
throw errorWithDetails(new Error(exports.errors.GET_PROJECT_JSON_PARSE_FAILED), { projectFilePath: fsu.consistentPath(projectFile), error: ex.message });
}
if (!projectSpec.compilerOptions)
projectSpec.compilerOptions = {};
var cwdPath = path.relative(process.cwd(), path.dirname(projectFile));
var filesGlob = invisibleFilesGlob;
var ignore = [];
if (Array.isArray(projectSpec.filesGlob)) {
filesGlob = projectSpec.filesGlob.length === 1 ? projectSpec.filesGlob[0] : "{" + projectSpec.filesGlob.join(',') + "}";
}
else if (projectSpec.exclude) {
ignore = projectSpec.exclude.map(function (path) { return (path + "/**"); });
}
if (filesGlob) {
try {
projectSpec.files = glob.sync(filesGlob, {
cwd: cwdPath,
ignore: ignore,
nodir: true
});
}
catch (ex) {
throw errorWithDetails(new Error(exports.errors.GET_PROJECT_GLOB_EXPAND_FAILED), { glob: projectSpec.filesGlob, projectFilePath: fsu.consistentPath(projectFile), errorMessage: ex.message });
}
}
if (projectSpec.filesGlob) {
var prettyJSONProjectSpec = prettyJSON(projectSpec);
var relativeProjectSpec = extend(projectSpec, {
files: projectSpec.files.map(function (x) { return fsu.consistentPath(path.relative(projectFileDirectory, x)); }),
exclude: projectSpec.exclude.map(function (x) { return fsu.consistentPath(path.relative(projectFileDirectory, x)); })
});
var prettyJSONProjectSpec = prettyJSON(relativeProjectSpec, detectIndent(projectFileTextContent).indent);
if (prettyJSONProjectSpec !== projectFileTextContent) {
fs.writeFileSync(projectFile, prettyJSON(projectSpec));
fs.writeFileSync(projectFile, prettyJSONProjectSpec);
}
}
projectSpec.files = projectSpec.files.map(function (file) { return path.resolve(projectFileDirectory, file); });
var pkg = null;
try {
var packagePath = travelUpTheDirectoryTreeTillYouFind(projectFileDirectory, 'package.json');
Expand Down Expand Up @@ -432,7 +409,8 @@ function getDefinitionsForNodeModules(projectDir, files) {
.filter(function (x) { return existing[x]; });
return { implicit: implicit, ours: ours, packagejson: packagejson };
}
function prettyJSON(object) {
function prettyJSON(object, indent) {
if (indent === void 0) { indent = 4; }
var cache = [];
var value = JSON.stringify(object, function (key, value) {
if (typeof value === 'object' && value !== null) {
Expand All @@ -442,7 +420,7 @@ function prettyJSON(object) {
cache.push(value);
}
return value;
}, 4);
}, indent);
value = value.split('\n').join(os.EOL) + os.EOL;
cache = null;
return value;
Expand Down
14 changes: 7 additions & 7 deletions lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ declare module 'escape-html' {
export = escape;
}

// courtesy @blakeembrey
declare module 'strip-bom' {
import Buffer = require('buffer')

function stripBom(value: string): string
function stripBom(value: Buffer): Buffer
declare module 'detect-indent' {
function detectIndent (string: string): { amount: number; type?: string; indent: string };
export = detectIndent;
}

export = stripBom
declare module 'xtend' {
function extend <T, U> (dest: T, src: U): T & U;
export = extend;
}

declare module 'atom-space-pen-views' {
Expand Down
77 changes: 26 additions & 51 deletions lib/main/tsconfig/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fsu from "../utils/fsUtil";

import simpleValidator = require('./simpleValidator');
import stripBom = require('strip-bom');
var types = simpleValidator.types;

// Most compiler options come from require('typescript').CompilerOptions, but
Expand Down Expand Up @@ -196,11 +195,14 @@ function errorWithDetails<T>(error: Error, details: T): Error {

import fs = require('fs');
import path = require('path');
import glob = require('glob');
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
*/
Expand Down Expand Up @@ -352,33 +354,28 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
throw new Error(errors.GET_PROJECT_INVALID_PATH);
}

// Get the path directory
var dir = fs.lstatSync(pathOrSrcFile).isDirectory() ? pathOrSrcFile : path.dirname(pathOrSrcFile);
var projectFile = tsconfig.resolveSync(dir);

// Keep going up till we find the project file
var projectFile = '';
try {
projectFile = travelUpTheDirectoryTreeTillYouFind(dir, projectFileName);
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' });
}
catch (e) {
let err: Error = e;
if (err.message == "not found") {
throw errorWithDetails<GET_PROJECT_NO_PROJECT_FOUND_Details>(
new Error(errors.GET_PROJECT_NO_PROJECT_FOUND), { projectFilePath: fsu.consistentPath(pathOrSrcFile), errorMessage: err.message });
}
}
projectFile = path.normalize(projectFile);

var projectFileDirectory = path.dirname(projectFile) + path.sep;

// We now have a valid projectFile. Parse it:
var projectSpec: TypeScriptProjectRawSpecification;
var projectFileTextContent: string;

try {
var projectFileTextContent = fs.readFileSync(projectFile, 'utf8');
projectFileTextContent = fs.readFileSync(projectFile, 'utf8');
} catch (ex) {
throw new Error(errors.GET_PROJECT_FAILED_TO_OPEN_PROJECT_FILE);
}

try {
projectSpec = JSON.parse(stripBom(projectFileTextContent));
projectSpec = tsconfig.parseFileSync(projectFileTextContent, projectFile);
} 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 });
Expand All @@ -387,42 +384,19 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
// Setup default project options
if (!projectSpec.compilerOptions) projectSpec.compilerOptions = {};

// Our customizations for "tsconfig.json"
// Use grunt.file.expand type of logic
var cwdPath = path.relative(process.cwd(), path.dirname(projectFile));
var filesGlob = invisibleFilesGlob;
var ignore = [];
if (projectSpec.filesGlob) { // for filesGlob we keep the files in sync
var relativeProjectSpec = extend(projectSpec, {
files: projectSpec.files.map(x => fsu.consistentPath(path.relative(projectFileDirectory, x))),
exclude: projectSpec.exclude.map(x => fsu.consistentPath(path.relative(projectFileDirectory, x)))
});

if (Array.isArray(projectSpec.filesGlob)) {
filesGlob = projectSpec.filesGlob.length === 1 ? projectSpec.filesGlob[0] : `{${projectSpec.filesGlob.join(',')}}`;
} else if (projectSpec.exclude) {
ignore = projectSpec.exclude.map(path => `${path}/**`)
}
var prettyJSONProjectSpec = prettyJSON(relativeProjectSpec, detectIndent(projectFileTextContent).indent);

if (filesGlob) { // Expand whatever needs expanding
try {
projectSpec.files = glob.sync(filesGlob, {
cwd: cwdPath,
ignore: ignore,
nodir: true
});
}
catch (ex) {
throw errorWithDetails<GET_PROJECT_GLOB_EXPAND_FAILED_Details>(
new Error(errors.GET_PROJECT_GLOB_EXPAND_FAILED),
{ glob: projectSpec.filesGlob, projectFilePath: fsu.consistentPath(projectFile), errorMessage: ex.message });
}
}
if (projectSpec.filesGlob) { // for filesGlob we keep the files in sync
var prettyJSONProjectSpec = prettyJSON(projectSpec);
if (prettyJSONProjectSpec !== projectFileTextContent) {
fs.writeFileSync(projectFile, prettyJSON(projectSpec));
fs.writeFileSync(projectFile, prettyJSONProjectSpec);
}
}

// Remove all relativeness
projectSpec.files = projectSpec.files.map((file) => path.resolve(projectFileDirectory, file));

var pkg: UsefulFromPackageJson = null;
try {
var packagePath = travelUpTheDirectoryTreeTillYouFind(projectFileDirectory, 'package.json');
Expand Down Expand Up @@ -703,9 +677,10 @@ function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ou
return { implicit, ours, packagejson };
}

export function prettyJSON(object: any): string {
export function prettyJSON(object: any, indent: string | number = 4): string {
var cache = [];
var value = JSON.stringify(object,
var value = JSON.stringify(
object,
// fixup circular reference
function(key, value) {
if (typeof value === 'object' && value !== null) {
Expand All @@ -718,8 +693,8 @@ export function prettyJSON(object: any): string {
}
return value;
},
// indent 4 spaces
4);
indent
);
value = value.split('\n').join(os.EOL) + os.EOL;
cache = null;
return value;
Expand Down
10 changes: 5 additions & 5 deletions lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
"./main/atom/views/plainMessageView.ts",
"./main/atom/views/projectSymbolsView.ts",
"./main/atom/views/renameView.ts",
"./main/atom/views/rView.tsx",
"./main/atom/views/semanticView.tsx",
"./main/atom/views/semanticViewGlobals.d.ts",
"./main/atom/views/simpleOverlaySelectionView.ts",
"./main/atom/views/simpleSelectionView.ts",
Expand All @@ -67,20 +65,20 @@
"./main/lang/core/project.ts",
"./main/lang/fixmyts/astUtils.ts",
"./main/lang/fixmyts/quickFix.ts",
"./main/lang/fixmyts/quickFixRegistry.ts",
"./main/lang/fixmyts/quickFixes/addClassMember.ts",
"./main/lang/fixmyts/quickFixes/addClassMethod.ts",
"./main/lang/fixmyts/quickFixes/addImportStatement.ts",
"./main/lang/fixmyts/quickFixes/equalsToEquals.ts",
"./main/lang/fixmyts/quickFixes/extractVariable.ts",
"./main/lang/fixmyts/quickFixes/implementInterface.ts",
"./main/lang/fixmyts/quickFixes/quotesToQuotes.ts",
"./main/lang/fixmyts/quickFixes/quoteToTemplate.ts",
"./main/lang/fixmyts/quickFixes/quotesToQuotes.ts",
"./main/lang/fixmyts/quickFixes/singleLineCommentToJsdoc.ts",
"./main/lang/fixmyts/quickFixes/stringConcatToTemplate.ts",
"./main/lang/fixmyts/quickFixes/typeAssertPropertyAccessToAny.ts",
"./main/lang/fixmyts/quickFixes/typeAssertPropertyAccessToType.ts",
"./main/lang/fixmyts/quickFixes/wrapInProperty.ts",
"./main/lang/fixmyts/quickFixRegistry.ts",
"./main/lang/modules/astToText.ts",
"./main/lang/modules/building.ts",
"./main/lang/modules/formatting.ts",
Expand Down Expand Up @@ -128,6 +126,8 @@
"./worker/debug.ts",
"./worker/lib/workerLib.ts",
"./worker/parent.ts",
"./worker/queryParent.ts"
"./worker/queryParent.ts",
"./main/atom/views/rView.tsx",
"./main/atom/views/semanticView.tsx"
]
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"babel": "^5.6.23",
"basarat-text-buffer": "6.0.0",
"d3": "^3.5.5",
"detect-indent": "^4.0.0",
"emissary": "^1.3.3",
"escape-html": "^1.0.1",
"fuzzaldrin": "^2.1.0",
Expand All @@ -56,7 +57,8 @@
"ntypescript": "1.201509200205.1",
"react": "^0.13.3",
"season": "^5.1.4",
"strip-bom": "^2.0.0"
"tsconfig": "^1.1.0",
"xtend": "^4.0.0"
},
"devDependencies": {},
"package-deps": [
Expand Down