UNPKG

@fusionauth/cli

Version:
83 lines (82 loc) 3.83 kB
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()); }); }; import { Command } from '@commander-js/extra-typings'; import { watch } from 'chokidar'; import { getLocaleFromLocalizedMessageFileName, reportError } from '../utils.js'; import Queue from 'queue'; import { FusionAuthClient } from '@fusionauth/typescript-client'; import { readFile } from 'fs/promises'; import logUpdate from "log-update"; import logSymbols from "log-symbols"; import chalk from "chalk"; import { apiKeyOption, hostOption, themeTypeOption } from "../options.js"; import { basename } from 'path'; // To prevent multiple uploads from happening at once, we use a queue const q = new Queue({ autostart: true, concurrency: 1 }); // noinspection JSUnusedGlobalSymbols export const themeWatch = new Command('theme:watch') .description('Watch a theme for changes and upload to FusionAuth') .argument('<themeId>', 'The theme id to watch') .option('-i, --input <input>', 'The input directory', './tpl/') .addOption(apiKeyOption) .addOption(hostOption) .addOption(themeTypeOption) .action((themeId, { input, key: apiKey, host, types }) => { console.log(`Watching theme directory ${input} for changes and uploading to ${themeId}`); const watchedFiles = []; if (types.includes('templates')) { watchedFiles.push(input + '/**/*.ftl'); } if (types.includes('stylesheet')) { watchedFiles.push(input + '/stylesheet.css'); } if (types.includes('messages')) { watchedFiles.push(input + '/defaultMessages.txt'); watchedFiles.push(input + '/localizedMessages.*.txt'); } watch(watchedFiles, { ignoreInitial: true, }) .on('all', (_, path) => { q.push(() => __awaiter(void 0, void 0, void 0, function* () { logUpdate(`Uploading ${path}`); const theme = {}; const content = yield readFile(path, 'utf-8'); if (path.endsWith('stylesheet.css')) { theme.stylesheet = content; } if (path.endsWith('defaultMessages.txt')) { theme.defaultMessages = content; } if (path.includes('localizedMessages.') && path.endsWith('.txt')) { const locale = getLocaleFromLocalizedMessageFileName(path); if (!locale) return; theme.localizedMessages = { [locale]: content }; } if (path.endsWith('.ftl')) { const name = basename(path).replace('.ftl', ''); theme.templates = { [name]: content }; } try { const fusionAuthClient = new FusionAuthClient(apiKey, host); yield fusionAuthClient.patchTheme(themeId, { theme }); logUpdate(`Uploading ${path} - ` + chalk.green(`${logSymbols.success} Success`)); logUpdate.done(); } catch (e) { logUpdate(`Uploading ${path} - ` + chalk.red(`${logSymbols.error} Failed`)); logUpdate.done(); reportError(`Error uploading theme ${themeId}: `, e); } return true; })); }); });