Skip to content

Split up loader #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

const Package = require('../package.json');


exports.userAgent = `${Package.name}/${Package.version} (${Package.homepage})`;
182 changes: 0 additions & 182 deletions lib/loader.js

This file was deleted.

19 changes: 19 additions & 0 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const NpmLoader = require('./npm');
const PathLoader = require('./path');
const RepositoryLoader = require('./repository');


exports.create = ({ path, repository, packageName }) => {

if (repository) {
return RepositoryLoader.create(repository);
}

if (packageName) {
return NpmLoader.create(packageName);
}

return PathLoader.create(path);
};
60 changes: 60 additions & 0 deletions lib/loader/npm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

const Pacote = require('pacote');

const Constants = require('../constants');
const RepositoryLoader = require('./repository');

const internals = {};


internals.parseRepository = (packument) => {

if (typeof packument.repository === 'string') {
return packument.repository;
}

if (!packument.repository || !packument.repository.url) {
throw new Error(`Unable to determine the git repository for ${packument.name}`);
}

return packument.repository.url;
};


exports.create = async (packageName) => {

try {
const packument = await Pacote.packument(packageName + '@latest', {
'fullMetadata': true,
'user-agent': Constants.userAgent
});

const repository = internals.parseRepository(packument);

const repositoryLoader = RepositoryLoader.create(repository);

return {
...repositoryLoader,
loadFile: async (filename, options) => {

const result = await repositoryLoader.loadFile(filename, options);

if (filename === 'package.json' && result.name !== packageName) {
throw new Error(`${repository} does not contain ${packageName}. Monorepo not supported: https://github.com/pkgjs/detect-node-support/issues/6`);
}

return result;
}
};
}
catch (err) {

if (err.statusCode === 404) {
throw new Error(`Package ${packageName} does not exist`);
}

throw err;

}
};
40 changes: 40 additions & 0 deletions lib/loader/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const Fs = require('fs');
const Path = require('path');

const Utils = require('../utils');


exports.create = async (path) => {

const simpleGit = Utils.simpleGit(path);
const isRepo = await simpleGit.checkIsRepo();

if (!isRepo) {
throw new Error(`${path} is not a git repository`);
}

if (!Fs.existsSync(Path.join(path, 'package.json'))) {
throw new Error(`${path} does not contain a package.json`);
}

return {
getCommit: () => {

return simpleGit.revparse(['HEAD']);
},
loadFile: (filename, options = {}) => {

const fullPath = Path.join(path, filename);

const buffer = Fs.readFileSync(fullPath);

if (options.json) {
return JSON.parse(buffer.toString());
}

return buffer;
}
};
};
79 changes: 79 additions & 0 deletions lib/loader/repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

const Debug = require('debug');
const GitUrlParse = require('git-url-parse');
const Wreck = require('@hapi/wreck');

const Utils = require('../utils');


const internals = {
cache: new Map(),
log: Debug('detect-node-support:loader'),
error: Debug('detect-node-support:error')
};


exports.create = (repository) => {

if (repository.split('/').length === 2) {
repository = `https://github.com/${repository}`;
}

const parsedRepository = GitUrlParse(repository);

return {
getCommit: async () => {

const simpleGit = Utils.simpleGit();
const httpRepository = GitUrlParse.stringify(parsedRepository, 'http');
const result = await simpleGit.listRemote([httpRepository, 'HEAD']);
const [head] = result.split(/\s+/);

return head;
},
loadFile: async (filename, options) => {

if (parsedRepository.source !== 'github.com') {
throw new Error('Only github.com paths supported, feel free to PR at https://github.com/pkgjs/detect-node-support');
}

const url = `https://raw.githubusercontent.com/${parsedRepository.full_name}/HEAD/${filename}`;
internals.log('Loading: %s', url);

if (options === undefined && internals.cache.has(url)) {
internals.log('From cache: %s', url);
return internals.cache.get(url);
}

try {
const { payload } = await Wreck.get(url, options);

if (options === undefined) {
internals.cache.set(url, payload);
}

internals.log('Loaded: %s', url);
return payload;
}
catch (err) {

if (err.data && err.data.res.statusCode === 404) {
internals.log('Not found: %s', url);
const error = new Error(`${repository} does not contain a ${filename}`);
error.code = 'ENOENT';
throw error;
}

internals.error('Failed to load: %s', url);
throw err;
}
}
};
};


exports.clearCache = () => {

internals.cache = new Map();
};
Loading