UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

173 lines (172 loc) 7.92 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import yaml from 'js-yaml'; import { Controller, RouteGet, RoutePost, RoutePut } from './Controller.js'; import { GitScanner } from '../../../git/GitScanner.js'; import { UserConfigService } from '../../google_folder/UserConfigService.js'; async function loadHugoThemes(filesService) { if (!await filesService.exists('hugo_themes.json')) { await filesService.writeJson('hugo_themes.json', [{ id: 'ananke', name: 'Anake', url: 'https://github.com/budparr/gohugo-theme-ananke.git', preview_img: 'https://raw.githubusercontent.com/budparr/gohugo-theme-ananke/master/images/screenshot.png' }]); } return await filesService.readJson('hugo_themes.json'); } export class ConfigController extends Controller { constructor(subPath, filesService, folderRegistryContainer, engine) { super(subPath); Object.defineProperty(this, "filesService", { enumerable: true, configurable: true, writable: true, value: filesService }); Object.defineProperty(this, "folderRegistryContainer", { enumerable: true, configurable: true, writable: true, value: folderRegistryContainer }); Object.defineProperty(this, "engine", { enumerable: true, configurable: true, writable: true, value: engine }); } async returnConfig(userConfigService) { const hugo_themes = await loadHugoThemes(this.filesService); return { config: { ...userConfigService.config, rewrite_rules_yaml: yaml.dump(userConfigService.config.rewrite_rules || []) }, public_key: await userConfigService.getDeployKey(), hugo_themes }; } async getConfig(ctx) { const driveId = await ctx.routeParamPath('driveId'); const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); const googleFileSystem = await this.filesService.getSubFileService(driveId, ''); const userConfigService = new UserConfigService(googleFileSystem); await userConfigService.load(); return { ...await this.returnConfig(userConfigService), remote_url: await gitScanner.getRemoteUrl() }; } async putConfig(ctx) { const driveId = await ctx.routeParamPath('driveId'); const body = await ctx.routeParamBody(); const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); await gitScanner.setSafeDirectory(); const googleFileSystem = await this.filesService.getSubFileService(driveId, ''); const userConfigService = new UserConfigService(googleFileSystem); await userConfigService.load(); if (body.config?.remote_branch) { userConfigService.config.remote_branch = body.config?.remote_branch || 'main'; } if (body.config?.hugo_theme) { userConfigService.config.hugo_theme = body.config?.hugo_theme; } if (body.config?.config_toml) { userConfigService.config.config_toml = body.config?.config_toml; } if (body.config?.rewrite_rules_yaml) { userConfigService.config.rewrite_rules = yaml.load(body.config?.rewrite_rules_yaml); } if (body.config?.preview_rewrite_rule) { userConfigService.config.preview_rewrite_rule = body.config?.preview_rewrite_rule; } if (body.config?.companion_files_rule) { userConfigService.config.companion_files_rule = body.config?.companion_files_rule; } let modified = false; if ('string' === typeof body.config?.transform_subdir) { let trimmed = body.config?.transform_subdir.trim(); if (trimmed.length > 0 && !trimmed.startsWith('/')) { trimmed = '/' + trimmed; } if (userConfigService.config.transform_subdir !== trimmed) { modified = true; } userConfigService.config.transform_subdir = trimmed; } if (body.config?.actions_yaml) { userConfigService.config.actions_yaml = body.config?.actions_yaml; } userConfigService.config.auto_sync = !!body.config?.auto_sync; userConfigService.config.use_google_markdowns = !!body.config?.use_google_markdowns; userConfigService.config.fm_without_version = !!body.config?.fm_without_version; await userConfigService.save(); if (body.remote_url) { await gitScanner.setRemoteUrl(body.remote_url); } else if (body.remote_url === '') { await gitScanner.setRemoteUrl(''); } if (modified) { this.engine.emit(driveId, 'toasts:added', { title: 'Config modified', type: 'tree:changed' }); } return { ...await this.returnConfig(userConfigService), remote_url: await gitScanner.getRemoteUrl() }; } async regenerateKey(ctx) { const driveId = await ctx.routeParamPath('driveId'); const transformedFileSystem = await this.filesService.getSubFileService(driveId + '_transform', ''); const gitScanner = new GitScanner(ctx.logger, transformedFileSystem.getRealPath(), 'wikigdrive@wikigdrive.com'); await gitScanner.initialize(); const googleFileSystem = await this.filesService.getSubFileService(driveId, ''); const userConfigService = new UserConfigService(googleFileSystem); await userConfigService.load(); await userConfigService.genKeys(true); return { ...await this.returnConfig(userConfigService), remote_url: await gitScanner.getRemoteUrl() }; } async pruneTransform(ctx) { const driveId = await ctx.routeParamPath('driveId'); await this.folderRegistryContainer.pruneTransformFolder(driveId); } async pruneAll(ctx) { const driveId = await ctx.routeParamPath('driveId'); await this.folderRegistryContainer.pruneFolder(driveId); } async pruneGit(ctx) { const driveId = await ctx.routeParamPath('driveId'); await this.folderRegistryContainer.pruneGitFolder(driveId); } } __decorate([ RouteGet('/:driveId') ], ConfigController.prototype, "getConfig", null); __decorate([ RoutePut('/:driveId') ], ConfigController.prototype, "putConfig", null); __decorate([ RoutePost('/:driveId/regenerate_key') ], ConfigController.prototype, "regenerateKey", null); __decorate([ RoutePost('/:driveId/prune_transform') ], ConfigController.prototype, "pruneTransform", null); __decorate([ RoutePost('/:driveId/prune_all') ], ConfigController.prototype, "pruneAll", null); __decorate([ RoutePost('/:driveId/prune_git') ], ConfigController.prototype, "pruneGit", null);