@teambit/isolator
Version:
153 lines (149 loc) • 5.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _anyFs() {
const data = require("@teambit/any-fs");
_anyFs = function () {
return data;
};
return data;
}
function _execa() {
const data = _interopRequireDefault(require("execa"));
_execa = function () {
return data;
};
return data;
}
function _fsExtra() {
const data = _interopRequireDefault(require("fs-extra"));
_fsExtra = function () {
return data;
};
return data;
}
function path() {
const data = _interopRequireWildcard(require("path"));
path = function () {
return data;
};
return data;
}
function _containerExec() {
const data = _interopRequireDefault(require("./container-exec"));
_containerExec = function () {
return data;
};
return data;
}
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const debug = require('debug')('fs-container');
class FsContainer {
constructor(wrkDir) {
this.wrkDir = wrkDir;
_defineProperty(this, "id", 'FS Container');
_defineProperty(this, "fs", new (_anyFs().NodeFS)(this.wrkDir));
}
// TODO: do we need this?
getPath() {
return this.wrkDir;
}
composePath(pathToCompose) {
return path().join(this.getPath(), pathToCompose);
}
outputFile(file, data, options) {
const filePath = this.composePath(file);
debug(`writing file on ${filePath}`);
return _fsExtra().default.outputFile(filePath, data, options);
}
removePath(dir) {
const pathToRemove = this.composePath(dir);
return _fsExtra().default.remove(pathToRemove);
}
async symlink(src, dest) {
const srcPath = this.composePath(src);
const destPath = this.composePath(dest);
await _fsExtra().default.ensureDir(path().dirname(destPath));
return _fsExtra().default.ensureSymlink(srcPath, destPath);
}
async exec(execOptions, exec = new (_containerExec().default)()) {
const cwd = execOptions.cwd ? this.composePath(execOptions.cwd) : this.getPath();
debug(`executing the following command: ${execOptions.command.join(' ')}, on cwd: ${cwd}`);
const subprocessP = _execa().default.command(execOptions.command.join(' '), {
shell: true,
cwd,
stdio: ['ipc']
});
// @TODO: FIX! This probably causes errors ad the promise is not handled properly!
// eslint-disable-next-line @typescript-eslint/no-floating-promises
subprocessP.on('message', function (msg) {
exec.emit('message', msg);
});
/* eslint-disable @typescript-eslint/no-non-null-assertion */
subprocessP.stderr?.pipe(exec.stderr);
subprocessP.stdout?.pipe(exec.stdout);
['close', 'exit'].forEach(function (eventName) {
// @TODO: FIX! This probably causes errors ad the promise is not handled properly!
// eslint-disable-next-line @typescript-eslint/no-floating-promises
subprocessP.on(eventName, function (statusCode) {
exec.setStatus(statusCode);
});
});
return exec;
}
async execP(execOptions) {
let hasError = false;
const exec = await this.exec(execOptions);
return new Promise((resolve, reject) => {
exec.stdout.on('error', () => {
hasError = true;
});
exec.on('close', () => {
if (hasError) reject(exec.stderr.getContents(exec.stderr.size).toString());
resolve(exec.stdout.getContents(exec.stdout.size).toString());
});
});
}
async terminal() {
const cwd = this.getPath();
return _execa().default.command(process.env.SHELL || '/bin/zsh', {
cwd,
stdio: 'inherit'
});
}
start() {
return _fsExtra().default.ensureDir(this.wrkDir);
}
// @ts-ignore
async inspect() {
// todo: probably not needed for this container
}
async pause() {
// do nothing
}
async resume() {
// do nothing
}
// eslint-disable-next-line
stop(ttl) {
return _fsExtra().default.remove(this.wrkDir);
}
async destroy() {
await this.stop();
}
log() {
throw new Error('Method not implemented.');
}
on(event, fn) {
return fn(event);
// throw new Error('Method not implemented.');
}
}
exports.default = FsContainer;
//# sourceMappingURL=container.js.map