Socket
Socket
Sign inDemoInstall

grunt-ts

Package Overview
Dependencies
Maintainers
1
Versions
149
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grunt-ts - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

tasks/ts.ts

96

Gruntfile.js

@@ -7,98 +7,20 @@ module.exports = function (grunt) {

test:[
"test/fixtures/**/*.js",
"test/fixtures/**/*.js.map",
"test/fixtures/**/*.d.ts",
"test/temp/**/*.*",
"test/temp"
"test/**/*.js",
]
},
ts:{
simple:{
src:["test/fixtures/simple.ts"]
work:{
src:["test/work.ts"]
},
declaration:{
src:"test/fixtures/declaration.ts",
options:{
declaration:true
}
},
sourcemap:{
src:"test/fixtures/sourcemap.ts",
dest:"test/fixtures/sourcemap/",
options:{
base_path: "test/fixtures/",
sourcemap:true
}
},
"sourcemap-fullpath":{
src:"test/fixtures/sourcemap-fullpath.ts",
dest:"test/fixtures/sourcemap/",
options:{
base_path: "test/fixtures/",
sourcemap:true,
fullSourceMapPath:true
}
},
es5:{
src:"test/fixtures/es5.ts",
options:{
target:"ES5"
}
},
"no-module":{
src:"test/fixtures/no-module.ts"
},
amd:{
src:"test/fixtures/amd.ts",
options:{
module:"amd"
}
},
commonjs:{
src:"test/fixtures/commonjs.ts",
options:{
module:"commonjs"
}
},
single:{
src:"test/fixtures/single/**/*.ts",
dest: "test/temp/single.js"
},
multi:{
src:"test/fixtures/multi/**/*.ts",
dest:"test/temp/multi"
},
basePath:{
src:"test/fixtures/multi/**/*.ts",
dest:"test/temp/basePath",
options: {
base_path: "test/fixtures/multi"
}
},
"utf8-with-bom":{
src:"test/fixtures/utf8-with-bom.ts"
},
"no-output":{
src:"text/fixtures/no-output.ts",
dest:"test/temp/no-output.js"
},
comments:{
src:"test/fixtures/comments.ts",
options:{
comments:true
}
}
},
nodeunit:{
tests:["test/test.js"]
fail:{
src:["test/fail.ts"]
}
}
});
grunt.loadTasks("tasks");
grunt.loadNpmTasks("grunt-contrib-nodeunit");
grunt.loadNpmTasks("grunt-contrib-clean");
//grunt.registerTask("test", ["clean", "ts", "nodeunit"]);
grunt.registerTask("test", ["clean", "ts"]);
grunt.loadTasks("tasks");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.registerTask("test", ["clean", "ts:work", "ts:fail"]);
grunt.registerTask("default", ["test"]);
};

@@ -5,3 +5,3 @@ {

"description": "compile typescript to javascript using computer installed typescript",
"version": "0.2.1",
"version": "0.2.2",
"homepage": "https://github.com/basarat/grunt-ts",

@@ -27,3 +27,3 @@ "repository": {

"grunt": "~0.4.0",
"typescript": "0.8.3",
"typescript": "0.9.0",
"grunt-lib-contrib": "~0.5.2"

@@ -33,5 +33,6 @@ },

"grunt": "~0.4.0",
"typescript": "0.8.3",
"typescript": "0.9.0",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-nodeunit": "~0.1.2"
"grunt-contrib-nodeunit": "~0.1.2",
"shelljs":"0.1.4"
},

@@ -38,0 +39,0 @@ "optionalDependencies": {},

@@ -1,178 +0,30 @@

