Skip to content
Browse files

Version 0.2: New binary and skipping of already downloaded directories.

  • Loading branch information...
1 parent a3ab1d9 commit 00a6a0f423fe5355731823d8eaa63cc37fc54447 @lightsofapollo lightsofapollo committed Jul 27, 2013
Showing with 142 additions and 25 deletions.
  1. +5 −0 HISTORY.md
  2. +17 −5 Makefile
  3. +62 −0 bin/mozilla-download
  4. +2 −2 fixtures.js
  5. +31 −10 lib/download.js
  6. +3 −2 package.json
  7. +21 −0 test/download-test.js
  8. +0 −6 test/extract-test.js
  9. +1 −0 test/mocha.opts
View
5 HISTORY.md
@@ -1,3 +1,8 @@
+# 0.2.0
+ - add binary mozilla-download
+ - don't attempt to clobber existing directories with error-less
+ and error resolving modes (new .strict option).
+
# 0.1.0
- make .download API eaiser to use with default autodetection and less
required params
View
22 Makefile
@@ -1,9 +1,21 @@
+default: test
+
+test/extract-out:
+ mkdir test/extract-out
+
+test/fixtures:
+ mkdir test/fixtures
+ node fixtures.js
+
+node_modules:
+ npm install
+
.PHONY: test
-test:
- ./node_modules/mocha/bin/mocha --ui tdd \
- test/detect-os.js \
+test: node_modules test/fixtures test/extract-out
+ ./node_modules/mocha/bin/mocha \
+ test/detectos-test.js \
test/extract-test.js
.PHONY: test-full
-test-full:
- ./node_modules/mocha/bin/mocha --reporter spec --ui tdd -t 100s
+test-full: node_modules test/fixtures test/extract-out
+ ./node_modules/mocha/bin/mocha --reporter spec -t 100s
View
62 bin/mozilla-download
@@ -0,0 +1,62 @@
+#! /usr/bin/env node
+
+var fs = require('fs'),
+ mozdown = require('../index'),
+ program = require('commander');
+
+function verbose() {
+ var args = Array.prototype.slice.call(arguments);
+ if (program.verbose)
+ console.log.apply(console, arguments);
+}
+
+program.
+ version(require('../package.json').version).
+ usage('[options] <target>').
+ option('--verbose').
+ option('--product-version [latest]', 'version of product to download').
+ option('-P, --product [firefox]', 'product to download').
+ option('--os [type]', 'override detected operating system').
+ parse(process.argv);
+
+var target = program.args[0];
+
+if (!target) {
+ console.log('must pass a target directory to download product in');
+ process.exit(1);
+}
+
+// abort with success if target exists
+if (fs.existsSync(target)) {
+ verbose('directory exists', target, 'exiting');
+ process.exit();
+}
+
+var product = program.product || 'firefox',
+ version = program.productVersion,
+ os = program.os;
+
+var downloadOptions = {};
+
+if (os)
+ downloadOptions.os = os;
+
+if (version)
+ downloadOptions.version = version;
+
+verbose(
+ 'downloading product: "%s" to "%s" with options "%s"',
+ product,
+ target,
+ JSON.stringify(downloadOptions)
+);
+
+mozdown.download(product, target, downloadOptions, function(err) {
+ if (err) {
+ console.log(err.message);
+ process.exit(1);
+ }
+
+ verbose('download success');
+ process.exit();
+});
View
4 fixtures.js
@@ -6,11 +6,11 @@ var FIXTURE_ROOT = fsPath.join(__dirname, 'test', 'fixtures');
var list = [
// source, target
[
- 'http://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/latest-mozilla-central/b2g-23.0a1.multi.mac64.dmg',
+ 'http://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/latest-mozilla-central/b2g-25.0a1.multi.mac64.dmg',
'b2g.dmg'
],
[
- 'http://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/latest-mozilla-central/b2g-24.0a1.multi.linux-x86_64.tar.bz2',
+ 'http://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/latest-mozilla-central/b2g-25.0a1.multi.linux-x86_64.tar.bz2',
'b2g.tar.bz2'
],
[
View
41 lib/download.js
@@ -18,22 +18,14 @@ var DEFAULT_VERSION = 'nightly';
* Options:
* - os: to download for (see firefox-get)
* - version: or channel (like 18 or 'nightly')
+ * - strict: send an error if given path exists.
*
* @param {String} product to download (like firefox)
* @param {String} path to download file to.
* @param {Object} [options] optional set of configuration options.
* @param {Function} callback [err, path];
*/
function download(product, path, options, callback) {
- if (typeof options === 'function') {
- callback = options;
- options = undefined;
- }
-
- if (!options) {
- options = {};
- }
-
var os = options.os || detectOS(product);
var version = options.version || DEFAULT_VERSION;
@@ -62,4 +54,33 @@ function download(product, path, options, callback) {
mozget[product](version, { os: os }, saveToTemp);
}
-module.exports = download;
+function checkDownload(product, path, options, callback) {
+ if (typeof options === 'function') {
+ callback = options;
+ options = null;
+ }
+
+ options = options || {};
+
+ var strictErrors = options && options.strict;
+
+ // don't clobber an existing path
+ fs.exists(path, function(itDoes) {
+
+ // bail immediately if path is filled
+ if (itDoes) {
+ // unless strict is on we simply return the given path assuming it works.
+ if (!strictErrors) return callback(null, path);
+
+ // in other cases the caller might want an error.
+ return callback(
+ new Error('cannot clobber existing path with download: "' + path + '"')
+ );
+ }
+
+ // if path does not exist continue with normal download process.
+ download(product, path, options, callback);
+ });
+}
+
+module.exports = checkDownload;
View
5 package.json
@@ -1,6 +1,6 @@
{
"name": "mozilla-download",
- "version": "0.1.0",
+ "version": "0.2.0",
"author": "James Lal [:lightsofapollo]",
"description": "Download firefox/b2g-desktop runtimes",
"main": "index.js",
@@ -15,7 +15,8 @@
"firefox-get": "~0.0.4",
"dmg": "~0.0.3",
"tmp": "~0.0.20",
- "ncp": "~0.4"
+ "ncp": "~0.4",
+ "commander": "~2.0.0"
},
"devDependencies": {
"mocha": "~1.7"
View
21 test/download-test.js
@@ -3,6 +3,27 @@ suite('download', function() {
var runner = require('../index');
var fs = require('fs');
+ suite('when path exists', function() {
+ var path = __dirname + '/fixtures/b2g.dmg';
+ test('non-strict', function(done) {
+ runner.download('b2g', path, function(err, givenPath) {
+ assert.equal(givenPath, path);
+ done(err);
+ });
+ });
+
+ test('strict', function(done) {
+ runner.download('b2g', path, { strict: true }, function(err, givenPath) {
+ if (!err) {
+ done(new Error('strict must return an error when file exists'));
+ return;
+ }
+ done();
+ });
+ });
+ });
+
+
suite('mac64', function() {
var path = __dirname + '/darwin-out/';
if (process.platform !== 'darwin')
View
6 test/extract-test.js
@@ -36,7 +36,6 @@ suite('extract', function() {
suite('product: b2g', function() {
suite('dmg', function() {
- this.timeout('20s');
if (process.platform !== 'darwin')
return test('dmg can only be run on process.platform === darwin');
@@ -52,7 +51,6 @@ suite('extract', function() {
});
suite('tar.bz2', function() {
- this.timeout('20s');
if (process.platform === 'win32')
return test('cannot run on windows');
@@ -70,7 +68,6 @@ suite('extract', function() {
suite('product: firefox', function() {
suite('nightly dmg', function() {
- this.timeout('20s');
if (process.platform !== 'darwin')
return test('dmg can only be run on process.platform === darwin');
@@ -86,7 +83,6 @@ suite('extract', function() {
});
suite('release dmg', function() {
- this.timeout('20s');
if (process.platform !== 'darwin')
return test('dmg can only be run on process.platform === darwin');
@@ -102,7 +98,6 @@ suite('extract', function() {
});
suite('nightly tar.bz2', function() {
- this.timeout('20s');
if (process.platform === 'win32')
return test('cannot run on windows');
@@ -118,7 +113,6 @@ suite('extract', function() {
});
suite('release tar.bz2', function() {
- this.timeout('20s');
if (process.platform === 'win32')
return test('cannot run on windows');
View
1 test/mocha.opts
@@ -0,0 +1 @@
+--ui tdd

0 comments on commit 00a6a0f

Please sign in to comment.
Something went wrong with that request. Please try again.