Skip to content

Commit 13be649

Browse files
author
Claus Reinke
committed
add configuration support (closes #44);bump version (#39)
1 parent 1fad764 commit 13be649

File tree

9 files changed

+193
-63
lines changed

9 files changed

+193
-63
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bin/tss.js: tss.ts harness.ts $(TYPESCRIPT)/bin/typescript.d.ts $(TYPESCRIPT)/bi
1414
bin/lib.d.ts: $(TYPESCRIPT)/bin/lib.d.ts
1515
cp $(TYPESCRIPT)/bin/lib.d.ts bin/lib.d.ts
1616

17-
tests/script.out2: $(TEST_SCRIPTS) $(TEST_SOURCES) tests/script.js tests/script.out bin/tss.js bin/lib.d.ts
17+
tests/script.out2: $(TEST_SCRIPTS) $(TEST_SOURCES) tests/tsconfig.json tests/script.js tests/script.out bin/tss.js bin/lib.d.ts
1818
cd tests;\
1919
node script.js >script.out2;
2020

README.md

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ or via the npm registry (the newest version isn't there yet, waiting for post-1.
2929
$ npm install -g typescript-tools
3030
```
3131

32+
From-source compilation should not be necessary, as a pre-compiled `bin/tss.js` is included, as well as a `bin/lib.d.ts`. But if you want to rebuild and test tss, you can run `make` in `typescript-tools`.
33+
3234
The installation should give you a global `tss` command, which you can use directly, as in this sample session (note that the absolute paths will differ in your installation):
3335

3436
```
@@ -50,36 +52,11 @@ The installation should give you a global `tss` command, which you can use direc
5052

5153
If you want to use tss from Vim, add the `typescript-tools` directory to your Vim's `rtp`. If you want to use this from other editors/IDEs, you will need to write some code, to communicate with `tss` as an asynchronous subprocess (please let me know how it goes, especially if you release a working plugin).
5254

53-
From-source compilation should not be necessary, as a pre-compiled `bin/tss.js` is included, as well as a `bin/lib.d.ts`. You might want to modify `bin/defaultLibs.d.ts`, if you want other declaration files included by default. (TODO: get rid of defaultLibs)
54-
55-
If you do want to compile from source:
56-
57-
```
58-
# install git and node/npm, then
59-
$ git clone https://github.com/Microsoft/TypeScript.git
60-
$ git clone git://github.com/clausreinke/typescript-tools.git
61-
$ cd typescript-tools
62-
$ npm install ../typescript
63-
$ make
64-
```
65-
66-
Alternatively, you can let npm handle the cloning of the typescript dependency:
67-
68-
```
69-
# install git and node/npm, then
70-
$ git clone git://github.com/clausreinke/typescript-tools.git
71-
$ cd typescript-tools
72-
$ npm install
73-
$ make
74-
```
75-
76-
The latter works better at the moment since we currently depend directly on the github version of the typescript package. The former gives you control over where to put the typescript clone, and how much to clone (try --depth 1), once we can drop the github dependency again.
77-
7855
TypeScript tools currently available:
7956

8057
## tss.ts: TypeScript Services Server
8158

82-
Simple commandline interface (commands in, info out) to TypeScript Services. Currently supported commands (with indication of purpose and output format) include:
59+
Simple commandline interface (commands in, info out) to TypeScript Services. Currently supported commands (with indication of purpose and output format) include:
8360

8461
```
8562
type <line> <pos> <file>
@@ -143,7 +120,7 @@ TypeScript tools currently available:
143120
[{ info: string
144121
, min: { line: number, character: number }
145122
, lim: { line: number, character: number }
146-
, childItems: <recursive>
123+
, childItems: <recursive structure>
147124
}]
148125
149126
showErrors
@@ -165,11 +142,47 @@ TypeScript tools currently available:
165142
"TSS closing"
166143
```
167144

168-
Start `tss` with project root file - may take several seconds to load
169-
all dependencies; then enter commands and get JSON info or error messages
170-
(NOTE: commands take absolute file paths, adjust example to your installation);
171-
for a sample session, see `tests/` (commands in `test.script`, output in `script.out`).
172-
145+
Start `tss` with project root file - may take several seconds to load all
146+
dependencies for larger projects; then enter commands and get JSON info or
147+
error messages.
148+
149+
### configuration: tsconfig.json or commandline options
150+
151+
tss can now be configured the same way as tsc, either via commandline options or
152+
via tsconfig.json files (since about TSv1.5). In both cases, only options that affect
153+
the language service have any effect. As a simple example, loading sources with
154+
external modules generates errors
155+
156+
```
157+
$ echo showErrors | bin/tss tests/issue-17.ts
158+
"loaded c:/javascript/typescript/github/typescript-tools/tests/issue-17.ts, TSS
159+
listening.."
160+
showErrors
161+
[{"file":"c:/javascript/typescript/github/typescript-tools/tests/issue-17-import
162+
.ts","start":{"line":1,"character":14},"end":{"line":1,"character":18},"text":"C
163+
annot compile external modules unless the '--module' flag is provided.","code":1
164+
148,"phase":"Syntax","category":"Error"}]
165+
```
166+
167+
unless a module system is selected:
168+
169+
```
170+
$ echo showErrors | bin/tss --module commonjs tests/issue-17.ts
171+
"loaded c:/javascript/typescript/github/typescript-tools/tests/issue-17.ts, TSS
172+
listening.."
173+
showErrors
174+
[]
175+
176+
$ echo showErrors | bin/tss --project tests/ tests/issue-17.ts
177+
"loaded c:/javascript/typescript/github/typescript-tools/tests/issue-17.ts, TSS
178+
listening.."
179+
showErrors
180+
[]
181+
182+
$ cat tests/tsconfig.json
183+
{"compilerOptions": {"target":"ES5","module":"commonjs"} }
184+
185+
```
173186

174187
## vim interface to tss.js
175188

bin/harness.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ var ScriptInfo = (function () {
4747
this.setContent(content);
4848
this.editRanges.push({
4949
length: content.length,
50-
textChangeRange: ts.createTextChangeRange(ts.createTextSpan(0, old_length), content.length)
50+
textChangeRange:
51+
// NOTE: no shortcut for "update everything" (null only works in some places, #10)
52+
ts.createTextChangeRange(ts.createTextSpan(0, old_length), content.length)
5153
});
5254
this.version++;
5355
};

bin/tss.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ var TSS = (function () {
136136
return errors;
137137
};
138138
/** load file and dependencies, prepare language service for queries */
139-
TSS.prototype.setup = function (file) {
139+
TSS.prototype.setup = function (file, options) {
140140
var _this = this;
141141
this.rootFile = this.resolveRelativePath(file);
142-
this.compilerOptions = ts.getDefaultCompilerOptions();
143-
this.compilerOptions.diagnostics = true;
144-
this.compilerOptions.target = 1 /* ES5 */;
142+
this.compilerOptions = options;
143+
// this.compilerOptions.diagnostics = true;
144+
// this.compilerOptions.target = ts.ScriptTarget.ES5;
145+
// this.compilerOptions.module = ts.ModuleKind.CommonJS;
145146
this.fileNameToContent = {};
146147
// build program from root file,
147148
// chase dependencies (references and imports), normalize file names, ...
@@ -328,7 +329,8 @@ var TSS = (function () {
328329
var endLine = parseInt(m[5]);
329330
var maxLines = script.lineMap.length;
330331
var startPos = startLine <= maxLines ? (startLine < 1 ? 0 : _this.lineColToPosition(file, startLine, 1)) : script.content.length;
331-
var endPos = endLine < maxLines ? (endLine < 1 ? 0 : _this.lineColToPosition(file, endLine + 1, 0) - 1) : script.content.length;
332+
var endPos = endLine < maxLines ? (endLine < 1 ? 0 : _this.lineColToPosition(file, endLine + 1, 0) - 1) //??CHECK
333+
: script.content.length;
332334
_this.editScript(file, startPos, endPos, lines.join(EOL));
333335
}
334336
var syn, sem;
@@ -393,7 +395,7 @@ var TSS = (function () {
393395
}
394396
else if (m = match(cmd, /^reload$/)) {
395397
// TODO: keep updated (in-memory-only) files?
396-
_this.setup(_this.rootFile);
398+
_this.setup(_this.rootFile, _this.compilerOptions);
397399
_this.outputJSON('"reloaded ' + _this.rootFile + ', TSS listening.."');
398400
}
399401
else if (m = match(cmd, /^quit$/)) {
@@ -421,10 +423,60 @@ var TSS = (function () {
421423
};
422424
return TSS;
423425
})();
424-
if (ts.sys.args.indexOf("--version") !== -1) {
426+
// from src/compiler/tsc.ts - not yet exported from there:-(
427+
function findConfigFile() {
428+
var searchPath = ts.normalizePath(ts.sys.getCurrentDirectory());
429+
var filename = "tsconfig.json";
430+
while (true) {
431+
if (ts.sys.fileExists(filename)) {
432+
return filename;
433+
}
434+
var parentPath = ts.getDirectoryPath(searchPath);
435+
if (parentPath === searchPath) {
436+
break;
437+
}
438+
searchPath = parentPath;
439+
filename = "../" + filename;
440+
}
441+
return undefined;
442+
}
443+
var arg;
444+
var configFile, configObject, configObjectParsed;
445+
// NOTE: partial options support only
446+
var commandLine = ts.parseCommandLine(ts.sys.args);
447+
if (commandLine.options.version) {
425448
console.log(require("../package.json").version);
426449
process.exit(0);
427450
}
451+
if (commandLine.options.project) {
452+
configFile = ts.normalizePath(ts.combinePaths(commandLine.options.project, "tsconfig.json"));
453+
}
454+
else if (commandLine.filenames.length === 0) {
455+
configFile = findConfigFile();
456+
if (!configFile) {
457+
console.error("can't find project root");
458+
console.error("please specify root source file");
459+
console.error(" or --project directory (containing a tsconfig.json)");
460+
process.exit(1);
461+
}
462+
}
463+
var options;
464+
if (configFile) {
465+
configObject = ts.readConfigFile(configFile);
466+
if (!configObject) {
467+
console.error("can't read tsconfig.json at", configFile);
468+
process.exit(1);
469+
}
470+
configObjectParsed = ts.parseConfigFile(configObject, ts.getDirectoryPath(configFile));
471+
if (configObjectParsed.errors.length > 0) {
472+
console.error(configObjectParsed.errors);
473+
process.exit(1);
474+
}
475+
options = ts.extend(commandLine.options, configObjectParsed.options);
476+
}
477+
else {
478+
options = ts.extend(commandLine.options, ts.getDefaultCompilerOptions());
479+
}
428480
var tss = new TSS();
429-
tss.setup(ts.sys.args[0]);
481+
tss.setup(commandLine.filenames[0], options);
430482
tss.listen();

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-tools",
3-
"version": "v0.4.2-testing-ts1.4",
3+
"version": "v0.4.3-testing-ts1.4",
44
"description": "TypeScript Services commandline server",
55
"bin": {
66
"tss": "bin/tss"
@@ -18,5 +18,7 @@
1818
"preferGlobal": true,
1919
"author": "Claus Reinke",
2020
"license": "Apache License, Version 2.0",
21-
"dependencies": "git+ssh://[email protected]:Microsoft/TypeScript.git"
21+
"dependencies": {
22+
"typescript": "git+ssh://[email protected]:Microsoft/TypeScript.git"
23+
}
2224
}

tests/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function test(scriptName,fileName) {
1414
var script = fs.readFileSync(scriptName,"utf8")
1515
.replace(/PREFIX/g,PREFIX);
1616

17-
var cmd = "node "+tss_path+" "+fileName;
17+
var cmd = "node "+tss_path+" --project ."+" "+fileName;
1818
tests.push(scriptName);
1919
log[scriptName] = ["// "+scriptName,cmd];
2020

tests/script.out

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// test.script
2-
node ../bin/tss.js test.ts
2+
node ../bin/tss.js --project . test.ts
33
// stdout
44
"loaded PREFIX/test.ts, TSS listening.."
55
[{"file":"PREFIX/test.ts"
@@ -525,7 +525,7 @@ var a = [];
525525
// stderr
526526

527527
// issue-9.script
528-
node ../bin/tss.js empty.ts
528+
node ../bin/tss.js --project . empty.ts
529529
// stdout
530530
"loaded PREFIX/empty.ts, TSS listening.."
531531
"updated PREFIX/empty.ts, (1/0) errors"
@@ -555,7 +555,7 @@ node ../bin/tss.js empty.ts
555555
// stderr
556556

557557
// issue-10.script
558-
node ../bin/tss.js empty.ts
558+
node ../bin/tss.js --project . empty.ts
559559
// stdout
560560
"loaded PREFIX/empty.ts, TSS listening.."
561561
"updated PREFIX/empty.ts, (0/0) errors"
@@ -576,7 +576,7 @@ node ../bin/tss.js empty.ts
576576
// stderr
577577

578578
// issue-11.script
579-
node ../bin/tss.js empty.ts
579+
node ../bin/tss.js --project . empty.ts
580580
// stdout
581581
"loaded PREFIX/empty.ts, TSS listening.."
582582
"updated PREFIX/empty.ts, (1/1) errors"
@@ -603,7 +603,7 @@ node ../bin/tss.js empty.ts
603603
// stderr
604604

605605
// issue-12.script
606-
node ../bin/tss.js empty.ts
606+
node ../bin/tss.js --project . empty.ts
607607
// stdout
608608
"loaded PREFIX/empty.ts, TSS listening.."
609609
"updated PREFIX/empty.ts, (0/0) errors"
@@ -613,7 +613,7 @@ null
613613
// stderr
614614

615615
// issue-13.script
616-
node ../bin/tss.js empty.ts
616+
node ../bin/tss.js --project . empty.ts
617617
// stdout
618618
"loaded PREFIX/empty.ts, TSS listening.."
619619
"updated PREFIX/empty.ts, (0/1) errors"
@@ -677,7 +677,7 @@ node ../bin/tss.js empty.ts
677677
// stderr
678678

679679
// partial-update.script
680-
node ../bin/tss.js empty.ts
680+
node ../bin/tss.js --project . empty.ts
681681
// stdout
682682
"loaded PREFIX/empty.ts, TSS listening.."
683683
"updated PREFIX/empty.ts, (0/0) errors"
@@ -732,7 +732,7 @@ bozo;
732732
// stderr
733733

734734
// update-nocheck-completion-chain.script
735-
node ../bin/tss.js empty.ts
735+
node ../bin/tss.js --project . empty.ts
736736
// stdout
737737
"loaded PREFIX/empty.ts, TSS listening.."
738738
"updated PREFIX/empty.ts"
@@ -795,7 +795,7 @@ node ../bin/tss.js empty.ts
795795
// stderr
796796

797797
// issue-15.script
798-
node ../bin/tss.js issue-15.ts
798+
node ../bin/tss.js --project . issue-15.ts
799799
// stdout
800800
"loaded PREFIX/issue-15.ts, TSS listening.."
801801
"updated PREFIX/issue-15.ts"
@@ -859,7 +859,7 @@ node ../bin/tss.js issue-15.ts
859859
// stderr
860860

861861
// issue-17.script
862-
node ../bin/tss.js issue-17.ts
862+
node ../bin/tss.js --project . issue-17.ts
863863
// stdout
864864
"loaded PREFIX/issue-17.ts, TSS listening.."
865865
[{"ref":{"fileName":"PREFIX/issue-17-import.ts"
@@ -903,7 +903,7 @@ node ../bin/tss.js issue-17.ts
903903
// stderr
904904

905905
// concat-map.script
906-
node ../bin/tss.js empty.ts
906+
node ../bin/tss.js --project . empty.ts
907907
// stdout
908908
"loaded PREFIX/empty.ts, TSS listening.."
909909
"updated PREFIX/empty.ts, (0/0) errors"

tests/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"compilerOptions": {"target":"ES5","module":"commonjs"} }

0 commit comments

Comments
 (0)