Skip to content

Commit

Permalink
add registry option (#20)
Browse files Browse the repository at this point in the history
* add registry option

* cleanup
  • Loading branch information
Meir017 authored Apr 15, 2021
1 parent e45cefa commit 6de579f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 37 deletions.
5 changes: 5 additions & 0 deletions bin/download-tgz
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ program
.command('package-lock <uri>')
.description('download tarballs based on a package-lock.json')
.option('--directory [directory]')
.option('--registry [registry]')
.action((uri, command) => commands.packageLockCommand(uri, command));

program
.command('package <name> [version]')
.description('download tarballs based on a package and a version')
.option('--directory [directory]')
.option('--registry [registry]')
.option('--devDependencies')
.option('--peerDependencies')
.action((name, version, command) => commands.packageCommand(name, version, command));
Expand All @@ -35,6 +37,7 @@ program
.command('package-json <uri>')
.description('download tarballs based on a package.json')
.option('--directory [directory]')
.option('--registry [registry]')
.option('--devDependencies')
.option('--peerDependencies')
.action((uri, command) => commands.packageJsonCommand(uri, command));
Expand All @@ -43,6 +46,7 @@ program
.command('search <keyword>')
.description('download tarballs based on a npm registry search results')
.option('--directory [directory]')
.option('--registry [registry]')
.option('--devDependencies')
.option('--peerDependencies')
.action((keyword, command) => commands.searchCommand(keyword, command));
Expand All @@ -51,6 +55,7 @@ program
.command('generate <name> [version]')
.description('generates the download links for a giving package and a version')
.option('--outputFile [outputFile]')
.option('--registry [registry]')
.option('--devDependencies')
.option('--peerDependencies')
.action((name, version, command) => commands.generateCommand(name, version, command));
Expand Down
28 changes: 12 additions & 16 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,62 @@ const generator = require('./generator');

/**
* @param {string} uri
* @param {{ directory: string }} options
* @param {{ directory: string, registry?: string }} options
*/
async function packageLockCommand(uri, options = {}) {
const packageLock = await retrieveFile(uri);
downloader.downloadFromPackageLock(packageLock, options.directory);
downloader.downloadFromPackageLock(packageLock, options);
}

/**
* @param {string} name
* @param {string} version
* @param {{ directory: string, devDependencies: boolean, peerDependencies: boolean }} options
* @param {{ directory: string, devDependencies: boolean, peerDependencies: boolean, registry?: string }} options
*/
async function packageCommand(name, version, options = {}) {
const tarballsSet = await crawler.getDependencies({
name,
version,
devDependencies: options.devDependencies,
peerDependencies: options.peerDependencies,
...options
});
downloader.downloadFromIterable(tarballsSet, options.directory);
downloader.downloadFromIterable(tarballsSet, options);
}

/**
* @param {string} uri
* @param {{ directory: string, devDependencies: boolean, peerDependencies: boolean }} options
* @param {{ directory: string, devDependencies: boolean, peerDependencies: boolean, registry?: string }} options
*/
async function packageJsonCommand(uri, options = {}) {
const packageJson = await retrieveFile(uri);
const tarballsSet = await crawler.getPackageJsonDependencies({
packageJson,
devDependencies: options.devDependencies,
peerDependencies: options.peerDependencies,
...options
});
downloader.downloadFromIterable(tarballsSet, options.directory);
downloader.downloadFromIterable(tarballsSet, options);
}

/**
* @param {string} keyword
* @param {{ directory: string, devDependencies: boolean, peerDependencies: boolean }} options
* @param {{ directory: string, devDependencies: boolean, peerDependencies: boolean, registry?: string }} options
*/
async function searchCommand(keyword, options = {}) {
const packageJson = await generatePackageJson({ keyword });
const tarballsSet = await crawler.getPackageJsonDependencies({
packageJson,
devDependencies: options.devDependencies,
peerDependencies: options.peerDependencies,
...options
});
downloader.downloadFromIterable(tarballsSet, options.directory);
}

/**
* @param {string} keyword
* @param {{ outputFile: string, devDependencies: boolean, peerDependencies: boolean }} options
* @param {{ outputFile: string, devDependencies: boolean, peerDependencies: boolean, registry?: string }} options
*/
async function generateCommand(name, version, options) {
const tarballsSet = await crawler.getDependencies({
name,
version,
devDependencies: options.devDependencies,
peerDependencies: options.peerDependencies
...options
});
generator.saveToFile(Array.from(tarballsSet), options.outputFile);
}
Expand Down
13 changes: 13 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const defaultNpmRegistry = 'http://registry.npmjs.org';

/**
*
* @param {{ registry?: string }} options
*/
function getNpmRegistry(options = {}) {
return options.registry || defaultNpmRegistry;
}

module.exports = {
getNpmRegistry
};
5 changes: 0 additions & 5 deletions lib/constants.js

This file was deleted.

7 changes: 4 additions & 3 deletions lib/crawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logger = require('./logger');

require('colors');

const { npmRegistry } = require('./constants');
const { getNpmRegistry } = require('./config');

let cacheHits = 1;
let registryHits = 1;
Expand All @@ -20,6 +20,7 @@ const tarballs = new Set();
* @property {boolean} devDependencies
* @property {boolean} peerDependencies
* @property {string} outputPrefix
* @property {string?} registry
*
* @param { DependenciesOptions } options
* @returns { Promise<Set<string>> }
Expand Down Expand Up @@ -71,8 +72,8 @@ async function getPackageJsonDependencies(options) {
return tarballs;
}

async function _retrievePackageVersion({ name, version, outputPrefix = '' }) {
const uri = `${npmRegistry}/${name.replace('/', '%2F')}`;
async function _retrievePackageVersion({ name, version, outputPrefix = '', registry = '' }) {
const uri = `${getNpmRegistry({ registry })}/${name.replace('/', '%2F')}`;

if (packagesCache.has(name)) {
logger(['cache'.yellow, cacheHits], `retrieving ${outputPrefix}${name.cyan} ${(version || '').cyan}`);
Expand Down
27 changes: 17 additions & 10 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,40 @@ const fs = require('fs');
const tar = require('tar');
const logger = require('./logger');
const downloadFileAsync = require('./download-file');
const urlResolver = require('./url-resolver');

require('colors');

function downloadFromPackageLock(packageLock, directory) {
/**
* @param {{directory: string, registry?: string}} options
*/
function downloadFromPackageLock(packageLock, options) {
const tarballs = [];
_enumerateDependencies(tarballs, packageLock.dependencies);
_enumerateDependencies(tarballs, packageLock.dependencies, options);

return _downloadTarballs(tarballs, directory);
return _downloadTarballs(tarballs, options.directory);
}

/**
* @param { Iterable<string> | ArrayLike<string> } tarballsIterable
* @param { string } directory
* @param {{directory: string, registry?: string}} options
*/
function downloadFromIterable(tarballsIterable, directory) {
function downloadFromIterable(tarballsIterable, options) {
const tarballs = Array.from(tarballsIterable)
.map(url => ({ url, directory: _convertUrlToDirectory(url) }));
return _downloadTarballs(tarballs, directory);
.map(url => ({ url: urlResolver.resolve(url, options), directory: _convertUrlToDirectory(url) }));
return _downloadTarballs(tarballs, options.directory);
}

function _enumerateDependencies(tarballs, dependencies) {
/**
* @param {{directory: string, registry?: string}} options
*/
function _enumerateDependencies(tarballs, dependencies, options) {
for (const [dependencyName, dependency] of Object.entries(dependencies)) {
if (dependency.resolved) {
tarballs.push({ url: dependency.resolved, directory: dependencyName });
tarballs.push({ url: urlResolver.resolve(dependency.resolved, options), directory: dependencyName });
}
if (dependency.dependencies) {
_enumerateDependencies(tarballs, dependency.dependencies);
_enumerateDependencies(tarballs, dependency.dependencies, options);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/npm-search.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const request = require('request-promise');
const { npmRegistry } = require('./constants');
const { getNpmRegistry } = require('./config');

/**
*
* @param {{ keyword: string }} options
* @param {{ keyword: string, registry?: string }} options
*/
async function generatePackageJson(options) {
const uri = `${npmRegistry}/-/v1/search?text=keywords:${options.keyword}&size=1000`;
const uri = `${getNpmRegistry(options)}/-/v1/search?text=keywords:${options.keyword}&size=1000`;
const response = await request({ uri, json: true });

const packageJson = response.objects.reduce((prev, curr) => {
Expand Down
14 changes: 14 additions & 0 deletions lib/url-resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param {string} url
* @param {{ registry?: string }} options
*/
function resolve(url, options = {}) {
if (!options.registry) return url;

const urlObject = new URL(url);
return urlObject.href.replace(urlObject.origin, options.registry);
}

module.exports = {
resolve
};

0 comments on commit 6de579f

Please sign in to comment.