gow
Version:
Watch your files on the gow.
348 lines • 18.7 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = require("fs");
var child_process_1 = require("child_process");
var Gow = /** @class */ (function () {
function Gow(path, config) {
var _this = this;
this.readyForNextReload = true;
this.reloadInQueue = false;
this.cache = {
project: { last: [], current: [] },
runtime: { last: [], current: [] }
};
this.delay = config.delay || 1000;
this.command = config.command || "node .";
this.silent = config.silent || false;
this.fileExpression = config.files ? new RegExp(config.files.map(function (fileGlob) { return _this.getRegularExpressionByGlob(fileGlob); }).join("|")) : new RegExp(this.getRegularExpressionByGlob("***/*"));
this.path = this.normalizePath(path);
this.runtimePath = this.path + ".gowing/";
this.createRuntime();
}
Gow.prototype.createProcesses = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.copyIntoRuntime()];
case 1:
_a.sent();
this.runtimeProcess = this.spawnCommandProcess(this.command);
return [2 /*return*/];
}
});
});
};
Gow.prototype.createRuntime = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.createProcesses()];
case 1:
_a.sent();
if (!this.silent)
console.log("\x1b[97;42m Gow \x1b[0m Created process");
setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
var modifiedRuntimeFiles, haveProjectFilesBeenModified, _i, _a, modifiedRuntimeFile;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.getModifiedRuntimeFiles()];
case 1:
modifiedRuntimeFiles = _b.sent();
return [4 /*yield*/, this.haveProjectFilesBeenModified()];
case 2:
haveProjectFilesBeenModified = _b.sent();
if (!modifiedRuntimeFiles) return [3 /*break*/, 6];
_i = 0, _a = modifiedRuntimeFiles;
_b.label = 3;
case 3:
if (!(_i < _a.length)) return [3 /*break*/, 6];
modifiedRuntimeFile = _a[_i];
return [4 /*yield*/, fs_1.promises.copyFile(modifiedRuntimeFile.path, this.normalizePath(modifiedRuntimeFile.path, false).replace(this.runtimePath, this.path))];
case 4:
_b.sent();
this.ignoreRuntimeChanges.push(modifiedRuntimeFile.name);
_b.label = 5;
case 5:
_i++;
return [3 /*break*/, 3];
case 6:
if (this.reloadInQueue && this.readyForNextReload) {
this.readyForNextReload = false;
this.reloadInQueue = false;
this.createProcesses();
if (!this.silent)
console.log("\x1b[97;42m Gow \x1b[0m Reloaded");
return [2 /*return*/];
}
if (!haveProjectFilesBeenModified)
return [2 /*return*/];
if (!this.readyForNextReload) {
this.reloadInQueue = true;
return [2 /*return*/];
}
this.readyForNextReload = false;
this.createProcesses();
if (!this.silent)
console.log("\x1b[97;42m Gow \x1b[0m Reloaded");
return [2 /*return*/];
}
});
}); }, 250);
return [2 /*return*/];
}
});
});
};
Gow.prototype.isWindows = function () {
return /^win/.test(process.platform);
};
Gow.prototype.normalizePath = function (path, tailingSlash) {
if (tailingSlash === void 0) { tailingSlash = true; }
var normalizedPath = path.replace(/(\/|\\)/, "/");
return tailingSlash ? normalizedPath.replace(/\/$/, "") + "/" : normalizedPath.replace(/\/$/, "");
};
Gow.prototype.getRegularExpressionByGlob = function (glob) {
return ((glob.includes("/") ? glob : "./" + glob) + "$")
.replace(/\*/g, "ALL")
.replace(/\//g, "SLASH")
.replace(/\./g, "\.")
.replace(/\[!/g, "[^")
.replace(/ALLALLALL/g, "((.*)(?=\\/)(\\/)|)")
.replace(/ALLALL/g, "(([a-zA-Z0-9-_]*)\\/|\.\/|)")
.replace(/ALL/g, "([a-zA-Z0-9-_.]*)")
.replace(/SLASH/g, "");
};
Gow.prototype.spawnCommandProcess = function (command) {
var _this = this;
process.chdir(this.runtimePath);
if (this.runtimeProcess) {
if (this.isWindows()) {
child_process_1.spawn("taskkill", ["/pid", this.runtimeProcess.pid.toString(), "/f", "/t"]);
}
else {
this.runtimeProcess.kill();
}
}
var processID = Math.round(Math.random() * 1000);
this.waitingForProcess = processID;
setTimeout(function () {
if (_this.waitingForProcess === processID)
_this.readyForNextReload = true;
}, this.delay);
return child_process_1.exec(command, function (error, stdout, stderr) {
(stdout || stderr).length > 2 && console.log((stdout || stderr).replace(/\n$/g, ""));
});
};
Gow.prototype.copyIntoRuntime = function () {
return __awaiter(this, void 0, void 0, function () {
var e_1, files, _i, files_1, path, relativePath, folders, _a, folders_1, folder, e_2;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
process.chdir(this.path);
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 5]);
return [4 /*yield*/, fs_1.promises.readdir(this.runtimePath)];
case 2:
_b.sent();
return [3 /*break*/, 5];
case 3:
e_1 = _b.sent();
return [4 /*yield*/, fs_1.promises.mkdir(this.runtimePath)];
case 4:
_b.sent();
return [3 /*break*/, 5];
case 5: return [4 /*yield*/, this.getFiles(this.path, { last: [], current: [] })];
case 6:
files = _b.sent();
_i = 0, files_1 = files;
_b.label = 7;
case 7:
if (!(_i < files_1.length)) return [3 /*break*/, 17];
path = files_1[_i].path;
relativePath = this.normalizePath(path, false).replace(this.path, "");
folders = relativePath.match(/([a-zA-Z0-9\/]*)(?=\/)/g);
if (!folders) return [3 /*break*/, 14];
_a = 0, folders_1 = folders;
_b.label = 8;
case 8:
if (!(_a < folders_1.length)) return [3 /*break*/, 14];
folder = folders_1[_a];
_b.label = 9;
case 9:
_b.trys.push([9, 11, , 13]);
return [4 /*yield*/, fs_1.promises.readdir(this.runtimePath + folder)];
case 10:
_b.sent();
return [3 /*break*/, 13];
case 11:
e_2 = _b.sent();
return [4 /*yield*/, fs_1.promises.mkdir(this.runtimePath + folder, { recursive: true })];
case 12:
_b.sent();
return [3 /*break*/, 13];
case 13:
_a++;
return [3 /*break*/, 8];
case 14: return [4 /*yield*/, fs_1.promises.copyFile(relativePath, this.runtimePath + relativePath)];
case 15:
_b.sent();
_b.label = 16;
case 16:
_i++;
return [3 /*break*/, 7];
case 17: return [2 /*return*/];
}
});
});
};
Gow.prototype.getFiles = function (path, cache) {
if (path === void 0) { path = "./"; }
return __awaiter(this, void 0, void 0, function () {
var files, _a, _b, folders, _i, folders_2, folder, _c, _d, _e;
var _this = this;
return __generator(this, function (_f) {
switch (_f.label) {
case 0:
_b = (_a = Promise).all;
return [4 /*yield*/, fs_1.promises.readdir(path, { withFileTypes: true })];
case 1: return [4 /*yield*/, _b.apply(_a, [(_f.sent())
.filter(function (entry) {
return !entry.isDirectory()
&& !entry.name.endsWith(".swp");
})
.map(function (_a) {
var name = _a.name;
return __awaiter(_this, void 0, void 0, function () {
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = {
name: name,
path: path + name
};
return [4 /*yield*/, fs_1.promises.stat(path + name)];
case 1: return [2 /*return*/, (_b.modified = (_c.sent()).mtime,
_b.lastModified = cache.last.length === 0 ? "FIRST_RUN" : cache.last.find(function (file) { return file.path === path + name; }) && cache.last.find(function (file) { return file.path === path + name; }).modified,
_b)];
}
});
});
})])];
case 2:
files = _f.sent();
return [4 /*yield*/, fs_1.promises.readdir(path, { withFileTypes: true })];
case 3:
folders = (_f.sent()).filter(function (folder) {
return folder.isDirectory()
&& !folder.name.startsWith(".")
&& folder.name !== "node_modules";
});
_i = 0, folders_2 = folders;
_f.label = 4;
case 4:
if (!(_i < folders_2.length)) return [3 /*break*/, 7];
folder = folders_2[_i];
_d = (_c = files.push).apply;
_e = [files];
return [4 /*yield*/, this.getFiles("" + path + folder.name + "/", cache)];
case 5:
_d.apply(_c, _e.concat([_f.sent()]));
_f.label = 6;
case 6:
_i++;
return [3 /*break*/, 4];
case 7: return [2 /*return*/, files];
}
});
});
};
Gow.prototype.getModifiedRuntimeFiles = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, changedFiles;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this.cache.runtime.last = this.cache.runtime.current;
_a = this.cache.runtime;
return [4 /*yield*/, this.getFiles(this.runtimePath, this.cache.runtime)];
case 1:
_a.current = _b.sent();
changedFiles = this.cache.runtime.current.filter(function (file) {
return (_this.normalizePath(file.path, false).replace(_this.runtimePath, "")).replace(_this.fileExpression, "") === ""
&& (file.lastModified && file.lastModified.toString()) !== file.modified.toString()
&& file.lastModified !== "FIRST_RUN";
});
return [2 /*return*/, changedFiles.length > 0 ? changedFiles : false];
}
});
});
};
Gow.prototype.haveProjectFilesBeenModified = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, haveProjectFilesBeenModified;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this.cache.project.last = this.cache.project.current;
_a = this.cache.project;
return [4 /*yield*/, this.getFiles(this.path, this.cache.project)];
case 1:
_a.current = _b.sent();
haveProjectFilesBeenModified = this.cache.project.current.filter(function (file) {
return (_this.normalizePath(file.path, false).replace(_this.path, "")).replace(_this.fileExpression, "") === ""
&& (file.lastModified && file.lastModified.toString()) !== file.modified.toString()
&& file.lastModified !== "FIRST_RUN"
&& !_this.ignoreRuntimeChanges.includes(file.name);
}).length > 0;
this.ignoreRuntimeChanges = [];
return [2 /*return*/, haveProjectFilesBeenModified];
}
});
});
};
return Gow;
}());
exports.Gow = Gow;
//# sourceMappingURL=gow.js.map