|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright Elasticsearch B.V. and contributors |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +import { exec } from "child_process"; |
| 9 | +import fs from "fs/promises"; |
| 10 | +import path from "path"; |
| 11 | +import { fileURLToPath } from "url"; |
| 12 | + |
| 13 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 14 | + |
| 15 | +// Execute license-checker command |
| 16 | +const generateNotice = () => { |
| 17 | + return new Promise((resolve, reject) => { |
| 18 | + exec("npx license-checker --json --production", (error, stdout) => { |
| 19 | + if (error) { |
| 20 | + return reject(error); |
| 21 | + } |
| 22 | + resolve(JSON.parse(stdout)); |
| 23 | + }); |
| 24 | + }); |
| 25 | +}; |
| 26 | + |
| 27 | +async function createNoticeFile() { |
| 28 | + // Custom header for notice file |
| 29 | + let noticeContent = `Elasticsearch MCP Server |
| 30 | +Copyright 2025 Elasticsearch B.V. |
| 31 | +
|
| 32 | +The Elasticsearch MCP Server contains the following third-party dependencies: |
| 33 | +
|
| 34 | +`; |
| 35 | + |
| 36 | + try { |
| 37 | + const licenses = await generateNotice(); |
| 38 | + |
| 39 | + for (const [pkgNameVer, info] of Object.entries(licenses)) { |
| 40 | + const pkgName = |
| 41 | + pkgNameVer.split("@")[0] === "" |
| 42 | + ? "@" + pkgNameVer.split("@")[1] |
| 43 | + : pkgNameVer.split("@")[0]; |
| 44 | + |
| 45 | + // Extract version from the pkgNameVer string |
| 46 | + let version; |
| 47 | + if (pkgNameVer.split("@")[0] === "") { |
| 48 | + // Handle scoped packages (@organization/[email protected]) |
| 49 | + const parts = pkgNameVer.split("@"); |
| 50 | + if (parts.length >= 3) { |
| 51 | + version = parts[2]; |
| 52 | + } |
| 53 | + } else { |
| 54 | + // Handle regular packages ([email protected]) |
| 55 | + const parts = pkgNameVer.split("@"); |
| 56 | + if (parts.length >= 2) { |
| 57 | + version = parts[1]; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Use extracted version if available, otherwise fall back to info.version |
| 62 | + const displayVersion = version || info.version || ""; |
| 63 | + |
| 64 | + noticeContent += `\n-------------------------------------------------------------------\n`; |
| 65 | + noticeContent += `Package: ${pkgName}\n`; |
| 66 | + noticeContent += `Version: ${displayVersion}\n`; |
| 67 | + noticeContent += `License: ${info.licenses || "Unknown"}\n`; |
| 68 | + |
| 69 | + if (info.publisher) { |
| 70 | + noticeContent += `Author: ${info.publisher}\n`; |
| 71 | + } |
| 72 | + |
| 73 | + noticeContent += `\n`; |
| 74 | + |
| 75 | + // Include license text if available |
| 76 | + if (info.licenseFile && typeof info.licenseFile === "string") { |
| 77 | + try { |
| 78 | + const licenseText = await fs.readFile(info.licenseFile, "utf-8"); |
| 79 | + noticeContent += `${licenseText.trim()}\n`; |
| 80 | + } catch (err) { |
| 81 | + noticeContent += `License text not available.\n`; |
| 82 | + } |
| 83 | + } else { |
| 84 | + noticeContent += `License text not available.\n`; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Write the NOTICE.txt file in the repo root |
| 89 | + await fs.writeFile( |
| 90 | + path.join(__dirname, "..", "NOTICE.txt"), |
| 91 | + noticeContent, |
| 92 | + "utf-8" |
| 93 | + ); |
| 94 | + console.log("NOTICE.txt file has been generated successfully."); |
| 95 | + } catch (error) { |
| 96 | + console.error("Error generating NOTICE.txt:", error); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +createNoticeFile(); |
0 commit comments