Skip to content

Commit 9a95f8d

Browse files
committed
refactor: extract yaml parsing into loaders
1 parent 78108f7 commit 9a95f8d

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

lib/loader/contents.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const Yaml = require('js-yaml');
4+
5+
exports.convert = (buffer, options) => {
6+
7+
if (options.json) {
8+
return JSON.parse(buffer.toString());
9+
}
10+
11+
if (options.yaml) {
12+
return Yaml.load(buffer, {
13+
schema: Yaml.FAILSAFE_SCHEMA,
14+
json: true
15+
});
16+
}
17+
18+
return buffer;
19+
};

lib/loader/path.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Fs = require('fs');
44
const Path = require('path');
55

6+
const Contents = require('./contents');
67
const Utils = require('../utils');
78

89

@@ -30,11 +31,7 @@ exports.create = async (path) => {
3031

3132
const buffer = Fs.readFileSync(fullPath);
3233

33-
if (options.json) {
34-
return JSON.parse(buffer.toString());
35-
}
36-
37-
return buffer;
34+
return Contents.convert(buffer, options);
3835
}
3936
};
4037
};

lib/loader/repository.js

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

33
const GitUrlParse = require('git-url-parse');
44

5+
const Contents = require('./contents');
56
const Logger = require('../logger');
67
const OctokitWrapper = require('./octokit-wrapper');
78
const Utils = require('../utils');
@@ -60,13 +61,9 @@ exports.create = (repository) => {
6061

6162
Logger.log(['loader'], 'Loaded: %s', resource);
6263

63-
const content = Buffer.from(result.data.content, 'base64');
64+
const buffer = Buffer.from(result.data.content, 'base64');
6465

65-
if (options.json) {
66-
return JSON.parse(content.toString());
67-
}
68-
69-
return content;
66+
return Contents.convert(buffer, options);
7067
}
7168
catch (err) {
7269

lib/travis/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const Nv = require('@pkgjs/nv');
4-
const Yaml = require('js-yaml');
54

65
const TravisImports = require('./imports');
76
const Utils = require('../utils');
@@ -75,7 +74,7 @@ internals.scan = async (travisYaml, options) => {
7574
exports.detect = async ({ loadFile }) => {
7675

7776
try {
78-
var buffer = await loadFile('.travis.yml');
77+
var travisYaml = await loadFile('.travis.yml', { yaml: true });
7978
}
8079
catch (err) {
8180

@@ -86,11 +85,6 @@ exports.detect = async ({ loadFile }) => {
8685
throw err;
8786
}
8887

89-
const travisYaml = Yaml.load(buffer, {
90-
schema: Yaml.FAILSAFE_SCHEMA,
91-
json: true
92-
});
93-
9488
return {
9589
travis: await internals.scan(travisYaml, { loadFile })
9690
};

0 commit comments

Comments
 (0)