/*
* grunt-ts
* Licensed under the MIT license.
*/
module.exports = function (grunt) {
var path = require('path'), fs = require('fs'), vm = require('vm'), shell = require('shelljs');
var path = require('path'),
fs = require('fs'),
vm = require('vm'),
gruntIO = function (currentPath, destPath, basePath, compSetting, outputOne) {
var createdFiles = [];
basePath = basePath || ".";
function resolveTypeScriptBinPath(currentPath, depth) {
var targetPath = path.resolve(__dirname, (new Array(depth + 1)).join("../../"), "../node_modules/typescript/bin");
if (path.resolve(currentPath, "node_modules/typescript/bin").length > targetPath.length) {
return;
}
if (fs.existsSync(path.resolve(targetPath, "typescript.js"))) {
return targetPath;
}
return {
getCreatedFiles:function () {
return createdFiles;
},
return resolveTypeScriptBinPath(currentPath, ++depth);
}
resolvePath:path.resolve,
readFile:function (file){
grunt.verbose.write('Reading ' + file + '...');
try{
var content = fs.readFileSync(file, 'utf8');
// strip UTF BOM
if(content.charCodeAt(0) === 0xFEFF){
content = content.slice(1);
}
grunt.verbose.ok();
return content;
}catch(e){
grunt.verbose.fail("CAN'T READ");
throw e;
}
},
dirName:path.dirname,
function getTsc(binPath) {
return '"' + binPath + '\\' + 'tsc" ';
}
createFile:function (writeFile, useUTF8) {
var code = "";
return {
Write:function (str) {
code += str;
},
WriteLine:function (str) {
code += str + grunt.util.linefeed;
},
Close:function () {
var created = (function(){
var source, type;
if (/\.js$/.test(writeFile)) {
source = writeFile.substr(0, writeFile.length - 3) + ".ts";
type = "js";
}
else if (/\.js\.map$/.test(writeFile)) {
source = writeFile.substr(0, writeFile.length - 7) + ".ts";
type = "map";
}
else if (/\.d\.ts$/.test(writeFile)) {
source = writeFile.substr(0, writeFile.length - 5) + ".ts";
type = "declaration";
}
if(outputOne){
source = "";
}
return {
source: source,
type: type
};
})();
if (code.trim().length < 1) {
return;
}
if (!outputOne) {
var g = path.join(currentPath, basePath);
writeFile = writeFile.substr(g.length);
writeFile = path.join(currentPath, destPath ? destPath.toString() : '', writeFile);
}
grunt.file.write(writeFile, code);
created.dest = writeFile;
createdFiles.push(created);
}
}
},
findFile: function (rootPath, partialFilePath) {
var file = path.join(rootPath, partialFilePath);
while(true) {
if(fs.existsSync(file)) {
try {
var content = grunt.file.read(file);
// strip UTF BOM
if(content.charCodeAt(0) === 0xFEFF){
content = content.slice(1);
}
return {
content: content,
path: file
};
} catch (err) {
}
} else {
var parentPath = path.resolve(rootPath, "..");
if(rootPath === parentPath) {
return null;
} else {
rootPath = parentPath;
file = path.resolve(rootPath, partialFilePath);
}
}
}
},
directoryExists:function (path) {
return fs.existsSync(path) && fs.lstatSync(path).isDirectory();
},
fileExists:function (path) {
return fs.existsSync(path);
},
stderr:{
Write:function (str) {
grunt.log.error(str);
},
WriteLine:function (str) {
grunt.log.error(str);
},
Close:function () {
}
}
}
},
resolveTypeScriptBinPath = function (currentPath, depth) {
var targetPath = path.resolve(__dirname,
(new Array(depth + 1)).join("../../"),
"../node_modules/typescript/bin");
if (path.resolve(currentPath, "node_modules/typescript/bin").length > targetPath.length) {
return;
}
if (fs.existsSync(path.resolve(targetPath, "typescript.js"))) {
return targetPath;
}
function compileFile(filepath, options) {
var cmd = 'node ' + tsc + ' ' + filepath;
var result = exec(cmd);
return result;
}
return resolveTypeScriptBinPath(currentPath, ++depth);
},
pluralizeFile = function (n) {
if (n === 1) {
return "1 file";
}
return n + " files";
},
prepareSourceMapPath = function(currentPath, options, createdFiles){
var useFullPath = options.fullSourceMapPath;
var exec = shell.exec;
var currentPath = path.resolve(".");
var tsc = getTsc(resolveTypeScriptBinPath(currentPath, 0));
if(!options.sourcemap){
return;
}
createdFiles.filter(function(item){
return item.type === "map" || (useFullPath && item.type == "js");
}).forEach(function(item){
var mapOpj, lines, sourceMapLine;
if(item.type === "map"){
mapObj = JSON.parse(grunt.file.read(item.dest));
mapObj.sources.length = 0;
mapObj.sources.push(path.relative(path.dirname(item.dest), item.source).replace(/\\/g, "/"));
if(useFullPath){
mapObj.file = "file:///" + (item.dest.substr(0, item.dest.length - 6) + "js").replace(/\\/g, "/");
}
grunt.file.write(item.dest, JSON.stringify(mapObj))
}else if(useFullPath && item.type === "js"){
lines = grunt.file.read(item.dest).split(grunt.util.linefeed);
sourceMapLine = lines[lines.length - 2];
if(/^\/\/@ sourceMappingURL\=.+\.js\.map$/.test(sourceMapLine)){
lines[lines.length - 2] = "//@ sourceMappingURL=file:///" + item.dest.replace(/\\/g, "/") + ".map";
grunt.file.write(item.dest, lines.join(grunt.util.linefeed));
}
}
});
};
grunt.registerMultiTask('ts', 'Compile TypeScript files', function () {

@@ -182,6 +34,3 @@ var that = this;

this.files.forEach(function (f) {
var dest = f.dest,
options = that.options(),
extension = that.data.extension,
files = [];
var dest = f.dest, options = that.options(), extension = that.data.extension, files = [];

@@ -195,153 +44,13 @@ grunt.file.expand(f.src).forEach(function (filepath) {

for(key in files){
var tmp = [files[key]];
compile(tmp, dest, grunt.util._.clone(options), extension);
}
if (grunt.task.current.errorCount) {
return false;
}
files.forEach(function (file) {
var result = compileFile(file, options);
if (result.code != 0) {
grunt.fail.warn("Failed to compile file: " + file);
console.log("output: ".cyan);
console.log(result.output.yellow);
return false;
}
});
});
if (grunt.task.current.errorCount) {
return false;
}
});
var compile = function (srces, destPath, options, extension) {
var currentPath = path.resolve("."),
basePath = options.base_path,
typeScriptBinPath = resolveTypeScriptBinPath(currentPath, 0),
typeScriptPath = path.resolve(typeScriptBinPath, "typescript.js"),
libDPath = path.resolve(typeScriptBinPath, "lib.d.ts"),
outputOne = !!destPath && path.extname(destPath) === ".js";
if (!typeScriptBinPath) {
grunt.fail.warn("typescript.js not found. please 'npm install typescript'.");
return false;
}
var code = grunt.file.read(typeScriptPath);
vm.runInThisContext(code, typeScriptPath);
var setting = new TypeScript.CompilationSettings();
var io = gruntIO(currentPath, destPath, basePath, setting, outputOne);
var env = new TypeScript.CompilationEnvironment(setting, io);
var resolver = new TypeScript.CodeResolver(env);
if (options) {
if (options.target) {
var target = options.target.toLowerCase();
if (target === 'es3') {
setting.codeGenTarget = TypeScript.CodeGenTarget.ES3;
} else if (target == 'es5') {
setting.codeGenTarget = TypeScript.CodeGenTarget.ES5;
}
}
if (options.style) {
setting.setStyleOptions(options.style);
}
if (options.module) {
var module = options.module.toLowerCase();
if (module === 'commonjs' || module === 'node') {
TypeScript.moduleGenTarget = TypeScript.ModuleGenTarget.Synchronous;
} else if (module === 'amd') {
TypeScript.moduleGenTarget = TypeScript.ModuleGenTarget.Asynchronous;
}
}
if (options.sourcemap) {
setting.mapSourceFiles = options.sourcemap;
}
if (outputOne && options.fullSourceMapPath) {
setting.emitFullSourceMapPath = options.fullSourceMapPath;
}
if (options.declaration_file || options.declaration) {
setting.generateDeclarationFiles = true;
if (options.declaration_file) {
grunt.log.writeln("'declaration_file' option now obsolate. use 'declaration' option".yellow);
}
}
if (options.comments) {
setting.emitComments = true;
}
}
if (outputOne) {
destPath = path.resolve(currentPath, destPath);
setting.outputOption = destPath;
}
var units = [
{
fileName:libDPath,
code:grunt.file.read(libDPath)
}
];
var compiler = new TypeScript.TypeScriptCompiler(io.stderr, new TypeScript.NullLogger(), setting),
resolutionDispatcher = {
postResolutionError:function (errorFile, line, col, errorMessage) {
io.stderr.Write(errorFile + "(" + line + "," + col + ") " + (errorMessage == "" ? "" : ": " + errorMessage));
compiler.errorReporter.hasErrors = true;
},
postResolution:function (path, code) {
if (!units.some(function (u) {
return u.fileName === path;
})) {
units.push({fileName:path, code:code.content});
}
}
};
srces.forEach(function (src) {
resolver.resolveCode(path.resolve(currentPath, src), "", false, resolutionDispatcher);
});
compiler.setErrorOutput(io.stderr);
if(setting.emitComments){
compiler.emitCommentsToOutput();
}
units.forEach(function (unit) {
try{
if (!unit.code) {
unit.code = grunt.file.read(unit.fileName);
}
compiler.addUnit(unit.code, unit.fileName, false);
}catch(err){
compiler.errorReporter.hasErrors = true;
io.stderr.WriteLine(err.message);
}
});
compiler.typeCheck();
if(compiler.errorReporter.hasErrors){
return false;
}
compiler.emit(io);
compiler.emitDeclarations();
if(compiler.errorReporter.hasErrors){
return false;
}
if(!outputOne){
prepareSourceMapPath(currentPath, options, io.getCreatedFiles());
}
var result = {js:[], m:[], d:[], other:[]};
io.getCreatedFiles().forEach(function (item) {
if (item.type === "js") result.js.push(item.dest);
else if (item.type === "map") result.m.push(item.dest);
else if (item.type === "declaration") result.d.push(item.dest);
else result.other.push(item.dest);
});
var resultMessage = "js: " + pluralizeFile(result.js.length)
+ ", map: " + pluralizeFile(result.m.length)
+ ", declaration: " + pluralizeFile(result.d.length);
if (outputOne) {
if(result.js.length > 0){
grunt.log.writeln("File " + (result.js[0]).cyan + " created.");
}
grunt.log.writeln(resultMessage);
} else {
grunt.log.writeln(pluralizeFile(io.getCreatedFiles().length).cyan + " created. " + resultMessage);
}
return true;
};
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc