Skip to content

Commit b34992c

Browse files
committed
refactor: extract travis module
1 parent 22663cb commit b34992c

File tree

2 files changed

+75
-50
lines changed

2 files changed

+75
-50
lines changed

lib/index.js

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,21 @@
11
'use strict';
22

3-
const Fs = require('fs');
43
const Path = require('path');
5-
const Yaml = require('js-yaml');
64

5+
const Travis = require('./travis');
76

8-
const internals = {};
9-
10-
internals.detectFromTravisYaml = (travisYaml) => {
11-
12-
const raw = new Set();
13-
14-
if (travisYaml.node_js) {
15-
const versions = Array.isArray(travisYaml.node_js) ? travisYaml.node_js : [travisYaml.node_js];
16-
17-
versions.forEach((v) => raw.add(v));
18-
}
19-
20-
if (travisYaml.env && travisYaml.env.matrix) {
21-
22-
for (const env of travisYaml.env.matrix) {
23-
24-
const matches = env.match(/(?:NODEJS_VER|TRAVIS_NODE_VERSION|NODE_VER)="?(node\/)?(?<version>[\w./*]+)"?/); /* hack syntax highlighter 🤦‍♂️ */
25-
26-
if (matches) {
27-
raw.add(matches.groups.version);
28-
}
29-
}
30-
}
31-
32-
if (travisYaml.matrix && travisYaml.matrix.include) {
33-
34-
const includes = Array.isArray(travisYaml.matrix.include) ? travisYaml.matrix.include : [travisYaml.matrix.include];
35-
for (const include of includes) {
36-
37-
if (include.node_js) {
38-
raw.add(include.node_js);
39-
}
40-
}
41-
}
42-
43-
if (!raw.size) {
44-
raw.add('latest');
45-
}
46-
47-
return { raw: [...raw] };
48-
};
49-
50-
51-
module.exports.detect = ({ path }) => {
7+
exports.detect = ({ path }) => {
528

539
const packageJson = require(Path.join(path, 'package.json'));
5410

5511
const { name, version } = packageJson;
5612

5713
const result = { name, version };
5814

59-
const travisYamlPath = Path.join(path, '.travis.yml');
15+
const travis = Travis.detect({ path });
6016

61-
if (Fs.existsSync(travisYamlPath)) {
62-
const travisYaml = Yaml.safeLoad(Fs.readFileSync(travisYamlPath), { schema: Yaml.FAILSAFE_SCHEMA });
63-
result.travis = internals.detectFromTravisYaml(travisYaml);
17+
if (travis) {
18+
result.travis = travis;
6419
}
6520

6621
return result;

lib/travis.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
const Fs = require('fs');
4+
const Path = require('path');
5+
const Yaml = require('js-yaml');
6+
7+
8+
const internals = {};
9+
10+
11+
internals.toArray = (v) => {
12+
13+
if (v === undefined) {
14+
return [];
15+
}
16+
17+
return Array.isArray(v) ? v : [v];
18+
};
19+
20+
21+
internals.scan = (travisYaml) => {
22+
23+
const raw = new Set();
24+
25+
for (const v of internals.toArray(travisYaml.node_js)) {
26+
raw.add(v);
27+
}
28+
29+
if (travisYaml.env) {
30+
31+
for (const env of internals.toArray(travisYaml.env.matrix)) {
32+
33+
const matches = env.match(/(?:NODEJS_VER|TRAVIS_NODE_VERSION|NODE_VER)="?(node\/)?(?<version>[\w./*]+)"?/); /* hack syntax highlighter 🤦‍♂️ */
34+
35+
if (matches) {
36+
raw.add(matches.groups.version);
37+
}
38+
}
39+
}
40+
41+
if (travisYaml.matrix) {
42+
43+
for (const include of internals.toArray(travisYaml.matrix.include)) {
44+
45+
if (include.node_js) {
46+
raw.add(include.node_js);
47+
}
48+
}
49+
}
50+
51+
if (!raw.size) {
52+
raw.add('latest');
53+
}
54+
55+
return { raw: [...raw] };
56+
};
57+
58+
59+
exports.detect = ({ path }) => {
60+
61+
const travisYamlPath = Path.join(path, '.travis.yml');
62+
63+
if (!Fs.existsSync(travisYamlPath)) {
64+
return;
65+
}
66+
67+
const travisYaml = Yaml.safeLoad(Fs.readFileSync(travisYamlPath), { schema: Yaml.FAILSAFE_SCHEMA });
68+
69+
return internals.scan(travisYaml);
70+
};

0 commit comments

Comments
 (0)