Skip to content

Commit 9f06c86

Browse files
committed
refactor: fold autodetect() into detect()
1 parent de9c320 commit 9f06c86

File tree

4 files changed

+187
-194
lines changed

4 files changed

+187
-194
lines changed

bin/detect-node-support

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exports.main = async ([nodeBin, thisBin, what]) => {
1818
return;
1919
}
2020

21-
const result = await NodeSupport.autoDetect(what);
21+
const result = await NodeSupport.detect(what);
2222

2323
console.log(result);
2424
};

lib/index.js

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
'use strict';
22

3-
const Fs = require('fs');
4-
const { URL } = require('url');
5-
63
const Engines = require('./engines');
74
const Package = require('./package');
85
const Travis = require('./travis');
96

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

12-
const packageInfo = await Package.detect({ path, repository, packageName });
9+
const packageInfo = await Package.detect(what);
1310

1411
const result = {};
1512

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

2623
return result;
2724
};
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-
};

lib/package.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
11
'use strict';
22

3+
const Fs = require('fs');
4+
const { URL } = require('url');
5+
36
const Loader = require('./loader');
47

58

6-
exports.detect = async ({ path, repository, packageName }) => {
9+
const internals = {};
10+
11+
internals.what = (what) => {
12+
13+
if (typeof what !== 'string') {
14+
return what;
15+
}
16+
17+
try {
18+
var url = new URL(what);
19+
}
20+
catch (err) {
21+
// do nothing - attempt to use the string as a package name
22+
}
23+
24+
if (url) {
25+
return { repository: url.href };
26+
}
27+
28+
if (Fs.existsSync(what)) {
29+
return { path: what };
30+
}
31+
32+
if (what.includes('/') && !what.startsWith('@')) {
33+
return { repository: `https://github.com/${what}` };
34+
}
35+
36+
return { packageName: what };
37+
};
38+
39+
40+
exports.detect = async (what) => {
41+
42+
const { path, repository, packageName } = internals.what(what);
743

844
const { loadFile, getCommit } = await Loader.create({ path, repository, packageName });
945

0 commit comments

Comments
 (0)