Comparing version 3.7.1 to 3.7.2
@@ -12,2 +12,3 @@ #!/usr/bin/env node | ||
.command('watch', 'Watch for file changes') | ||
.command('version', 'Get version information') | ||
.example('$0 --cartridge app-storefront-core', 'uploading a cartridge') | ||
@@ -46,2 +47,5 @@ .example('$0 watch --cartridge base', 'watch for changes and upload automatically') | ||
}, | ||
'skip-upload': { | ||
describe: 'Skip initial upload when watching' | ||
}, | ||
'root': { | ||
@@ -48,0 +52,0 @@ alias: 'r', |
155
lib/index.js
@@ -14,2 +14,3 @@ 'use strict'; | ||
var log = require('./log'); | ||
var chalk = require('chalk'); | ||
@@ -35,2 +36,3 @@ // return an array of all option values | ||
var dwdav = DWDAV(conf); | ||
var command = conf._[0]; | ||
var excludes = processOpts(conf.exclude); | ||
@@ -45,2 +47,11 @@ // default exclude patterns | ||
if (!conf.file && !conf.cartridge) { | ||
return cb(new Error('Either a file or cartridge must be declared. See --help for more details.')); | ||
} | ||
var cartridges = processOpts(conf.cartridge); | ||
var files = processOpts(conf.file); | ||
var isCartridge = Boolean(cartridges); | ||
// having a cartridge flag will override file flag | ||
var toUploads = isCartridge ? cartridges : files; | ||
function deleteFile (filePath) { | ||
@@ -53,2 +64,5 @@ return dwdav.delete(filePath) | ||
function uploadFile (filePath) { | ||
if (!fs.existsSync(filePath)) { | ||
return Promise.reject(new Error(filePath + ' does not exist.')); | ||
} | ||
return dwdav.delete(filePath) | ||
@@ -61,3 +75,12 @@ .then(function () { | ||
} | ||
function createDirectory (dirPath) { | ||
return dwdav.mkcol(dirPath) | ||
.then(function () { | ||
log.success('Successfully created directory: ' + dirPath); | ||
}); | ||
} | ||
function uploadCartridge (cartridge) { | ||
if (!fs.existsSync(cartridge)) { | ||
return Promise.reject(new Error(cartridge + ' does not exist')); | ||
} | ||
/* | ||
@@ -69,3 +92,3 @@ * if `cartridge` is `path/to/cartridge`, then | ||
* - `realPath` is `full/system/path/to/cartridge/file.txt` | ||
* - `meatdataPath` is `cartrdige/file.txt` | ||
* - `metadataPath` is `cartridge/file.txt` | ||
*/ | ||
@@ -76,2 +99,4 @@ var dirname = path.dirname(cartridge); | ||
var zipCartridgeName = cartridgeName + '.zip'; | ||
// put the zip cartridge in the root folder, default to cwd | ||
var zipCartridgePath = path.resolve(conf.root || '', zipCartridgeName); | ||
@@ -101,3 +126,3 @@ return new Promise(function (resolve, reject) { | ||
zipCartridge.outputStream | ||
.pipe(fs.createWriteStream(zipCartridgeName)) | ||
.pipe(fs.createWriteStream(zipCartridgePath)) | ||
.on('close', function () { | ||
@@ -109,11 +134,14 @@ resolve(); | ||
}).then(function () { | ||
return dwdav.postAndUnzip(zipCartridgeName); | ||
// upload the zip file | ||
return dwdav.postAndUnzip(zipCartridgePath); | ||
}).then(function () { | ||
// delete the zip file on the webdav server | ||
return dwdav.delete(zipCartridgeName); | ||
}).then(function () { | ||
log.success('Uploaded cartridge: ' + cartridge); | ||
return del(zipCartridgeName); | ||
log.success('Successfully uploaded cartridge: ' + cartridge); | ||
// delete local zip file | ||
return del(zipCartridgePath); | ||
}, function (err) { | ||
// delete local zip even when there's an error with the upload | ||
return del(zipCartridgeName) | ||
return del(zipCartridgePath) | ||
.then(function () { | ||
@@ -126,24 +154,70 @@ // pass the error along | ||
// actions | ||
if (!conf.file && !conf.cartridge) { | ||
return cb(new Error('Either a file or cartridge must be declared. See --help for more details.')); | ||
function showVersionInformation () { | ||
console.log('Current target version is: ' + chalk.blue.bold(conf.version)); | ||
// get .version file, which lives at the parent Cartridges folder | ||
dwdav.get('../.version') | ||
.then((response) => { | ||
// parse .version file to find active version | ||
// sample | ||
/* | ||
########################################### | ||
# Generated file, do not edit. | ||
# Copyright (c) 2016 by Demandware, Inc. | ||
########################################### | ||
fileVersion=1 | ||
maxVersions=0 | ||
version1/1473910765602/1481569878000 | ||
remote/1473910759457/1456936758000 | ||
# end of file marker | ||
*/ | ||
let lines = response.split('\n'); | ||
let activeVersion = ''; | ||
let otherVersions = []; | ||
for (let line of lines) { | ||
// get first non-commented line with slash | ||
if (line[0] === '#') { | ||
continue; | ||
} | ||
let slashIndex = line.indexOf('/'); | ||
if (slashIndex < 0) { | ||
continue; | ||
} | ||
if (!activeVersion) { | ||
activeVersion = line.substring(0, slashIndex); | ||
continue; | ||
} | ||
otherVersions.push(line.substring(0, slashIndex)); | ||
} | ||
console.log('Current active version is: ' + chalk.green.bold(activeVersion)); | ||
console.log('Other versions available:'); | ||
otherVersions.forEach((v) => { | ||
console.log('- ' + chalk.blue(v)); | ||
}) | ||
}).then(null, (err) => { | ||
console.error(err); | ||
}); | ||
} | ||
var cartridges = processOpts(conf.cartridge); | ||
var files = processOpts(conf.file); | ||
var isCartridge = Boolean(cartridges); | ||
var command = conf._[0]; | ||
// having a cartridge flag will override file flag | ||
var toUploads = isCartridge ? cartridges : files; | ||
function uploadOrDelete (toUploads) { | ||
if (conf['skip-upload']) { | ||
return Promise.resolve(); | ||
} | ||
var action; | ||
let action; | ||
if (command === 'delete') { | ||
action = deleteFile; | ||
} else { | ||
action = isCartridge ? uploadCartridge : uploadFile; | ||
if (command === 'delete') { | ||
action = deleteFile; | ||
} else { | ||
action = isCartridge ? uploadCartridge : uploadFile; | ||
} | ||
return toUploads.reduce(function (acc, toUpload) { | ||
return acc.then(function () { | ||
return action(toUpload); | ||
}); | ||
}, Promise.resolve()); | ||
} | ||
if (command === 'watch') { | ||
function watchAndUpload (toWatch) { | ||
var queue = new Queue(); | ||
toUploads.forEach(function (dir) { | ||
toWatch.forEach(function (dir) { | ||
watch.watchTree(dir, { | ||
@@ -160,3 +234,5 @@ filter: function (f, stat) { | ||
if (typeof f === 'object' && prev === null && curr === null) { | ||
// ignore, finished walking the tree | ||
// finished walking the tree | ||
// First item in f is the parent directory | ||
log.info('Watching ' + Object.keys(f)[0]); | ||
} else if (curr.nlink === 0) { | ||
@@ -174,5 +250,16 @@ // f was removed | ||
}) | ||
} else if (fs.lstatSync(f).isDirectory()) { | ||
log.info('Directory ' + f + ' was changed.'); | ||
queue.place(function () { | ||
createDirectory(f) | ||
.then(function () { | ||
queue.next(); | ||
}, function (err) { | ||
log.error(err); | ||
queue.next(); | ||
}); | ||
}); | ||
} else { | ||
// f was created or changed | ||
log.info(f + ' was changed.'); | ||
log.info('File ' + f + ' was changed.'); | ||
queue.place(function () { | ||
@@ -190,10 +277,16 @@ uploadFile(f) | ||
}); | ||
} else { | ||
toUploads.reduce(function (acc, toUpload) { | ||
return acc.then(function () { | ||
return action(toUpload); | ||
}); | ||
}, Promise.resolve()) | ||
.then(cb.bind(undefined, null), cb); | ||
} | ||
// actions | ||
if (command === 'version') { | ||
showVersionInformation(); | ||
return; | ||
} | ||
uploadOrDelete(toUploads) | ||
.then(function () { | ||
if (command !== 'watch') { | ||
return cb(); | ||
} | ||
watchAndUpload(toUploads); | ||
}, cb); | ||
}; |
@@ -7,3 +7,9 @@ var chalk = require('chalk'); | ||
function makePrefix(str) { | ||
const types = { | ||
info: chalk.blue, | ||
success: chalk.green, | ||
error: chalk.red | ||
}; | ||
function addTimePrefix(str) { | ||
return chalk.reset('[' + new Date().toLocaleTimeString('en-US', {hour12: false}) + | ||
@@ -13,18 +19,16 @@ '] ') + str | ||
function logInfo(str) { | ||
console.log(makePrefix(info(str))); | ||
function makeLogMethod(method, str, opts = {silent: false, time: true}) { | ||
let logMethod = console.log.bind(console); | ||
if (method === 'error') { | ||
logMethod = console.error.bind(console); | ||
} | ||
return function (str, silent) { | ||
logMethod(addTimePrefix(types[method](str))); | ||
} | ||
} | ||
function logSuccess(str) { | ||
console.log(makePrefix(success(str))); | ||
} | ||
function logError(str) { | ||
console.error(makePrefix(error(str))); | ||
} | ||
module.exports = { | ||
info: logInfo, | ||
success: logSuccess, | ||
error: logError | ||
info: makeLogMethod('info'), | ||
success: makeLogMethod('success'), | ||
error: makeLogMethod('error') | ||
}; |
{ | ||
"name": "dwupload", | ||
"version": "3.7.1", | ||
"version": "3.7.2", | ||
"description": "Upload storefront cartridges to Demandware WebDAV server", | ||
@@ -24,3 +24,3 @@ "main": "index.js", | ||
"del": "^1.2.0", | ||
"dwdav": "2fa", | ||
"dwdav": "^3.1.0", | ||
"jszip": "^2.5.0", | ||
@@ -27,0 +27,0 @@ "multimatch": "^2.1.0", |
@@ -14,8 +14,20 @@ # dwupload | ||
```shell | ||
:; dwupload --hostname example.demandware.net --username admin --password password --cartridge app_storefront_core --code-version version1 # uploading a carridge | ||
:; dwupload --file path/to/app.js --file path/to/style.css # uploading file(s) using configuration in `dw.json` | ||
:; dwupload watch --cartridge app_storefront_controllers # watch for file changes and upload automatically | ||
:; dwupload delete --file rootDir/path/to/file --root rootDir # delete a file, with root option | ||
:; dwupload --hostname cert.example.demandware.net --username admin --password password --p12 admin.p12 --passphrase passphrase # 2-factor authentication | ||
```shell | ||
# uploading a carridge | ||
:; dwupload --hostname example.demandware.net --username admin --password password --cartridge app_storefront_core --code-version version1 | ||
# uploading file(s) using configuration in `dw.json` | ||
:; dwupload --file path/to/app.js --file path/to/style.css | ||
# watch for file changes and upload automatically | ||
:; dwupload watch --cartridge app_storefront_controllers | ||
# delete a file, with root option | ||
:; dwupload delete --file rootDir/path/to/file --root rootDir | ||
# 2-factor authentication | ||
:; dwupload --hostname cert.example.demandware.net --username admin --password password --p12 admin.p12 --passphrase passphrase | ||
# get version information | ||
:; dwupload version | ||
``` | ||
@@ -22,0 +34,0 @@ See `--help` for more information. |
@@ -5,2 +5,3 @@ var tap = require('tap'); | ||
var sinon = require('sinon'); | ||
var path = require('path'); | ||
@@ -57,3 +58,3 @@ var execPath = require.resolve('../'); | ||
_: [], | ||
file: 'fixtures/cartridge/index.html' | ||
file: 'test/fixtures/cartridge/index.html' | ||
}; | ||
@@ -88,1 +89,54 @@ dwupload(conf, function (err) { | ||
}); | ||
tap.test('upload a cartridge', function (t) { | ||
var dwdavDelete = sinon.stub().returns(Promise.resolve()); | ||
var dwdavPostAndUnzip = sinon.stub().returns(Promise.resolve()); | ||
var dwdav = sinon.stub().returns({ | ||
delete: dwdavDelete, | ||
postAndUnzip: dwdavPostAndUnzip | ||
}); | ||
var dwupload = requireInject('../lib', { | ||
dwdav: dwdav | ||
}); | ||
var conf = { | ||
_: [], | ||
cartridge: 'test/fixtures', | ||
root: 'test' | ||
}; | ||
dwupload(conf, function (err) { | ||
t.notOk(err); | ||
t.ok(dwdav.calledWith(conf)); | ||
t.ok(dwdavDelete.firstCall.calledWith('fixtures')); | ||
t.ok(dwdavPostAndUnzip.calledWith(path.resolve('test', 'fixtures.zip'))); | ||
t.ok(dwdavDelete.secondCall.calledWith('fixtures.zip')); | ||
t.end(); | ||
}) | ||
}); | ||
tap.test('upload a cartridge without root', function (t) { | ||
var dwdavDelete = sinon.stub().returns(Promise.resolve()); | ||
var dwdavPostAndUnzip = sinon.stub().returns(Promise.resolve()); | ||
var dwdav = sinon.stub().returns({ | ||
delete: dwdavDelete, | ||
postAndUnzip: dwdavPostAndUnzip | ||
}); | ||
var dwupload = requireInject('../lib', { | ||
dwdav: dwdav | ||
}); | ||
var conf = { | ||
_: [], | ||
cartridge: 'test/fixtures' | ||
}; | ||
dwupload(conf, function (err) { | ||
t.notOk(err); | ||
t.ok(dwdav.calledWith(conf)); | ||
t.ok(dwdavDelete.firstCall.calledWith('fixtures')); | ||
t.ok(dwdavPostAndUnzip.calledWith(path.resolve('fixtures.zip'))); | ||
t.ok(dwdavDelete.secondCall.calledWith('fixtures.zip')); | ||
t.end(); | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25417
13
497
61
+ Addeddwdav@3.5.1(transitive)
+ Addedlodash@4.17.21(transitive)
- Removeddwdav@2.4.0(transitive)
- Removedlodash@3.10.1(transitive)
Updateddwdav@^3.1.0