Skip to content

Commit 0127ae8

Browse files
committed
refactor: extract loader
1 parent 7ab9d3e commit 0127ae8

File tree

4 files changed

+56
-44
lines changed

4 files changed

+56
-44
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exports.detect = async ({ path, repository, packageName }) => {
1212
result.name = packageInfo.name;
1313
result.version = packageInfo.version;
1414

15-
const travis = await Travis.detect({ path, repository });
15+
const travis = await Travis.detect(packageInfo);
1616

1717
if (travis) {
1818
result.travis = travis;

lib/loader.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const Fs = require('fs');
4+
const GitUrlParse = require('git-url-parse');
5+
const Path = require('path');
6+
const Wreck = require('@hapi/wreck');
7+
8+
9+
const internals = {};
10+
11+
12+
internals.createRepositoryLoader = (repository) => {
13+
14+
const parsedRepository = GitUrlParse(repository);
15+
16+
return async (filename) => {
17+
18+
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
19+
20+
const { payload } = await Wreck.get(url);
21+
22+
return payload;
23+
};
24+
};
25+
26+
27+
internals.createPathLoader = (path) => {
28+
29+
return (filename) => {
30+
31+
const fullPath = Path.join(path, filename);
32+
33+
if (!Fs.existsSync(fullPath)) {
34+
return;
35+
}
36+
37+
return Fs.readFileSync(fullPath);
38+
};
39+
};
40+
41+
42+
exports.create = ({ path, repository }) => {
43+
44+
return repository
45+
? internals.createRepositoryLoader(repository)
46+
: internals.createPathLoader(path);
47+
};

lib/package.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
'use strict';
22

3-
const GitUrlParse = require('git-url-parse');
4-
const Path = require('path');
5-
const Wreck = require('@hapi/wreck');
3+
const Loader = require('./loader');
64

75

86
exports.detect = async ({ path, repository, packageName }) => {
97

10-
let packageJson;
8+
const loadFile = Loader.create({ path, repository, packageName });
119

12-
if (repository) {
13-
const parsedRepository = GitUrlParse(repository);
14-
15-
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/package.json`;
16-
17-
const { payload } = await Wreck.get(url);
18-
19-
packageJson = JSON.parse(payload.toString());
20-
}
21-
22-
if (path) {
23-
packageJson = require(Path.join(path, 'package.json'));
24-
}
10+
const packageJson = JSON.parse((await loadFile('package.json')).toString());
2511

2612
const { name, version } = packageJson;
2713

28-
return { name, version };
14+
return { name, version, loadFile };
2915
};

lib/travis.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
'use strict';
22

3-
const Fs = require('fs');
4-
const GitUrlParse = require('git-url-parse');
5-
const Path = require('path');
6-
const Wreck = require('@hapi/wreck');
73
const Yaml = require('js-yaml');
84

95

@@ -58,29 +54,12 @@ internals.scan = (travisYaml) => {
5854
};
5955

6056

61-
exports.detect = async ({ path, repository }) => {
57+
exports.detect = async ({ loadFile }) => {
6258

63-
let buffer;
59+
const buffer = await loadFile('.travis.yml');
6460

65-
if (repository) {
66-
const parsedRepository = GitUrlParse(repository);
67-
68-
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/.travis.yml`;
69-
70-
const result = await Wreck.get(url);
71-
72-
buffer = result.payload;
73-
}
74-
75-
if (path) {
76-
77-
const travisYamlPath = Path.join(path, '.travis.yml');
78-
79-
if (!Fs.existsSync(travisYamlPath)) {
80-
return;
81-
}
82-
83-
buffer = Fs.readFileSync(travisYamlPath);
61+
if (buffer === undefined) {
62+
return;
8463
}
8564

8665
const travisYaml = Yaml.safeLoad(buffer, { schema: Yaml.FAILSAFE_SCHEMA });

0 commit comments

Comments
 (0)