UNPKG

@codesandbox/sandpack-client

Version:

<img style="width:100%" src="https://user-images.githubusercontent.com/4838076/143581035-ebee5ba2-9cb1-4fe8-a05b-2f44bd69bb4b.gif" alt="Component toolkit for live running code editing experiences" />

181 lines 6.29 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var file_system_1 = require("../core/file_system"); var path = require("path"); var api_error_1 = require("../core/api_error"); /** * The FolderAdapter file system wraps a file system, and scopes all interactions to a subfolder of that file system. * * Example: Given a file system `foo` with folder `bar` and file `bar/baz`... * * ```javascript * BrowserFS.configure({ * fs: "FolderAdapter", * options: { * folder: "bar", * wrapped: foo * } * }, function(e) { * var fs = BrowserFS.BFSRequire('fs'); * fs.readdirSync('/'); // ['baz'] * }); * ``` */ var FolderAdapter = /** @class */ (function (_super) { __extends(FolderAdapter, _super); function FolderAdapter(folder, wrapped) { var _this = _super.call(this) || this; _this._folder = folder; _this._wrapped = wrapped; return _this; } /** * Creates a FolderAdapter instance with the given options. */ FolderAdapter.Create = function (opts, cb) { var fa = new FolderAdapter(opts.folder, opts.wrapped); fa._initialize(function (e) { if (e) { cb(e); } else { cb(null, fa); } }); }; FolderAdapter.isAvailable = function () { return true; }; FolderAdapter.prototype.getName = function () { return this._wrapped.getName(); }; FolderAdapter.prototype.isReadOnly = function () { return this._wrapped.isReadOnly(); }; FolderAdapter.prototype.supportsProps = function () { return this._wrapped.supportsProps(); }; FolderAdapter.prototype.supportsSynch = function () { return this._wrapped.supportsSynch(); }; FolderAdapter.prototype.supportsLinks = function () { return false; }; /** * Initialize the file system. Ensures that the wrapped file system * has the given folder. */ FolderAdapter.prototype._initialize = function (cb) { var _this = this; this._wrapped.exists(this._folder, function (exists) { if (exists) { cb(); } else if (_this._wrapped.isReadOnly()) { cb(api_error_1.ApiError.ENOENT(_this._folder)); } else { _this._wrapped.mkdir(_this._folder, 0x1ff, cb); } }); }; FolderAdapter.Name = "FolderAdapter"; FolderAdapter.Options = { folder: { type: "string", description: "The folder to use as the root directory" }, wrapped: { type: "object", description: "The file system to wrap" } }; return FolderAdapter; }(file_system_1.BaseFileSystem)); exports.default = FolderAdapter; /** * @hidden */ function translateError(folder, e) { if (e !== null && typeof e === 'object') { var err = e; var p = err.path; if (p) { p = '/' + path.relative(folder, p); err.message = err.message.replace(err.path, p); err.path = p; } } return e; } /** * @hidden */ function wrapCallback(folder, cb) { if (typeof cb === 'function') { return function (err) { if (arguments.length > 0) { arguments[0] = translateError(folder, err); } cb.apply(null, arguments); }; } else { return cb; } } /** * @hidden */ function wrapFunction(name, wrapFirst, wrapSecond) { if (name.slice(name.length - 4) !== 'Sync') { // Async function. Translate error in callback. return function () { if (arguments.length > 0) { if (wrapFirst) { arguments[0] = path.join(this._folder, arguments[0]); } if (wrapSecond) { arguments[1] = path.join(this._folder, arguments[1]); } arguments[arguments.length - 1] = wrapCallback(this._folder, arguments[arguments.length - 1]); } return this._wrapped[name].apply(this._wrapped, arguments); }; } else { // Sync function. Translate error in catch. return function () { try { if (wrapFirst) { arguments[0] = path.join(this._folder, arguments[0]); } if (wrapSecond) { arguments[1] = path.join(this._folder, arguments[1]); } return this._wrapped[name].apply(this._wrapped, arguments); } catch (e) { throw translateError(this._folder, e); } }; } } // First argument is a path. ['diskSpace', 'stat', 'statSync', 'open', 'openSync', 'unlink', 'unlinkSync', 'rmdir', 'rmdirSync', 'mkdir', 'mkdirSync', 'readdir', 'readdirSync', 'exists', 'existsSync', 'realpath', 'realpathSync', 'truncate', 'truncateSync', 'readFile', 'readFileSync', 'writeFile', 'writeFileSync', 'appendFile', 'appendFileSync', 'chmod', 'chmodSync', 'chown', 'chownSync', 'utimes', 'utimesSync', 'readlink', 'readlinkSync'].forEach(function (name) { FolderAdapter.prototype[name] = wrapFunction(name, true, false); }); // First and second arguments are paths. ['rename', 'renameSync', 'link', 'linkSync', 'symlink', 'symlinkSync'].forEach(function (name) { FolderAdapter.prototype[name] = wrapFunction(name, true, true); });