Skip to content

Commit 96e04e0

Browse files
committed
buildOnSave
closes #497 closes #206
1 parent 820703b commit 96e04e0

File tree

5 files changed

+69
-40
lines changed

5 files changed

+69
-40
lines changed

dist/main/atom/onSaveHandler.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ function handle(event) {
1111
});
1212
mainPanelView_1.show();
1313
parent.getProjectFileDetails({ filePath: event.filePath }).then(function (fileDetails) {
14-
if (!fileDetails.project.compileOnSave)
15-
return;
16-
if (fileDetails.project.compilerOptions.out)
17-
return;
18-
textUpdated.then(function () { return parent.emitFile({ filePath: event.filePath }); })
19-
.then(function (res) {
20-
var status = fileStatusCache_1.getFileStatus(event.filePath);
21-
status.modified = false;
22-
status.emitDiffers = res.emitError;
23-
mainPanelView_1.panelView.updateFileStatus(event.filePath);
24-
mainPanelView_1.errorView.showEmittedMessage(res);
25-
});
14+
if (fileDetails.project.compileOnSave
15+
&& !fileDetails.project.compilerOptions.out
16+
&& !fileDetails.project.buildOnSave) {
17+
textUpdated.then(function () { return parent.emitFile({ filePath: event.filePath }); })
18+
.then(function (res) {
19+
var status = fileStatusCache_1.getFileStatus(event.filePath);
20+
status.modified = false;
21+
status.emitDiffers = res.emitError;
22+
mainPanelView_1.panelView.updateFileStatus(event.filePath);
23+
mainPanelView_1.errorView.showEmittedMessage(res);
24+
});
25+
}
26+
if (fileDetails.project.buildOnSave) {
27+
atom.commands.dispatch(atom.views.getView(event.editor), 'typescript:build');
28+
}
2629
});
2730
}
2831
exports.handle = handle;

