|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { parseArgs } from "node:util"; |
| 18 | +import fs from "node:fs"; |
| 19 | +import path from "node:path"; |
| 20 | +import { fileURLToPath } from "node:url"; |
| 21 | + |
| 22 | +const __filename = fileURLToPath(import.meta.url); |
| 23 | +const __dirname = path.dirname(__filename); |
| 24 | + |
| 25 | +const { values, positionals } = parseArgs({ |
| 26 | + options: { |
| 27 | + outDir: { |
| 28 | + type: "string", |
| 29 | + default: "./public-dev/r", |
| 30 | + }, |
| 31 | + }, |
| 32 | + allowPositionals: true, |
| 33 | +}); |
| 34 | + |
| 35 | +const command = positionals[0]; |
| 36 | +const outputPath = positionals[1] || values.outDir; |
| 37 | + |
| 38 | +// Registry is at dist/registry, CLI is at dist/bin, so go up one level |
| 39 | +const sourceDir = "../registry"; |
| 40 | + |
| 41 | +if (command === "copy") { |
| 42 | + const sourcePath = path.resolve(__dirname, sourceDir); |
| 43 | + |
| 44 | + // Output path should be relative to where the user runs the command from |
| 45 | + const destPath = path.resolve(process.cwd(), outputPath); |
| 46 | + |
| 47 | + // Check if source directory exists |
| 48 | + if (!fs.existsSync(sourcePath)) { |
| 49 | + console.error(`Error: Source directory "${sourcePath}" does not exist`); |
| 50 | + process.exit(1); |
| 51 | + } |
| 52 | + |
| 53 | + // Create destination directory if it doesn't exist |
| 54 | + if (!fs.existsSync(destPath)) { |
| 55 | + fs.mkdirSync(destPath, { recursive: true }); |
| 56 | + } |
| 57 | + |
| 58 | + // Copy files from source to destination |
| 59 | + const files = fs.readdirSync(sourcePath); |
| 60 | + let copiedCount = 0; |
| 61 | + |
| 62 | + for (const file of files) { |
| 63 | + const sourceFile = path.join(sourcePath, file); |
| 64 | + const destFile = path.join(destPath, file); |
| 65 | + |
| 66 | + const stat = fs.statSync(sourceFile); |
| 67 | + if (stat.isFile()) { |
| 68 | + fs.copyFileSync(sourceFile, destFile); |
| 69 | + copiedCount++; |
| 70 | + console.log(`Copied: ${file}`); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + console.log(`\nSuccessfully copied ${copiedCount} item(s) from "${sourcePath}" to "${destPath}"`); |
| 75 | +} else { |
| 76 | + console.error(`Unknown command: ${command || "none"}`); |
| 77 | + console.error("Usage: copy <output-path>"); |
| 78 | + process.exit(1); |
| 79 | +} |
0 commit comments