now accepts config file

This commit is contained in:
Philipp Kunz 2016-02-04 20:41:34 +01:00
parent 7d18e92ff1
commit de4bbaed9f
18 changed files with 245 additions and 43 deletions

View File

@ -37,8 +37,23 @@ the TypeScript Compiler will use the declaration file to resolve typings.
### Custom behaviour
We are currently building support for custom behaviour with a super simple config file.
Check back soon.
NPMTS looks for an npmts.json at the root of your package.
```json
{
"mode":"custom",
"ts":{
"./customdir/custom.ts":"./customcompiled.js"
},
"typings":[
"./customdir"
]
}
```
* **mode** can be "default" or "custom"
* **ts** You can list as many TypeScript files as you like. The key represents the source TypeScript file, the value the output file.
* **typings** is an array of all direcories that have a typings.json present. Uses the new typings tool from npm.
## Readme for Devs
There is a [README-dev.md](README-dev.md) in the repo.

5
index.d.ts vendored
View File

@ -14,6 +14,7 @@ declare module NpmtsPlugins {
path: any;
q: any;
smartcli: any;
smartfile: any;
typings: any;
};
}
@ -21,9 +22,10 @@ declare module NpmtsPaths {
var init: () => any;
}
declare module NpmtsOptions {
var config: any;
var run: () => any;
}
declare module NpmtsTypings {
declare module NpmtsCustom {
var run: () => any;
}
declare module NpmtsDefault {
@ -49,6 +51,7 @@ declare var plugins: {
path: any;
q: any;
smartcli: any;
smartfile: any;
typings: any;
};
declare var paths: any;

112
index.js
View File

@ -18,6 +18,7 @@ var NpmtsPlugins;
path: require("path"),
q: require("q"),
smartcli: require("smartcli"),
smartfile: require("smartfile"),
typings: require("typings")
};
return plugins;
@ -40,36 +41,110 @@ var NpmtsPaths;
/// <reference path="./index.ts" />
var NpmtsOptions;
(function (NpmtsOptions) {
NpmtsOptions.config = {};
NpmtsOptions.run = function () {
var done = plugins.q.defer();
done.resolve(); //TODO: check for options
var configPath = plugins.path.join(paths.cwd, "npmts.json");
if (plugins.smartfile.checks.fileExistsSync(configPath)) {
plugins.beautylog.info("npmts.json".blue + " config file found!");
NpmtsOptions.config = plugins.smartfile.readFileToObject(configPath);
switch (NpmtsOptions.config.mode) {
case "default":
plugins.beautylog.log("mode is " + NpmtsOptions.config.mode.yellow);
done.resolve();
break;
case "custom":
plugins.beautylog.log("mode is " + NpmtsOptions.config.mode.yellow);
done.resolve();
break;
default:
plugins.beautylog.error("mode " + NpmtsOptions.config.mode.yellow + " not recognised!".red);
}
;
}
else {
plugins.beautylog.log("no config file found: so mode is " + "default".yellow);
NpmtsOptions.config.mode = "default";
done.resolve();
}
;
return done.promise;
};
})(NpmtsOptions || (NpmtsOptions = {}));
/// <reference path="./index.ts" />
var NpmtsTypings;
(function (NpmtsTypings) {
NpmtsTypings.run = function () {
var NpmtsCustom;
(function (NpmtsCustom) {
NpmtsCustom.run = function () {
var done = plugins.q.defer();
plugins.beautylog.log("now installing typings");
plugins.typings.install({ production: false, cwd: paths.tsDir })
.then(function () {
done.resolve();
});
var config = NpmtsOptions.config;
if (config.mode === "custom") {
plugins.beautylog.log("now running custom tasks");
var moduleStream = plugins.mergeStream({ end: false });
/* -------------------------------------------------
* ----------- first install typings ---------------
* ----------------------------------------------- */
var typingsDone = plugins.q.defer();
var checkTypingsDone = function (indexArg, compareArray) {
if ((indexArg + 1) == compareArray.length) {
plugins.beautylog.success("custom typings installed successfully");
typingsDone.resolve();
}
};
for (var key in config.typings) {
plugins.beautylog.log("now installing " + "typings.json".yellow + " from " + config.typings[key].blue);
plugins.typings.install({ production: false, cwd: plugins.path.join(paths.cwd, config.typings[key]) })
.then(function () {
checkTypingsDone(key, config.typings);
});
}
/* -------------------------------------------------
* ----------- second compile TS -------------------
* ----------------------------------------------- */
typingsDone.promise.then(function () {
for (var key in config.ts) {
plugins.beautylog.log("now compiling" + key.blue);
var tsStream = plugins.gulp.src(plugins.path.join(paths.cwd, key))
.pipe(plugins.g.typescript({
out: plugins.path.basename(config.ts[key]),
declaration: true
}));
var stream = plugins.mergeStream([
tsStream.dts.pipe(plugins.gulp.dest(paths.cwd)),
tsStream.js
.pipe(plugins.g.insert.prepend('#!/usr/bin/env node\n\n'))
.pipe(plugins.gulp.dest(plugins.path.dirname(plugins.path.join(paths.cwd, config.ts[key]))))
]);
moduleStream.add(stream);
}
moduleStream.on("queueDrain", function () {
plugins.beautylog.success("custom TypeScript installed successfully");
moduleStream.on("finish", function () {
done.resolve();
});
moduleStream.end();
});
});
}
return done.promise;
};
})(NpmtsTypings || (NpmtsTypings = {}));
/// <reference path="./index.ts" />
})(NpmtsCustom || (NpmtsCustom = {}));
/// <reference path="./index.ts" />
var NpmtsDefault;
(function (NpmtsDefault) {
NpmtsDefault.run = function () {
var done = plugins.q.defer();
plugins.gulp.task("defaultTypings", function (cb) {
plugins.beautylog.log("now installing default typings");
plugins.typings.install({ production: false, cwd: paths.tsDir })
.then(function () {
cb();
});
});
plugins.gulp.task("defaultIndexTS", function () {
plugins.beautylog.log("now compiling" + " ts/index.ts".blue);
var tsResult = plugins.gulp.src(paths.indexTS)
.pipe(plugins.g.typescript({
out: "index.js",
out: "./index.js",
declaration: true
}));
return plugins.mergeStream([
@ -89,12 +164,18 @@ var NpmtsDefault;
return stream;
});
plugins.gulp.task("defaultCleanup", function (cb) {
plugins.beautylog.success("TypeScript for this module compiled successfully.");
plugins.beautylog.success("default TypeScript for this module compiled successfully.");
done.resolve();
cb();
});
plugins.gulp.task("default", function (cb) {
plugins.g.sequence("defaultIndexTS", "defaultTestTS", "defaultCleanup", cb);
if (NpmtsOptions.config.mode == "default") {
plugins.g.sequence("defaultTypings", "defaultIndexTS", "defaultTestTS", "defaultCleanup", cb);
}
else {
cb();
done.resolve();
}
});
plugins.gulp.start.apply(plugins.gulp, ['default']);
return done.promise;
@ -123,8 +204,8 @@ var NpmtsPromisechain;
NpmtsPromisechain.init = function () {
var promisechain;
NpmtsOptions.run()
.then(NpmtsTypings.run)
.then(NpmtsDefault.run)
.then(NpmtsCustom.run)
.then(NpmtsTests.run);
return promisechain;
};
@ -134,7 +215,6 @@ var NpmtsPromisechain;
/// <reference path="./npmts.cli.ts" />
/// <reference path="./npmts.paths.ts" />
/// <reference path="./npmts.options.ts" />
/// <reference path="./npmts.typings.ts" />
/// <reference path="./npmts.custom.ts" />
/// <reference path="./npmts.default.ts" />
/// <reference path="./npmts.tests.ts" />

View File

@ -27,17 +27,18 @@
},
"homepage": "https://github.com/pushrocks/npmts#readme",
"dependencies": {
"beautylog": "2.0.6",
"beautylog": "2.0.7",
"fs-extra": "^0.26.5",
"gulp": "3.9.0",
"gulp-insert": "0.5.0",
"gulp-sequence": "^0.4.4",
"gulp-typescript": "2.10.0",
"gulp-typings": "0.0.0",
"merge2": "1.0.0",
"merge2": "1.0.1",
"mocha": "^2.4.5",
"q": "^1.4.1",
"smartcli": "0.0.11",
"typings": "^0.6.5"
"smartfile": "0.0.11",
"typings": "^0.6.6"
}
}

1
test/assets/customcompiled.d.ts vendored Normal file
View File

@ -0,0 +1 @@
declare var hello: string;

View File

@ -0,0 +1,3 @@
#!/usr/bin/env node
var hello = "hello";

View File

@ -0,0 +1,2 @@
var hello = "hello";
//# sourceMappingURL=custom.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"custom.js","sourceRoot":"","sources":["custom.ts"],"names":[],"mappings":"AAAA,IAAI,KAAK,GAAG,OAAO,CAAC"}

View File

@ -0,0 +1 @@
var hello = "hello";

View File

@ -0,0 +1,7 @@
{
"ambientDependencies": {
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#78d36dd49b6b55b9fdfe61776a12bf05c8b07777",
"colors": "github:DefinitelyTyped/DefinitelyTyped/colors/colors.d.ts#09e37435ffb2c56a6f908081194a74756f24f99d",
"vinyl": "github:DefinitelyTyped/DefinitelyTyped/vinyl/vinyl.d.ts#78d36dd49b6b55b9fdfe61776a12bf05c8b07777"
}
}

9
test/assets/npmts.json Normal file
View File

@ -0,0 +1,9 @@
{
"mode":"custom",
"ts":{
"./customdir/custom.ts":"./customcompiled.js"
},
"typings":[
"./customdir"
]
}

View File

@ -3,7 +3,6 @@
/// <reference path="./npmts.cli.ts" />
/// <reference path="./npmts.paths.ts" />
/// <reference path="./npmts.options.ts" />
/// <reference path="./npmts.typings.ts" />
/// <reference path="./npmts.custom.ts" />
/// <reference path="./npmts.default.ts" />
/// <reference path="./npmts.tests.ts" />

View File

@ -1 +1,60 @@
/// <reference path="./index.ts" />
/// <reference path="./index.ts" />
module NpmtsCustom {
export var run = function(){
var done = plugins.q.defer();
var config = NpmtsOptions.config;
if(config.mode === "custom"){
plugins.beautylog.log("now running custom tasks");
var moduleStream = plugins.mergeStream({end: false});
/* -------------------------------------------------
* ----------- first install typings ---------------
* ----------------------------------------------- */
var typingsDone = plugins.q.defer();
var checkTypingsDone = function(indexArg:number,compareArray){
if((indexArg + 1) == compareArray.length){
plugins.beautylog.success("custom typings installed successfully");
typingsDone.resolve();
}
};
for (var key in config.typings) {
plugins.beautylog.log("now installing " + "typings.json".yellow + " from " + config.typings[key].blue);
plugins.typings.install({production: false, cwd: plugins.path.join(paths.cwd,config.typings[key])})
.then(function(){
checkTypingsDone(key,config.typings);
});
}
/* -------------------------------------------------
* ----------- second compile TS -------------------
* ----------------------------------------------- */
typingsDone.promise.then(function(){
for (var key in config.ts) {
plugins.beautylog.log("now compiling" + key.blue);
var tsStream = plugins.gulp.src(plugins.path.join(paths.cwd,key))
.pipe(plugins.g.typescript({
out: plugins.path.basename(config.ts[key]),
declaration: true
}));
var stream = plugins.mergeStream([
tsStream.dts.pipe(plugins.gulp.dest(paths.cwd)),
tsStream.js
.pipe(plugins.g.insert.prepend('#!/usr/bin/env node\n\n'))
.pipe(plugins.gulp.dest(
plugins.path.dirname(
plugins.path.join(paths.cwd,config.ts[key])
)
))
]);
moduleStream.add(stream);
}
moduleStream.on("queueDrain",function(){
plugins.beautylog.success("custom TypeScript installed successfully");
moduleStream.on("finish",function(){
done.resolve();
});
moduleStream.end();
});
});
}
return done.promise;
}
}

View File

@ -3,13 +3,18 @@
module NpmtsDefault {
export var run = function() {
var done = plugins.q.defer();
plugins.gulp.task("defaultTypings",function(cb){
plugins.beautylog.log("now installing default typings");
plugins.typings.install({production: false, cwd: paths.tsDir})
.then(function(){
cb();
});
});
plugins.gulp.task("defaultIndexTS", function(){
plugins.beautylog.log("now compiling" + " ts/index.ts".blue);
var tsResult = plugins.gulp.src(paths.indexTS)
.pipe(plugins.g.typescript({
out:"index.js",
out:"./index.js",
declaration:true
}));
@ -32,13 +37,19 @@ module NpmtsDefault {
});
plugins.gulp.task("defaultCleanup",function(cb){
plugins.beautylog.success("TypeScript for this module compiled successfully.");
plugins.beautylog.success("default TypeScript for this module compiled successfully.");
done.resolve();
cb();
});
plugins.gulp.task("default",function(cb){
plugins.g.sequence("defaultIndexTS","defaultTestTS","defaultCleanup",cb);
if(NpmtsOptions.config.mode == "default"){
plugins.g.sequence("defaultTypings","defaultIndexTS","defaultTestTS","defaultCleanup",cb);
} else {
cb();
done.resolve();
}
});
plugins.gulp.start.apply(plugins.gulp, ['default']);

View File

@ -1,8 +1,29 @@
/// <reference path="./index.ts" />
module NpmtsOptions {
export var config:any = {};
export var run = function(){
var done = plugins.q.defer();
done.resolve(); //TODO: check for options
var configPath = plugins.path.join(paths.cwd,"npmts.json");
if(plugins.smartfile.checks.fileExistsSync(configPath)){
plugins.beautylog.info("npmts.json".blue + " config file found!");
config = plugins.smartfile.readFileToObject(configPath);
switch (config.mode){
case "default":
plugins.beautylog.log("mode is " + config.mode.yellow);
done.resolve();
break;
case "custom":
plugins.beautylog.log("mode is " + config.mode.yellow);
done.resolve();
break;
default:
plugins.beautylog.error("mode " + config.mode.yellow + " not recognised!".red);
};
} else {
plugins.beautylog.log("no config file found: so mode is " + "default".yellow);
config.mode = "default";
done.resolve();
};
return done.promise;
}
}

View File

@ -16,6 +16,7 @@ module NpmtsPlugins {
path: require("path"),
q:require("q"),
smartcli: require("smartcli"),
smartfile: require("smartfile"),
typings: require("typings")
};
return plugins;

View File

@ -3,8 +3,8 @@ module NpmtsPromisechain {
export var init = function(){
var promisechain;
NpmtsOptions.run()
.then(NpmtsTypings.run)
.then(NpmtsDefault.run)
.then(NpmtsCustom.run)
.then(NpmtsTests.run);
return promisechain;
}

View File

@ -1,12 +0,0 @@
/// <reference path="./index.ts" />
module NpmtsTypings {
export var run = function(){
var done = plugins.q.defer();
plugins.beautylog.log("now installing typings");
plugins.typings.install({production: false, cwd: paths.tsDir})
.then(function(){
done.resolve();
});
return done.promise;
}
}