@salesforce/core
Version:
Core libraries to interact with SFDX projects, orgs, and APIs.
79 lines • 3.53 kB
JavaScript
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resetFs = exports.setFs = exports.getVirtualFs = exports.fs = void 0;
const nodeFs = __importStar(require("node:fs"));
// yes, we're going to import it even though it might not be used.
// the alternatives were all worse without top-level await (iife, runtime errors from something trying to use it before it's initialized)
const memfs = __importStar(require("memfs"));
const isWeb = () => process.env.FORCE_MEMFS === 'true' || 'window' in globalThis || 'self' in globalThis;
const getVirtualFs = (memfsVolume) => {
if (isWeb()) {
const memfsInstance = memfs.createFsFromVolume(memfsVolume ?? new memfs.Volume());
// Start with memfs instance and only override problematic methods
const webFs = {
...memfsInstance,
// Override only the methods that have incompatible signatures
promises: {
...memfsInstance.promises,
writeFile: async (file, data, options) => {
const finalOptions = typeof options === 'string' ? { encoding: options } : options;
await memfsInstance.promises.writeFile(file, data, finalOptions);
},
readFile: async (path, encoding) => {
const result = await memfsInstance.promises.readFile(path, encoding);
return encoding === 'utf8' ? String(result) : Buffer.from(result);
},
},
readFileSync: (path, encoding) => {
const result = memfsInstance.readFileSync(path, encoding);
return encoding === 'utf8' ? String(result) : Buffer.from(result);
},
writeFileSync: (file, data, encoding) => {
memfsInstance.writeFileSync(file, data, { encoding });
},
};
return webFs;
}
return nodeFs;
};
exports.getVirtualFs = getVirtualFs;
const setFs = (providedFs) => {
exports.fs = providedFs;
};
exports.setFs = setFs;
const resetFs = () => {
exports.fs = (0, exports.getVirtualFs)();
};
exports.resetFs = resetFs;
// Initialize fs at module load time
exports.fs = (0, exports.getVirtualFs)();
//# sourceMappingURL=fs.js.map
;