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]: Make use of flow-typed.config.js for ignored #4288

Merged
merged 2 commits into from Mar 20, 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
62 changes: 62 additions & 0 deletions cli/src/commands/__tests__/install-test.js
Expand Up @@ -969,6 +969,68 @@ describe('install (command)', () => {
});
});

it("doesn't install definitions if ignored with flow-typed.config.js", () => {
return fakeProjectEnv(async FLOWPROJ_DIR => {
// Create some dependencies
await Promise.all([
mkdirp(path.join(FLOWPROJ_DIR, 'src')),
writePkgJson(path.join(FLOWPROJ_DIR, 'package.json'), {
name: 'test',
devDependencies: {
'flow-bin': '^0.43.0',
},
dependencies: {
'@scoped/package': '1.2.3',
foo: '1.2.3',
},
}),
mkdirp(path.join(FLOWPROJ_DIR, 'node_modules', 'foo')),
mkdirp(path.join(FLOWPROJ_DIR, 'node_modules', 'flow-bin')),
]);

await touchFile(path.join(FLOWPROJ_DIR, 'src', '.flowconfig'));
await mkdirp(path.join(FLOWPROJ_DIR, 'src', 'flow-typed'));
await touchFile(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed.config.json'),
);
await fs.writeJson(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed.config.json'),
{ignore: ['foo', '@scoped']},
);

// Run the install command
await run({
...defaultRunProps,
rootDir: path.join(FLOWPROJ_DIR, 'src'),
});

// Installs libdef
expect(
await fs.exists(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'npm',
'@scoped',
'package_vx.x.x.js',
),
),
).toEqual(false);
expect(
await fs.exists(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'npm',
'foo_vx.x.x.js',
),
),
).toEqual(false);
});
});

it('treats dependency prefixed with `>=` the same as `^`', () => {
return fakeProjectEnv(async FLOWPROJ_DIR => {
// Create some dependencies
Expand Down
28 changes: 18 additions & 10 deletions cli/src/commands/install.js
Expand Up @@ -190,6 +190,7 @@ export async function run(args: Args): Promise<number> {
skipCache: Boolean(args.skipCache),
ignoreDeps: ignoreDeps,
useCacheUntil: Number(args.useCacheUntil) || CACHE_REPO_EXPIRY,
ftConfig,
});
if (npmLibDefResult !== 0) {
return npmLibDefResult;
Expand Down Expand Up @@ -348,6 +349,7 @@ type installNpmLibDefsArgs = {|
skipCache: boolean,
ignoreDeps: Array<string>,
useCacheUntil: number,
ftConfig?: FtConfig,
|};
async function installNpmLibDefs({
cwd,
Expand All @@ -360,6 +362,7 @@ async function installNpmLibDefs({
skipCache,
ignoreDeps,
useCacheUntil,
ftConfig = {},
}: installNpmLibDefsArgs): Promise<number> {
const flowProjectRoot = await findFlowRoot(cwd);
if (flowProjectRoot === null) {
Expand All @@ -374,17 +377,22 @@ async function installNpmLibDefs({
const libdefsToSearchFor: Map<string, string> = new Map();

let ignoreDefs;
try {
ignoreDefs = fs
.readFileSync(path.join(cwd, libdefDir, '.ignore'), 'utf-8')
.replace(/"/g, '')
.split('\n');
} catch (err) {
// If the error is unrelated to file not existing we should continue throwing
if (err.code !== 'ENOENT') {
throw err;

if (Array.isArray(ftConfig.ignore)) {
ignoreDefs = ftConfig.ignore;
} else {
try {
ignoreDefs = fs
.readFileSync(path.join(cwd, libdefDir, '.ignore'), 'utf-8')
.replace(/"/g, '')
.split('\n');
} catch (err) {
// If the error is unrelated to file not existing we should continue throwing
if (err.code !== 'ENOENT') {
throw err;
}
ignoreDefs = [];
}
ignoreDefs = [];
}

// If a specific pkg/version was specified, only add those packages.
Expand Down
1 change: 1 addition & 0 deletions cli/src/lib/ftConfig.js
Expand Up @@ -3,6 +3,7 @@ import {fs, path} from './node';

export type FtConfig = {
env?: mixed, // Array<string>,
ignore?: Array<string>,
};

export const getFtConfig = (cwd: string): FtConfig | void => {
Expand Down
14 changes: 12 additions & 2 deletions docs/flow-typed-config.md
Expand Up @@ -8,10 +8,20 @@ Flow-typed supports a config file to help you set various project level settings

`env` accepts an array of strings that map to environment definitions that you can you can find [here](https://github.com/flow-typed/flow-typed/tree/master/definitions/environments).

```json
```js
{
"env": ["jsx", "node"]
env: ['jsx', 'node'],
}
```

Learn more about [environment definitions](env-definitions.md)

## ignore

When you have a dependencies you don't want updated or swapped out during the `install` command you can add this property which takes an array of strings referencing either package scopes or package names explicitly to ignore.

```js
{
ignore: ['@babel', '@custom/', 'eslint', 'eslint-plugin-ft-flow']
}
```
5 changes: 4 additions & 1 deletion docs/install.md
Expand Up @@ -31,8 +31,11 @@ flow-typed install --ignoreDeps peer bundle

### Ignoring dependencies

Sometimes you will come across dependencies that you both don't use in code and aren't defined in `flow-typed`, `@babel/core` is a good example and you don't want it polluting your codebase or causing unnecessarily large diffs during updates. Alternatively you may have some specific libdef version different to what is referenced in package.json and you don't want that swapped out.
Sometimes you will come across dependencies that you both don't use in code and aren't defined in `flow-typed`. `@babel/core` is a good example of this where you don't want it polluting your codebase or causing unnecessarily large diffs during updates. Alternatively you may have some specific libdef version different to what is referenced in package.json and you don't want that swapped out.

You can achieve this with the `ignore` property in [flow-typed.config.js](flow-typed-config.md)

#### .ignore _(deprecated)_
The `.ignore` file which you can place in `./flow-typed` can solve this by referencing scopes or packages you'd like to explicitly exclude when `flow-typed` tries to install/update libdefs for you.

```
Expand Down