|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * (c) Copyright Ascensio System SIA 2025 |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import JSZip from "jszip"; |
| 20 | +import * as fs from "fs"; |
| 21 | +import * as path from "path"; |
| 22 | +import { fileURLToPath } from "url"; |
| 23 | + |
| 24 | +/** |
| 25 | + * Dynamically reads information from the installed SDK package. |
| 26 | + * @returns {{minDocSpaceVersion: string}} |
| 27 | + */ |
| 28 | +function getSdkInfo() { |
| 29 | + try { |
| 30 | + // Find the package.json of the installed SDK |
| 31 | + const sdkPackageUrl = new URL("../package.json", import.meta.url); |
| 32 | + const sdkPackagePath = fileURLToPath(sdkPackageUrl); |
| 33 | + |
| 34 | + const sdkPackage = JSON.parse(fs.readFileSync(sdkPackagePath, "utf8")); |
| 35 | + |
| 36 | + const minDocSpaceVersion = sdkPackage.minDocSpaceVersion; |
| 37 | + |
| 38 | + return { minDocSpaceVersion }; |
| 39 | + } catch (error) { |
| 40 | + console.error( |
| 41 | + `❌ Error: Could not read information from '@onlyoffice/docspace-plugin-sdk'.` |
| 42 | + ); |
| 43 | + console.error( |
| 44 | + " Please make sure the package is installed correctly (`npm install`)." |
| 45 | + ); |
| 46 | + process.exit(1); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Builds the plugin zip file from the current directory |
| 52 | + */ |
| 53 | +async function buildPlugin() { |
| 54 | + const currentDir = process.cwd(); |
| 55 | + |
| 56 | + // Check if required files exist |
| 57 | + const pluginJsPath = path.join(currentDir, "dist", "plugin.js"); |
| 58 | + const packageJsonPath = path.join(currentDir, "package.json"); |
| 59 | + |
| 60 | + if (!fs.existsSync(pluginJsPath)) { |
| 61 | + console.error( |
| 62 | + "❌ Error: dist/plugin.js not found. Please build your plugin first." |
| 63 | + ); |
| 64 | + process.exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + if (!fs.existsSync(packageJsonPath)) { |
| 68 | + console.error("❌ Error: package.json not found in current directory."); |
| 69 | + process.exit(1); |
| 70 | + } |
| 71 | + |
| 72 | + console.log("🔨 Building plugin..."); |
| 73 | + |
| 74 | + const zip = new JSZip(); |
| 75 | + |
| 76 | + // Read plugin.js |
| 77 | + const jsData = fs.readFileSync(pluginJsPath, "utf-8"); |
| 78 | + |
| 79 | + // Read package.json |
| 80 | + const jsonData = fs.readFileSync(packageJsonPath, "utf-8"); |
| 81 | + const jsonDataObj = JSON.parse(jsonData); |
| 82 | + |
| 83 | + // Get the latest SDK info directly from the source |
| 84 | + const sdkInfo = getSdkInfo(); |
| 85 | + |
| 86 | + // Create config.json for the plugin |
| 87 | + const docspace = { |
| 88 | + name: jsonDataObj.name.toLowerCase(), |
| 89 | + version: jsonDataObj.version || DEFAULT_PLUGIN_VERSION, |
| 90 | + minDocSpaceVersion: sdkInfo.minDocSpaceVersion || "", |
| 91 | + description: jsonDataObj.description || "", |
| 92 | + license: jsonDataObj.license || "", |
| 93 | + author: jsonDataObj.author || "", |
| 94 | + pluginName: jsonDataObj.pluginName || "", |
| 95 | + homePage: jsonDataObj.homepage || "", |
| 96 | + image: jsonDataObj.logo || "", |
| 97 | + scopes: jsonDataObj.scopes ? jsonDataObj.scopes.join(",") : "", |
| 98 | + cspDomains: (jsonDataObj.cspDomains && jsonDataObj.cspDomains.join(",")) || "", |
| 99 | + }; |
| 100 | + |
| 101 | + // Add files to zip |
| 102 | + zip.file("plugin.js", jsData); |
| 103 | + zip.file("config.json", JSON.stringify(docspace, null, 2)); |
| 104 | + |
| 105 | + // Add assets if they exist |
| 106 | + const assetsPath = path.join(currentDir, "assets"); |
| 107 | + if (fs.existsSync(assetsPath)) { |
| 108 | + const assetsFiles = fs.readdirSync(assetsPath); |
| 109 | + |
| 110 | + assetsFiles.forEach((file) => { |
| 111 | + const filePath = path.join(assetsPath, file); |
| 112 | + const data = fs.readFileSync(filePath, "base64"); |
| 113 | + zip.file(`assets/${file}`, data, { base64: true }); |
| 114 | + }); |
| 115 | + |
| 116 | + console.log(`📁 Added ${assetsFiles.length} asset(s) to plugin`); |
| 117 | + } |
| 118 | + |
| 119 | + const distPath = path.join(currentDir, "dist"); |
| 120 | + // Generate and save the zip file |
| 121 | + try { |
| 122 | + const content = await zip.generateAsync({ type: "nodebuffer" }); |
| 123 | + const outputPath = path.join(distPath, "plugin.zip"); |
| 124 | + fs.writeFileSync(outputPath, content); |
| 125 | + |
| 126 | + console.log(`✅ Plugin built successfully: ${outputPath}`); |
| 127 | + console.log(`📦 Plugin name: ${docspace.name}`); |
| 128 | + console.log(`🔢 Version: ${docspace.version}`); |
| 129 | + console.log(`🎯 Min DocSpace version: ${docspace.minDocSpaceVersion}`); |
| 130 | + } catch (error) { |
| 131 | + console.error("❌ Error generating plugin zip:", error); |
| 132 | + process.exit(1); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// Run the build process |
| 137 | +buildPlugin().catch((error) => { |
| 138 | + console.error("❌ Build failed:", error); |
| 139 | + process.exit(1); |
| 140 | +}); |
0 commit comments