now reading tsOptions from tsConfig and supporting declaration file handling
This commit is contained in:
parent
07d752449e
commit
3d59f6d393
12
README.md
12
README.md
@ -11,6 +11,9 @@ Write npm modules with TypeScript without hassle.
|
||||
## What is NPMTS?
|
||||
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
|
||||
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.
|
||||
|
||||
#### Declaration files
|
||||
**npmts** also creates an `index.d.ts` declaration file by default.
|
||||
You can reference it in your package.json like this:
|
||||
**npmts** also creates an `./dist/index.d.ts` declaration file by default.
|
||||
You can reference it in your package.json like this.
|
||||
|
||||
```json
|
||||
"main": "index.js",
|
||||
"typings": "./index.d.ts",
|
||||
"main": "dist/index.js",
|
||||
"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
|
||||
and tsc will pick up the declaration file automatically.
|
||||
|
||||
|
29
dist/npmts.compile.helpers.js
vendored
29
dist/npmts.compile.helpers.js
vendored
@ -2,30 +2,15 @@
|
||||
var plugins = require("./npmts.plugins");
|
||||
var paths = require("./npmts.paths");
|
||||
var outputPathIsDir = function (configArg, keyArg) {
|
||||
try {
|
||||
return plugins.fs.statSync(plugins.path.join(paths.cwd, configArg.ts[keyArg])).isDirectory();
|
||||
}
|
||||
catch (err) {
|
||||
return plugins.smartpath.check.isDir(plugins.path.join(paths.cwd, configArg.ts[keyArg]));
|
||||
};
|
||||
exports.checkOutputPath = function (configArg, keyArg) {
|
||||
if (!outputPathIsDir(configArg, keyArg)) {
|
||||
plugins.beautylog.warn("Skipping " + keyArg + " because " + configArg.ts[keyArg] + " it is no directory!");
|
||||
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 {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
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]);
|
||||
return true;
|
||||
}
|
||||
;
|
||||
};
|
||||
|
34
dist/npmts.compile.js
vendored
34
dist/npmts.compile.js
vendored
@ -11,17 +11,29 @@ exports.run = function (configArg) {
|
||||
/* -------------------------------------------------
|
||||
* ----------- compile TypeScript --------------------------
|
||||
* ----------------------------------------------- */
|
||||
for (var key in config.ts) {
|
||||
var stream = plugins.gulp.src([plugins.path.join(paths.cwd, key), "!**/typings/**"])
|
||||
.pipe(plugins.g.sourcemaps.init()) // This means sourcemaps will be generated
|
||||
.pipe(plugins.g.typescript({
|
||||
out: helpers.outputName(config, key),
|
||||
target: "ES5",
|
||||
module: "commonjs"
|
||||
}))
|
||||
.pipe(plugins.g.sourcemaps.write()) // Now the sourcemaps are added to the .js file
|
||||
.pipe(plugins.gulp.dest(helpers.outputDir(config, key)));
|
||||
moduleStream.add(stream);
|
||||
var tsOptionsDefault = {
|
||||
declaration: true,
|
||||
target: "ES5",
|
||||
module: "commonjs"
|
||||
};
|
||||
/**
|
||||
* merges default ts options with those found in npmts.json
|
||||
*/
|
||||
var tsOptions = function (keyArg) {
|
||||
return plugins.lodashObject.assign(tsOptionsDefault, config.tsOptions);
|
||||
};
|
||||
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 () {
|
||||
plugins.beautylog.ok("TypeScript has been compiled!");
|
||||
|
2
dist/npmts.options.js
vendored
2
dist/npmts.options.js
vendored
@ -28,6 +28,8 @@ exports.run = function (configArg) {
|
||||
);
|
||||
config.test = ["./index.js"];
|
||||
}
|
||||
//check if config.tsOptions is available
|
||||
config.tsOptions ? void (0) : config.tsOptions = {};
|
||||
// handle state of current build
|
||||
exports.isRelease() ? plugins.beautylog.info("All right: This is a RELEASE build!")
|
||||
: plugins.beautylog.info("NOT A RELEASE build!");
|
||||
|
3
dist/npmts.plugins.js
vendored
3
dist/npmts.plugins.js
vendored
@ -13,6 +13,7 @@ exports.g = {
|
||||
typescript: require("gulp-typescript"),
|
||||
typings: require("gulp-typings")
|
||||
};
|
||||
exports.lodashObject = require('lodash/fp/object');
|
||||
exports.merge2 = require("merge2");
|
||||
exports.projectinfo = require("projectinfo");
|
||||
exports.path = require("path");
|
||||
@ -24,4 +25,4 @@ exports.smartcov = require("smartcov");
|
||||
exports.smartenv = require("smartenv");
|
||||
exports.smartfile = require("smartfile");
|
||||
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
|
||||
|
@ -38,6 +38,7 @@
|
||||
"gulp-sourcemaps": "^2.0.0-alpha",
|
||||
"gulp-typescript": "2.13.0",
|
||||
"gulp-typings": "1.3.4",
|
||||
"lodash": "^4.11.1",
|
||||
"merge2": "1.0.2",
|
||||
"projectinfo": "1.0.1",
|
||||
"q": "^1.4.1",
|
||||
@ -47,7 +48,7 @@
|
||||
"smartcov": "0.0.6",
|
||||
"smartenv": "1.2.2",
|
||||
"smartfile": "3.0.5",
|
||||
"smartpath": "3.1.4",
|
||||
"smartpath": "3.1.5",
|
||||
"source-map-support": "^0.4.0"
|
||||
},
|
||||
"devDependencies": {}
|
||||
|
4
test/assets/dist/index.d.ts
vendored
Normal file
4
test/assets/dist/index.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/// <reference path="typings/main.d.ts" />
|
||||
declare let testplugin: {
|
||||
logSomething: () => void;
|
||||
};
|
@ -3,7 +3,9 @@
|
||||
"ts":{
|
||||
"./customdir/*.ts":"./"
|
||||
},
|
||||
"tsconfig":true,
|
||||
"tsOptions":{
|
||||
"target":"ES5"
|
||||
},
|
||||
"typings":[
|
||||
"./ts/typings.json",
|
||||
"./subts1/typings.json",
|
||||
|
2
test/assets/test/test.d.ts
vendored
Normal file
2
test/assets/test/test.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/// <reference path="../ts/typings/main.d.ts" />
|
||||
declare var testplugin: any;
|
@ -2,33 +2,14 @@ import plugins = require("./npmts.plugins");
|
||||
import paths = require("./npmts.paths");
|
||||
|
||||
let outputPathIsDir = function (configArg,keyArg) {
|
||||
try {
|
||||
return plugins.fs.statSync(plugins.path.join(paths.cwd, configArg.ts[keyArg])).isDirectory();
|
||||
}
|
||||
catch (err) {
|
||||
return false;
|
||||
}
|
||||
return plugins.smartpath.check.isDir(plugins.path.join(paths.cwd, configArg.ts[keyArg]));
|
||||
};
|
||||
|
||||
export let outputNameSpecified = function (configArg, keyArg) {
|
||||
return !outputPathIsDir(configArg,keyArg)
|
||||
&& (plugins.path.extname(configArg.ts[keyArg]) == ".js");
|
||||
export let checkOutputPath = function(configArg,keyArg){
|
||||
if(!outputPathIsDir(configArg,keyArg)) {
|
||||
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])
|
||||
}
|
||||
};
|
@ -4,31 +4,47 @@ import paths = require("./npmts.paths");
|
||||
|
||||
import helpers = require("./npmts.compile.helpers");
|
||||
|
||||
export let run = function(configArg){
|
||||
export let run = function (configArg) {
|
||||
let done = plugins.Q.defer();
|
||||
let config = configArg;
|
||||
plugins.beautylog.log("now compiling " + "TypeScript".yellow);
|
||||
let moduleStream = plugins.merge2({end: false});
|
||||
let moduleStream = plugins.merge2({ end: false });
|
||||
|
||||
/* -------------------------------------------------
|
||||
* ----------- compile TypeScript --------------------------
|
||||
* ----------------------------------------------- */
|
||||
for (let key in config.ts) {
|
||||
let stream = plugins.gulp.src([plugins.path.join(paths.cwd,key),"!**/typings/**"])
|
||||
.pipe(plugins.g.sourcemaps.init()) // This means sourcemaps will be generated
|
||||
.pipe(plugins.g.typescript({
|
||||
out: helpers.outputName(config,key),
|
||||
target: "ES5",
|
||||
module: "commonjs"
|
||||
}))
|
||||
.pipe(plugins.g.sourcemaps.write()) // Now the sourcemaps are added to the .js file
|
||||
.pipe(plugins.gulp.dest(helpers.outputDir(config,key)));
|
||||
moduleStream.add(stream);
|
||||
|
||||
let tsOptionsDefault = {
|
||||
declaration: true,
|
||||
target: "ES5",
|
||||
module: "commonjs"
|
||||
};
|
||||
|
||||
/**
|
||||
* merges default ts options with those found in npmts.json
|
||||
*/
|
||||
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!");
|
||||
moduleStream.on("finish",function(){
|
||||
moduleStream.on("finish", function () {
|
||||
done.resolve(config);
|
||||
});
|
||||
moduleStream.end();
|
||||
|
@ -33,6 +33,9 @@ export var run = function(configArg){
|
||||
config.test = ["./index.js"];
|
||||
}
|
||||
|
||||
//check if config.tsOptions is available
|
||||
config.tsOptions ? void(0) : config.tsOptions = {};
|
||||
|
||||
// handle state of current build
|
||||
|
||||
isRelease() ? plugins.beautylog.info("All right: This is a RELEASE build!")
|
||||
|
@ -13,6 +13,7 @@ export let g = {
|
||||
typings: require("gulp-typings")
|
||||
|
||||
};
|
||||
export let lodashObject = require('lodash/fp/object');
|
||||
export let merge2 = require("merge2");
|
||||
export let projectinfo = require("projectinfo");
|
||||
export let path = require("path");
|
||||
@ -24,4 +25,4 @@ export let smartcov = require("smartcov");
|
||||
export let smartenv = require("smartenv");
|
||||
export let smartfile = require("smartfile");
|
||||
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
|
Loading…
Reference in New Issue
Block a user