UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

212 lines (211 loc) 12.7 kB
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.unshift(_); } else if (_ = accept(result)) { if (kind === "field") initializers.unshift(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; 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'); } let ConfigController = (() => { var _a; let _classSuper = Controller; let _instanceExtraInitializers = []; let _getConfig_decorators; let _putConfig_decorators; let _regenerateKey_decorators; let _pruneTransform_decorators; let _pruneAll_decorators; let _pruneGit_decorators; return _a = class ConfigController extends _classSuper { constructor(subPath, filesService, folderRegistryContainer, engine) { super(subPath); Object.defineProperty(this, "filesService", { enumerable: true, configurable: true, writable: true, value: (__runInitializers(this, _instanceExtraInitializers), 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); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _getConfig_decorators = [RouteGet('/:driveId')]; _putConfig_decorators = [RoutePut('/:driveId')]; _regenerateKey_decorators = [RoutePost('/:driveId/regenerate_key')]; _pruneTransform_decorators = [RoutePost('/:driveId/prune_transform')]; _pruneAll_decorators = [RoutePost('/:driveId/prune_all')]; _pruneGit_decorators = [RoutePost('/:driveId/prune_git')]; __esDecorate(_a, null, _getConfig_decorators, { kind: "method", name: "getConfig", static: false, private: false, access: { has: obj => "getConfig" in obj, get: obj => obj.getConfig }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _putConfig_decorators, { kind: "method", name: "putConfig", static: false, private: false, access: { has: obj => "putConfig" in obj, get: obj => obj.putConfig }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _regenerateKey_decorators, { kind: "method", name: "regenerateKey", static: false, private: false, access: { has: obj => "regenerateKey" in obj, get: obj => obj.regenerateKey }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _pruneTransform_decorators, { kind: "method", name: "pruneTransform", static: false, private: false, access: { has: obj => "pruneTransform" in obj, get: obj => obj.pruneTransform }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _pruneAll_decorators, { kind: "method", name: "pruneAll", static: false, private: false, access: { has: obj => "pruneAll" in obj, get: obj => obj.pruneAll }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _pruneGit_decorators, { kind: "method", name: "pruneGit", static: false, private: false, access: { has: obj => "pruneGit" in obj, get: obj => obj.pruneGit }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); export { ConfigController };