|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require("node:fs"); |
| 4 | +const path = require("node:path"); |
| 5 | +const { spawnSync } = require("node:child_process"); |
| 6 | + |
| 7 | +const DEFAULT_REPO = "https://github.com/XortexAI/XMem.git"; |
| 8 | +const DEFAULT_BRANCH = "main"; |
| 9 | + |
| 10 | +function usage(exitCode = 0) { |
| 11 | + console.log(`Create a local XMem workspace |
| 12 | +
|
| 13 | +Usage: |
| 14 | + npx create-xmem@latest |
| 15 | + npx create-xmem@latest my-xmem |
| 16 | +
|
| 17 | +Options: |
| 18 | + --repo <url> XMem git repository URL |
| 19 | + --branch <name> XMem branch to use |
| 20 | + --help Show this message |
| 21 | +
|
| 22 | +After creation: |
| 23 | + cd xmem |
| 24 | + npm run dev |
| 25 | +`); |
| 26 | + process.exit(exitCode); |
| 27 | +} |
| 28 | + |
| 29 | +function parseArgs(argv) { |
| 30 | + const options = { |
| 31 | + target: "xmem", |
| 32 | + repo: process.env.XMEM_REPO || DEFAULT_REPO, |
| 33 | + branch: process.env.XMEM_BRANCH || DEFAULT_BRANCH, |
| 34 | + }; |
| 35 | + let targetSet = false; |
| 36 | + |
| 37 | + function readOptionValue(index, name) { |
| 38 | + const value = argv[index + 1]; |
| 39 | + if (!value || value.startsWith("-")) { |
| 40 | + console.error(`[create-xmem] ${name} requires a value.`); |
| 41 | + usage(1); |
| 42 | + } |
| 43 | + return value; |
| 44 | + } |
| 45 | + |
| 46 | + for (let index = 0; index < argv.length; index += 1) { |
| 47 | + const arg = argv[index]; |
| 48 | + |
| 49 | + if (arg === "--help" || arg === "-h") { |
| 50 | + usage(0); |
| 51 | + } |
| 52 | + |
| 53 | + if (arg === "--repo") { |
| 54 | + options.repo = readOptionValue(index, arg); |
| 55 | + index += 1; |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if (arg === "--branch") { |
| 60 | + options.branch = readOptionValue(index, arg); |
| 61 | + index += 1; |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (arg.startsWith("-")) { |
| 66 | + console.error(`[create-xmem] Unknown option: ${arg}`); |
| 67 | + usage(1); |
| 68 | + } |
| 69 | + |
| 70 | + if (!targetSet) { |
| 71 | + options.target = arg; |
| 72 | + targetSet = true; |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + console.error(`[create-xmem] Unexpected extra argument: ${arg}`); |
| 77 | + usage(1); |
| 78 | + } |
| 79 | + |
| 80 | + if (!options.repo || !options.branch) { |
| 81 | + console.error("[create-xmem] --repo and --branch require values."); |
| 82 | + usage(1); |
| 83 | + } |
| 84 | + |
| 85 | + return options; |
| 86 | +} |
| 87 | + |
| 88 | +function runGit(args, cwd) { |
| 89 | + const result = spawnSync("git", args, { |
| 90 | + cwd, |
| 91 | + stdio: "inherit", |
| 92 | + shell: false, |
| 93 | + }); |
| 94 | + |
| 95 | + if (result.error) { |
| 96 | + console.error(`[create-xmem] Git is required: ${result.error.message}`); |
| 97 | + console.error("[create-xmem] Install Git, reopen your terminal, and run the command again."); |
| 98 | + process.exit(1); |
| 99 | + } |
| 100 | + |
| 101 | + if (result.status !== 0) { |
| 102 | + process.exit(result.status || 1); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function assertCleanTarget(targetPath) { |
| 107 | + if (!fs.existsSync(targetPath)) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const entries = fs.readdirSync(targetPath); |
| 112 | + if (entries.length > 0) { |
| 113 | + console.error(`[create-xmem] Target folder is not empty: ${targetPath}`); |
| 114 | + console.error("[create-xmem] Choose a new folder name or empty the existing folder."); |
| 115 | + process.exit(1); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function removeGitMetadata(targetPath) { |
| 120 | + fs.rmSync(path.join(targetPath, ".git"), { |
| 121 | + recursive: true, |
| 122 | + force: true, |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +const options = parseArgs(process.argv.slice(2)); |
| 127 | +const targetPath = path.resolve(process.cwd(), options.target); |
| 128 | + |
| 129 | +assertCleanTarget(targetPath); |
| 130 | + |
| 131 | +console.log(`[create-xmem] Creating XMem workspace in ${targetPath}`); |
| 132 | +runGit(["clone", "--depth", "1", "--branch", options.branch, options.repo, targetPath], process.cwd()); |
| 133 | +removeGitMetadata(targetPath); |
| 134 | + |
| 135 | +console.log(""); |
| 136 | +console.log("[create-xmem] Created local XMem workspace."); |
| 137 | +console.log(""); |
| 138 | +console.log("Next:"); |
| 139 | +console.log(` cd ${path.relative(process.cwd(), targetPath) || "."}`); |
| 140 | +console.log(" npm run dev"); |
| 141 | +console.log(""); |
| 142 | +console.log("Chrome extension after setup:"); |
| 143 | +console.log(" Load unpacked: repos/xmem-extension/dist"); |
0 commit comments