Skip to content
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
4 changes: 3 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const licenseTreePlugin = require('./plugins/licenseTree');
const testsPlugin = require('./plugins/tests');
const maintenancePlugin = require('./plugins/maintenance');
const supportPlugin = require('./plugins/support');
const typingsPlugin = require('./plugins/typings');

const { getInfoFromNPM } = require('./lib/npm');
const { merge, createError } = require('./lib/result');
Expand Down Expand Up @@ -50,7 +51,8 @@ module.exports = {
licenseTreePlugin,
testsPlugin,
maintenancePlugin,
supportPlugin
supportPlugin,
typingsPlugin
];

for (const pkg of config.modules) {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/fetch.js → src/lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ const axios = require('axios');
const cache = new Map();
const clearCache = () => cache.clear();

const fetch = async (url) => {
const response = await axios.get(url);
return response.data;
};

const fetchGithub = async (target, token, override = false) => {
// check if we already have the response in cache
if (cache.has(target)) return cache.get(target);
Expand All @@ -20,4 +25,4 @@ const fetchGithub = async (target, token, override = false) => {
return response.data;
};

module.exports = { fetchGithub, cache, clearCache };
module.exports = { fetch, fetchGithub, cache, clearCache };
2 changes: 1 addition & 1 deletion src/plugins/archive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { createError } = require('../lib/result');
const { stringBuilder, success, failure } = require('../lib/format');
const { fetchGithub } = require('../lib/fetch');
const { fetchGithub } = require('../lib/network');

const archivePlugin = async (pkg, _, options) => {
const githubTarget = pkg.repository.url
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const R = require('ramda');
const { createWarning } = require('../lib/result');
const { fetchGithub } = require('../lib/fetch');
const { fetchGithub } = require('../lib/network');
const {
stringBuilder,
printStatuses,
Expand Down
48 changes: 48 additions & 0 deletions src/plugins/typings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { fetch } = require('../lib/network');
const { stringBuilder, warning, success } = require('../lib/format');
const { createWarning } = require('../lib/result');

let cache = null;

// helper function when parsing @types/<package> list
const unmangle = (name) => {
return name.replace('__', '/').replace('@', '');
};

const typingsPlugin = async (pkg) => {
// Typings plugin output
const output = stringBuilder('\nChecking for TypeScript typings').withPadding(
66
);

// First we'll check if typings are defined in module's package.json file.
if (pkg.types || pkg.typings) {
success(output.get());
return null;
}

// Then we'll check Microsoft's @types/<package> list
const TYPES_URI =
'https://typespublisher.blob.core.windows.net/typespublisher/data/search-index-min.json';

// We don't want to pull Microsoft's list for every module
if (cache === null) {
const response = await fetch(TYPES_URI);
cache = response;
}

const hasTypesPackage = cache.find((item) => unmangle(item.t) === pkg.name);

if (hasTypesPackage) {
success(output.get());
return null;
}

// It seems that there're no available typescript typings
warning(output.get());
return createWarning(
`The module "${pkg.name}" seems to have no available TypeScript typings.`
);
};

module.exports = typingsPlugin;
2 changes: 1 addition & 1 deletion test/lib/fetch.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-env jest */

const axios = require('axios');
const network = require('../../src/lib/fetch');
const network = require('../../src/lib/network');

jest.mock('axios');

Expand Down
4 changes: 2 additions & 2 deletions test/plugins/archive.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-env jest */

const network = require('../../src/lib/fetch');
const network = require('../../src/lib/network');
const format = require('../../src/lib/format');
const archivePlugin = require('../../src/plugins/archive');

jest.mock('../../src/lib/fetch');
jest.mock('../../src/lib/network');
jest.mock('../../src/lib/format', () => ({
...jest.requireActual('../../src/lib/format'),
failure: jest.fn(),
Expand Down