Skip to content

Commit

Permalink
feat(volume): add env variable to suppress fs.promise api warnings
Browse files Browse the repository at this point in the history
To suppress experimental fs.promise api warnings the SUPRESS_EXPERIMENTAL_PROMISE_WARNINGS env variable can be set to a truthy value
  • Loading branch information
Richard Samuelsson committed Jan 27, 2019
1 parent d7d99bc commit e6b6d0a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,11 @@ you get from `require('fs')`. Here are some things this function does:
```

- Adds constants `fs.constants`, `fs.F_OK`, etc.

## Experimental fs.promise api warnings

Supress warnings when using the promise api of fs by setting

```js
process.env['SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS'] = true;
```
12 changes: 11 additions & 1 deletion src/__tests__/process.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _process, { createProcess } from '../process';
import _process, { createProcess, SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS } from '../process';

describe('process', () => {
describe('createProcess', () => {
Expand All @@ -17,5 +17,15 @@ describe('process', () => {
expect(typeof proc.nextTick).toBe('function');
proc.nextTick(done);
});
it('.env', () => {
expect(typeof proc.env).toBe('object');
expect(!!proc.env[SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS]).toBe(false);
});
});
test('createProcess with env variable', () => {
process.env[SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS] = 'true';
const proc = createProcess();
expect(!!proc.env[SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS]).toBe(true);
delete process.env[SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS];
});
});
8 changes: 8 additions & 0 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// Here we mock the global `process` variable in case we are not in Node's environment.

export type SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS = 'SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS';
export const SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS: SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS =
'SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS';

export interface IProcess {
getuid(): number;
getgid(): number;
cwd(): string;
platform: string;
nextTick: (callback: (...args) => void, ...args) => void;
env: {
[SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS]?: boolean;
};
}

export function createProcess(p: IProcess = process): IProcess {
Expand All @@ -20,6 +27,7 @@ export function createProcess(p: IProcess = process): IProcess {
if (!p.getgid) p.getgid = () => 0;
if (!p.cwd) p.cwd = () => '/';
if (!p.nextTick) p.nextTick = require('./setImmediate').default;
if (!p.env) p.env = {};
return p;
}

Expand Down
4 changes: 2 additions & 2 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Stats, { TStatNumber } from './Stats';
import Dirent from './Dirent';
import { Buffer } from 'buffer';
import setImmediate from './setImmediate';
import process from './process';
import process, { SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS } from './process';
import setTimeoutUnref, { TSetTimeout } from './setTimeoutUnref';
import { Readable, Writable } from 'stream';
import { constants } from './constants';
Expand Down Expand Up @@ -516,7 +516,7 @@ function validateGid(gid: number) {

// ---------------------------------------- Volume

let promisesWarn = true;
let promisesWarn = process[SUPPRESS_EXPERIMENTAL_PROMISE_WARNINGS] ? false : true;

/**
* `Volume` represents a file system.
Expand Down

0 comments on commit e6b6d0a

Please sign in to comment.