ghostify
Version:
A ghost theme deployer
74 lines (73 loc) • 2.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-ignore
const admin_api_1 = __importDefault(require("@tryghost/admin-api"));
// @ts-ignore
const logging_1 = __importDefault(require("@tryghost/logging"));
const path_1 = require("path");
const adm_zip_1 = __importDefault(require("adm-zip"));
const fs_1 = require("fs");
const child_process_1 = require("child_process");
const os_1 = require("os");
function rmFile(fpath) {
if ((0, fs_1.existsSync)(fpath)) {
let isDir = (0, fs_1.statSync)(fpath).isDirectory();
let command = "";
if ((0, os_1.platform)() === "win32") {
command += (isDir ? "rmdir /S /Q " : "del /Q ") + fpath;
}
else {
command += `rm -r ${fpath}`;
}
return (0, child_process_1.execSync)(command).toString();
}
}
async function deploy(options) {
let { apiKey, apiUrl, basePath, themeName, excludes, preserve, debug } = options;
let zipPath = (0, path_1.resolve)(basePath, `${themeName}.zip`);
let hasError = false;
excludes = Array.isArray(excludes) ? excludes : (excludes || "").trim().split(" ");
excludes = excludes.filter((v) => !!v.trim().length);
excludes.push("ghostify.js(on)?");
excludes = excludes.map((rule) => rule.trim().replace(/[*.]/g, (m) => (m === "*" ? ".*" : "\\."))).join("|");
let excludeReg = new RegExp("(.*?(\\.(git.*|zip|(routes|redirects)\\.yaml|redirects\\.json)))|(yarn|npm|node_modules).*" +
(excludes.length ? "|" + excludes : ""), "gi");
// console.log(excludeReg)
// return
try {
let zip = new adm_zip_1.default();
zip.addLocalFolder(basePath, undefined, entry => {
let excl = !!entry.match(excludeReg);
if (debug && !excl)
logging_1.default.info("adding entry " + entry);
return !excl;
});
zip.writeZip(zipPath, (err) => {
if (err) {
hasError = true;
logging_1.default.error(err);
}
});
if (hasError) {
return Promise.reject({ error: "zip ended with errors" });
}
const api = new admin_api_1.default({
url: apiUrl,
key: apiKey,
version: "v5.0"
});
logging_1.default.info("sending theme " + themeName + ".zip ...");
return await api.themes.upload({ file: zipPath });
}
catch (error) {
return Promise.reject(error);
}
finally {
if (!preserve)
rmFile(zipPath);
}
}
exports.default = deploy;