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

[cli] Turn off validate-def checks against npm #4249

Merged
merged 2 commits into from Feb 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 40 additions & 31 deletions cli/src/lib/npm/npmLibDefs.js
@@ -1,5 +1,4 @@
// @flow

import {
ensureCacheRepo,
getCacheRepoDir,
Expand Down Expand Up @@ -31,7 +30,7 @@ import {

import semver from 'semver';

import got from 'got';
// import got from 'got';

import {ValidationError} from '../ValidationError';
import {TEST_FILE_NAME_RE} from '../libDefs';
Expand All @@ -58,6 +57,8 @@ async function extractLibDefsFromNpmPkgDir(
pkgDirPath: string,
scope: null | string,
pkgNameVer: string,
// Remove eslint error after `core.es5` change
// eslint-disable-next-line no-unused-vars
validating?: boolean,
): Promise<Array<NpmLibDef>> {
const parsedPkgNameVer = parsePkgNameVer(pkgNameVer);
Expand All @@ -70,31 +71,36 @@ async function extractLibDefsFromNpmPkgDir(
const libDefFileName = `${pkgName}_${pkgVersionStr}.js`;
const pkgDirItems = await fs.readdir(pkgDirPath);

if (validating) {
const fullPkgName = `${scope === null ? '' : scope + '/'}${pkgName}`;
await _npmExists(fullPkgName)
.then()
.catch(error => {
if (error.HTTPError && error.HTTPError.response.statusCode === 404) {
// Some times NPM returns 404 even though the package exists.
// Try to avoid false negatives by retrying
return new Promise((resolve, reject) =>
setTimeout(() => {
_npmExists(fullPkgName)
.then(resolve)
.catch(reject);
}, 1000),
);
}
})
.then()
.catch(error => {
// Only fail on 404, not on timeout
if (error.HTTPError && error.HTTPError.statusCode === 404) {
throw new ValidationError(`Package does not exist on npm!`);
}
});
}
/**
* TODO:
* The following block is commented out until `core.es5` has been moved to
* somewhere else such as core
*/
// if (validating) {
// const fullPkgName = `${scope === null ? '' : scope + '/'}${pkgName}`;
// await _npmExists(fullPkgName)
// .then()
// .catch(error => {
// if (error.HTTPError && error.HTTPError.response.statusCode === 404) {
// // Some times NPM returns 404 even though the package exists.
// // Try to avoid false negatives by retrying
// return new Promise((resolve, reject) =>
// setTimeout(() => {
// _npmExists(fullPkgName)
// .then(resolve)
// .catch(reject);
// }, 1000),
// );
// }
// })
// .then()
// .catch(error => {
// // Only fail on 404, not on timeout
// if (error.HTTPError && error.HTTPError.statusCode === 404) {
// throw new ValidationError(`Package does not exist on npm!`);
// }
// });
// }

const commonTestFiles = [];
const parsedFlowDirs: Array<[string, FlowVersion]> = [];
Expand Down Expand Up @@ -360,10 +366,13 @@ function filterLibDefs(
});
}

async function _npmExists(pkgName: string): Promise<Function> {
const pkgUrl = `https://www.npmjs.com/package/${pkgName}`;
return got(pkgUrl, {method: 'HEAD'});
}
// TODO Unused until `core.es5`
// async function _npmExists(pkgName: string): Promise<Function> {
// const pkgUrl = `https://api.npms.io/v2/package/${encodeURIComponent(
// pkgName,
// )}`;
// return got(pkgUrl, {method: 'HEAD'});
// }

export async function findNpmLibDef(
pkgName: string,
Expand Down
6 changes: 4 additions & 2 deletions docs/validate.md
@@ -1,11 +1,13 @@
# validate-defs

Validate the structure of the `/definitions` directory.
Validate the structure of the `/definitions` directory given a path that contains an `npm` dir.

### Examples

```
flow-typed validate-defs
flow-typed validate-defs definitions
flow-typed validate-defs ../definitions
flow-typed validate-defs ~/projects/flow-typed/definitions
```

## Flags
Expand Down