Skip to content

Commit de9c320

Browse files
committed
refactor: extract autodetect logic out of CLI
1 parent 0cf79dc commit de9c320

File tree

4 files changed

+221
-34
lines changed

4 files changed

+221
-34
lines changed

bin/detect-node-support

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
'use strict';
44

5-
const Fs = require('fs');
65
const NodeSupport = require('..');
7-
const { URL } = require('url');
8-
96

107
const internals = {};
118

@@ -14,46 +11,19 @@ internals.help = () => {
1411
return `Usage: detect-node-support [ path | Github URL | npm package name ]`;
1512
};
1613

17-
18-
internals.autoDetect = (what) => {
19-
20-
try {
21-
var url = new URL(what);
22-
}
23-
catch (err) {
24-
if (err.code !== 'ERR_INVALID_URL') {
25-
throw err;
26-
}
27-
}
28-
29-
if (url) {
30-
return NodeSupport.detect({ repository: url.href });
31-
}
32-
33-
if (Fs.existsSync(what)) {
34-
return NodeSupport.detect({ path: what });
35-
}
36-
37-
if (what.includes('/') && !what.startsWith('@')) {
38-
return NodeSupport.detect({ repository: `https://github.com/${what}` });
39-
}
40-
41-
return NodeSupport.detect({ packageName: what });
42-
};
43-
44-
exports.main = async (nodeBin, thisBin, what) => {
14+
exports.main = async ([nodeBin, thisBin, what]) => {
4515

4616
if (!what) {
4717
console.log(internals.help());
4818
return;
4919
}
5020

51-
const result = await internals.autoDetect(what);
21+
const result = await NodeSupport.autoDetect(what);
5222

5323
console.log(result);
5424
};
5525

56-
exports.main(...process.argv)
26+
exports.main(process.argv)
5727
.catch((err) => {
5828

5929
console.error(err);

lib/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict';
22

3+
const Fs = require('fs');
4+
const { URL } = require('url');
5+
36
const Engines = require('./engines');
47
const Package = require('./package');
58
const Travis = require('./travis');
69

7-
exports.detect = async ({ path, repository, packageName }) => {
10+
exports.detect = async function ({ path, repository, packageName }) {
811

912
const packageInfo = await Package.detect({ path, repository, packageName });
1013

@@ -22,3 +25,30 @@ exports.detect = async ({ path, repository, packageName }) => {
2225

2326
return result;
2427
};
28+
29+
// eslint-disable-next-line require-await
30+
exports.autoDetect = async function (what) {
31+
32+
try {
33+
var url = new URL(what);
34+
}
35+
catch (err) {
36+
if (err.code !== 'ERR_INVALID_URL') {
37+
throw err;
38+
}
39+
}
40+
41+
if (url) {
42+
return exports.detect({ repository: url.href });
43+
}
44+
45+
if (Fs.existsSync(what)) {
46+
return exports.detect({ path: what });
47+
}
48+
49+
if (what.includes('/') && !what.startsWith('@')) {
50+
return exports.detect({ repository: `https://github.com/${what}` });
51+
}
52+
53+
return exports.detect({ packageName: what });
54+
};

test/fixtures/hapi-package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@hapi/hapi",
3+
"version": "0.0.0-development",
4+
"repository": {
5+
"type": "git",
6+
"url": "git+https://github.com/hapijs/hapi.git"
7+
}
8+
}

test/index.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,183 @@ describe('detect-node-support', () => {
766766
});
767767
});
768768
});
769+
770+
describe('autoDetect()', () => {
771+
772+
it('returns node versions from `.travis.yml` at the path', async () => {
773+
774+
const path = Path.join(__dirname, '..');
775+
776+
const result = await NodeSupport.autoDetect(path);
777+
778+
internals.assertCommit(result);
779+
780+
expect(result).to.equal({
781+
name: 'detect-node-support',
782+
version: '0.0.0-development',
783+
timestamp: 1580673602000,
784+
travis: {
785+
raw: ['10', '12', '13'],
786+
resolved: {
787+
'10': '10.19.0',
788+
'12': '12.15.0',
789+
'13': '13.8.0'
790+
}
791+
},
792+
engines: '>=10'
793+
});
794+
});
795+
796+
it('returns node versions from `.travis.yml` in the repository (url case)', async () => {
797+
798+
listRemoteStub
799+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
800+
801+
Nock('https://raw.githubusercontent.com')
802+
.get('/pkgjs/detect-node-support/HEAD/package.json')
803+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
804+
.get('/pkgjs/detect-node-support/HEAD/.travis.yml')
805+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
806+
807+
const result = await NodeSupport.autoDetect('git+https://github.com/pkgjs/detect-node-support.git');
808+
809+
expect(listRemoteStub.callCount).to.equal(1);
810+
expect(listRemoteStub.args[0]).to.equal([['http://github.com/pkgjs/detect-node-support.git', 'HEAD']]);
811+
812+
expect(result).to.equal({
813+
name: 'detect-node-support',
814+
version: '0.0.0-development',
815+
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
816+
timestamp: 1580673602000,
817+
travis: {
818+
raw: ['10', '12', '13'],
819+
resolved: {
820+
'10': '10.19.0',
821+
'12': '12.15.0',
822+
'13': '13.8.0'
823+
}
824+
},
825+
engines: '>=10'
826+
});
827+
});
828+
829+
it('returns node versions from `.travis.yml` in the repository ("org/repo" case)', async () => {
830+
831+
listRemoteStub
832+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
833+
834+
Nock('https://raw.githubusercontent.com')
835+
.get('/pkgjs/detect-node-support/HEAD/package.json')
836+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
837+
.get('/pkgjs/detect-node-support/HEAD/.travis.yml')
838+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
839+
840+
const result = await NodeSupport.autoDetect('pkgjs/detect-node-support');
841+
842+
expect(listRemoteStub.callCount).to.equal(1);
843+
expect(listRemoteStub.args[0]).to.equal([['http://github.com/pkgjs/detect-node-support', 'HEAD']]);
844+
845+
expect(result).to.equal({
846+
name: 'detect-node-support',
847+
version: '0.0.0-development',
848+
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
849+
timestamp: 1580673602000,
850+
travis: {
851+
raw: ['10', '12', '13'],
852+
resolved: {
853+
'10': '10.19.0',
854+
'12': '12.15.0',
855+
'13': '13.8.0'
856+
}
857+
},
858+
engines: '>=10'
859+
});
860+
});
861+
862+
it('returns node versions from `.travis.yml` in the package repository', async () => {
863+
864+
listRemoteStub
865+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
866+
867+
Nock('https://raw.githubusercontent.com')
868+
.get('/pkgjs/detect-node-support/HEAD/package.json')
869+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')))
870+
.get('/pkgjs/detect-node-support/HEAD/.travis.yml')
871+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
872+
873+
Nock('https://registry.npmjs.org')
874+
.get('/detect-node-support')
875+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', 'package.json')));
876+
877+
const result = await NodeSupport.autoDetect('detect-node-support');
878+
879+
expect(listRemoteStub.callCount).to.equal(1);
880+
expect(listRemoteStub.args[0]).to.equal([['http://github.com/pkgjs/detect-node-support.git', 'HEAD']]);
881+
882+
expect(result).to.equal({
883+
name: 'detect-node-support',
884+
version: '0.0.0-development',
885+
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
886+
timestamp: 1580673602000,
887+
travis: {
888+
raw: ['10', '12', '13'],
889+
resolved: {
890+
'10': '10.19.0',
891+
'12': '12.15.0',
892+
'13': '13.8.0'
893+
}
894+
},
895+
engines: '>=10'
896+
});
897+
});
898+
899+
it('returns node versions from `.travis.yml` in the package repository (scoped)', async () => {
900+
901+
listRemoteStub
902+
.returns('9cef39d21ad229dea4b10295f55b0d9a83800b23\tHEAD\n');
903+
904+
Nock('https://raw.githubusercontent.com')
905+
.get('/hapijs/hapi/HEAD/package.json')
906+
.reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'hapi-package.json')))
907+
.get('/hapijs/hapi/HEAD/.travis.yml')
908+
.reply(200, Fs.readFileSync(Path.join(__dirname, '..', '.travis.yml')));
909+
910+
Nock('https://registry.npmjs.org')
911+
.get('/@hapi%2fhapi')
912+
.reply(200, Fs.readFileSync(Path.join(__dirname, 'fixtures', 'hapi-package.json')));
913+
914+
const result = await NodeSupport.autoDetect('@hapi/hapi');
915+
916+
expect(listRemoteStub.callCount).to.equal(1);
917+
expect(listRemoteStub.args[0]).to.equal([['http://github.com/hapijs/hapi.git', 'HEAD']]);
918+
919+
expect(result).to.equal({
920+
name: '@hapi/hapi',
921+
version: '0.0.0-development',
922+
commit: '9cef39d21ad229dea4b10295f55b0d9a83800b23',
923+
timestamp: 1580673602000,
924+
travis: {
925+
raw: ['10', '12', '13'],
926+
resolved: {
927+
'10': '10.19.0',
928+
'12': '12.15.0',
929+
'13': '13.8.0'
930+
}
931+
}
932+
});
933+
});
934+
935+
it('rethrows URL parser errors', async () => {
936+
937+
const badUrl = {
938+
toString: () => {
939+
940+
// dirty way to avoid getting an ERR_INVALID_URL from new Url()
941+
throw new Error('Bad URL');
942+
}
943+
};
944+
945+
await expect(NodeSupport.autoDetect(badUrl)).to.reject('Bad URL');
946+
});
947+
});
769948
});

0 commit comments

Comments
 (0)