ng-spec-shot-reviewer
Version:
combined server of spec shot reviewer backend and optionally multiple static websites (e.g. compiled angular apps)
132 lines • 5.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const fs_1 = require("fs");
const path_1 = require("path");
const api_1 = require("../api");
const utility_functions_1 = require("./utility.functions");
class FsSsrServer {
constructor(cfg) {
this.cfg = cfg;
this._specShots = [];
this.refresh();
}
refresh() {
this.scanForAllImages();
this.loadApprovements();
}
scanForAllImages() {
this._specShots = [];
api_1.specShotFileTypes.forEach((type) => this.scanForImages(type));
}
loadApprovements() {
if (!utility_functions_1.canReadFsNode(this.cfg.approvedFilePath)) {
console.warn(chalk_1.default.yellow('WARN: did not found any approvements, yet'));
return [];
}
const approvements = JSON.parse(fs_1.readFileSync(this.cfg.approvedFilePath, 'UTF-8'));
this._specShots.forEach((specShot) => {
specShot.approved = approvements.includes(specShot.id);
});
}
scanForImages(type) {
const baseDir = this.cfg.directories[type];
if (!utility_functions_1.canReadFsNode(baseDir)) {
console.warn(chalk_1.default.yellow(`WARN: no '${type}'-images found!`));
return;
}
console.log('scan for images', baseDir);
this.findSpecShots(baseDir)
.forEach((specShotFile) => {
const path = path_1.parse(path_1.relative(baseDir, specShotFile.filename));
const id = encodeURIComponent(path_1.join(path.dir, path.name));
specShotFile.filename = path_1.join(type, path_1.relative(baseDir, specShotFile.filename));
const specShot = this.getOrCreateSpecShot(id);
specShot[type] = specShotFile;
});
}
getOrCreateSpecShot(id) {
const existingSpecShot = this._specShots.find((specShot) => specShot.id === id);
if (existingSpecShot) {
return existingSpecShot;
}
const newSpecShot = new api_1.SpecShot(id);
this._specShots.push(newSpecShot);
return newSpecShot;
}
findSpecShots(dir) {
console.log(chalk_1.default.gray(`scan dir ${dir}`));
return fs_1.readdirSync(dir)
.map((entry) => path_1.join(dir, entry))
.map((entry) => {
const stats = fs_1.statSync(entry);
return stats.isDirectory()
? this.findSpecShots(entry)
: this.foundSpecShot({
filename: entry,
timestamp: stats.mtime.getTime(),
size: stats.size,
});
})
.reduce((ids, more) => ids.concat(...more), new Array());
}
foundSpecShot(specShotFile) {
console.log(chalk_1.default.cyan(`found image ${specShotFile.filename}`));
return [specShotFile];
}
specShots() {
return Promise.resolve(this._specShots.slice());
}
approve(id) {
return this.vote(id, true);
}
disapprove(id) {
return this.vote(id, false);
}
vote(id, approved) {
const specShotToApprove = this._specShots.find((specShot) => specShot.id === id);
if (!specShotToApprove) {
throw new Error(`spec shot with id ${id} not found`);
}
specShotToApprove.approved = approved;
this.updateApprovedFile();
return Promise.resolve();
}
updateApprovedFile() {
fs_1.writeFileSync(this.cfg.approvedFilePath, JSON.stringify(this._specShots.filter((specShot) => specShot.approved).map((specShot) => specShot.id)), 'utf-8');
}
applyApprovements(ids) {
console.warn(chalk_1.default.green('applyApprovements'), ids);
ids.forEach((id) => {
const specShotIndex = this._specShots.findIndex((specShot) => specShot.id === id);
if (specShotIndex < 0) {
throw new Error(`spec shot with id ${id} not found`);
}
const specShot = this._specShots.splice(specShotIndex, 1).pop();
const actualFile = specShot.actual;
const baselineFile = specShot.actual;
if (actualFile) {
const actual = path_1.resolve(path_1.join(this.cfg.directories.baseDir, actualFile.filename));
const relative = path_1.relative(this.cfg.directories.actual, actual);
const baseline = path_1.join(this.cfg.directories.baseline, relative);
utility_functions_1.mkdirRecursive(this.cfg.directories.baseline);
fs_1.copyFileSync(actual, baseline);
fs_1.unlinkSync(actual);
}
else if (baselineFile) {
const baseline = path_1.resolve(path_1.join(this.cfg.directories.baseDir, baselineFile.filename));
fs_1.unlinkSync(baseline);
}
const diffFile = specShot.diff;
if (diffFile) {
const diff = path_1.resolve(path_1.join(this.cfg.directories.baseDir, diffFile.filename));
fs_1.unlinkSync(diff);
}
});
this.updateApprovedFile();
this.refresh();
return this.specShots();
}
}
exports.FsSsrServer = FsSsrServer;
//# sourceMappingURL=server.js.map