Comparing version 0.7.2 to 0.9.0
module.exports = function (grunt) { | ||
"use strict"; | ||
// load all grunt tasks | ||
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); | ||
grunt.initConfig({ | ||
@@ -25,4 +22,3 @@ | ||
dev: { // a particular target | ||
src: ["test/work/**/*.ts"], // The source typescript files, See : http://gruntjs.com/configuring-tasks#files | ||
reference: "./test/work", // If specified, generate a reference.ts file at this place | ||
src: ["test/work/**/*.ts"], // The source typescript files, See : http://gruntjs.com/configuring-tasks#files | ||
out: 'test/work/out.js', // If specified, generate an out.js file which is the merged js file | ||
@@ -43,3 +39,3 @@ watch: 'test/work', // If specified, configures this target to watch the specified director for ts changes and reruns itself. | ||
src: ['test/abtest/**/*.ts'], | ||
reference: 'test/abtest', | ||
reference: 'test/abtest/reference.ts', | ||
out: 'test/abtest/out.js', | ||
@@ -46,0 +42,0 @@ watch: 'test/abtest' |
{ | ||
"author": "basarat", | ||
"name": "grunt-ts", | ||
"description": "Compile typescript files to javascript using tsc command", | ||
"version": "0.7.2", | ||
"description": "Compile and manage your complete typescript workflow", | ||
"version": "0.9.0", | ||
"homepage": "https://github.com/basarat/grunt-ts", | ||
@@ -28,5 +28,6 @@ "repository": { | ||
"grunt-lib-contrib": "0.5.2", | ||
"shelljs":"0.1.4", | ||
"shelljs": "0.1.4", | ||
"chokidar": "0.6.2", | ||
"underscore": "1.5.1" | ||
"underscore": "1.5.1", | ||
"underscore.string": "2.3.3" | ||
}, | ||
@@ -33,0 +34,0 @@ "devDependencies": { |
@@ -67,3 +67,3 @@ grunt-ts | ||
src: ["test/work/**/*.ts"], // The source typescript files, http://gruntjs.com/configuring-tasks#files | ||
reference: "./test/", // If specified, generate a reference.ts file at this place | ||
reference: "./test/reference.ts", // If specified, generate this file that you can use for your reference management | ||
out: 'test/out.js', // If specified, generate an out.js file which is the merged js file | ||
@@ -70,0 +70,0 @@ watch: 'test', // If specified, configures this target to watch the specified director for ts changes and reruns itself. |
135
tasks/ts.js
@@ -0,3 +1,23 @@ | ||
// General util functions | ||
function insertArrayAt(array, index, arrayToInsert) { | ||
Array.prototype.splice.apply(array, [index, 0].concat(arrayToInsert)); | ||
return array; | ||
} | ||
// Useful string functions | ||
// used to make sure string ends with a slash | ||
function endsWith(str, suffix) { | ||
return str.indexOf(suffix, str.length - suffix.length) !== -1; | ||
} | ||
; | ||
function endWithSlash(path) { | ||
if (!endsWith(path, '/') && !endsWith(path, '\\')) { | ||
return path + '/'; | ||
} | ||
return path; | ||
} | ||
// Typescript imports | ||
var _ = require('underscore'); | ||
var _str = require('underscore.string'); | ||
var path = require('path'); | ||
@@ -7,6 +27,7 @@ var fs = require('fs'); | ||
// plain vanilla imports | ||
var shell = require('shelljs'); | ||
var eol = os.EOL; | ||
function pluginFn(grunt) { | ||
// plain vanilla imports | ||
var shell = require('shelljs'), eol = os.EOL; | ||
function resolveTypeScriptBinPath(currentPath, depth) { | ||
@@ -53,2 +74,5 @@ var targetPath = path.resolve(__dirname, (new Array(depth + 1)).join("../../"), "../node_modules/typescript/bin"); | ||
} | ||
// To debug the tsc command | ||
//console.log(cmd); | ||
var result = exec(cmd); | ||
@@ -58,13 +82,70 @@ return result; | ||
// Useful string functions | ||
// used to make sure string ends with a slash | ||
function endsWith(str, suffix) { | ||
return str.indexOf(suffix, str.length - suffix.length) !== -1; | ||
} | ||
; | ||
function endWithSlash(path) { | ||
if (!endsWith(path, '/') && !endsWith(path, '\\')) { | ||
return path + '/'; | ||
// Updates the reference file | ||
function updateReferenceFile(files, referenceFile, referencePath) { | ||
var referenceIntro = '/// <reference path="'; | ||
var referenceEnd = '" />'; | ||
var referenceMatch = /\/\/\/ <reference path=\"(.*?)\"/; | ||
var ourSignatureStart = '//grunt-start'; | ||
var ourSignatureEnd = '//grunt-end'; | ||
var origFileLines = []; | ||
var origFileReferences = []; | ||
// Location of our generated references | ||
// By default at start of file | ||
var signatureSectionPosition = 0; | ||
if (fs.existsSync(referenceFile)) { | ||
var lines = fs.readFileSync(referenceFile).toString().split('\n'); | ||
var inSignatureSection = false; | ||
// By default at end of file | ||
signatureSectionPosition = lines.length; | ||
for (var i = 0; i < lines.length; i++) { | ||
var line = _str.trim(lines[i]); | ||
if (_str.include(line, ourSignatureStart)) { | ||
//Wait for the end signature: | ||
signatureSectionPosition = i; | ||
inSignatureSection = true; | ||
continue; | ||
} | ||
if (_str.include(line, ourSignatureEnd)) { | ||
inSignatureSection = false; | ||
continue; | ||
} | ||
if (inSignatureSection) | ||
continue; | ||
// store the line | ||
origFileLines.push(line); | ||
if (_str.include(line, referenceIntro)) { | ||
var match = line.match(referenceMatch); | ||
var filename = match[1]; | ||
origFileReferences.push(filename); | ||
} | ||
} | ||
} | ||
return path; | ||
var contents = [ourSignatureStart]; | ||
files.forEach(function (filename) { | ||
// The file we are about to add | ||
var filepath = path.relative(referencePath, filename).split('\\').join('/'); | ||
if (origFileReferences.length) { | ||
if (_.contains(origFileReferences, filepath)) { | ||
return; | ||
} | ||
} | ||
// Finally add the filepath | ||
contents.push(referenceIntro + filepath + referenceEnd); | ||
}); | ||
contents.push(ourSignatureEnd); | ||
// Modify the orig contents to put in our contents | ||
origFileLines = insertArrayAt(origFileLines, signatureSectionPosition, contents); | ||
fs.writeFileSync(referenceFile, origFileLines.join(eol)); | ||
} | ||
@@ -104,4 +185,4 @@ | ||
if (!!reference) { | ||
referencePath = path.resolve(reference); | ||
referenceFile = path.resolve(referencePath, 'reference.ts'); | ||
referenceFile = path.resolve(reference); | ||
referencePath = path.dirname(referenceFile); | ||
} | ||
@@ -132,3 +213,2 @@ function isReferenceFile(filename) { | ||
// TODO: Idea: Customize the targets based on file contents | ||
// Time the task and go | ||
@@ -138,11 +218,20 @@ 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)); | ||
updateReferenceFile(files, referenceFile, referencePath); | ||
} | ||
// Compile all the files | ||
var result = compileAllFiles(files, target, options); | ||
// The files to compile | ||
var filesToCompile = files; | ||
if (!!referencePath && target.out) { | ||
filesToCompile = [referenceFile]; | ||
} | ||
; | ||
// Quote the files to compile | ||
filesToCompile = _.map(filesToCompile, function (item) { | ||
return '"' + item + '"'; | ||
}); | ||
// Compile the files | ||
var result = compileAllFiles(filesToCompile, target, options); | ||
if (result.code != 0) { | ||
@@ -149,0 +238,0 @@ var msg = "Compilation failed"/*+result.output*/ ; |
161
tasks/ts.ts
/// <reference path="../defs/node/node.d.ts"/> | ||
/// <reference path="../defs/grunt/grunt.d.ts"/> | ||
/// <reference path="../defs/underscore/underscore.d.ts"/> | ||
/// <reference path="../defs/underscore.string/underscore.string.d.ts"/> | ||
@@ -37,15 +38,32 @@ /* | ||
// General util functions | ||
function insertArrayAt(array:string[], index:number, arrayToInsert:string[]) { | ||
Array.prototype.splice.apply(array, [index, 0].concat(arrayToInsert)); | ||
return array; | ||
} | ||
// Useful string functions | ||
// used to make sure string ends with a slash | ||
function endsWith(str: string, suffix: string) { | ||
return str.indexOf(suffix, str.length - suffix.length) !== -1; | ||
}; | ||
function endWithSlash(path: string): string { | ||
if (!endsWith(path, '/') && !endsWith(path, '\\')) { | ||
return path + '/'; | ||
} | ||
return path; | ||
} | ||
// Typescript imports | ||
import _ = require('underscore'); | ||
import _str = require('underscore.string'); | ||
import path = require('path'); | ||
import fs = require('fs'); | ||
import os = require('os'); | ||
// plain vanilla imports | ||
var shell = require('shelljs'); | ||
var eol = os.EOL; | ||
function pluginFn(grunt: IGrunt) { | ||
// plain vanilla imports | ||
var shell = require('shelljs'), | ||
eol = os.EOL; | ||
function resolveTypeScriptBinPath(currentPath, depth): string { | ||
@@ -97,2 +115,6 @@ var targetPath = path.resolve(__dirname, | ||
} | ||
// To debug the tsc command | ||
//console.log(cmd); | ||
var result = exec(cmd); | ||
@@ -102,14 +124,75 @@ return result; | ||
// Useful string functions | ||
// used to make sure string ends with a slash | ||
function endsWith(str: string, suffix: string) { | ||
return str.indexOf(suffix, str.length - suffix.length) !== -1; | ||
}; | ||
function endWithSlash(path: string): string { | ||
if (!endsWith(path, '/') && !endsWith(path, '\\')) { | ||
return path + '/'; | ||
// Updates the reference file | ||
function updateReferenceFile(files: string[], referenceFile: string, referencePath: string) { | ||
var referenceIntro = '/// <reference path="'; | ||
var referenceEnd = '" />'; | ||
var referenceMatch = /\/\/\/ <reference path=\"(.*?)\"/; | ||
var ourSignatureStart = '//grunt-start'; | ||
var ourSignatureEnd = '//grunt-end'; | ||
var origFileLines = []; // The lines we do not modify and send out as is. Lines will we reach grunt-ts generated | ||
var origFileReferences = []; // The list of files already there that we do not need to manage | ||
// Location of our generated references | ||
// By default at start of file | ||
var signatureSectionPosition = 0; | ||
// Read the original file if it exists | ||
if (fs.existsSync(referenceFile)) { | ||
var lines = fs.readFileSync(referenceFile).toString().split('\n'); | ||
var inSignatureSection = false; | ||
// By default at end of file | ||
signatureSectionPosition = lines.length; | ||
for (var i = 0; i < lines.length; i++) { | ||
var line = _str.trim(lines[i]); | ||
// Skip logic for our generated section | ||
if (_str.include(line, ourSignatureStart)) { | ||
//Wait for the end signature: | ||
signatureSectionPosition = i; | ||
inSignatureSection = true; | ||
continue; | ||
} | ||
if (_str.include(line, ourSignatureEnd)) { | ||
inSignatureSection = false; | ||
continue; | ||
} | ||
if (inSignatureSection) continue; | ||
// store the line | ||
origFileLines.push(line); | ||
// Fetch the existing reference's filename if any: | ||
if (_str.include(line, referenceIntro)) { | ||
var match = line.match(referenceMatch); | ||
var filename = match[1]; | ||
origFileReferences.push(filename); | ||
} | ||
} | ||
} | ||
return path; | ||
} | ||
var contents = [ourSignatureStart]; | ||
files.forEach((filename: string) => { | ||
// The file we are about to add | ||
var filepath = path.relative(referencePath, filename).split('\\').join('/'); | ||
// If there are orig references | ||
if (origFileReferences.length) { | ||
if (_.contains(origFileReferences, filepath)) { | ||
return; | ||
} | ||
} | ||
// Finally add the filepath | ||
contents.push(referenceIntro + filepath + referenceEnd); | ||
}); | ||
contents.push(ourSignatureEnd); | ||
// Modify the orig contents to put in our contents | ||
origFileLines = insertArrayAt(origFileLines, signatureSectionPosition, contents); | ||
fs.writeFileSync(referenceFile, origFileLines.join(eol)); | ||
} | ||
// Note: this funciton is called once for each target | ||
@@ -129,3 +212,3 @@ // so task + target options are a bit blurred inside this function | ||
comments: false | ||
}); | ||
}); | ||
@@ -152,4 +235,4 @@ // Was the whole process successful | ||
if (!!reference) { | ||
referencePath = path.resolve(reference); | ||
referenceFile = path.resolve(referencePath, 'reference.ts'); | ||
referenceFile = path.resolve(reference); | ||
referencePath = path.dirname(referenceFile) | ||
} | ||
@@ -180,4 +263,2 @@ function isReferenceFile(filename: string) { | ||
// TODO: Idea: Customize the targets based on file contents | ||
// Time the task and go | ||
@@ -188,11 +269,17 @@ var starttime = new Date().getTime(); | ||
if (!!referencePath) { | ||
var contents = []; | ||
files.forEach((filename: string) => { | ||
contents.push('/// <reference path="' + path.relative(referencePath, filename).split('\\').join('/') + '" />'); | ||
}); | ||
fs.writeFileSync(referenceFile, contents.join(eol)); | ||
updateReferenceFile(files, referenceFile, referencePath); | ||
} | ||
// Compile all the files | ||
var result = compileAllFiles(files, target, options); | ||
// The files to compile | ||
var filesToCompile = files; | ||
// If reference and out are both specified. | ||
// Then only compile the udpated reference file as that contains the correct order | ||
if (!!referencePath && target.out) { filesToCompile = [referenceFile] }; | ||
// Quote the files to compile | ||
filesToCompile = _.map(filesToCompile, (item) => {return '"' + item + '"' }); | ||
// Compile the files | ||
var result = compileAllFiles(filesToCompile, target, options); | ||
if (result.code != 0) { | ||
@@ -208,3 +295,3 @@ var msg = "Compilation failed"/*+result.output*/; | ||
} | ||
} | ||
} | ||
@@ -214,13 +301,13 @@ // Find out which files to compile | ||
// Also this funciton is debounced | ||
function filterFilesAndCompile() { | ||
// Reexpand the original file glob: | ||
var files = grunt.file.expand(currenttask.data.src); | ||
function filterFilesAndCompile() { | ||
// 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)); | ||
}); | ||
// Clear the files of output.d.ts and reference.ts | ||
files = _.filter(files, (filename) => { | ||
return (!isReferenceFile(filename) && !isOutFile(filename)); | ||
}); | ||
// compile | ||
runCompilation(files); | ||
// compile | ||
runCompilation(files); | ||
} | ||
@@ -246,3 +333,3 @@ var debouncedCompile = _.debounce(filterFilesAndCompile, 150); // randomly chosen 150. Choice was made because chokidar looks at file system every 100ms | ||
var chokidar = require('chokidar'); | ||
var watcher = chokidar.watch(watchpath, { ignoreInitial: true, persistent: true }); | ||
var watcher = chokidar.watch(watchpath, { ignoreInitial: true, persistent: true }); | ||
@@ -249,0 +336,0 @@ // local event to handle file event |
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
110537
15
2035
7
+ Addedunderscore.string@2.3.3