diff --git a/src/cli.js b/src/cli.js index a23c56f..ef4d92f 100644 --- a/src/cli.js +++ b/src/cli.js @@ -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'); @@ -50,7 +51,8 @@ module.exports = { licenseTreePlugin, testsPlugin, maintenancePlugin, - supportPlugin + supportPlugin, + typingsPlugin ]; for (const pkg of config.modules) { diff --git a/src/lib/fetch.js b/src/lib/network.js similarity index 79% rename from src/lib/fetch.js rename to src/lib/network.js index 7b07819..6e7003e 100644 --- a/src/lib/fetch.js +++ b/src/lib/network.js @@ -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); @@ -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 }; diff --git a/src/plugins/archive.js b/src/plugins/archive.js index 217a176..4c6b62f 100644 --- a/src/plugins/archive.js +++ b/src/plugins/archive.js @@ -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 diff --git a/src/plugins/tests.js b/src/plugins/tests.js index 4522526..18e18d4 100644 --- a/src/plugins/tests.js +++ b/src/plugins/tests.js @@ -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, diff --git a/src/plugins/typings.js b/src/plugins/typings.js new file mode 100644 index 0000000..558dabc --- /dev/null +++ b/src/plugins/typings.js @@ -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/ 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/ 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; diff --git a/test/lib/fetch.test.js b/test/lib/fetch.test.js index f2954b7..8e17eef 100644 --- a/test/lib/fetch.test.js +++ b/test/lib/fetch.test.js @@ -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'); diff --git a/test/plugins/archive.test.js b/test/plugins/archive.test.js index 398d643..e80320e 100644 --- a/test/plugins/archive.test.js +++ b/test/plugins/archive.test.js @@ -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(),