Skip to content

Commit cf5dae9

Browse files
authored
Merge pull request #33 from pkgjs/octokit-prefactor
2 parents 0b02516 + e826964 commit cf5dae9

File tree

7 files changed

+206
-184
lines changed

7 files changed

+206
-184
lines changed

lib/constants.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
const Package = require('../package.json');
4+
5+
6+
exports.userAgent = `${Package.name}/${Package.version} (${Package.homepage})`;

lib/loader.js

-182
This file was deleted.

lib/loader/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const NpmLoader = require('./npm');
4+
const PathLoader = require('./path');
5+
const RepositoryLoader = require('./repository');
6+
7+
8+
exports.create = ({ path, repository, packageName }) => {
9+
10+
if (repository) {
11+
return RepositoryLoader.create(repository);
12+
}
13+
14+
if (packageName) {
15+
return NpmLoader.create(packageName);
16+
}
17+
18+
return PathLoader.create(path);
19+
};

lib/loader/npm.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
const Pacote = require('pacote');
4+
5+
const Constants = require('../constants');
6+
const RepositoryLoader = require('./repository');
7+
8+
const internals = {};
9+
10+
11+
internals.parseRepository = (packument) => {
12+
13+
if (typeof packument.repository === 'string') {
14+
return packument.repository;
15+
}
16+
17+
if (!packument.repository || !packument.repository.url) {
18+
throw new Error(`Unable to determine the git repository for ${packument.name}`);
19+
}
20+
21+
return packument.repository.url;
22+
};
23+
24+
25+
exports.create = async (packageName) => {
26+
27+
try {
28+
const packument = await Pacote.packument(packageName + '@latest', {
29+
'fullMetadata': true,
30+
'user-agent': Constants.userAgent
31+
});
32+
33+
const repository = internals.parseRepository(packument);
34+
35+
const repositoryLoader = RepositoryLoader.create(repository);
36+
37+
return {
38+
...repositoryLoader,
39+
loadFile: async (filename, options) => {
40+
41+
const result = await repositoryLoader.loadFile(filename, options);
42+
43+
if (filename === 'package.json' && result.name !== packageName) {
44+
throw new Error(`${repository} does not contain ${packageName}. Monorepo not supported: https://github.com/pkgjs/detect-node-support/issues/6`);
45+
}
46+
47+
return result;
48+
}
49+
};
50+
}
51+
catch (err) {
52+
53+
if (err.statusCode === 404) {
54+
throw new Error(`Package ${packageName} does not exist`);
55+
}
56+
57+
throw err;
58+
59+
}
60+
};

lib/loader/path.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const Fs = require('fs');
4+
const Path = require('path');
5+
6+
const Utils = require('../utils');
7+
8+
9+
exports.create = async (path) => {
10+
11+
const simpleGit = Utils.simpleGit(path);
12+
const isRepo = await simpleGit.checkIsRepo();
13+
14+
if (!isRepo) {
15+
throw new Error(`${path} is not a git repository`);
16+
}
17+
18+
if (!Fs.existsSync(Path.join(path, 'package.json'))) {
19+
throw new Error(`${path} does not contain a package.json`);
20+
}
21+
22+
return {
23+
getCommit: () => {
24+
25+
return simpleGit.revparse(['HEAD']);
26+
},
27+
loadFile: (filename, options = {}) => {
28+
29+
const fullPath = Path.join(path, filename);
30+
31+
const buffer = Fs.readFileSync(fullPath);
32+
33+
if (options.json) {
34+
return JSON.parse(buffer.toString());
35+
}
36+
37+
return buffer;
38+
}
39+
};
40+
};

lib/loader/repository.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
const Debug = require('debug');
4+
const GitUrlParse = require('git-url-parse');
5+
const Wreck = require('@hapi/wreck');
6+
7+
const Utils = require('../utils');
8+
9+
10+
const internals = {
11+
cache: new Map(),
12+
log: Debug('detect-node-support:loader'),
13+
error: Debug('detect-node-support:error')
14+
};
15+
16+
17+
exports.create = (repository) => {
18+
19+
if (repository.split('/').length === 2) {
20+
repository = `https://github.com/${repository}`;
21+
}
22+
23+
const parsedRepository = GitUrlParse(repository);
24+
25+
return {
26+
getCommit: async () => {
27+
28+
const simpleGit = Utils.simpleGit();
29+
const httpRepository = GitUrlParse.stringify(parsedRepository, 'http');
30+
const result = await simpleGit.listRemote([httpRepository, 'HEAD']);
31+
const [head] = result.split(/\s+/);
32+
33+
return head;
34+
},
35+
loadFile: async (filename, options) => {
36+
37+
if (parsedRepository.source !== 'github.com') {
38+
throw new Error('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/detect-node-support');
39+
}
40+
41+
const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
42+
internals.log('Loading: %s', url);
43+
44+
if (options === undefined && internals.cache.has(url)) {
45+
internals.log('From cache: %s', url);
46+
return internals.cache.get(url);
47+
}
48+
49+
try {
50+
const { payload } = await Wreck.get(url, options);
51+
52+
if (options === undefined) {
53+
internals.cache.set(url, payload);
54+
}
55+
56+
internals.log('Loaded: %s', url);
57+
return payload;
58+
}
59+
catch (err) {
60+
61+
if (err.data && err.data.res.statusCode === 404) {
62+
internals.log('Not found: %s', url);
63+
const error = new Error(`${repository} does not contain a ${filename}`);
64+
error.code = 'ENOENT';
65+
throw error;
66+
}
67+
68+
internals.error('Failed to load: %s', url);
69+
throw err;
70+
}
71+
}
72+
};
73+
};
74+
75+
76+
exports.clearCache = () => {
77+
78+
internals.cache = new Map();
79+
};

0 commit comments

Comments
 (0)