Skip to content

Commit 11329ed

Browse files
committed
refactor: split up travis/index.js
1 parent a8a1d86 commit 11329ed

File tree

3 files changed

+110
-100
lines changed

3 files changed

+110
-100
lines changed

lib/travis/imports.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
3+
const Debug = require('debug');
4+
const Yaml = require('js-yaml');
5+
6+
const Loader = require('../loader');
7+
const Utils = require('../utils');
8+
9+
10+
const internals = {};
11+
12+
13+
internals.log = Debug('detect-node-support');
14+
15+
16+
internals.normalizeImports = (travisYaml, { relativeTo }) => {
17+
18+
return Utils.toArray(travisYaml.import)
19+
.map((entry) => {
20+
21+
if (typeof entry === 'string') {
22+
entry = { source: entry };
23+
}
24+
25+
if (entry.source.startsWith('./')) {
26+
entry.source = entry.source.substring(2);
27+
28+
if (relativeTo) {
29+
const relativeParts = relativeTo.source.split('/');
30+
relativeParts.pop();
31+
relativeParts.push(entry.source);
32+
entry.source = relativeParts.join('/');
33+
}
34+
}
35+
36+
return entry;
37+
})
38+
.filter((entry) => !entry.if); // @todo: log a warning
39+
};
40+
41+
42+
internals.loadSource = async (source, { loadFile, cache }) => {
43+
44+
if (cache[source]) {
45+
internals.log('Returning cached %s', source);
46+
return cache[source];
47+
}
48+
49+
let path = source;
50+
51+
if (source.includes(':')) {
52+
const [repository, fileName] = source.split(':');
53+
const loader = await Loader.create({ repository: `https://github.com/${repository}` });
54+
55+
path = fileName;
56+
loadFile = loader.loadFile;
57+
}
58+
59+
internals.log('Loading %s', source);
60+
const result = await loadFile(path);
61+
cache[source] = result;
62+
return result;
63+
};
64+
65+
66+
exports.apply = async (yaml, { loadFile, relativeTo, cache = new Map() }) => {
67+
68+
if (!yaml.import) {
69+
return;
70+
}
71+
72+
const imports = internals.normalizeImports(yaml, { relativeTo });
73+
74+
for (const entry of imports) {
75+
76+
const buffer = await internals.loadSource(entry.source, { loadFile, cache });
77+
78+
const imported = Yaml.safeLoad(buffer, {
79+
schema: Yaml.FAILSAFE_SCHEMA,
80+
json: true
81+
});
82+
83+
await exports.apply(imported, { loadFile, relativeTo: entry, cache });
84+
85+
for (const key in imported) {
86+
87+
if (key === 'import') {
88+
continue;
89+
}
90+
91+
yaml[key] = imported[key];
92+
}
93+
}
94+
};

lib/travis/index.js

Lines changed: 6 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,35 @@
11
'use strict';
22

3-
const Debug = require('debug');
43
const Nv = require('@pkgjs/nv');
54
const Yaml = require('js-yaml');
65

7-
const Loader = require('./loader');
6+
const TravisImports = require('./imports');
7+
const Utils = require('../utils');
88

99

1010
const internals = {};
1111

1212

13-
internals.log = Debug('detect-node-support');
14-
15-
1613
internals.nodeAliases = {
1714
latest: 'active',
1815
node: 'active',
1916
stable: 'active'
2017
};
2118

2219

23-
internals.toArray = (v) => {
24-
25-
if (v === undefined) {
26-
return [];
27-
}
28-
29-
return Array.isArray(v) ? v : [v];
30-
};
31-
32-
33-
internals.normalizeImports = (travisYaml, { relativeTo }) => {
34-
35-
return internals.toArray(travisYaml.import)
36-
.map((entry) => {
37-
38-
if (typeof entry === 'string') {
39-
entry = { source: entry };
40-
}
41-
42-
if (entry.source.startsWith('./')) {
43-
entry.source = entry.source.substring(2);
44-
45-
if (relativeTo) {
46-
const relativeParts = relativeTo.source.split('/');
47-
relativeParts.pop();
48-
relativeParts.push(entry.source);
49-
entry.source = relativeParts.join('/');
50-
}
51-
}
52-
53-
return entry;
54-
})
55-
.filter((entry) => !entry.if); // @todo: log a warning
56-
};
57-
58-
59-
internals.loadSource = async (source, { loadFile, cache }) => {
60-
61-
if (cache[source]) {
62-
internals.log('Returning cached %s', source);
63-
return cache[source];
64-
}
65-
66-
let path = source;
67-
68-
if (source.includes(':')) {
69-
const [repository, fileName] = source.split(':');
70-
const loader = await Loader.create({ repository: `https://github.com/${repository}` });
71-
72-
path = fileName;
73-
loadFile = loader.loadFile;
74-
}
75-
76-
internals.log('Loading %s', source);
77-
const result = await loadFile(path);
78-
cache[source] = result;
79-
return result;
80-
};
81-
82-
83-
internals.applyImports = async (yaml, { loadFile, relativeTo, cache = new Map() }) => {
84-
85-
if (!yaml.import) {
86-
return;
87-
}
88-
89-
const imports = internals.normalizeImports(yaml, { relativeTo });
90-
91-
for (const entry of imports) {
92-
93-
const buffer = await internals.loadSource(entry.source, { loadFile, cache });
94-
95-
const imported = Yaml.safeLoad(buffer, {
96-
schema: Yaml.FAILSAFE_SCHEMA,
97-
json: true
98-
});
99-
100-
await internals.applyImports(imported, { loadFile, relativeTo: entry, cache });
101-
102-
for (const key in imported) {
103-
104-
if (key === 'import') {
105-
continue;
106-
}
107-
108-
yaml[key] = imported[key];
109-
}
110-
}
111-
};
112-
113-
11420
internals.scan = async (travisYaml, options) => {
11521

116-
await internals.applyImports(travisYaml, options);
22+
await TravisImports.apply(travisYaml, options);
11723

11824
const rawSet = new Set();
11925

120-
for (const v of internals.toArray(travisYaml.node_js)) {
26+
for (const v of Utils.toArray(travisYaml.node_js)) {
12127
rawSet.add(v);
12228
}
12329

12430
if (travisYaml.env) {
12531

126-
for (const env of internals.toArray(travisYaml.env.matrix)) {
32+
for (const env of Utils.toArray(travisYaml.env.matrix)) {
12733

12834
const matches = env.match(/(?:NODEJS_VER|TRAVIS_NODE_VERSION|NODE_VER)="?(node\/)?(?<version>[\w./*]+)"?/);
12935

@@ -135,7 +41,7 @@ internals.scan = async (travisYaml, options) => {
13541

13642
if (travisYaml.matrix) {
13743

138-
for (const include of internals.toArray(travisYaml.matrix.include)) {
44+
for (const include of Utils.toArray(travisYaml.matrix.include)) {
13945

14046
if (include.node_js) {
14147
rawSet.add(include.node_js);

lib/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ exports.getErrorMessage = (error) => {
2222

2323
return null;
2424
};
25+
26+
27+
exports.toArray = (v) => {
28+
29+
if (v === undefined) {
30+
return [];
31+
}
32+
33+
return Array.isArray(v) ? v : [v];
34+
};

0 commit comments

Comments
 (0)