dist/main/tsconfig/tsconfig.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ function getDefaultInMemoryProject(srcFile) {
154154
typings: typings.ours.concat(typings.implicit),
155155
formatCodeOptions: formatting.defaultFormatCodeOptions(),
156156
compileOnSave: true,
157+
buildOnSave: false,
157158
scripts: {}
158159
};
159160
return {
@@ -243,7 +244,8 @@ function getProjectSync(pathOrSrcFile) {
243244
package: pkg,
244245
typings: [],
245246
externalTranspiler: projectSpec.externalTranspiler == undefined ? undefined : projectSpec.externalTranspiler,
246-
scripts: projectSpec.scripts || {}
247+
scripts: projectSpec.scripts || {},
248+
buildOnSave: !!projectSpec.buildOnSave
247249
};
248250
var validationResult = validator.validate(projectSpec.compilerOptions);
249251
if (validationResult.errorMessage) {

docs/tsconfig.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ i.e. an empty JSON file at the *root* of your project :heart: This will be suffi
1717
* [`filesGlob`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#filesglob): To make it easier for you to just add / remove files in your project we add `filesGlob` which accepts an array of `glob / minimatch / RegExp` patterns (similar to grunt) to specify source files.
1818
* [`formatCodeOptions`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#formatcodeoptions) : Code formatting options
1919
* [`compileOnSave`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#compileonsave) : Should AtomTS compile on save
20+
* [`buildOnSave`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#buildOnSave) : Should AtomTS build on save
2021
* [`scripts`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#scripts) : Sometimes its useful to have post build scripts
2122
* [`version`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#version) : The TypeScript version
2223

@@ -90,6 +91,15 @@ Inspired by `project.json` : https://github.com/aspnet/Home/wiki/Project.json-fi
9091
}
9192
```
9293

94+
### buildOnSave
95+
Build means *compile all files*. Useful if for some reason you are using `--out`. Default is `false`. Note that build is a slow process, therefore we recommend leaving it off. But in case this is the way you want to go its there for your convenience.
96+
97+
```json
98+
{
99+
"buildOnSave": true
100+
}
101+
```
102+
93103
### version
94104
This exists simply to make it easier for the future you to know which TypeScript version was used when this project file was created. You can read more here [Microsoft/TypeScript#2113](https://github.com/Microsoft/TypeScript/issues/2133)
95105

lib/main/atom/onSaveHandler.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,28 @@ export function handle(event: { filePath: string; editor: AtomCore.IEditor }) {
3030

3131
// Compile on save
3232
parent.getProjectFileDetails({ filePath: event.filePath }).then(fileDetails => {
33-
if (!fileDetails.project.compileOnSave) return;
34-
if (fileDetails.project.compilerOptions.out) return;
35-
36-
textUpdated.then(() => parent.emitFile({ filePath: event.filePath }))
37-
.then((res) => {
38-
let status = getFileStatus(event.filePath);
39-
status.modified = false;
40-
41-
// If there was a compilation error, the file differs from the one on the disk
42-
status.emitDiffers = res.emitError;
43-
panelView.updateFileStatus(event.filePath);
44-
errorView.showEmittedMessage(res);
45-
});
33+
if (fileDetails.project.compileOnSave
34+
&& !fileDetails.project.compilerOptions.out
35+
&& !fileDetails.project.buildOnSave) {
36+
37+
textUpdated.then(() => parent.emitFile({ filePath: event.filePath }))
38+
.then((res) => {
39+
let status = getFileStatus(event.filePath);
40+
status.modified = false;
41+
42+
// If there was a compilation error, the file differs from the one on the disk
43+
status.emitDiffers = res.emitError;
44+
panelView.updateFileStatus(event.filePath);
45+
errorView.showEmittedMessage(res);
46+
});
47+
}
48+
49+
if (fileDetails.project.buildOnSave) {
50+
// Trigger a build ;)
51+
atom.commands.dispatch(
52+
atom.views.getView(event.editor),
53+
'typescript:build');
54+
}
55+
4656
});
4757
}

lib/main/tsconfig/tsconfig.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ interface TypeScriptProjectRawSpecification {
115115
filesGlob?: string[]; // optional: An array of 'glob / minimatch / RegExp' patterns to specify source files
116116
formatCodeOptions?: formatting.FormatCodeOptions; // optional: formatting options
117117
compileOnSave?: boolean; // optional: compile on save. Ignored to build tools. Used by IDEs
118+
buildOnSave?: boolean;
118119
externalTranspiler?: string;
119120
scripts?: { postbuild?: string };
120121
}
@@ -130,6 +131,7 @@ export interface TypeScriptProjectSpecification {
130131
filesGlob?: string[];
131132
formatCodeOptions: ts.FormatCodeOptions;
132133
compileOnSave: boolean;
134+
buildOnSave: boolean;
133135
package?: UsefulFromPackageJson;
134136
externalTranspiler?: string;
135137
scripts: { postbuild?: string };
@@ -299,12 +301,13 @@ export function getDefaultInMemoryProject(srcFile: string): TypeScriptProjectFil
299301
files = increaseProjectForReferenceAndImports(files);
300302
files = uniq(files.map(fsu.consistentPath));
301303

302-
let project : TypeScriptProjectSpecification = {
304+
let project: TypeScriptProjectSpecification = {
303305
compilerOptions: defaults,
304306
files,
305307
typings: typings.ours.concat(typings.implicit),
306308
formatCodeOptions: formatting.defaultFormatCodeOptions(),
307309
compileOnSave: true,
310+
buildOnSave: false,
308311
scripts: {}
309312
};
310313

@@ -417,7 +420,8 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
417420
package: pkg,
418421
typings: [],
419422
externalTranspiler: projectSpec.externalTranspiler == undefined ? undefined : projectSpec.externalTranspiler,
420-
scripts: projectSpec.scripts || {}
423+
scripts: projectSpec.scripts || {},
424+
buildOnSave: !!projectSpec.buildOnSave
421425
};
422426

423427
// Validate the raw compiler options before converting them to TS compiler options
@@ -426,7 +430,7 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
426430
throw errorWithDetails<GET_PROJECT_PROJECT_FILE_INVALID_OPTIONS_Details>(
427431
new Error(errors.GET_PROJECT_PROJECT_FILE_INVALID_OPTIONS),
428432
{ projectFilePath: fsu.consistentPath(projectFile), errorMessage: validationResult.errorMessage }
429-
);
433+
);
430434
}
431435

432436
// Convert the raw options to TS options
@@ -525,17 +529,17 @@ function increaseProjectForReferenceAndImports(files: string[]): string[] {
525529
return null;
526530
}).filter(file=> !!file)
527531
.concat(
528-
preProcessedFileInfo.importedFiles
529-
.filter((fileReference) => pathIsRelative(fileReference.fileName))
530-
.map(fileReference => {
531-
var file = path.resolve(dir, fileReference.fileName + '.ts');
532-
if (!fs.existsSync(file)) {
533-
file = path.resolve(dir, fileReference.fileName + '.d.ts');
534-
}
535-
return file;
536-
})
537-
)
538-
);
532+
preProcessedFileInfo.importedFiles
533+
.filter((fileReference) => pathIsRelative(fileReference.fileName))
534+
.map(fileReference => {
535+
var file = path.resolve(dir, fileReference.fileName + '.ts');
536+
if (!fs.existsSync(file)) {
537+
file = path.resolve(dir, fileReference.fileName + '.d.ts');
538+
}
539+
return file;
540+
})
541+
)
542+
);
539543
});
540544

541545
return selectMany(referenced);

0 commit comments

Comments
 (0)