gulp-webdav-sync
Advanced tools
Comparing version 0.0.3 to 0.3.0
@@ -26,14 +26,5 @@ var debug = require( 'gulp-debug' ) | ||
gulp.task( 'debug', function () { | ||
if ( npmconf.loaded.sources.global ) { | ||
uri = npmconf.loaded.sources.global.data.dav | ||
} | ||
if ( npmconf.loaded.sources.user ) { | ||
uri = npmconf.loaded.sources.user.data.dav | ||
} | ||
if ( npmconf.loaded.sources.project ) { | ||
uri = npmconf.loaded.sources.project.data.dav | ||
} | ||
return gulp.src( 'test/assets/*' ) | ||
return gulp.src( 'test/assets/**' ) | ||
.pipe( debug( { title: 'pre' } ) ) | ||
.pipe( dav() ) | ||
.pipe( dav( { 'log': 'info', 'parent': 'test/assets' } ) ) | ||
.pipe( debug( { title: 'post' } ) ) | ||
@@ -40,0 +31,0 @@ } ) |
179
index.js
@@ -0,1 +1,2 @@ | ||
var chalk = require( 'chalk' ) | ||
var gutil = require( 'gulp-util' ) | ||
@@ -6,2 +7,3 @@ var http = require( 'http' ) | ||
var Stream = require( 'stream' ) | ||
var underscore = require( 'underscore' ) | ||
var url = require( 'url' ) | ||
@@ -11,3 +13,18 @@ | ||
module.exports = function ( i ) { | ||
module.exports = function () { | ||
_string = '' | ||
_options = { | ||
'log': 'error' | ||
, 'parent': process.cwd() | ||
} | ||
for ( var i in arguments ) { | ||
if ( typeof arguments[i] === 'string' ) { | ||
_string = arguments[i] | ||
} | ||
if ( typeof arguments[i] === 'object' && arguments[i] ) { | ||
_options = underscore.extend( _options, arguments[i] ) | ||
} | ||
} | ||
stream = new Stream.Transform( { objectMode: true } ) | ||
@@ -22,7 +39,3 @@ stream._transform = function ( vinyl, encoding, callback ) { | ||
function init() { | ||
if ( i && typeof i == 'string' ) { | ||
uri = i + vinyl.relative | ||
} else { | ||
uri = target() + vinyl.relative | ||
} | ||
uri = target( _string, vinyl ) | ||
if ( vinyl.isBuffer() ) { | ||
@@ -33,3 +46,3 @@ _put( uri, vinyl, resume ) | ||
if ( vinyl.isNull() ) { | ||
_mkdir( uri, resume ) | ||
_mkcol( uri, resume ) | ||
return | ||
@@ -44,4 +57,32 @@ } | ||
function target() { | ||
var href | ||
function target( href, vinyl ) { | ||
var vinyl_stem = '' | ||
var opt = path.resolve( _options.parent ) | ||
log.log( _gulp_prefix( 'main#target$opt' ), opt ) | ||
if ( vinyl.path.length < opt.length ) { | ||
_on_error( | ||
new gutil.PluginError( | ||
PLUGIN_NAME | ||
, 'Incoherent Target: options.parent too long.\n' | ||
+ '\tvinyl.path is ' + vinyl.path + '\n' | ||
+ '\toptions.parent is ' + opt + '\n' | ||
) | ||
) | ||
} | ||
if ( vinyl.path.substr( 0, opt.length ) === opt ) { | ||
vinyl_stem = vinyl.path.substr( opt.length+1 ) | ||
} else { | ||
_on_error( | ||
new gutil.PluginError( | ||
PLUGIN_NAME | ||
, 'Incoherent Target: paths diverge.\n' | ||
+ '\tvinyl.path is ' + vinyl.path + '\n' | ||
+ '\toptions.parent is ' + opt + '\n' | ||
) | ||
) | ||
} | ||
log.log( _gulp_prefix( 'main#target$vinyl_stem' ), vinyl_stem ) | ||
if ( href && path.toString() !== '' ) { | ||
return href + vinyl_stem | ||
} | ||
if ( npmconf.loaded.sources.global ) { | ||
@@ -56,3 +97,3 @@ href = npmconf.loaded.sources.global.data.dav | ||
} | ||
return href | ||
return href + vinyl_stem | ||
} | ||
@@ -68,7 +109,59 @@ | ||
function report( res ) { | ||
gutil.log( | ||
uri | ||
, res.statusCode | ||
, http.STATUS_CODES[res.statusCode] | ||
) | ||
function code_fn( statusCode ) { | ||
switch ( statusCode ) { | ||
case 102: | ||
return chalk.bgYellow.white | ||
case 200: | ||
case 201: | ||
case 204: | ||
return chalk.bgGreen.white | ||
case 207: | ||
return chalk.bgWhite.black | ||
case 403: | ||
case 409: | ||
case 412: | ||
case 415: | ||
case 422: | ||
case 423: | ||
case 424: | ||
case 502: | ||
case 507: | ||
return chalk.bgRed.white | ||
default: | ||
return chalk.bgWhite.black | ||
} | ||
} | ||
function msg_fn( statusMessage ) { | ||
switch ( statusMessage ) { | ||
case 102: | ||
return chalk.yellow | ||
case 200: | ||
case 201: | ||
case 204: | ||
return chalk.green | ||
case 207: | ||
return chalk.white | ||
case 403: | ||
case 409: | ||
case 412: | ||
case 415: | ||
case 422: | ||
case 423: | ||
case 424: | ||
case 502: | ||
case 507: | ||
return chalk.red | ||
default: | ||
return chalk.white | ||
} | ||
} | ||
var from = chalk.underline.cyan( vinyl.path ) | ||
var to = chalk.underline.cyan( uri ) | ||
var code = | ||
code_fn( res.statusCode ).call( this, res.statusCode ) | ||
var msg = | ||
msg_fn( res.statusCode ).call( this, http.STATUS_CODES[res.statusCode] ) | ||
log.info( from ) | ||
log.info( '-->', to ) | ||
log.info( code, msg ) | ||
} | ||
@@ -79,6 +172,35 @@ } | ||
function _mkdir( uri, callback ) { | ||
function _delete( uri, callback ) { | ||
} | ||
function _get( uri, vinyl, callback ) { | ||
} | ||
function _gulp_prefix() { | ||
var time = '[' + chalk.grey( ( new Date() ).toLocaleTimeString() ) + ']' | ||
var name = '[' + chalk.grey( PLUGIN_NAME ) + ']' | ||
var item = chalk.grey( arguments[0] ) | ||
return [ time, name, item ].join( ' ' ) | ||
} | ||
var log = ( function () { | ||
var methods = [ 'error', 'warn', 'info', 'log' ] | ||
var _log = {} | ||
methods.forEach( function ( element, index, array ) { | ||
_log[element] = function () { | ||
if ( index <= methods.indexOf( _options.log ) ) { | ||
console[element].apply( this, arguments ) | ||
} | ||
} | ||
} ) | ||
return _log | ||
} )() | ||
function _mkcol( uri, callback ) { | ||
var options, req | ||
options = url.parse( uri ) | ||
options.method = 'MKCOL' | ||
options = underscore.extend( | ||
_options | ||
, url.parse( uri ) | ||
, { method: 'MKCOL' } | ||
) | ||
req = http.request( options, callback ) | ||
@@ -89,6 +211,19 @@ req.on( 'error', _on_error ) | ||
function _on_error( error ) { | ||
stream.emit( 'error', error ) | ||
} | ||
function _propfind( uri, callback ) { | ||
} | ||
function _proppatch( uri, props, callback ) { | ||
} | ||
function _put( uri, vinyl, callback ) { | ||
var options, req | ||
options = url.parse( uri ) | ||
options.method = 'PUT' | ||
options = underscore.extend( | ||
_options | ||
, url.parse( uri ) | ||
, { method: 'PUT' } | ||
) | ||
req = http.request( options, callback ) | ||
@@ -98,5 +233,1 @@ vinyl.pipe( req ) | ||
} | ||
function _on_error( error ) { | ||
stream.emit( 'error', error ) | ||
} |
{ | ||
"name": "gulp-webdav-sync", | ||
"version": "0.0.3", | ||
"version": "0.3.0", | ||
"description": "Put files and folders on a WebDAV server. Deploy with gulp", | ||
@@ -33,5 +33,8 @@ "repository": { | ||
"mocha": "~2.2.5", | ||
"run-sequence": "~1.1.1" | ||
"run-sequence": "~1.1.1", | ||
"vinyl": "~0.5.0", | ||
"underscore": "~1.8.3" | ||
}, | ||
"dependencies": { | ||
"chalk": "~1.1.0", | ||
"gulp-util": "~3.0.6", | ||
@@ -38,0 +41,0 @@ "npmconf": "~2.1.2" |
@@ -5,3 +5,3 @@ # gulp-webdav-sync | ||
## Usage | ||
Target is loaded from npmrc - global, user, or project file, or may be passed as the first parameter. | ||
Target is loaded from npmrc - global, user, or project file, or may be passed as a string parameter. | ||
```shell | ||
@@ -15,7 +15,31 @@ npm set dav http://user:pass@localhost:8000/ | ||
gulp.task( 'default', function () { | ||
return gulp.src( '*' ) | ||
.pipe( dav() ) | ||
var options = { | ||
'log': 'info' | ||
, 'parent': 'dist' | ||
} | ||
return gulp.src( 'dist/**' ) | ||
.pipe( dav( options ) ) | ||
} ) | ||
``` | ||
## Options | ||
Superset of [http.request options parameter](https://nodejs.org/api/http.html#http_http_request_options_callback). | ||
### options.log | ||
Logging threshold. Orthogonal to the `console` methods. | ||
string | output | ||
:-------: | -------------- | ||
`'error'` | | ||
`'warn'` | | ||
`'info'` | HTTP Responses | ||
`'log'` | Debug | ||
**Default:** `'error'` | ||
### options.parent | ||
Relative or absolute path which halves the source path [`vinyl.path`] for appending the subsequent to the DAV target URI. Use with glob `**` to prevent super-directories from being created on the target. e.g. `gulp.src( 'dist/**' )`. | ||
**Default:** `process.cwd()` | ||
## Development | ||
@@ -22,0 +46,0 @@ ```shell |
@@ -47,3 +47,6 @@ var assert = require( 'assert' ) | ||
var expected_path = path.join( node, MOCK ) | ||
var mock = new Vinyl( { path: MOCK, contents: new Buffer( MOCK ) } ) | ||
var mock = new Vinyl( { | ||
path: path.resolve( MOCK ) | ||
, contents: new Buffer( MOCK ) | ||
} ) | ||
var uri = url.parse( HREF ) | ||
@@ -72,3 +75,6 @@ uri.protocol = 'dav:' | ||
var expected_path = path.join( node, MOCK ) | ||
var mock = new Vinyl( { path: MOCK, contents: new Buffer( MOCK ) } ) | ||
var mock = new Vinyl( { | ||
path: path.resolve( MOCK ) | ||
, contents: new Buffer( MOCK ) | ||
} ) | ||
var uri = url.parse( HREF ) | ||
@@ -113,3 +119,5 @@ var mal = { | ||
var expected_path = path.join( node, MOCK ) | ||
var mock = new Vinyl( { path: MOCK } ) | ||
var mock = new Vinyl( { | ||
path: path.resolve( MOCK ) | ||
} ) | ||
assert( mock.isNull(), 'vinyl.isNull()' ) | ||
@@ -130,3 +138,6 @@ var unit = mod( HREF ) | ||
var expected_path = path.join( node, MOCK ) | ||
var mock = new Vinyl( { path: MOCK, contents: new Buffer( MOCK ) } ) | ||
var mock = new Vinyl( { | ||
path: path.resolve( MOCK ) | ||
, contents: new Buffer( MOCK ) | ||
} ) | ||
assert( mock.isBuffer(), 'vinyl.isBuffer()' ) | ||
@@ -144,4 +155,6 @@ var unit = mod( HREF ) | ||
var expected_path = path.join( node, MOCK ) | ||
var mock = new Vinyl | ||
( { path: MOCK , contents: es.readArray( [ MOCK ] ) } ) | ||
var mock = new Vinyl( { | ||
path: path.resolve( MOCK ) | ||
, contents: es.readArray( [ MOCK ] ) | ||
} ) | ||
assert( mock.isStream(), 'vinyl.isStream()' ) | ||
@@ -148,0 +161,0 @@ var unit = mod( HREF ) |
18964
9
514
52
3
12
+ Addedchalk@~1.1.0