Skip to content
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

chore(cleanup): update and make the tests pass #29

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules
yarn.lock
.npm-cache-info.json
.axios-cache
.test-cache
.cache
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 100,
"singleQuote": true,
"trailingComma": "none",
"semi": true,
"arrowParens": "avoid",
"bracketSpacing": false
}
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
node_js:
- '6'
- '8'
- '10'
- '11'
- '12'
cache:
directories:
- .test-cache
- node_modules
8 changes: 0 additions & 8 deletions .yo-rc.json

This file was deleted.

123 changes: 123 additions & 0 deletions cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const axios = require('axios');
const axiosRetry = require('axios-retry');
const fs = require('fs');
const path = require('path');

const execa = require('execa');
const semver = require('semver');
const mkdirp = require('mkdirp');

const CACHE_BASE_PATH = '.test-cache';
if (!fs.existsSync(CACHE_BASE_PATH)) {
console.log('Setup cache for testing');
fs.mkdirSync(CACHE_BASE_PATH);
}

const CACHE_NPM_PATH = `${CACHE_BASE_PATH}/.npm-cache-info.json`;
const CACHE_NPM = {
__last: Date.now()
};

if (fs.existsSync(CACHE_NPM_PATH)) {
const cache = require(`./${CACHE_NPM_PATH}`);
if (Date.now() - cache.__last < 1000 * 60 * 60) {
console.log('use cache on NPM');
Object.assign(CACHE_NPM, cache);
} else {
console.log('reset NPM cache');
}
}

const AXIOS_CACHE_PATH = `${CACHE_BASE_PATH}/axios`;

if (!fs.existsSync(AXIOS_CACHE_PATH)) {
console.log(`Setup cache folder for axios request in ${AXIOS_CACHE_PATH}`);
fs.mkdirSync(AXIOS_CACHE_PATH);
}

axiosRetry(axios, {retries: 3});

function getInfo(url) {
const info = {};
const splitted = url.replace('https://unpkg.com/', '').split('/');
const [packageAndVersion, inCase, ...path] = splitted;

if (packageAndVersion.startsWith('@')) {
const [n, version] = inCase.split('@');
info.name = `${packageAndVersion}/${n}`;
info.version = version;
info.path = path.join('/');
} else {
const [n, version] = packageAndVersion.split('@');
info.name = n;
info.version = version;
info.path = [inCase].concat(path).join('/');
}

return info;
}

function getPathFromURL(url) {
const info = getInfo(url);
return `./${AXIOS_CACHE_PATH}/${info.name}/${info.version}/${info.path}`;
}

function cachedGet(url) {
const pathFromURL = getPathFromURL(url);
if (fs.existsSync(pathFromURL)) {
return Promise.resolve({data: fs.readFileSync(pathFromURL).toString()});
}

return axios.get(url).then(response => {
mkdirp.sync(path.dirname(pathFromURL));
fs.writeFileSync(pathFromURL, response.data);
return response;
});
}

function isInCache(url) {
return fs.existsSync(getPathFromURL(url));
}

function getModuleInfo(moduleName) {
if (!CACHE_NPM[moduleName]) {
const info = JSON.parse(execa.sync('npm', ['info', '--json', `${moduleName}`]).stdout);
CACHE_NPM[moduleName] = {
'dist-tags': info['dist-tags'],
versions: info.versions
};
fs.writeFileSync(CACHE_NPM_PATH, JSON.stringify(CACHE_NPM));
}

return CACHE_NPM[moduleName];
}

function getAllVersions(moduleName) {
return getModuleInfo(moduleName).versions;
}

function getRangeEdgeVersions(allVersions) {
return function (range) {
const result = [];
const values = allVersions.filter(version => semver.satisfies(version, range));

if (values.length > 0) {
result.push(values[0]);
}

if (values.length > 1) {
result.push(values[values.length - 1]);
}

return result;
};
}

module.exports = {
cachedGet,
isInCache,
getModuleInfo,
getAllVersions,
getRangeEdgeVersions,
getPathFromURL
};
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const semver = require('semver');

const modules = require('./modules');
const {getURL} = require('./url');

module.exports = function (moduleName, version, options) {
options = options || {};
Expand All @@ -29,12 +30,21 @@ module.exports = function (moduleName, version, options) {
return null;
}

let url = env === 'development' ? config.development : config.production;
url = url.replace('[version]', version);
const path = env === 'development' ? config.development : config.production;
let url;
if (path.startsWith('/')) {
url = getURL({
name: moduleName,
version,
path
});
} else {
url = path.replace('[version]', version);
}

return {
name: moduleName,
var: modules[moduleName].var,
var: modules[moduleName].var || modules[moduleName].versions[range].var,
url,
version
};
Expand Down
Loading