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

Convert to esmodules #341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions devserver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const http = require('http');
const handler = require('./src/handler');
import http from 'http';
import handler from './src/handler';

http.createServer((req, res) => handler(req, res))
.listen(3000);
Expand Down
7 changes: 3 additions & 4 deletions functional.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const http = require('http');
const got = require('got');

const handler = require('./src/handler');
import http from 'http';
import got from 'got';
import handler from './src/handler';

describe('functional', () => {
let server;
Expand Down
9 changes: 5 additions & 4 deletions scripts/update-mapping-files.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const got = require('got');
const jsdom = require('jsdom'); // eslint-disable-line import/no-extraneous-dependencies
import fs from 'fs';

import path from 'path';
import got from 'got';
import jsdom from 'jsdom'; // eslint-disable-line import/no-extraneous-dependencies

const { JSDOM } = jsdom;

Expand Down
17 changes: 8 additions & 9 deletions src/go.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const findReachableUrls = require('find-reachable-urls');
const readMeta = require('lets-get-meta');
const got = require('got');
const { tldExists } = require('tldjs');

const cache = require('./utils/cache');
const log = require('./utils/log');
import findReachableUrls from 'find-reachable-urls';
import readMeta from 'lets-get-meta';
import got from 'got';
import { tldExists } from 'tldjs';
import cache from './utils/cache';
import log from './utils/log';

const getGoMeta = async (url) => {
const response = await got.get(url);
Expand Down Expand Up @@ -62,7 +61,7 @@ const resolveUrl = async (url) => {
return reachableUrl;
};

module.exports = async function (pkg) {
export default async function (pkg) {
try {
return await resolveUrl(pkg);
} catch (err) {
Expand All @@ -73,4 +72,4 @@ module.exports = async function (pkg) {
}
return undefined;
}
};
}
28 changes: 13 additions & 15 deletions src/handler.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
const { parse } = require('url');
const { json } = require('micro');
const pMap = require('p-map');

const go = require('./go');
const java = require('./java');
const nuget = require('./nuget');
const ping = require('./ping');
const registries = require('./registries');

const log = require('./utils/log');
const cache = require('./utils/cache');
const tracking = require('./utils/tracking');
const preparePayload = require('./utils/payload');
import { parse } from 'url';
import { json } from 'micro';
import pMap from 'p-map';
import go from './go';
import java from './java';
import nuget from './nuget';
import ping from './ping';
import registries from './registries';
import log from './utils/log';
import cache from './utils/cache';
import tracking from './utils/tracking';
import preparePayload from './utils/payload';

const logPrefix = log.prefix;

Expand Down Expand Up @@ -50,7 +48,7 @@ function errorHandler(error, res) {

tracking.init();

module.exports = async (req, res) => {
export default async (req, res) => {
if (['POST', 'GET'].includes(req.method)) {
const timingTotalStart = Date.now();

Expand Down
10 changes: 5 additions & 5 deletions src/java/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const findReachableUrls = require('find-reachable-urls');
const flatMappingList = require('./mapping.json');
const cache = require('../utils/cache');
import findReachableUrls from 'find-reachable-urls';
import flatMappingList from './mapping.json';
import cache from '../utils/cache';

const SUPPORTED_JAVA_VERSIONS = [9, 8, 7];

module.exports = async function (pkg) {
export default async function (pkg) {
const targetAsPath = pkg.replace(/\./g, '/');
const isBuildIn = !!pkg.match(/^javax?/);

Expand Down Expand Up @@ -43,4 +43,4 @@ module.exports = async function (pkg) {
await cache.set(cacheKey, reachableUrl);

return reachableUrl;
};
}
17 changes: 8 additions & 9 deletions src/nuget.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const findReachableUrls = require('find-reachable-urls');
const got = require('got');
const isUrl = require('is-url');
const { xml2js } = require('xml-js');

const cache = require('./utils/cache');
const log = require('./utils/log');
const repositoryUrl = require('./registries/repository-url');
import findReachableUrls from 'find-reachable-urls';
import got from 'got';
import isUrl from 'is-url';
import { xml2js } from 'xml-js';
import cache from './utils/cache';
import log from './utils/log';
import repositoryUrl from './registries/repository-url';

const getLatestVersion = async (pkg) => {
let response;
Expand Down Expand Up @@ -80,7 +79,7 @@ const getProjectUrls = async (pkg, version) => {
return urls;
};

module.exports = async (pkg) => {
export default async (pkg) => {
pkg = pkg.toLowerCase();

const cacheKey = `nuget_${pkg}`;
Expand Down
8 changes: 4 additions & 4 deletions src/ping.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const got = require('got');
const cache = require('./utils/cache');
import got from 'got';
import cache from './utils/cache';

const ERR_PING_NOT_FOUND = 'ERR_PING_NOT_FOUND';

module.exports = async function (url) {
export default async function (url) {
const cacheKey = `ping_${url}`;

const cacheValue = await cache.get(cacheKey);
Expand All @@ -28,4 +28,4 @@ module.exports = async function (url) {

return undefined;
});
};
}
2 changes: 1 addition & 1 deletion src/registries/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const config = require('./config.json');
import config from './config.json';

describe('config.json', () => {
const props = ['registry', 'xpaths', 'fallback'];
Expand Down
22 changes: 11 additions & 11 deletions src/registries/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const util = require('util');
const got = require('got');
const isUrl = require('is-url');
const findReachableUrls = require('find-reachable-urls');
const repositoryUrl = require('./repository-url');
const xpathHelper = require('./xpath-helper');
const registryConfig = require('./config.json');
const cache = require('../utils/cache');
const log = require('../utils/log');
const { prioritiesHost } = require('../utils/url');
import util from 'util';
import got from 'got';
import isUrl from 'is-url';
import findReachableUrls from 'find-reachable-urls';
import repositoryUrl from './repository-url';
import xpathHelper from './xpath-helper';
import registryConfig from './config.json';
import cache from '../utils/cache';
import log from '../utils/log';
import { prioritiesHost } from '../utils/url';

const ERR_PACKAGE_NOT_FOUND = 'ERR_PACKAGE_NOT_FOUND';

Expand Down Expand Up @@ -112,7 +112,7 @@ async function resolve(type, packageName) {
return reachableUrl;
}

module.exports = {
export default {
supported: Object.keys(registryConfig),
resolve,
};
8 changes: 4 additions & 4 deletions src/registries/repository-url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const nodeUrl = require('url');
const githubUrl = require('github-url-to-object');
import nodeUrl from 'url';
import githubUrl from 'github-url-to-object';

module.exports = function (url) {
export default function (url) {
if (typeof url !== 'string') {
return null;
}
Expand Down Expand Up @@ -30,4 +30,4 @@ module.exports = function (url) {

const githubInfo = githubUrl(url);
return githubInfo ? githubInfo.https_url : url;
};
}
2 changes: 1 addition & 1 deletion src/registries/repository-url.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const findRepositoryUrl = require('./repository-url');
import findRepositoryUrl from './repository-url';

describe('repository url', () => {
const urls = [
Expand Down
6 changes: 3 additions & 3 deletions src/registries/xpath-helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const jpath = require('json-path');
import jpath from 'json-path';

function xpathResolver(json, selector) {
try {
Expand All @@ -8,11 +8,11 @@ function xpathResolver(json, selector) {
}
}

module.exports = function (json, xpaths) {
export default function (json, xpaths) {
return xpaths
.map((selector) => xpathResolver(json, selector))
.filter(
(result) => result.length && typeof result[0] === 'string' && result[0],
)
.map((result) => result[0]);
};
}
4 changes: 2 additions & 2 deletions src/registries/xpath-helper.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const jpath = require('json-path');
const xpathHelper = require('./xpath-helper');
import jpath from 'json-path';
import xpathHelper from './xpath-helper';

jest.mock('json-path');

Expand Down
6 changes: 3 additions & 3 deletions src/utils/cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Redis = require('ioredis');
const log = require('./log');
import Redis from 'ioredis';
import log from './log';

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~ Thanks to RedisGreen and ZEIT for sponsoring ~~
Expand Down Expand Up @@ -136,7 +136,7 @@ async function get(key) {
}
}

module.exports = {
export default {
auth,
set,
get,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/cache.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const redis = require('ioredis');
const cache = require('./cache');
require('./log');
import redis from 'ioredis';
import cache from './cache';
import './log';

jest.mock('./log');
jest.mock('ioredis');
Expand Down
2 changes: 1 addition & 1 deletion src/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ function log(...rest) {

log.prefix = name;

module.exports = log;
export default log;
11 changes: 5 additions & 6 deletions src/utils/payload.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const uniqWith = require('lodash.uniqwith');
const isEqual = require('lodash.isequal');

const registries = require('../registries');
import uniqWith from 'lodash.uniqwith';
import isEqual from 'lodash.isequal';
import registries from '../registries';

const supportedTypes = ['ping', 'go', 'java', 'nuget', ...registries.supported];

module.exports = function (payload) {
export default function (payload) {
// Remove invalid items which does not follow format {type:'foo', target: 'bar'}
// Filter out types which are not supported
// Remove duplicates
Expand All @@ -15,4 +14,4 @@ module.exports = function (payload) {
&& item.target.length
&& supportedTypes.includes(item.type),
);
};
}
6 changes: 3 additions & 3 deletions src/utils/tracking.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const Mixpanel = require('mixpanel');
const log = require('./log');
import Mixpanel from 'mixpanel';
import log from './log';

let instance;

module.exports = {
export default {
init: () => {
if (!process.env.MIXPANEL_TOKEN) {
return;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function prioritiesHost(host, urls = []) {
}, []);
}

module.exports = {
export {
// eslint-disable-next-line import/prefer-default-export
prioritiesHost,
};
2 changes: 1 addition & 1 deletion src/utils/url.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { prioritiesHost } = require('./url');
import { prioritiesHost } from './url';

const inputUrls = [
'https://foo.com',
Expand Down