Files
flipper/desktop/flipper-server-core/src/utils/openFile.tsx
Andres Suarez 79023ee190 Update copyright headers from Facebook to Meta
Reviewed By: bhamodi

Differential Revision: D33331422

fbshipit-source-id: 016e8dcc0c0c7f1fc353a348b54fda0d5e2ddc01
2021-12-27 14:31:45 -08:00

33 lines
771 B
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import fs from 'fs-extra';
import open from 'open';
export async function openFile(path: string | null) {
if (!path) {
return;
}
let fileStat;
try {
fileStat = await fs.stat(path);
} catch (err) {
throw new Error(`Couldn't open file: ${path}: ${err}`);
}
// Rather randomly chosen. Some FSs still reserve 8 bytes for empty files.
// If this doesn't reliably catch "corrupt" files, you might want to increase this.
if (fileStat.size <= 8) {
throw new Error('File seems to be (almost) empty: ' + path);
}
await open(path);
}