snapcube
Version:
📦 Snapshot, backup, and restore any project — binary-safe, fast, and simple CLI.
45 lines • 1.91 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createProject = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const core_1 = require("../core");
/**
* Recreate a project from a SnapCube JSON snapshot file.
*
* @param filePath - Path to the `.snapcube.json` file generated by SnapCube
*/
const createProject = (filePath) => {
console.log("Validating snapcube file");
// Step 1: Validate file format
if (!(0, core_1.isSnapcubeJsonValid)(filePath))
throw new Error("Invalid snapcube file");
console.log(`Restoring project from: ${filePath}`);
// Step 2: Parse the JSON snapshot into a list of files
const data = JSON.parse((0, fs_1.readFileSync)(filePath, "utf-8"));
// Step 3: Iterate over each file and restore it
for (const file of data) {
// Ensure the directory exists (create recursively if needed)
(0, fs_1.mkdirSync)(file.filePath, { recursive: true });
// If content was skipped (ignored during snapshot), don’t write it
const fullPath = (0, path_1.join)(file.filePath, file.fileName);
if (file.content === null) {
console.log(`Skipping content for ignored file: ${fullPath}`);
continue;
}
// Restore binary files (images, PDFs, etc.) from base64
if (file.isBinary) {
console.log(`🖼 Restoring binary file: ${fullPath}`);
(0, fs_1.writeFileSync)(fullPath, Buffer.from(file.content, "base64"));
}
// Restore text files in UTF-8 encoding
else {
console.log(`Restoring text file: ${fullPath}`);
(0, fs_1.writeFileSync)(fullPath, file.content, "utf-8");
}
}
// Step 4: Confirm completion
console.log(`Project restored successfully.`);
};
exports.createProject = createProject;
//# sourceMappingURL=createProject.js.map
;