alm
Version:
The best IDE for TypeScript
107 lines (106 loc) • 3.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var cp = require("child_process");
var fsu = require("../../utils/fsu");
var utils = require("../../../common/utils");
var events_1 = require("../../../common/events");
var treeKill_1 = require("../../utils/treeKill");
var workerPrefix = "[DEMO]";
var nodeModulesFolder = fsu.travelUpTheDirectoryTreeTillYouFind(__dirname, "node_modules");
var tsNodeCompilerOptions = JSON.stringify({
/**
* Keep getting "cannot write file" ts / ts-node errors otherwise
*/
allowJs: false,
/** Node's not quite there yet */
target: 'es6',
module: 'commonjs',
/** Hopefully prevent a few source map bugs */
sourceMap: true,
inlineSources: true,
});
var FileExecutor = /** @class */ (function () {
function FileExecutor(filePath, cb) {
var _this = this;
this.cb = cb;
this.disposed = false;
/** Find key paths */
var tsNodePath = nodeModulesFolder + "/ts-node/dist/bin.js";
/** In this dir */
var cwd = utils.getDirectory(filePath);
/** With these compiler options */
var TS_NODE_COMPILER_OPTIONS = tsNodeCompilerOptions;
/** Execute this */
var toExec = [
tsNodePath,
filePath,
];
var child = cp.spawn(process.execPath, toExec, {
cwd: cwd,
env: {
TS_NODE_COMPILER_OPTIONS: TS_NODE_COMPILER_OPTIONS,
/**
* Disable cache just because
*/
TS_NODE_CACHE: false,
/**
* disableWarnings as we don't want it to prevent us from running the js
*/
TS_NODE_DISABLE_WARNINGS: true,
}
});
this.child = child;
child.stdout.on('data', function (data) {
if (_this.disposed)
return;
cb({ type: 'data', data: data.toString() });
});
child.stderr.on('data', function (data) {
if (_this.disposed)
return;
cb({ type: 'data', data: data.toString() });
});
child.on('close', function (code) {
if (_this.disposed)
return;
cb({ type: 'end', code: code });
console.log(workerPrefix, 'process ended');
});
}
FileExecutor.prototype.dispose = function () {
this.disposed = true;
if (this.child) {
treeKill_1.kill(this.child.pid);
this.child = undefined;
}
};
return FileExecutor;
}());
var WorkerImplementation;
(function (WorkerImplementation) {
var executor;
WorkerImplementation.currentFilePath = '';
WorkerImplementation.liveDemoData = new events_1.TypedEvent();
WorkerImplementation.enableLiveDemo = function (_a) {
var filePath = _a.filePath;
console.log(workerPrefix, "Started on filePath: " + filePath);
if (executor) {
executor.dispose();
}
WorkerImplementation.liveDemoData.emit({ type: 'start' });
executor = new FileExecutor(filePath, function (data) {
WorkerImplementation.liveDemoData.emit(data);
});
WorkerImplementation.currentFilePath = filePath;
return Promise.resolve({});
};
WorkerImplementation.disableLiveDemo = function () {
if (executor) {
WorkerImplementation.liveDemoData.emit({ type: 'end', code: 0 });
executor.dispose();
executor = undefined;
WorkerImplementation.currentFilePath = '';
}
return Promise.resolve({});
};
})(WorkerImplementation = exports.WorkerImplementation || (exports.WorkerImplementation = {}));