Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit c96558a

Browse files
aigoncharovfacebook-github-bot
authored andcommitted
Remove fs and os usage from Mobile Builds plugin
Summary: Changelog: Expose env info and FS rm command to flipper plugins. Reviewed By: mweststrate Differential Revision: D32988478 fbshipit-source-id: 3d0233f9eb34d3478b07e39b9401c0e30ca95135
1 parent adb2573 commit c96558a

File tree

15 files changed

+166
-6
lines changed

15 files changed

+166
-6
lines changed

desktop/flipper-common/src/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {sleep} from './utils/sleep';
2121
export {timeout} from './utils/timeout';
2222
export {isTest} from './utils/isTest';
2323
export {assertNever} from './utils/assertNever';
24+
export {fsConstants} from './utils/fsConstants';
2425
export {
2526
logPlatformSuccessRate,
2627
reportPlatformFailures,

desktop/flipper-common/src/server-types.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export type FlipperServerCommands = {
140140
path: string,
141141
options?: {recursive?: boolean} & MkdirOptions,
142142
) => Promise<string | void>;
143+
'node-api-fs-rm': (path: string, options?: RmOptions) => Promise<void>;
143144
'node-api-fs-copyFile': (
144145
src: string,
145146
dest: string,
@@ -332,6 +333,10 @@ export interface MkdirOptions {
332333
mode?: string | number;
333334
}
334335

336+
export interface RmOptions {
337+
maxRetries?: number;
338+
}
339+
335340
export interface DownloadFileStartOptions {
336341
method?: 'GET' | 'POST';
337342
timeout?: number;

desktop/flipper-common/src/settings.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ export type ProcessConfig = {
6969
launcherEnabled: boolean;
7070
};
7171

72+
export type Platform =
73+
| 'aix'
74+
| 'android'
75+
| 'darwin'
76+
| 'freebsd'
77+
| 'haiku'
78+
| 'linux'
79+
| 'openbsd'
80+
| 'sunos'
81+
| 'win32'
82+
| 'cygwin'
83+
| 'netbsd';
84+
7285
export type EnvironmentInfo = {
7386
processId: number;
7487
isProduction: boolean;
@@ -77,7 +90,7 @@ export type EnvironmentInfo = {
7790
appVersion: string;
7891
os: {
7992
arch: string;
80-
platform: NodeJS.Platform;
93+
platform: Platform;
8194
unixname: string;
8295
};
8396
versions: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
// https://github.com/nodejs/node/blob/b66a75a3a4361614dde9bc1a52d7e9021b4efc26/typings/internalBinding/constants.d.ts
11+
export const fsConstants = {
12+
UV_FS_SYMLINK_DIR: 1,
13+
UV_FS_SYMLINK_JUNCTION: 2,
14+
O_RDONLY: 0,
15+
O_WRONLY: 1,
16+
O_RDWR: 2,
17+
UV_DIRENT_UNKNOWN: 0,
18+
UV_DIRENT_FILE: 1,
19+
UV_DIRENT_DIR: 2,
20+
UV_DIRENT_LINK: 3,
21+
UV_DIRENT_FIFO: 4,
22+
UV_DIRENT_SOCKET: 5,
23+
UV_DIRENT_CHAR: 6,
24+
UV_DIRENT_BLOCK: 7,
25+
S_IFMT: 61440,
26+
S_IFREG: 32768,
27+
S_IFDIR: 16384,
28+
S_IFCHR: 8192,
29+
S_IFBLK: 24576,
30+
S_IFIFO: 4096,
31+
S_IFLNK: 40960,
32+
S_IFSOCK: 49152,
33+
O_CREAT: 512,
34+
O_EXCL: 2048,
35+
UV_FS_O_FILEMAP: 0,
36+
O_NOCTTY: 131072,
37+
O_TRUNC: 1024,
38+
O_APPEND: 8,
39+
O_DIRECTORY: 1048576,
40+
O_NOFOLLOW: 256,
41+
O_SYNC: 128,
42+
O_DSYNC: 4194304,
43+
O_SYMLINK: 2097152,
44+
O_NONBLOCK: 4,
45+
S_IRWXU: 448,
46+
S_IRUSR: 256,
47+
S_IWUSR: 128,
48+
S_IXUSR: 64,
49+
S_IRWXG: 56,
50+
S_IRGRP: 32,
51+
S_IWGRP: 16,
52+
S_IXGRP: 8,
53+
S_IRWXO: 7,
54+
S_IROTH: 4,
55+
S_IWOTH: 2,
56+
S_IXOTH: 1,
57+
F_OK: 0,
58+
R_OK: 4,
59+
W_OK: 2,
60+
X_OK: 1,
61+
UV_FS_COPYFILE_EXCL: 1,
62+
COPYFILE_EXCL: 1,
63+
UV_FS_COPYFILE_FICLONE: 2,
64+
COPYFILE_FICLONE: 2,
65+
UV_FS_COPYFILE_FICLONE_FORCE: 4,
66+
COPYFILE_FICLONE_FORCE: 4,
67+
};

desktop/flipper-plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"@emotion/css": "^11.5.0",
1414
"@emotion/react": "^11.6.0",
1515
"@reach/observe-rect": "^1.2.0",
16-
"@types/uuid": "^8.3.1",
1716
"eventemitter3": "^4.0.7",
1817
"flipper-common": "0.0.0",
1918
"immer": "^9.0.6",
@@ -27,6 +26,7 @@
2726
},
2827
"devDependencies": {
2928
"@types/string-natural-compare": "^3.0.2",
29+
"@types/uuid": "^8.3.3",
3030
"jest-mock-console": "^1.2.3",
3131
"typescript": "^4.4.4"
3232
},

desktop/flipper-plugin/src/__tests__/api.node.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ test('Correct top level API exposed', () => {
7070
"usePlugin",
7171
"useTrackedCallback",
7272
"useValue",
73+
"uuid",
7374
"withTrackingScope",
7475
]
7576
`);

desktop/flipper-plugin/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export {createTablePlugin} from './utils/createTablePlugin';
133133
export {textContent} from './utils/textContent';
134134
import * as path from './utils/path';
135135
export {path};
136+
export * from './utils/uuid';
136137

137138
// It's not ideal that this exists in flipper-plugin sources directly,
138139
// but is the least pain for plugin authors.

desktop/flipper-plugin/src/plugin/FlipperLib.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import {
2121
DownloadFileStartOptions,
2222
DownloadFileStartResponse,
2323
DownloadFileUpdate,
24+
RmOptions,
25+
fsConstants,
26+
EnvironmentInfo,
2427
} from 'flipper-common';
2528

2629
export type FileEncoding = 'utf-8' | 'base64';
@@ -57,7 +60,9 @@ export type RemoteServerContext = {
5760
path: string,
5861
options?: {recursive?: false} & MkdirOptions,
5962
): Promise<void>;
63+
rm(path: string, options?: RmOptions): Promise<void>;
6064
copyFile(src: string, dest: string, flags?: number): Promise<void>;
65+
constants: typeof fsConstants;
6166
};
6267
downloadFile(
6368
url: string,
@@ -150,6 +155,10 @@ export interface FlipperLib {
150155
paths: {
151156
homePath: string;
152157
appPath: string;
158+
tempPath: string;
159+
};
160+
environmentInfo: {
161+
os: EnvironmentInfo['os'];
153162
};
154163
remoteServerContext: RemoteServerContext;
155164
}

desktop/flipper-plugin/src/test-utils/test-utils.tsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import {
1414
act as testingLibAct,
1515
} from '@testing-library/react';
1616
import {queries} from '@testing-library/dom';
17-
import {BundledPluginDetails, InstalledPluginDetails} from 'flipper-common';
17+
import {
18+
BundledPluginDetails,
19+
fsConstants,
20+
InstalledPluginDetails,
21+
} from 'flipper-common';
1822

1923
import {
2024
RealFlipperClient,
@@ -388,6 +392,14 @@ export function createMockFlipperLib(options?: StartPluginOptions): FlipperLib {
388392
paths: {
389393
appPath: process.cwd(),
390394
homePath: `/dev/null`,
395+
tempPath: `/dev/null`,
396+
},
397+
environmentInfo: {
398+
os: {
399+
arch: 'Test',
400+
unixname: 'test',
401+
platform: 'linux',
402+
},
391403
},
392404
remoteServerContext: {
393405
childProcess: {
@@ -398,7 +410,9 @@ export function createMockFlipperLib(options?: StartPluginOptions): FlipperLib {
398410
pathExists: jest.fn(),
399411
unlink: jest.fn(),
400412
mkdir: jest.fn(),
413+
rm: jest.fn(),
401414
copyFile: jest.fn(),
415+
constants: fsConstants,
402416
},
403417
downloadFile: jest.fn(),
404418
},
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
import {v4 as uuid} from 'uuid';
11+
export {uuid};

desktop/flipper-server-core/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"open": "^8.3.0",
2929
"openssl-wrapper": "^0.3.4",
3030
"promisify-child-process": "^4.1.1",
31+
"rimraf": "^3.0.2",
3132
"rsocket-core": "^0.0.27",
3233
"rsocket-flowable": "^0.0.27",
3334
"rsocket-tcp-server": "^0.0.25",
@@ -43,6 +44,7 @@
4344
"devDependencies": {
4445
"@types/memorystream": "^0.3.0",
4546
"@types/node": "^15.12.5",
47+
"@types/rimraf": "^3.0.2",
4648
"@types/tmp": "^0.2.2",
4749
"memorystream": "^0.3.1",
4850
"tmp": "^0.2.1"

desktop/flipper-server-core/src/FlipperServerImpl.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import {
4646
import {commandNodeApiExec} from './commands/NodeApiExec';
4747
import {commandDownloadFileStartFactory} from './commands/DownloadFile';
4848
import {promises} from 'fs';
49+
// Electron 11 runs on Node 12 which does not support fs.promises.rm
50+
import rm from 'rimraf';
4951

5052
const {access, copyFile, mkdir, unlink} = promises;
5153

@@ -230,6 +232,12 @@ export class FlipperServerImpl implements FlipperServer {
230232
},
231233
'node-api-fs-unlink': unlink,
232234
'node-api-fs-mkdir': mkdir,
235+
'node-api-fs-rm': async (path, {maxRetries} = {}) =>
236+
new Promise<void>((resolve, reject) =>
237+
rm(path, {disableGlob: true, maxBusyTries: maxRetries}, (err) =>
238+
err ? reject(err) : resolve(),
239+
),
240+
),
233241
'node-api-fs-copyFile': copyFile,
234242
// TODO: Do we need API to cancel an active download?
235243
'download-file-start': commandDownloadFileStartFactory(

desktop/flipper-ui-core/src/utils/flipperLibImplementation/index.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import {
1414
import {
1515
BufferEncoding,
1616
ExecOptions,
17+
fsConstants,
1718
Logger,
1819
MkdirOptions,
20+
RmOptions,
1921
} from 'flipper-common';
2022
import type {Store} from '../../reducers';
2123
import createPaste from '../../fb-stubs/createPaste';
@@ -71,6 +73,10 @@ export function initializeFlipperLibImplementation(
7173
paths: {
7274
appPath: renderHost.serverConfig.paths.appPath,
7375
homePath: renderHost.serverConfig.paths.homePath,
76+
tempPath: renderHost.serverConfig.paths.tempPath,
77+
},
78+
environmentInfo: {
79+
os: renderHost.serverConfig.environmentInfo.os,
7480
},
7581
remoteServerContext: {
7682
childProcess: {
@@ -95,13 +101,16 @@ export function initializeFlipperLibImplementation(
95101
path,
96102
options,
97103
)) as RemoteServerContext['fs']['mkdir'],
104+
rm: async (path: string, options?: RmOptions) =>
105+
renderHost.flipperServer.exec('node-api-fs-rm', path, options),
98106
copyFile: async (src: string, dest: string, flags?: number) =>
99107
renderHost.flipperServer.exec(
100108
'node-api-fs-copyFile',
101109
src,
102110
dest,
103111
flags,
104112
),
113+
constants: fsConstants,
105114
},
106115
downloadFile: downloadFileFactory(renderHost),
107116
},

desktop/yarn.lock

+16-3
Original file line numberDiff line numberDiff line change
@@ -3046,6 +3046,14 @@
30463046
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
30473047
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==
30483048

3049+
"@types/rimraf@^3.0.2":
3050+
version "3.0.2"
3051+
resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-3.0.2.tgz#a63d175b331748e5220ad48c901d7bbf1f44eef8"
3052+
integrity sha512-F3OznnSLAUxFrCEu/L5PY8+ny8DtcFRjx7fZZ9bycvXRi3KPTRS9HOitGZwvPg0juRhXFWIeKX58cnX5YqLohQ==
3053+
dependencies:
3054+
"@types/glob" "*"
3055+
"@types/node" "*"
3056+
30493057
"@types/rsocket-core@*", "@types/rsocket-core@^0.0.7":
30503058
version "0.0.7"
30513059
resolved "https://registry.yarnpkg.com/@types/rsocket-core/-/rsocket-core-0.0.7.tgz#e9ed6d9ec918ec7a9aab0c48fefbb74b33712235"
@@ -3174,6 +3182,11 @@
31743182
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.1.tgz#1a32969cf8f0364b3d8c8af9cc3555b7805df14f"
31753183
integrity sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg==
31763184

3185+
"@types/uuid@^8.3.3":
3186+
version "8.3.3"
3187+
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.3.tgz#c6a60686d953dbd1b1d45e66f4ecdbd5d471b4d0"
3188+
integrity sha512-0LbEEx1zxrYB3pgpd1M5lEhLcXjKJnYghvhTRgaBeUivLHMDM1TzF3IJ6hXU2+8uA4Xz+5BA63mtZo5DjVT8iA==
3189+
31773190
"@types/verror@^1.10.3":
31783191
version "1.10.4"
31793192
resolved "https://registry.yarnpkg.com/@types/verror/-/verror-1.10.4.tgz#805c0612b3a0c124cf99f517364142946b74ba3b"
@@ -6795,9 +6808,9 @@ flow-parser@0.*:
67956808
integrity sha512-IVHejqogTgZL2e206twVsdfX5he6mXS5F0AY315ao+6rEifbElEoVWKLYdEBsVl7QMp4buPbMe5FqXSdYQMUSQ==
67966809

67976810
follow-redirects@^1.14.0:
6798-
version "1.14.5"
6799-
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.5.tgz#f09a5848981d3c772b5392309778523f8d85c381"
6800-
integrity sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==
6811+
version "1.14.6"
6812+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.6.tgz#8cfb281bbc035b3c067d6cd975b0f6ade6e855cd"
6813+
integrity sha512-fhUl5EwSJbbl8AR+uYL2KQDxLkdSjZGR36xy46AO7cOMTrCMON6Sa28FmAnC2tRTDbd/Uuzz3aJBv7EBN7JH8A==
68016814

68026815
for-in@^1.0.2:
68036816
version "1.0.2"

docs/extending/flipper-plugin.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,12 @@ path.normalize('/foo/bar//baz/asdf/quux/..');
11601160
// Returns: '/foo/bar/baz/asdf'
11611161
```
11621162
1163+
### uuid
1164+
1165+
Usage: `uuid()`
1166+
1167+
Returns UUID V4.
1168+
11631169
## TestUtils
11641170
11651171
The object `TestUtils` as exposed from `flipper-plugin` exposes utilities to write unit tests for Sandy plugins.

0 commit comments

Comments
 (0)