Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

Commit 950ea39

Browse files
author
axelrindle
committed
Added Mocha as test framework and the exported check method can now return a Promise.
1 parent edbc48b commit 950ea39

5 files changed

Lines changed: 83 additions & 46 deletions

File tree

lib/main.coffee

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,40 @@ request = require 'request'
33
semcmp = require 'semver-compare'
44
chalk = require 'chalk'
55
sprintf = require('sprintf-js').sprintf
6+
Promise = require 'promise'
67

78
baseApiUrl = 'https://api.github.com/repos/%s/releases'
89

910
module.exports = (options, callback) ->
11+
if callback
12+
check options, callback
13+
else
14+
return new Promise (resolve, reject) ->
15+
check options, (release, error) ->
16+
reject error if error
17+
if release then resolve release
18+
else resolve null
19+
20+
check = (options, callback) ->
1021

1122
# get options
1223
repo = options.repo
1324
currentVersion = options.currentVersion
1425

1526
# check if required options are defined
16-
error 'no repository specified' if repo is undefined
17-
error 'no current version given' if currentVersion is undefined
27+
callback(null, 'no repository specified') if repo is undefined
28+
callback(null, 'no current version given') if currentVersion is undefined
1829

1930
apiUrl = sprintf baseApiUrl, repo
2031

2132
# request options
2233
reqOpts =
2334
url: apiUrl
2435
headers:
25-
'User-Agent': options.useragent or 'github-version-checker'
36+
'User-Agent': 'github-version-checker'
2637

2738
request reqOpts, (error, response, body) ->
28-
throw error if error
39+
callback(null, error) if error
2940

3041
# parse the response body into an object
3142
releases = JSON.parse body
@@ -39,11 +50,9 @@ module.exports = (options, callback) ->
3950
tag = release.tag_name.replace(/[^0-9$.,]/g, '')
4051
if semcmp(currentVersion, tag) is -1
4152
found = true
42-
callback release
53+
callback release, null
4354
break
4455

45-
callback null if not found
56+
callback(null, null) if not found
4657

47-
# error method
48-
error = (cause) ->
49-
throw chalk.bgRed.bold('Error!') + ' ' + cause
58+
return null

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Version checker working with GitHub releases.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "node test.js"
7+
"test": "mocha"
88
},
99
"keywords": [
1010
"node",
@@ -15,14 +15,17 @@
1515
"license": "MIT",
1616
"dependencies": {
1717
"chalk": "^1.1.3",
18-
"coffee-script": "^1.12.4",
18+
"coffee-script": "^1.12.5",
1919
"gulp": "github:gulpjs/gulp#4.0",
20+
"promise": "^7.1.1",
2021
"request": "^2.81.0",
2122
"semver-compare": "^1.0.0",
2223
"sprintf-js": "^1.0.3"
2324
},
2425
"devDependencies": {
2526
"gulp": "^3.9.1",
26-
"gulp-coffeelint": "^0.6.0"
27+
"gulp-coffeelint": "^0.6.0",
28+
"mocha": "^3.2.0",
29+
"mocha-logger": "^1.0.5"
2730
}
2831
}

test.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/test-promise-handling.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
// register coffee-script
5+
require('coffee-script/register');
6+
7+
// require modules
8+
const versionCheck = require('../lib/main.coffee');
9+
const assert = require('assert');
10+
11+
const options = {
12+
repo: 'axelrindle/github-version-checker',
13+
currentVersion: "1.0.0"
14+
};
15+
16+
describe('github-version-checker', function () {
17+
describe('#versionCheck with one parameter and correct options', function () {
18+
it('should resolve the Promise without errors', function () {
19+
versionCheck(options).then(
20+
function (update) {
21+
assert.notEqual(update, undefined);
22+
},
23+
function (error) {
24+
throw error;
25+
}
26+
);
27+
});
28+
});
29+
});

test/test-return-types.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
// register coffee-script
5+
require('coffee-script/register');
6+
7+
// require modules
8+
const versionCheck = require('../lib/main.coffee');
9+
const assert = require('assert');
10+
11+
const options = {
12+
repo: 'axelrindle/github-version-checker',
13+
currentVersion: "1.0.0"
14+
};
15+
16+
describe('github-version-checker', function () {
17+
describe('#versionCheck with two parameters', function () {
18+
it('should return null', function () {
19+
assert.equal(versionCheck(options, function(update, error) {}), null);
20+
});
21+
});
22+
});
23+
24+
describe('github-version-checker', function () {
25+
describe('#versionCheck with only one options parameter', function () {
26+
it('should return a Promise', function () {
27+
assert.equal(typeof versionCheck(options).then, 'function');
28+
});
29+
});
30+
});

0 commit comments

Comments
 (0)