From de4bbaed9fa013ef36a99f5d153f2669ed3e5fd7 Mon Sep 17 00:00:00 2001 From: PhilKunz Date: Thu, 4 Feb 2016 20:41:34 +0100 Subject: [PATCH] now accepts config file --- README.md | 19 ++++- index.d.ts | 5 +- index.js | 112 ++++++++++++++++++++++++---- package.json | 7 +- test/assets/customcompiled.d.ts | 1 + test/assets/customcompiled.js | 3 + test/assets/customdir/custom.js | 2 + test/assets/customdir/custom.js.map | 1 + test/assets/customdir/custom.ts | 1 + test/assets/customdir/typings.json | 7 ++ test/assets/npmts.json | 9 +++ ts/index.ts | 1 - ts/npmts.custom.ts | 61 ++++++++++++++- ts/npmts.default.ts | 21 ++++-- ts/npmts.options.ts | 23 +++++- ts/npmts.plugins.ts | 1 + ts/npmts.promisechain.ts | 2 +- ts/npmts.typings.ts | 12 --- 18 files changed, 245 insertions(+), 43 deletions(-) create mode 100644 test/assets/customcompiled.d.ts create mode 100644 test/assets/customcompiled.js create mode 100644 test/assets/customdir/custom.js create mode 100644 test/assets/customdir/custom.js.map create mode 100644 test/assets/customdir/custom.ts create mode 100644 test/assets/customdir/typings.json create mode 100644 test/assets/npmts.json delete mode 100644 ts/npmts.typings.ts diff --git a/README.md b/README.md index 87ea877..85e5061 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.d.ts b/index.d.ts index 283b9a3..514653d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; diff --git a/index.js b/index.js index 6b27a0a..2415139 100644 --- a/index.js +++ b/index.js @@ -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; /// 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 = {})); /// -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 = {})); -/// +})(NpmtsCustom || (NpmtsCustom = {})); /// 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; /// /// /// -/// /// /// /// diff --git a/package.json b/package.json index 06909f1..4806642 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/assets/customcompiled.d.ts b/test/assets/customcompiled.d.ts new file mode 100644 index 0000000..d8c1b2d --- /dev/null +++ b/test/assets/customcompiled.d.ts @@ -0,0 +1 @@ +declare var hello: string; diff --git a/test/assets/customcompiled.js b/test/assets/customcompiled.js new file mode 100644 index 0000000..bed1bad --- /dev/null +++ b/test/assets/customcompiled.js @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +var hello = "hello"; diff --git a/test/assets/customdir/custom.js b/test/assets/customdir/custom.js new file mode 100644 index 0000000..5c546bf --- /dev/null +++ b/test/assets/customdir/custom.js @@ -0,0 +1,2 @@ +var hello = "hello"; +//# sourceMappingURL=custom.js.map \ No newline at end of file diff --git a/test/assets/customdir/custom.js.map b/test/assets/customdir/custom.js.map new file mode 100644 index 0000000..4fd50d4 --- /dev/null +++ b/test/assets/customdir/custom.js.map @@ -0,0 +1 @@ +{"version":3,"file":"custom.js","sourceRoot":"","sources":["custom.ts"],"names":[],"mappings":"AAAA,IAAI,KAAK,GAAG,OAAO,CAAC"} \ No newline at end of file diff --git a/test/assets/customdir/custom.ts b/test/assets/customdir/custom.ts new file mode 100644 index 0000000..1f87661 --- /dev/null +++ b/test/assets/customdir/custom.ts @@ -0,0 +1 @@ +var hello = "hello"; \ No newline at end of file diff --git a/test/assets/customdir/typings.json b/test/assets/customdir/typings.json new file mode 100644 index 0000000..fd6d68d --- /dev/null +++ b/test/assets/customdir/typings.json @@ -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" + } +} diff --git a/test/assets/npmts.json b/test/assets/npmts.json new file mode 100644 index 0000000..015469b --- /dev/null +++ b/test/assets/npmts.json @@ -0,0 +1,9 @@ +{ + "mode":"custom", + "ts":{ + "./customdir/custom.ts":"./customcompiled.js" + }, + "typings":[ + "./customdir" + ] +} \ No newline at end of file diff --git a/ts/index.ts b/ts/index.ts index af956e6..b4ce72d 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -3,7 +3,6 @@ /// /// /// -/// /// /// /// diff --git a/ts/npmts.custom.ts b/ts/npmts.custom.ts index 019254e..2235351 100644 --- a/ts/npmts.custom.ts +++ b/ts/npmts.custom.ts @@ -1 +1,60 @@ -/// \ No newline at end of file +/// +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; + } +} \ No newline at end of file diff --git a/ts/npmts.default.ts b/ts/npmts.default.ts index 085fea1..7b544ea 100644 --- a/ts/npmts.default.ts +++ b/ts/npmts.default.ts @@ -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']); diff --git a/ts/npmts.options.ts b/ts/npmts.options.ts index bf16d9b..01a0de3 100644 --- a/ts/npmts.options.ts +++ b/ts/npmts.options.ts @@ -1,8 +1,29 @@ /// 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; } } \ No newline at end of file diff --git a/ts/npmts.plugins.ts b/ts/npmts.plugins.ts index a328e94..039e92b 100644 --- a/ts/npmts.plugins.ts +++ b/ts/npmts.plugins.ts @@ -16,6 +16,7 @@ module NpmtsPlugins { path: require("path"), q:require("q"), smartcli: require("smartcli"), + smartfile: require("smartfile"), typings: require("typings") }; return plugins; diff --git a/ts/npmts.promisechain.ts b/ts/npmts.promisechain.ts index f6e6d37..b74a30e 100644 --- a/ts/npmts.promisechain.ts +++ b/ts/npmts.promisechain.ts @@ -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; } diff --git a/ts/npmts.typings.ts b/ts/npmts.typings.ts deleted file mode 100644 index e6922f6..0000000 --- a/ts/npmts.typings.ts +++ /dev/null @@ -1,12 +0,0 @@ -/// -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; - } -} \ No newline at end of file