@united-robotics/cli 0.4.5 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +41 -7
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // src/index.ts
4
4
  import { Command } from "commander";
5
- import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "fs";
5
+ import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync, appendFileSync } from "fs";
6
6
  import { homedir, tmpdir } from "os";
7
7
  import { basename, dirname, join, parse } from "path";
8
8
  import { spawnSync } from "child_process";
@@ -58,10 +58,14 @@ project.command("clone").argument("<projectId>").option("--dest <path>").option(
58
58
  const dest = opts.inPlace ? "." : opts.dest;
59
59
  const preservedConfig = opts.inPlace ? preserveInPlaceWorkspaceConfig(process.cwd()) : null;
60
60
  try {
61
- if (opts.inPlace) assertInPlaceCloneTarget(process.cwd());
62
- const cloneArgs = ["clone", primary.url];
63
- if (dest) cloneArgs.push(dest);
64
- runGit(cloneArgs, { cwd: process.cwd(), env: credentials.env, authHint: true });
61
+ if (opts.inPlace && isEmptyGitWorktree(process.cwd())) {
62
+ checkoutPrimaryIntoExistingGitWorktree(primary, opts.name, opts.email, credentials.env);
63
+ } else {
64
+ if (opts.inPlace) assertInPlaceCloneTarget(process.cwd());
65
+ const cloneArgs = ["clone", primary.url];
66
+ if (dest) cloneArgs.push(dest);
67
+ runGit(cloneArgs, { cwd: process.cwd(), env: credentials.env, authHint: true });
68
+ }
65
69
  } finally {
66
70
  preservedConfig?.restore();
67
71
  }
@@ -185,11 +189,26 @@ ${result.stderr}`)) {
185
189
  return { status, stdout: result.stdout ?? "", stderr: result.stderr ?? "" };
186
190
  }
187
191
  function assertInPlaceCloneTarget(cwd) {
188
- const entries = readdirSync(cwd).filter((entry) => ![".DS_Store"].includes(entry));
192
+ const entries = inPlaceRelevantEntries(cwd);
189
193
  if (entries.length > 0) {
190
- throw new Error(`Cannot in-place clone into a non-empty directory (${cwd}). Start from an empty Codex project root, or use --dest <path> intentionally.`);
194
+ throw new Error(`Cannot in-place clone into a non-empty directory (${cwd}). Start from an empty Codex project root, use an empty Git repo root, or use --dest <path> intentionally.`);
191
195
  }
192
196
  }
197
+ function isEmptyGitWorktree(cwd) {
198
+ const entries = inPlaceRelevantEntries(cwd);
199
+ return entries.length === 1 && entries[0] === ".git";
200
+ }
201
+ function inPlaceRelevantEntries(cwd) {
202
+ return readdirSync(cwd).filter((entry) => ![".DS_Store", configDirName].includes(entry));
203
+ }
204
+ function checkoutPrimaryIntoExistingGitWorktree(repo, name, email, env) {
205
+ const branch = repo.defaultBranch || "main";
206
+ configureGitIdentity(process.cwd(), name, email, env);
207
+ if (gitRemoteExists(process.cwd(), "origin")) runGit(["remote", "set-url", "origin", repo.url], { cwd: process.cwd(), env });
208
+ else runGit(["remote", "add", "origin", repo.url], { cwd: process.cwd(), env });
209
+ runGit(["fetch", "origin", branch], { cwd: process.cwd(), env, authHint: true });
210
+ runGit(["checkout", "-B", branch, `origin/${branch}`], { cwd: process.cwd(), env });
211
+ }
193
212
  function localConfigPath(cwd) {
194
213
  return join(cwd, configDirName, configFileName);
195
214
  }
@@ -213,9 +232,24 @@ function preserveInPlaceWorkspaceConfig(cwd) {
213
232
  restore: () => {
214
233
  mkdirSync(configDir, { recursive: true });
215
234
  writeFileSync(configPath, content);
235
+ ignoreWorkspaceConfigInGit(cwd);
216
236
  }
217
237
  };
218
238
  }
239
+ function gitRemoteExists(cwd, remote) {
240
+ const result = spawnSync(gitBin(), ["remote", "get-url", remote], { cwd, encoding: "utf8", stdio: "ignore" });
241
+ return result.status === 0;
242
+ }
243
+ function ignoreWorkspaceConfigInGit(cwd) {
244
+ const gitDir = join(cwd, ".git");
245
+ if (!existsSync(gitDir)) return;
246
+ const excludePath = join(gitDir, "info", "exclude");
247
+ mkdirSync(dirname(excludePath), { recursive: true });
248
+ const existing = existsSync(excludePath) ? readFileSync(excludePath, "utf8") : "";
249
+ if (!existing.split(/\r?\n/).includes(`${configDirName}/`)) appendFileSync(excludePath, `
250
+ ${configDirName}/
251
+ `);
252
+ }
219
253
  function gitBin() {
220
254
  return process.env.UR_GIT_BIN || (process.platform === "darwin" ? "/usr/bin/git" : "git");
221
255
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@united-robotics/cli",
3
- "version": "0.4.5",
3
+ "version": "0.4.6",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "ur": "dist/index.js"