From 04a8ccaf652baaac4f30f5d17fb2bad29ecbd893 Mon Sep 17 00:00:00 2001 From: Andre Wiggins Date: Fri, 7 Jul 2017 16:52:27 -0700 Subject: [PATCH] Add gulp-connect typings --- types/gulp-connect/gulp-connect-tests.ts | 120 +++++++++++++++++++++++ types/gulp-connect/index.d.ts | 76 ++++++++++++++ types/gulp-connect/tsconfig.json | 22 +++++ types/gulp-connect/tslint.json | 1 + 4 files changed, 219 insertions(+) create mode 100644 types/gulp-connect/gulp-connect-tests.ts create mode 100644 types/gulp-connect/index.d.ts create mode 100644 types/gulp-connect/tsconfig.json create mode 100644 types/gulp-connect/tslint.json diff --git a/types/gulp-connect/gulp-connect-tests.ts b/types/gulp-connect/gulp-connect-tests.ts new file mode 100644 index 0000000000..03722a4c15 --- /dev/null +++ b/types/gulp-connect/gulp-connect-tests.ts @@ -0,0 +1,120 @@ +// Copying examples from Readme which currently depend on gulp v3 +import * as gulp from 'gulp/v3'; +import * as connect from 'gulp-connect'; + +// Simple example +gulp.task('connect', () => { + connect.server(); +}); + +gulp.task('default', ['connect']); + +// LiveReload +gulp.task('connect', () => { + connect.server({ + root: 'app', + livereload: true + }); +}); + +gulp.task('html', () => { + gulp.src('./app/*.html') + .pipe(connect.reload()); +}); + +gulp.task('watch', () => { + gulp.watch(['./app/*.html'], ['html']); +}); + +gulp.task('default', ['connect', 'watch']); + +// Start and stop server +gulp.task('jenkins-tests', () => { + connect.server({ + port: 8888 + }); + // run some headless tests with phantomjs + // when process exits: + connect.serverClose(); +}); + +// Multiple server +gulp.task('connectDev', () => { + connect.server({ + name: 'Dev App', + root: ['app', 'tmp'], + port: 8000, + livereload: true + }); +}); + +gulp.task('connectDist', () => { + connect.server({ + name: 'Dist App', + root: 'dist', + port: 8001, + livereload: true + }); +}); + +gulp.task('html', () => { + gulp.src('./app/*.html') + .pipe(connect.reload()); +}); + +gulp.task('stylus', () => { + gulp.src('./app/stylus/*.styl') + // .pipe(stylus()) // just here for demonstration purposes + .pipe(gulp.dest('./app/css')) + .pipe(connect.reload()); +}); + +gulp.task('watch', () => { + gulp.watch(['./app/*.html'], ['html']); + gulp.watch(['./app/stylus/*.styl'], ['stylus']); +}); + +gulp.task('default', ['connectDist', 'connectDev', 'watch']); + +// Barebones middleware example from Readme +gulp.task('connect', () => { + connect.server({ + root: "app", + middleware(connect, opt) { + return []; + } + }); +}); + +// The following tests are custom tests to validate the more complicated APIs + +// Validate gulp-connect typings allow express apps to be passed in as middleware +import * as express from "express"; + +gulp.task('connect', () => { + let middleware = [ + express() + ]; + + return connect.server({ + root: [__dirname], + port: 8081, + livereload: true, + middleware: (connect, opt) => middleware + }); +}); + +// Validate using paths to restrict handler functions works +gulp.task('connect', () => { + let middleware: connect.ConnectRouteHandler[] = [ + ["/path", express()], + ["/path2", express()], + ]; + + return connect.server({ + root: [__dirname], + port: 8081, + livereload: true, + middleware: (connect, opt) => middleware + }); +}); diff --git a/types/gulp-connect/index.d.ts b/types/gulp-connect/index.d.ts new file mode 100644 index 0000000000..f023c2bff1 --- /dev/null +++ b/types/gulp-connect/index.d.ts @@ -0,0 +1,76 @@ +// Type definitions for gulp-connect 5.0 +// Project: https://github.com/avevlad/gulp-connect#readme +// Definitions by: Andre Wiggins +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import * as http from "http"; +import * as https from "https"; +import * as connectModule from "connect"; + +export interface LiveReloadOptions { + /** Port to run live reload server on. Defauls to 35729. */ + port: number; + + /** Hostname to bind live reload server to */ + hostname?: string; +} + +/** a list of [string, HandlerFunction] where the string is the path (i.e. route) that the handler function should be invoked for */ +export type ConnectRouteHandler = [ string, connectModule.HandleFunction ]; + +/** + * Factory function that returns a list of middleware handlers to pass to a connect server's use function. + * The list contain normal Connect middleware handler functions or ConnectRoutHandlers + */ +export type MiddlewareFactory = (connect: typeof connectModule, options: ConnectAppOptions) => Array; + +export interface ConnectAppOptions { + /** The name of this server. Used in logs. Defaults to "Server". */ + name?: string; + + /** The root path. Defaults to directory with gulpfile */ + root?: string | string[]; + + /** The connect webserver port. Defaults to 8080 */ + port?: number; + + /** Host to bind server to. Defaults to localhost. */ + host?: string; + + /** Don't log any messages. Defaults to false. */ + slient?: boolean; + + /** + * Options to pass to http.createServer (or false to disable https). + * Defaults to false. When https is just set to true, then internally + * some defaults will be used. + */ + https?: boolean | https.ServerOptions; + + /** Enable/disable livereload or set live reload options. Defaults to false. */ + livereload?: boolean | LiveReloadOptions; + + /** A function to run custom initialization on the underlying http or https server */ + serverInit?(server: http.Server | https.Server): void; + + /** File to serve if url results in a 404. Defaults to undefined */ + fallback?: string; + + /** Middleware factory function which should return a list of connect handler functions . Defaults to () => []; */ + middleware?: MiddlewareFactory; + + /** Whether or not to log debug messages. Defaults to false. */ + debug?: boolean; + + /** Value to pass into the serve-static's index option. See serve-static documentation for details. Defaults to true. */ + index?: boolean | string | string[]; +} + +/** Create a gulp-connect server with the given options */ +export function server(options?: ConnectAppOptions): ConnectAppOptions; + +/** Reload all gulp-connect servers that have livereload enabled */ +export function reload(): any; + +/** Close all gulp-connect servers */ +export function serverClose(): void; diff --git a/types/gulp-connect/tsconfig.json b/types/gulp-connect/tsconfig.json new file mode 100644 index 0000000000..750605f81f --- /dev/null +++ b/types/gulp-connect/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "gulp-connect-tests.ts" + ] +} diff --git a/types/gulp-connect/tslint.json b/types/gulp-connect/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/gulp-connect/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }