Skip to content

Commit

Permalink
buildOnSave
Browse files Browse the repository at this point in the history
closes #497 closes #206
  • Loading branch information
basarat committed Jul 27, 2015
1 parent 820703b commit 96e04e0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 40 deletions.
27 changes: 15 additions & 12 deletions dist/main/atom/onSaveHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ function handle(event) {
});
mainPanelView_1.show();
parent.getProjectFileDetails({ filePath: event.filePath }).then(function (fileDetails) {
if (!fileDetails.project.compileOnSave)
return;
if (fileDetails.project.compilerOptions.out)
return;
textUpdated.then(function () { return parent.emitFile({ filePath: event.filePath }); })
.then(function (res) {
var status = fileStatusCache_1.getFileStatus(event.filePath);
status.modified = false;
status.emitDiffers = res.emitError;
mainPanelView_1.panelView.updateFileStatus(event.filePath);
mainPanelView_1.errorView.showEmittedMessage(res);
});
if (fileDetails.project.compileOnSave
&& !fileDetails.project.compilerOptions.out
&& !fileDetails.project.buildOnSave) {
textUpdated.then(function () { return parent.emitFile({ filePath: event.filePath }); })
.then(function (res) {
var status = fileStatusCache_1.getFileStatus(event.filePath);
status.modified = false;
status.emitDiffers = res.emitError;
mainPanelView_1.panelView.updateFileStatus(event.filePath);
mainPanelView_1.errorView.showEmittedMessage(res);
});
}
if (fileDetails.project.buildOnSave) {
atom.commands.dispatch(atom.views.getView(event.editor), 'typescript:build');
}
});
}
exports.handle = handle;
4 changes: 3 additions & 1 deletion dist/main/tsconfig/tsconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function getDefaultInMemoryProject(srcFile) {
typings: typings.ours.concat(typings.implicit),
formatCodeOptions: formatting.defaultFormatCodeOptions(),
compileOnSave: true,
buildOnSave: false,
scripts: {}
};
return {
Expand Down Expand Up @@ -243,7 +244,8 @@ function getProjectSync(pathOrSrcFile) {
package: pkg,
typings: [],
externalTranspiler: projectSpec.externalTranspiler == undefined ? undefined : projectSpec.externalTranspiler,
scripts: projectSpec.scripts || {}
scripts: projectSpec.scripts || {},
buildOnSave: !!projectSpec.buildOnSave
};
var validationResult = validator.validate(projectSpec.compilerOptions);
if (validationResult.errorMessage) {
Expand Down
10 changes: 10 additions & 0 deletions docs/tsconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ i.e. an empty JSON file at the *root* of your project :heart: This will be suffi
* [`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.
* [`formatCodeOptions`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#formatcodeoptions) : Code formatting options
* [`compileOnSave`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#compileonsave) : Should AtomTS compile on save
* [`buildOnSave`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#buildOnSave) : Should AtomTS build on save
* [`scripts`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#scripts) : Sometimes its useful to have post build scripts
* [`version`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#version) : The TypeScript version

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

### buildOnSave
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.

```json
{
"buildOnSave": true
}
```

### version
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)

Expand Down
36 changes: 23 additions & 13 deletions lib/main/atom/onSaveHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,28 @@ export function handle(event: { filePath: string; editor: AtomCore.IEditor }) {

// Compile on save
parent.getProjectFileDetails({ filePath: event.filePath }).then(fileDetails => {
if (!fileDetails.project.compileOnSave) return;
if (fileDetails.project.compilerOptions.out) return;

textUpdated.then(() => parent.emitFile({ filePath: event.filePath }))
.then((res) => {
let status = getFileStatus(event.filePath);
status.modified = false;

// If there was a compilation error, the file differs from the one on the disk
status.emitDiffers = res.emitError;
panelView.updateFileStatus(event.filePath);
errorView.showEmittedMessage(res);
});
if (fileDetails.project.compileOnSave
&& !fileDetails.project.compilerOptions.out
&& !fileDetails.project.buildOnSave) {

textUpdated.then(() => parent.emitFile({ filePath: event.filePath }))
.then((res) => {
let status = getFileStatus(event.filePath);
status.modified = false;

// If there was a compilation error, the file differs from the one on the disk
status.emitDiffers = res.emitError;
panelView.updateFileStatus(event.filePath);
errorView.showEmittedMessage(res);
});
}

if (fileDetails.project.buildOnSave) {
// Trigger a build ;)
atom.commands.dispatch(
atom.views.getView(event.editor),
'typescript:build');
}

});
}
32 changes: 18 additions & 14 deletions lib/main/tsconfig/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ interface TypeScriptProjectRawSpecification {
filesGlob?: string[]; // optional: An array of 'glob / minimatch / RegExp' patterns to specify source files
formatCodeOptions?: formatting.FormatCodeOptions; // optional: formatting options
compileOnSave?: boolean; // optional: compile on save. Ignored to build tools. Used by IDEs
buildOnSave?: boolean;
externalTranspiler?: string;
scripts?: { postbuild?: string };
}
Expand All @@ -130,6 +131,7 @@ export interface TypeScriptProjectSpecification {
filesGlob?: string[];
formatCodeOptions: ts.FormatCodeOptions;
compileOnSave: boolean;
buildOnSave: boolean;
package?: UsefulFromPackageJson;
externalTranspiler?: string;
scripts: { postbuild?: string };
Expand Down Expand Up @@ -299,12 +301,13 @@ export function getDefaultInMemoryProject(srcFile: string): TypeScriptProjectFil
files = increaseProjectForReferenceAndImports(files);
files = uniq(files.map(fsu.consistentPath));

let project : TypeScriptProjectSpecification = {
let project: TypeScriptProjectSpecification = {
compilerOptions: defaults,
files,
typings: typings.ours.concat(typings.implicit),
formatCodeOptions: formatting.defaultFormatCodeOptions(),
compileOnSave: true,
buildOnSave: false,
scripts: {}
};

Expand Down Expand Up @@ -417,7 +420,8 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
package: pkg,
typings: [],
externalTranspiler: projectSpec.externalTranspiler == undefined ? undefined : projectSpec.externalTranspiler,
scripts: projectSpec.scripts || {}
scripts: projectSpec.scripts || {},
buildOnSave: !!projectSpec.buildOnSave
};

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

// Convert the raw options to TS options
Expand Down Expand Up @@ -525,17 +529,17 @@ function increaseProjectForReferenceAndImports(files: string[]): string[] {
return null;
}).filter(file=> !!file)
.concat(
preProcessedFileInfo.importedFiles
.filter((fileReference) => pathIsRelative(fileReference.fileName))
.map(fileReference => {
var file = path.resolve(dir, fileReference.fileName + '.ts');
if (!fs.existsSync(file)) {
file = path.resolve(dir, fileReference.fileName + '.d.ts');
}
return file;
})
)
);
preProcessedFileInfo.importedFiles
.filter((fileReference) => pathIsRelative(fileReference.fileName))
.map(fileReference => {
var file = path.resolve(dir, fileReference.fileName + '.ts');
if (!fs.existsSync(file)) {
file = path.resolve(dir, fileReference.fileName + '.d.ts');
}
return file;
})
)
);
});

return selectMany(referenced);
Expand Down

0 comments on commit 96e04e0

Please sign in to comment.