now reading tsOptions from tsConfig and supporting declaration file handling

This commit is contained in:
LosslessBot 2016-04-30 11:55:42 +02:00
parent 07d752449e
commit 3d59f6d393
13 changed files with 98 additions and 84 deletions

View File

@ -11,6 +11,9 @@ Write npm modules with TypeScript without hassle.
## What is NPMTS? ## What is NPMTS?
NPMTS is your friend when it comes to write, test, publish and document NPM modules written in TypeScript. NPMTS is your friend when it comes to write, test, publish and document NPM modules written in TypeScript.
There is a docker image available that includes npmts to make CI a breeze:
[hosttoday/ht-docker-npmg on Dockerhub](https://hub.docker.com/r/hosttoday/ht-docker-npmg/)
### Install ### Install
First install npmts as dev dependency: First install npmts as dev dependency:
@ -88,14 +91,15 @@ by default npmts looks for `./ts/*.ts` and `./test/test.ts` that will compile to
Use commonjs module system for wiring up files. Use commonjs module system for wiring up files.
#### Declaration files #### Declaration files
**npmts** also creates an `index.d.ts` declaration file by default. **npmts** also creates an `./dist/index.d.ts` declaration file by default.
You can reference it in your package.json like this: You can reference it in your package.json like this.
```json ```json
"main": "index.js", "main": "dist/index.js",
"typings": "./index.d.ts", "typings": ".dist/index.d.ts",
``` ```
This is in line with the latest TypeScript best practices.
You can then import plugins via the TypeScript `import` Syntax You can then import plugins via the TypeScript `import` Syntax
and tsc will pick up the declaration file automatically. and tsc will pick up the declaration file automatically.

View File

@ -2,30 +2,15 @@
var plugins = require("./npmts.plugins"); var plugins = require("./npmts.plugins");
var paths = require("./npmts.paths"); var paths = require("./npmts.paths");
var outputPathIsDir = function (configArg, keyArg) { var outputPathIsDir = function (configArg, keyArg) {
try { return plugins.smartpath.check.isDir(plugins.path.join(paths.cwd, configArg.ts[keyArg]));
return plugins.fs.statSync(plugins.path.join(paths.cwd, configArg.ts[keyArg])).isDirectory(); };
} exports.checkOutputPath = function (configArg, keyArg) {
catch (err) { if (!outputPathIsDir(configArg, keyArg)) {
plugins.beautylog.warn("Skipping " + keyArg + " because " + configArg.ts[keyArg] + " it is no directory!");
return false; return false;
} }
};
exports.outputNameSpecified = function (configArg, keyArg) {
return !outputPathIsDir(configArg, keyArg)
&& (plugins.path.extname(configArg.ts[keyArg]) == ".js");
};
exports.outputName = function (configArg, keyArg) {
if (exports.outputNameSpecified(configArg, keyArg)) {
return plugins.path.basename(configArg.ts[keyArg]);
}
else { else {
return undefined; return true;
}
};
exports.outputDir = function (configArg, keyArg) {
if (exports.outputNameSpecified(configArg, keyArg)) {
return plugins.path.dirname(plugins.path.join(paths.cwd, configArg.ts[keyArg]));
}
else {
return plugins.path.join(paths.cwd, configArg.ts[keyArg]);
} }
;
}; };

34
dist/npmts.compile.js vendored
View File

@ -11,17 +11,29 @@ exports.run = function (configArg) {
/* ------------------------------------------------- /* -------------------------------------------------
* ----------- compile TypeScript -------------------------- * ----------- compile TypeScript --------------------------
* ----------------------------------------------- */ * ----------------------------------------------- */
for (var key in config.ts) { var tsOptionsDefault = {
var stream = plugins.gulp.src([plugins.path.join(paths.cwd, key), "!**/typings/**"]) declaration: true,
.pipe(plugins.g.sourcemaps.init()) // This means sourcemaps will be generated target: "ES5",
.pipe(plugins.g.typescript({ module: "commonjs"
out: helpers.outputName(config, key), };
target: "ES5", /**
module: "commonjs" * merges default ts options with those found in npmts.json
})) */
.pipe(plugins.g.sourcemaps.write()) // Now the sourcemaps are added to the .js file var tsOptions = function (keyArg) {
.pipe(plugins.gulp.dest(helpers.outputDir(config, key))); return plugins.lodashObject.assign(tsOptionsDefault, config.tsOptions);
moduleStream.add(stream); };
for (var keyArg in config.ts) {
if (helpers.checkOutputPath(config, keyArg)) {
var tsStream = plugins.gulp.src([plugins.path.join(paths.cwd, keyArg), "!**/typings/**"])
.pipe(plugins.g.sourcemaps.init()) // This means sourcemaps will be generated
.pipe(plugins.g.typescript(tsOptions(keyArg)));
var jsStream = tsStream.js
.pipe(plugins.g.sourcemaps.write()) // Now the sourcemaps are added to the .js file
.pipe(plugins.gulp.dest(config.ts[keyArg]));
var declarationStream = tsStream.dts
.pipe(plugins.gulp.dest(config.ts[keyArg]));
moduleStream.add(tsStream, jsStream, declarationStream);
}
} }
moduleStream.on("queueDrain", function () { moduleStream.on("queueDrain", function () {
plugins.beautylog.ok("TypeScript has been compiled!"); plugins.beautylog.ok("TypeScript has been compiled!");

View File

@ -28,6 +28,8 @@ exports.run = function (configArg) {
); );
config.test = ["./index.js"]; config.test = ["./index.js"];
} }
//check if config.tsOptions is available
config.tsOptions ? void (0) : config.tsOptions = {};
// handle state of current build // handle state of current build
exports.isRelease() ? plugins.beautylog.info("All right: This is a RELEASE build!") exports.isRelease() ? plugins.beautylog.info("All right: This is a RELEASE build!")
: plugins.beautylog.info("NOT A RELEASE build!"); : plugins.beautylog.info("NOT A RELEASE build!");

View File

@ -13,6 +13,7 @@ exports.g = {
typescript: require("gulp-typescript"), typescript: require("gulp-typescript"),
typings: require("gulp-typings") typings: require("gulp-typings")
}; };
exports.lodashObject = require('lodash/fp/object');
exports.merge2 = require("merge2"); exports.merge2 = require("merge2");
exports.projectinfo = require("projectinfo"); exports.projectinfo = require("projectinfo");
exports.path = require("path"); exports.path = require("path");
@ -24,4 +25,4 @@ exports.smartcov = require("smartcov");
exports.smartenv = require("smartenv"); exports.smartenv = require("smartenv");
exports.smartfile = require("smartfile"); exports.smartfile = require("smartfile");
exports.smartpath = require("smartpath"); exports.smartpath = require("smartpath");
exports.sourceMapSupport = require("source-map-support").install(); exports.sourceMapSupport = require("source-map-support").install(); // this is required to display errors correctly during testing

View File

@ -38,6 +38,7 @@
"gulp-sourcemaps": "^2.0.0-alpha", "gulp-sourcemaps": "^2.0.0-alpha",
"gulp-typescript": "2.13.0", "gulp-typescript": "2.13.0",
"gulp-typings": "1.3.4", "gulp-typings": "1.3.4",
"lodash": "^4.11.1",
"merge2": "1.0.2", "merge2": "1.0.2",
"projectinfo": "1.0.1", "projectinfo": "1.0.1",
"q": "^1.4.1", "q": "^1.4.1",
@ -47,7 +48,7 @@
"smartcov": "0.0.6", "smartcov": "0.0.6",
"smartenv": "1.2.2", "smartenv": "1.2.2",
"smartfile": "3.0.5", "smartfile": "3.0.5",
"smartpath": "3.1.4", "smartpath": "3.1.5",
"source-map-support": "^0.4.0" "source-map-support": "^0.4.0"
}, },
"devDependencies": {} "devDependencies": {}

4
test/assets/dist/index.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
/// <reference path="typings/main.d.ts" />
declare let testplugin: {
logSomething: () => void;
};

View File

@ -3,7 +3,9 @@
"ts":{ "ts":{
"./customdir/*.ts":"./" "./customdir/*.ts":"./"
}, },
"tsconfig":true, "tsOptions":{
"target":"ES5"
},
"typings":[ "typings":[
"./ts/typings.json", "./ts/typings.json",
"./subts1/typings.json", "./subts1/typings.json",

2
test/assets/test/test.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
/// <reference path="../ts/typings/main.d.ts" />
declare var testplugin: any;

View File

@ -2,33 +2,14 @@ import plugins = require("./npmts.plugins");
import paths = require("./npmts.paths"); import paths = require("./npmts.paths");
let outputPathIsDir = function (configArg,keyArg) { let outputPathIsDir = function (configArg,keyArg) {
try { return plugins.smartpath.check.isDir(plugins.path.join(paths.cwd, configArg.ts[keyArg]));
return plugins.fs.statSync(plugins.path.join(paths.cwd, configArg.ts[keyArg])).isDirectory();
}
catch (err) {
return false;
}
}; };
export let outputNameSpecified = function (configArg, keyArg) { export let checkOutputPath = function(configArg,keyArg){
return !outputPathIsDir(configArg,keyArg) if(!outputPathIsDir(configArg,keyArg)) {
&& (plugins.path.extname(configArg.ts[keyArg]) == ".js"); plugins.beautylog.warn("Skipping " + keyArg + " because " + configArg.ts[keyArg] + " it is no directory!")
return false
} else {
return true;
};
} }
export let outputName = function (configArg, keyArg) {
if (outputNameSpecified(configArg,keyArg)) {
return plugins.path.basename(configArg.ts[keyArg])
} else {
return undefined
}
};
export let outputDir = function (configArg, keyArg) {
if (outputNameSpecified(configArg,keyArg)) {
return plugins.path.dirname(
plugins.path.join(paths.cwd, configArg.ts[keyArg])
)
} else {
return plugins.path.join(paths.cwd, configArg.ts[keyArg])
}
};

View File

@ -4,31 +4,47 @@ import paths = require("./npmts.paths");
import helpers = require("./npmts.compile.helpers"); import helpers = require("./npmts.compile.helpers");
export let run = function(configArg){ export let run = function (configArg) {
let done = plugins.Q.defer(); let done = plugins.Q.defer();
let config = configArg; let config = configArg;
plugins.beautylog.log("now compiling " + "TypeScript".yellow); plugins.beautylog.log("now compiling " + "TypeScript".yellow);
let moduleStream = plugins.merge2({end: false}); let moduleStream = plugins.merge2({ end: false });
/* ------------------------------------------------- /* -------------------------------------------------
* ----------- compile TypeScript -------------------------- * ----------- compile TypeScript --------------------------
* ----------------------------------------------- */ * ----------------------------------------------- */
for (let key in config.ts) {
let stream = plugins.gulp.src([plugins.path.join(paths.cwd,key),"!**/typings/**"]) let tsOptionsDefault = {
.pipe(plugins.g.sourcemaps.init()) // This means sourcemaps will be generated declaration: true,
.pipe(plugins.g.typescript({ target: "ES5",
out: helpers.outputName(config,key), module: "commonjs"
target: "ES5", };
module: "commonjs"
})) /**
.pipe(plugins.g.sourcemaps.write()) // Now the sourcemaps are added to the .js file * merges default ts options with those found in npmts.json
.pipe(plugins.gulp.dest(helpers.outputDir(config,key))); */
moduleStream.add(stream); let tsOptions = function (keyArg:string) {
return plugins.lodashObject.assign(tsOptionsDefault, config.tsOptions)
};
for (let keyArg in config.ts) {
if (helpers.checkOutputPath(config,keyArg)) {
let tsStream = plugins.gulp.src([plugins.path.join(paths.cwd, keyArg), "!**/typings/**"])
.pipe(plugins.g.sourcemaps.init()) // This means sourcemaps will be generated
.pipe(plugins.g.typescript(tsOptions(keyArg)));
let jsStream = tsStream.js
.pipe(plugins.g.sourcemaps.write()) // Now the sourcemaps are added to the .js file
.pipe(plugins.gulp.dest(config.ts[keyArg]));
let declarationStream = tsStream.dts
.pipe(plugins.gulp.dest(config.ts[keyArg]));
moduleStream.add(tsStream,jsStream,declarationStream);
}
} }
moduleStream.on("queueDrain",function(){ moduleStream.on("queueDrain", function () {
plugins.beautylog.ok("TypeScript has been compiled!"); plugins.beautylog.ok("TypeScript has been compiled!");
moduleStream.on("finish",function(){ moduleStream.on("finish", function () {
done.resolve(config); done.resolve(config);
}); });
moduleStream.end(); moduleStream.end();

View File

@ -33,6 +33,9 @@ export var run = function(configArg){
config.test = ["./index.js"]; config.test = ["./index.js"];
} }
//check if config.tsOptions is available
config.tsOptions ? void(0) : config.tsOptions = {};
// handle state of current build // handle state of current build
isRelease() ? plugins.beautylog.info("All right: This is a RELEASE build!") isRelease() ? plugins.beautylog.info("All right: This is a RELEASE build!")

View File

@ -13,6 +13,7 @@ export let g = {
typings: require("gulp-typings") typings: require("gulp-typings")
}; };
export let lodashObject = require('lodash/fp/object');
export let merge2 = require("merge2"); export let merge2 = require("merge2");
export let projectinfo = require("projectinfo"); export let projectinfo = require("projectinfo");
export let path = require("path"); export let path = require("path");
@ -24,4 +25,4 @@ export let smartcov = require("smartcov");
export let smartenv = require("smartenv"); export let smartenv = require("smartenv");
export let smartfile = require("smartfile"); export let smartfile = require("smartfile");
export let smartpath = require("smartpath"); export let smartpath = require("smartpath");
export let sourceMapSupport = require("source-map-support").install(); export let sourceMapSupport = require("source-map-support").install(); // this is required to display errors correctly during testing