Skip to content

Commit

Permalink
Merge pull request #8 from sandersn/require-matching-npm-version
Browse files Browse the repository at this point in the history
Require matching npm version
  • Loading branch information
sandersn committed Nov 29, 2021
1 parent 6250cfb commit a60d267
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 17 deletions.
35 changes: 31 additions & 4 deletions packages/dts-critic/dt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@ const fs = require('fs')
const path = require('path')
const stripJsonComments = require('strip-json-comments')
/** @param {string} tslintPath */
function hasDtHeaderLintRule(tslintPath) {
function hasNpmNamingLintRule(tslintPath) {
if (fs.existsSync(tslintPath)) {
const tslint = JSON.parse(stripJsonComments(fs.readFileSync(tslintPath, 'utf-8')))
if(tslint.rules && tslint.rules["dt-header"] !== undefined) {
return !!tslint.rules["dt-header"]
if(tslint.rules && tslint.rules["npm-naming"] !== undefined) {
return !!tslint.rules["npm-naming"]
}
return true;
}
return false;
}

/** @param {string} tslintPath */
function addNpmNamingLintRule(tslintPath) {
if (fs.existsSync(tslintPath)) {
const tslint = JSON.parse(stripJsonComments(fs.readFileSync(tslintPath, 'utf-8')))
if (tslint.rules) {
tslint.rules["npm-naming"] = false;
}
else {
tslint.rules = { "npm-naming": false }
}
fs.writeFileSync(tslintPath, JSON.stringify(tslint, undefined, 4), 'utf-8')
}
}

function main() {
for (const item of fs.readdirSync('../DefinitelyTyped/types')) {
const entry = '../DefinitelyTyped/types/' + item
try {
if (hasDtHeaderLintRule(entry + '/tslint.json')) {
if (hasNpmNamingLintRule(entry + '/tslint.json')) {
critic(entry + '/index.d.ts')
}
}
Expand Down Expand Up @@ -46,6 +60,19 @@ function main() {
s = s.replace(id, 'export =$1;')
fs.writeFileSync(entry + '/index.d.ts', s, 'utf-8')
}
else if (/must match a version that exists on npm/.test(e.message)) {
const m = /** @type {string} */(e.message).match(/in the header, ([0-9.]+), to match one on npm, ([0-9., ]+)\./)
if (m) {
const headerver = parseFloat(m[1])
const npmvers = m[2].split(',').map(s => parseFloat(s.trim()))
const fixto = npmvers.every(v => headerver > v) ? -1.0 : Math.max(...npmvers)
console.log(`npm-version:${item}:${m[1]}:${m[2]}:${fixto}`)
addNpmNamingLintRule(entry + '/tslint.json')
}
else {
console.log('could not parse error message: ', e.message)
}
}
else {
console.log('*** ERROR for ' + item + ' ***')
console.log(e)
Expand Down
35 changes: 29 additions & 6 deletions packages/dts-critic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const headerParser = require("definitelytyped-header-parser");
const fs = require("fs");
const path = require("path");
const download = require("download-file-sync");
const semver = require("semver");

/**
* @param {string} dtsPath
Expand All @@ -24,7 +25,7 @@ function dtsCritic(dtsPath, sourcePath) {
}
dtsCritic.findDtsName = findDtsName;
dtsCritic.findNames = findNames;
dtsCritic.retrieveNpmHomepageOrFail = retrieveNpmHomepageOrFail;
dtsCritic.retrieveNpmOrFail = retrieveNpmOrFail;
dtsCritic.checkSource = checkSource;

module.exports = dtsCritic;
Expand All @@ -36,7 +37,7 @@ if (!module.parent) {
/** @typedef {{
* dts: string,
* src: string,
* homepage?: string
* homepage?: string,
* }}
* Names
*/
Expand Down Expand Up @@ -80,6 +81,7 @@ function findNames(dtsPath, sourcePath, header) {
const dts = findDtsName(dtsPath);
let src;
let homepage;
let versions;
if (sourcePath) {
src = findSourceName(sourcePath);
if (dts !== src) {
Expand All @@ -88,10 +90,18 @@ function findNames(dtsPath, sourcePath, header) {
}
else {
let nonNpmHasMatchingPackage = false;
let noMatchingVersion = false;
src = dts;
try {
homepage = retrieveNpmHomepageOrFail(dts);
const npm = retrieveNpmOrFail(dts)
homepage = npm.homepage;
versions = Object.keys(npm.versions).map(v => {
const ver = semver.parse(v);
if (!ver) return "";
return ver.major + "." + ver.minor;
});
nonNpmHasMatchingPackage = !!header && header.nonNpm && !isExistingSquatter(dts);
noMatchingVersion = !!header && !header.nonNpm && !versions.includes(header.libraryMajorVersion + "." + header.libraryMinorVersion);
}
catch (e) {
if (!header || !header.nonNpm) {
Expand All @@ -118,6 +128,19 @@ Try adding -browser to the end of the name to get
${dts}-browser
`);

}
if (noMatchingVersion) {
const verstring = versions ? versions.join(', ') : "NO VERSIONS FOUND";
const lateststring = versions ? versions[versions.length - 1] : "NO LATEST VERSION FOUND";
const headerstring = header ? header.libraryMajorVersion + '.' + header.libraryMinorVersion : "NO HEADER VERSION FOUND";
throw new Error(`The types for ${dts} must match a version that exists on npm.
You should copy the major and minor version from the package on npm.
To resolve this error, change the version in the header, ${headerstring},
to match one on npm: ${verstring}.
For example, if you're trying to match the latest version, use ${lateststring}.`);

}
}
return { dts, src, homepage };
Expand Down Expand Up @@ -146,14 +169,14 @@ function findSourceName(sourcePath) {
/**
* This function makes it an ERROR not to have a npm package that matches the base name.
* @param {string} baseName
* @return {string}
* @return {{ homepage: string, versions: { [s: string]: any } }}
*/
function retrieveNpmHomepageOrFail(baseName) {
function retrieveNpmOrFail(baseName) {
const npm = JSON.parse(download("https://registry.npmjs.org/" + mangleScoped(baseName)));
if ("error" in npm) {
throw new Error(baseName + " " + npm.error);
}
return npm.homepage;
return npm;
}

/** @param {string} baseName */
Expand Down
6 changes: 3 additions & 3 deletions packages/dts-critic/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { findDtsName, findNames, retrieveNpmHomepageOrFail, checkSource } = require("./index");
const { findDtsName, findNames, retrieveNpmOrFail, checkSource } = require("./index");
/**
* @param {string} description
* @param {{ [s: string]: () => void }} tests
Expand Down Expand Up @@ -109,10 +109,10 @@ Try adding -browser to the end of the name to get
suite("retrieveNpmHomepageOrFail", {
retrieveFailure() {
// surely parseltongue will never exist
expect(() => retrieveNpmHomepageOrFail("parseltongue")).toThrow(`parseltongue Not found`);
expect(() => retrieveNpmOrFail("parseltongue")).toThrow(`parseltongue Not found`);
},
retrieveShelljs() {
expect(retrieveNpmHomepageOrFail("shelljs")).toBe("https://github.com/shelljs/shelljs")
expect(retrieveNpmOrFail("shelljs").homepage).toBe("https://github.com/shelljs/shelljs")
}
})

Expand Down
59 changes: 55 additions & 4 deletions packages/dts-critic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/dts-critic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dependencies": {
"definitelytyped-header-parser": "^1.2.0",
"download-file-sync": "^1.0.4",
"semver": "^6.2.0",
"yargs": "^12.0.5"
},
"main": "index.js",
Expand Down Expand Up @@ -33,6 +34,7 @@
"devDependencies": {
"@types/jest": "^24.0.0",
"@types/node": "^10.12.21",
"@types/semver": "^6.0.1",
"@types/strip-json-comments": "0.0.30",
"@types/yargs": "^12.0.8",
"jest": "^24.7.1",
Expand Down

0 comments on commit a60d267

Please sign in to comment.