Implement fs.stat and fs.readlink

Summary: Fixes issue for the stackTraceMapper

Reviewed By: aigoncharov

Differential Revision: D32987161

fbshipit-source-id: 660f49a1bdf61b2fd2963874ef23dfd284f71128
This commit is contained in:
Michel Weststrate
2021-12-13 05:46:42 -08:00
committed by Facebook GitHub Bot
parent af83523798
commit 34a1da3345
5 changed files with 47 additions and 2 deletions

View File

@@ -132,6 +132,21 @@ export type IOSDeviceParams = {
state?: string; state?: string;
}; };
// Serializable subset of StatsBase from fs.d.ts
export interface FSStatsLike {
isFile: boolean;
isDirectory: boolean;
isSymbolicLink: boolean;
mode: number;
uid: number;
gid: number;
size: number;
atimeMs: number;
mtimeMs: number;
ctimeMs: number;
birthtimeMs: number;
}
export type FlipperServerCommands = { export type FlipperServerCommands = {
'node-api-fs-access': (path: string, mode?: number) => Promise<void>; 'node-api-fs-access': (path: string, mode?: number) => Promise<void>;
'node-api-fs-pathExists': (path: string, mode?: number) => Promise<boolean>; 'node-api-fs-pathExists': (path: string, mode?: number) => Promise<boolean>;
@@ -146,6 +161,8 @@ export type FlipperServerCommands = {
dest: string, dest: string,
flags?: number, flags?: number,
) => Promise<void>; ) => Promise<void>;
'node-api-fs-stat': (path: string) => Promise<FSStatsLike>;
'node-api-fs-readlink': (path: string) => Promise<string>;
/** /**
* @throws ExecError * @throws ExecError
*/ */

View File

@@ -24,6 +24,7 @@ import {
RmOptions, RmOptions,
fsConstants, fsConstants,
EnvironmentInfo, EnvironmentInfo,
FSStatsLike,
} from 'flipper-common'; } from 'flipper-common';
export type FileEncoding = 'utf-8' | 'base64'; export type FileEncoding = 'utf-8' | 'base64';
@@ -49,6 +50,7 @@ export type RemoteServerContext = {
): Promise<ExecOut<string>>; ): Promise<ExecOut<string>>;
}; };
fs: { fs: {
constants: typeof fsConstants;
access(path: string, mode?: number): Promise<void>; access(path: string, mode?: number): Promise<void>;
pathExists(path: string, mode?: number): Promise<boolean>; pathExists(path: string, mode?: number): Promise<boolean>;
unlink(path: string): Promise<void>; unlink(path: string): Promise<void>;
@@ -62,7 +64,8 @@ export type RemoteServerContext = {
): Promise<void>; ): Promise<void>;
rm(path: string, options?: RmOptions): Promise<void>; rm(path: string, options?: RmOptions): Promise<void>;
copyFile(src: string, dest: string, flags?: number): Promise<void>; copyFile(src: string, dest: string, flags?: number): Promise<void>;
constants: typeof fsConstants; stat(path: string): Promise<FSStatsLike>;
readlink(path: string): Promise<string>;
}; };
downloadFile( downloadFile(
url: string, url: string,

View File

@@ -413,6 +413,8 @@ export function createMockFlipperLib(options?: StartPluginOptions): FlipperLib {
rm: jest.fn(), rm: jest.fn(),
copyFile: jest.fn(), copyFile: jest.fn(),
constants: fsConstants, constants: fsConstants,
stat: jest.fn(),
readlink: jest.fn(),
}, },
downloadFile: jest.fn(), downloadFile: jest.fn(),
}, },

View File

@@ -49,7 +49,7 @@ import {promises} from 'fs';
// Electron 11 runs on Node 12 which does not support fs.promises.rm // Electron 11 runs on Node 12 which does not support fs.promises.rm
import rm from 'rimraf'; import rm from 'rimraf';
const {access, copyFile, mkdir, unlink} = promises; const {access, copyFile, mkdir, unlink, stat, readlink} = promises;
export const SERVICE_FLIPPER = 'flipper.oAuthToken'; export const SERVICE_FLIPPER = 'flipper.oAuthToken';
@@ -239,6 +239,25 @@ export class FlipperServerImpl implements FlipperServer {
), ),
), ),
'node-api-fs-copyFile': copyFile, 'node-api-fs-copyFile': copyFile,
'node-api-fs-stat': async (path) => {
const stats = await stat(path);
const {atimeMs, birthtimeMs, ctimeMs, gid, mode, mtimeMs, size, uid} =
stats;
return {
atimeMs,
birthtimeMs,
ctimeMs,
gid,
mode,
mtimeMs,
size,
uid,
isDirectory: stats.isDirectory(),
isFile: stats.isFile(),
isSymbolicLink: stats.isSymbolicLink(),
};
},
'node-api-fs-readlink': readlink,
// TODO: Do we need API to cancel an active download? // TODO: Do we need API to cancel an active download?
'download-file-start': commandDownloadFileStartFactory( 'download-file-start': commandDownloadFileStartFactory(
this.emit.bind(this), this.emit.bind(this),

View File

@@ -111,6 +111,10 @@ export function initializeFlipperLibImplementation(
flags, flags,
), ),
constants: fsConstants, constants: fsConstants,
stat: async (path: string) =>
renderHost.flipperServer.exec('node-api-fs-stat', path),
readlink: async (path: string) =>
renderHost.flipperServer.exec('node-api-fs-readlink', path),
}, },
downloadFile: downloadFileFactory(renderHost), downloadFile: downloadFileFactory(renderHost),
}, },