flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
124 lines • 5.63 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const command_1 = require("../command");
const flagpole_execution_1 = require("../../flagpole-execution");
const fs = require("fs-extra");
const path = require("path");
const crypto = require("crypto");
const cli_helper_1 = require("../cli-helper");
const cli_1 = require("../cli");
const hash = (input) => {
return crypto.createHash("md5").update(input).digest("hex");
};
class About extends command_1.Command {
constructor() {
super(...arguments);
this.commandString = "watch";
this.files = {};
this.isHidden = true;
this.fsWait = null;
this.changeCount = 0;
}
action() {
return __awaiter(this, void 0, void 0, function* () {
cli_helper_1.printSubheader("Watch");
if (!flagpole_execution_1.FlagpoleExecution.global.config.project.isTypeScript) {
cli_helper_1.printLine("This Flagpole project is not using typescript.");
return cli_1.Cli.exit(1);
}
this.watchTestSrc();
});
}
watchTestSrc() {
const srcFolder = flagpole_execution_1.FlagpoleExecution.global.config.getSourceFolder();
const distFolder = flagpole_execution_1.FlagpoleExecution.global.config.getTestsFolder();
if (!srcFolder || !distFolder) {
return cli_1.Cli.log("TypeScript is not configured correctly for this Flagpole project.").exit(1);
}
cli_helper_1.printLine(`Watching for updates to: ${srcFolder}`);
fs.watch(srcFolder, { recursive: true }, (eventType, fileName) => {
if (fileName) {
if (typeof this.files[fileName] == "undefined") {
this.files[fileName] = {
contentMd5: null,
fileWait: null,
};
}
if (this.files[fileName].fileWait !== null) {
return;
}
this.files[fileName].fileWait = setTimeout(() => {
this.files[fileName].fileWait = null;
}, 100);
const md5Current = hash(fs.readFileSync(path.join(srcFolder, fileName), "utf8"));
if (md5Current === this.files[fileName].contentMd5) {
return;
}
this.files[fileName].contentMd5 = md5Current;
this.changeCount++;
if (this.fsWait !== null) {
return;
}
this.fsWait = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
cli_helper_1.printLine(`${this.changeCount} ${this.changeCount > 1 ? "files" : "file"} changed.`, "Re-transpiling tests...");
this.fsWait = null;
this.changeCount = 0;
try {
yield flagpole_execution_1.FlagpoleExecution.global.config.tsc();
cli_helper_1.printLine("Done!");
}
catch (err) {
cli_helper_1.printLine(String(err));
}
}), 1000);
}
});
}
watchDist(suiteNames, tag) {
const packageJson = require(path.join(flagpole_execution_1.FlagpoleExecution.global.config.getConfigFolder() || process.cwd(), "package.json"));
const entryPoint = packageJson.main;
const distFolder = path.dirname(entryPoint);
cli_1.Cli.log(`Watching for updates to: ${distFolder}`);
fs.watch(distFolder, { recursive: true }, (eventType, fileName) => {
if (fileName) {
if (typeof this.files[fileName] == "undefined") {
this.files[fileName] = {
contentMd5: null,
fileWait: null,
};
}
if (this.files[fileName].fileWait !== null) {
return;
}
this.files[fileName].fileWait = setTimeout(() => {
this.files[fileName].fileWait = null;
}, 100);
const md5Current = hash(fs.readFileSync(`${distFolder}/${fileName}`, "utf8"));
if (md5Current === this.files[fileName].contentMd5) {
return;
}
this.files[fileName].contentMd5 = md5Current;
this.changeCount++;
if (this.fsWait !== null) {
return;
}
this.fsWait = setTimeout(() => {
this.changeCount = 0;
this.fsWait = null;
cli_1.Cli.log(`${this.changeCount} files changed. Running tests...`);
}, 1000);
}
});
}
}
exports.default = About;
//# sourceMappingURL=watch.js.map