Skip to content

Commit

Permalink
[cli] Turn off validate-def checks against npm (#4249)
Browse files Browse the repository at this point in the history
* comment out validate-def

* update docs with more detail
  • Loading branch information
Brianzchen committed Feb 3, 2022
1 parent 59f2ead commit d576e7d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
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

0 comments on commit d576e7d

Please sign in to comment.