Comparing version 0.6.7 to 0.7.0
@@ -26,5 +26,5 @@ module.exports = function (grunt) { | ||
src: ["test/work/**/*.ts"], // The source typescript files, See : http://gruntjs.com/configuring-tasks#files | ||
reference: "./test/", // If specified, generate a reference.ts file at this place | ||
out: 'test/out.js', // If specified, generate an out.js file which is the merged js file | ||
watch: 'test', // If specified, configures this target to watch the specified director for ts changes and reruns itself. | ||
reference: "./test/work", // If specified, generate a reference.ts file at this place | ||
out: 'test/work/out.js', // If specified, generate an out.js file which is the merged js file | ||
watch: 'test/work', // If specified, configures this target to watch the specified director for ts changes and reruns itself. | ||
options: { // override the main options, See : http://gruntjs.com/configuring-tasks#options | ||
@@ -31,0 +31,0 @@ sourcemap: true, |
@@ -5,3 +5,3 @@ { | ||
"description": "Compile typescript files to javascript using tsc command", | ||
"version": "0.6.7", | ||
"version": "0.7.0", | ||
"homepage": "https://github.com/basarat/grunt-ts", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -28,2 +28,3 @@ // Typescript imports | ||
// Blindly runs the tsc task using provided options | ||
function compileAllFiles(files, target, task) { | ||
@@ -91,20 +92,47 @@ var filepath = files.join(' '); | ||
this.files.forEach(function (target) { | ||
// Create a reference file | ||
// Create a reference file? | ||
var reference = target.reference; | ||
var referenceFile; | ||
var referencePath; | ||
if (!!reference) { | ||
reference = endWithSlash(reference); | ||
var contents = []; | ||
target.src.forEach(function (filename) { | ||
if (filename.indexOf('reference.ts') == -1) | ||
contents.push('/// <reference path="' + path.relative(reference, filename).split('\\').join('/') + '" />'); | ||
}); | ||
fs.writeFileSync(reference + '/reference.ts', contents.join(eol)); | ||
referencePath = path.resolve(reference); | ||
referenceFile = path.resolve(referencePath, 'reference.ts'); | ||
} | ||
function isReferenceFile(filename) { | ||
return path.resolve(filename) == referenceFile; | ||
} | ||
// Create an output file? | ||
var out = target.out; | ||
var outFile; | ||
var outFile_d_ts; | ||
if (!!out) { | ||
outFile = path.resolve(out); | ||
outFile_d_ts = outFile.replace('.js', '.d.ts'); | ||
} | ||
function isOutFile(filename) { | ||
return path.resolve(filename) == outFile_d_ts; | ||
} | ||
// Compiles all the files | ||
// Uses the blind tsc compile task | ||
// Creates custom files | ||
// logs errors | ||
// Time the whole process | ||
function runCompilation(files) { | ||
grunt.log.writeln('Compiling.'.yellow); | ||
// Customize the targets based on file contents | ||
// TODO: Idea: Customize the targets based on file contents | ||
// Time the task and go | ||
var starttime = new Date().getTime(); | ||
if (!!referencePath) { | ||
var contents = []; | ||
files.forEach(function (filename) { | ||
contents.push('/// <reference path="' + path.relative(referencePath, filename).split('\\').join('/') + '" />'); | ||
}); | ||
fs.writeFileSync(referenceFile, contents.join(eol)); | ||
} | ||
// Compile all the files | ||
var result = compileAllFiles(files, target, options); | ||
@@ -121,4 +149,22 @@ if (result.code != 0) { | ||
} | ||
runCompilation(target.src); | ||
// Find out which files to compile | ||
// Then calls the compile function on those files | ||
// Also this funciton is debounced | ||
var debouncedCompile = _.debounce(function () { | ||
// Reexpand the original file glob: | ||
var files = grunt.file.expand(currenttask.data.src); | ||
// Clear the files of output.d.ts and reference.ts | ||
files = _.filter(files, function (filename) { | ||
return (!isReferenceFile(filename) && !isOutFile(filename)); | ||
}); | ||
// compile | ||
runCompilation(files); | ||
}, 150); | ||
// Initial compilation: | ||
debouncedCompile(); | ||
// Watches all the files | ||
@@ -140,13 +186,5 @@ watch = target.watch; | ||
var debouncedCompile = _.debounce(function () { | ||
// Reexpand the original file glob: | ||
var files = grunt.file.expand(currenttask.data.src); | ||
// compile | ||
runCompilation(files); | ||
}, 150); | ||
// local event to handle file event | ||
function handleFileEvent(filepath, displaystr) { | ||
if (target.out && endsWith(filepath, '.d.ts')) { | ||
if (isOutFile(filepath) || isReferenceFile(filepath)) { | ||
return; | ||
@@ -153,0 +191,0 @@ } |
100
tasks/ts.ts
@@ -48,4 +48,4 @@ /// <reference path="../defs/grunt/grunt.d.ts"/> | ||
eol = require('os').EOL; | ||
function resolveTypeScriptBinPath(currentPath, depth): string { | ||
@@ -73,2 +73,3 @@ var targetPath = path.resolve(__dirname, | ||
// Blindly runs the tsc task using provided options | ||
function compileAllFiles(files: string[], target: ITargetOptions, task: ITaskOptions): ICompileResult { | ||
@@ -106,4 +107,4 @@ | ||
}; | ||
function endWithSlash(path: string): string { | ||
if (!endsWith(path, '/') && !endsWith(path,'\\')) { | ||
function endWithSlash(path: string): string { | ||
if (!endsWith(path, '/') && !endsWith(path, '\\')) { | ||
return path + '/'; | ||
@@ -113,4 +114,4 @@ } | ||
} | ||
grunt.registerMultiTask('ts', 'Compile TypeScript files', function () { | ||
@@ -145,22 +146,49 @@ | ||
// Create a reference file | ||
// Create a reference file? | ||
var reference = target.reference; | ||
var referenceFile; | ||
var referencePath; | ||
if (!!reference) { | ||
reference = endWithSlash(reference); // probably not required | ||
var contents = []; | ||
target.src.forEach((filename: string) => { | ||
// do not add a reference to reference: | ||
if (filename.indexOf('reference.ts') == -1) | ||
contents.push('/// <reference path="' + path.relative(reference, filename).split('\\').join('/') + '" />') | ||
}); | ||
fs.writeFileSync(reference + '/reference.ts', contents.join(eol)); | ||
referencePath = path.resolve(reference); | ||
referenceFile = path.resolve(referencePath, 'reference.ts'); | ||
} | ||
function isReferenceFile(filename: string) { | ||
return path.resolve(filename) == referenceFile; | ||
} | ||
// Create an output file? | ||
var out = target.out; | ||
var outFile; | ||
var outFile_d_ts; | ||
if (!!out) { | ||
outFile = path.resolve(out); | ||
outFile_d_ts = outFile.replace('.js', '.d.ts'); | ||
} | ||
function isOutFile(filename: string): boolean { | ||
return path.resolve(filename) == outFile_d_ts; | ||
} | ||
// Compiles all the files | ||
// Uses the blind tsc compile task | ||
// Creates custom files | ||
// logs errors | ||
// Time the whole process | ||
function runCompilation(files) { | ||
grunt.log.writeln('Compiling.'.yellow); | ||
// Customize the targets based on file contents | ||
// TODO: Idea: Customize the targets based on file contents | ||
// Time the task and go | ||
var starttime = new Date().getTime(); | ||
// Create a reference file if specified | ||
if (!!referencePath) { | ||
var contents = []; | ||
files.forEach((filename: string) => { | ||
contents.push('/// <reference path="' + path.relative(referencePath, filename).split('\\').join('/') + '" />'); | ||
}); | ||
fs.writeFileSync(referenceFile, contents.join(eol)); | ||
} | ||
// Compile all the files | ||
var result = compileAllFiles(files, target, options); | ||
@@ -174,8 +202,26 @@ if (result.code != 0) { | ||
var endtime = new Date().getTime(); | ||
var time = (endtime - starttime)/1000; | ||
grunt.log.writeln(('Success: '+time.toFixed(2) +'s for '+files.length + ' typescript files').green); | ||
var time = (endtime - starttime) / 1000; | ||
grunt.log.writeln(('Success: ' + time.toFixed(2) + 's for ' + files.length + ' typescript files').green); | ||
} | ||
} | ||
runCompilation(target.src); | ||
} | ||
// Find out which files to compile | ||
// Then calls the compile function on those files | ||
// Also this funciton is debounced | ||
var debouncedCompile = _.debounce(() => { | ||
// Reexpand the original file glob: | ||
var files = grunt.file.expand(currenttask.data.src); | ||
// Clear the files of output.d.ts and reference.ts | ||
files = _.filter(files, (filename) => { | ||
return (!isReferenceFile(filename) && !isOutFile(filename)); | ||
}); | ||
// compile | ||
runCompilation(files); | ||
}, 150); // randomly chosen 150. Choice was made because chokidar looks at file system every 100ms | ||
// Initial compilation: | ||
debouncedCompile(); | ||
// Watches all the files | ||
@@ -195,21 +241,13 @@ watch = target.watch; | ||
var chokidar = require('chokidar'); | ||
var watcher = chokidar.watch(watchpath, { ignoreInitial: true, persistent: true }); | ||
var watcher = chokidar.watch(watchpath, { ignoreInitial: true, persistent: true }); | ||
var debouncedCompile = _.debounce(() => { | ||
// Reexpand the original file glob: | ||
var files = grunt.file.expand(currenttask.data.src); | ||
// compile | ||
runCompilation(files); | ||
},150); // randomly 150 as chokidar looks at file system every 100ms | ||
// local event to handle file event | ||
function handleFileEvent(filepath: string, displaystr: string) { | ||
// Ignore the special case for generated out.d.ts :) | ||
if (target.out && endsWith(filepath,'.d.ts')) { | ||
// Ignore the special cases for files we generate | ||
if (isOutFile(filepath) || isReferenceFile(filepath)) { | ||
return; | ||
} | ||
if (!endsWith(filepath,'.ts')) { // should not happen | ||
if (!endsWith(filepath, '.ts')) { // should not happen | ||
return; | ||
} | ||
} | ||
@@ -216,0 +254,0 @@ grunt.log.writeln((displaystr + ' >>' + filepath).yellow); |
@@ -7,2 +7,2 @@ module Simple1 { | ||
Simple1.main(); | ||
Simple1.main(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58197
32
952