UNPKG

draaft

Version:

A CLI to pull content from https://pilot.pm content collaboration platform and feed your static site generator with markdown files

74 lines (73 loc) 2.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("@oclif/errors"); const fs = require("fs-extra"); const path = require("path"); const chalk_1 = require("chalk"); const signal_1 = require("./signal"); const currentPath = process.cwd(); exports.IMAGE_DIR = path.join(currentPath, "static", "img"); function ensureDir(dirPath) { try { if (!fs.existsSync(dirPath)) { fs.ensureDirSync(dirPath); signal_1.signal.created(`📁 ${dirPath}`); } } catch (error) { signal_1.signal.fatal(`📁 ${dirPath} not created`); throw new errors_1.CLIError(error); } } exports.ensureDir = ensureDir; function createFile(filePath, content) { try { ensureDir(path.dirname(filePath)); fs.writeFileSync(filePath, content); } catch (error) { signal_1.signal.fatal("Could not write file", error); throw new errors_1.CLIError(error); } } exports.createFile = createFile; function createFileSafe(filePath, content) { if (fs.existsSync(filePath)) { try { fs.copySync(filePath, `${filePath}.backup`); createFile(filePath, content); } catch (error) { signal_1.signal.fatal("Could not create backup file", error); throw new errors_1.CLIError(error); } } else { createFile(filePath, content); } } exports.createFileSafe = createFileSafe; function createContentFile(dirPath, fileName, content) { try { createFile(path.join(dirPath, fileName), content); signal_1.signal.created(`📄 ${chalk_1.default.gray(dirPath)}${path.sep}${fileName}`); } catch (error) { signal_1.signal.fatal(error); throw new errors_1.CLIError(error); } } exports.createContentFile = createContentFile; function createImageFile(dirPath, imageName, axiosResponse) { try { ensureDir(dirPath); let writeStream = fs.createWriteStream(path.join(dirPath, imageName)); axiosResponse.data.pipe(writeStream); signal_1.signal.downloaded(`${chalk_1.default.gray(dirPath)}${path.sep}${imageName}`); } catch (error) { signal_1.signal.fatal(error); throw new errors_1.CLIError(error); } } exports.createImageFile = createImageFile;