sample-pilet-service
Version:
Piral: Sample pilet feed service.
138 lines • 6.31 kB
JavaScript
;
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 });
exports.useSnapshot = useSnapshot;
const path_1 = require("path");
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
function readJson(path) {
return __awaiter(this, void 0, void 0, function* () {
try {
const content = yield (0, promises_1.readFile)(path, 'utf8');
return JSON.parse(content);
}
catch (_a) {
// let's just ignore it - maybe the file does not exist
}
return undefined;
});
}
function readdirSyncRecursive(dir) {
return __awaiter(this, void 0, void 0, function* () {
const files = yield (0, promises_1.readdir)(dir);
const result = [];
for (const file of files) {
const path = (0, path_1.resolve)(dir, file);
const s = yield (0, promises_1.stat)(path);
if (s.isFile()) {
result.push(file);
}
else if (s.isDirectory()) {
const subfiles = yield readdirSyncRecursive(path);
result.push(...subfiles.map((subfile) => `${file}/${subfile}`));
}
}
return result;
});
}
function getSnapshotDbFileName(snapshotDir) {
return (0, path_1.resolve)(process.cwd(), snapshotDir, 'db.json');
}
function getSnapshotPiletMetaFileName(snapshotDir, name, version) {
return (0, path_1.resolve)(process.cwd(), snapshotDir, name, `${version}.json`);
}
function getSnapshotPiletRootName(snapshotDir, name, version) {
return (0, path_1.resolve)(process.cwd(), snapshotDir, name, version);
}
function readSnapshotPiletFiles(snapshotDir, name, version) {
return __awaiter(this, void 0, void 0, function* () {
const dir = getSnapshotPiletRootName(snapshotDir, name, version);
const files = yield readdirSyncRecursive(dir);
const result = {};
for (const file of files) {
result[file] = yield (0, promises_1.readFile)((0, path_1.resolve)(dir, file));
}
return result;
});
}
function readPiletsFromSnapshot(snapshotDir) {
return __awaiter(this, void 0, void 0, function* () {
const path = getSnapshotDbFileName(snapshotDir);
return (yield readJson(path)) || {};
});
}
function useSnapshot(snapshotDir) {
if (snapshotDir) {
return {
read(piletData) {
return __awaiter(this, void 0, void 0, function* () {
const pilets = yield readPiletsFromSnapshot(snapshotDir);
for (const [name, version] of Object.entries(pilets)) {
const path = getSnapshotPiletMetaFileName(snapshotDir, name, version);
const meta = (yield readJson(path)) || {};
piletData[name] = {
current: version,
versions: {
[version]: {
meta,
files: yield readSnapshotPiletFiles(snapshotDir, name, version),
},
},
};
}
});
},
update(piletData) {
return __awaiter(this, void 0, void 0, function* () {
const path = getSnapshotDbFileName(snapshotDir);
const pilets = Object.entries(piletData).map(([name, value]) => [name, value.current]);
const content = JSON.stringify(Object.fromEntries(pilets), undefined, 2);
yield (0, promises_1.mkdir)((0, path_1.dirname)(path), { recursive: true });
yield (0, promises_1.writeFile)(path, content, 'utf8');
for (const [name] of pilets) {
const version = piletData[name].current;
const meta = getSnapshotPiletMetaFileName(snapshotDir, name, version);
if (!(0, fs_1.existsSync)(meta)) {
const root = getSnapshotPiletRootName(snapshotDir, name, version);
const data = piletData[name].versions[version];
const content = JSON.stringify(data.meta, undefined, 2);
const dirs = [root];
const files = [[meta, Buffer.from(content, 'utf8')]];
for (const [file, buffer] of Object.entries(data.files)) {
const fn = (0, path_1.resolve)(root, file);
const dir = (0, path_1.dirname)(fn);
files.push([fn, buffer]);
if (!dirs.includes(dir)) {
dirs.push(dir);
}
}
for (const dir of dirs) {
yield (0, promises_1.mkdir)(dir, { recursive: true });
}
for (const [file, buffer] of files) {
yield (0, promises_1.writeFile)(file, buffer);
}
}
}
});
},
};
}
return {
read() {
return __awaiter(this, void 0, void 0, function* () { });
},
update() {
return __awaiter(this, void 0, void 0, function* () { });
},
};
}
//# sourceMappingURL=snapshot.js.map