UNPKG

a-mail-signature

Version:

Generate and modify Apple Mail E-Mail signatures

162 lines (161 loc) 6.6 kB
#!/usr/bin/env node "use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * The above line is needed to be able to run in npx and CI. */ const fs_1 = __importDefault(require("fs")); const plist_1 = __importDefault(require("plist")); const yargs_1 = __importDefault(require("yargs")); const uuid_1 = require("uuid"); const log_1 = require("./utils/log"); const prompt = require('select-prompt'); var fileDefaults; (function (fileDefaults) { fileDefaults["accountMap"] = "AccountsMap.plist"; fileDefaults["allSignatures"] = "AllSignatures.plist"; fileDefaults["persistenceInfo"] = "PersistenceInfo.plist"; })(fileDefaults || (fileDefaults = {})); /** * determine the current used base path * for storing mail signatures in apple mail */ const getBasePath = () => { const baseDir = 'test-drive/home-drive'; // os.homedir() const persistenceInfoPath = `${baseDir}/Library/Mail/${fileDefaults.persistenceInfo}`; const persistenceInfoParsed = plist_1.default.parse(fs_1.default.readFileSync(persistenceInfoPath, 'utf8')); return `${baseDir}/Library/Mail/${persistenceInfoParsed.LastUsedVersionDirectoryName}/MailData/Signatures`; }; /** * add a signature entry to the file containing all signatures * @param uuid the uuid for the signature to be added */ const addSignatureToAllSignatures = (uuid) => { const allSignaturesParsed = plist_1.default.parse(fs_1.default.readFileSync(`${getBasePath()}/${fileDefaults.allSignatures}`, 'utf8')); if (allSignaturesParsed.find((signature) => signature.SignatureUniqueId === uuid)) { log_1.logError(`Signature with unique ID "${uuid}" already exists`); process.exit(1); } const dictJson = [ ...allSignaturesParsed, { SignatureIsRich: false, SignatureName: 'My-Signature', SignatureUniqueId: uuid, }, ]; const plistResult = plist_1.default.build(dictJson); fs_1.default.writeFileSync(`${getBasePath()}/${fileDefaults.allSignatures}`, plistResult, 'utf8'); }; /** * Promt the user to select the account to which the signature will be added * @param uuid the uuid for the signature to be added */ const addSignatureToAccount = (uuid) => { const accountMapParsed = plist_1.default.parse(fs_1.default.readFileSync(`${getBasePath()}/${fileDefaults.accountMap}`, 'utf8')); return new Promise((resolve) => { const accounts = []; for (let [key, value] of Object.entries(accountMapParsed)) { accounts.push({ title: value.AccountURL.replace('%40', '@'), value: key, }); } prompt('To which account you want to add this signature?', accounts) .on('abort', (v) => { log_1.logWarn('No account selected. No signature added.'); process.exit(2); }) .on('submit', (v) => { if (accountMapParsed[v].Signatures.includes(uuid)) { log_1.logWarn(`Signature with unique ID "${uuid}" is already linked with account ${accountMapParsed[v].AccountURL}. Action skipped.`); } else { accountMapParsed[v].Signatures.push(uuid); } const plistResult = plist_1.default.build( // workaround because of wrong type matching JSON.parse(JSON.stringify(accountMapParsed))); fs_1.default.writeFileSync(`${getBasePath()}/${fileDefaults.accountMap}`, plistResult, 'utf8'); resolve(); }); }); }; /** * create the mail signature file * @param uuid the uuid for the signature to be added * @param templateFilePath the template file to be used */ const createMailSignature = (uuid, templateFilePath) => { const filePath = `${getBasePath()}/${uuid}.mailsignature`; if (fs_1.default.existsSync(filePath)) { log_1.logError(`Signature file "${uuid}.mailsignature" already exists`); process.exit(1); } const htmlTemplate = fs_1.default .readFileSync(templateFilePath, 'utf8') .replace(/(.|\n)*<body.*>/, '') .replace(/<\/body(.|\n)*/g, ''); const fileContent = `Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Mime-Version: 1.0 <body>${htmlTemplate}</body>`; fs_1.default.writeFileSync(filePath, fileContent, 'utf8'); }; /** * Create a new mail signature from an HTML template * @param path the HTML template file path */ const createSignature = (path) => __awaiter(void 0, void 0, void 0, function* () { const signatureUuid = uuid_1.v4().toUpperCase(); const accountMapPath = `${getBasePath()}/${fileDefaults.accountMap}`; const accountMapParsed = plist_1.default.parse(fs_1.default.readFileSync(accountMapPath, 'utf8')); createMailSignature(signatureUuid, path); addSignatureToAllSignatures(signatureUuid); yield addSignatureToAccount(signatureUuid); }); yargs_1.default .scriptName('a-mail-signature') .command({ command: 'create [path]', aliases: ['c', 'add', 'a'], describe: 'Create a signature from an HTML file', handler: (args) => { createSignature(args.path); }, }) /* .command({ command: 'update [path]', aliases: ['u', 'modify', 'm'], describe: 'Update a signature from an HTML file', handler: (args: { path: string }) => { updateSignature(args.path); }, }) .command({ command: 'delete [name]', aliases: ['d', 'remove', 'rm', 'r'], describe: 'Delete an existing mail signature', handler: (args: { name: string }) => { deleteSignature(args.name); }, }) */ .wrap(100) .demandCommand(2) .help().argv; //# sourceMappingURL=index.js.map