create-agent 0.0.20 → 0.0.21

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.
@@ -4,11 +4,13 @@ import { generateAgent } from '../index.js'
4
4
  import { execSync } from 'child_process'
5
5
  import fs from 'fs'
6
6
 
7
- // Check if in a git repo
8
- function isGitRepo() {
7
+ // Check if this directory is the top of its own git repo. A subfolder of
8
+ // another repo does not count: its git config is the parent's, so a key saved
9
+ // there would be shared with (and overwritten by) every other subfolder.
10
+ function isRepoRoot() {
9
11
  try {
10
- execSync('git rev-parse --git-dir', { stdio: 'ignore' })
11
- return true
12
+ const top = execSync('git rev-parse --show-toplevel', { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
13
+ return fs.realpathSync(top) === fs.realpathSync(process.cwd())
12
14
  } catch {
13
15
  return false
14
16
  }
@@ -27,7 +29,7 @@ function hasIdentity() {
27
29
  }
28
30
 
29
31
  // Main
30
- if (!isGitRepo()) {
32
+ if (!isRepoRoot()) {
31
33
  console.error('\x1b[36mInitializing git repository...\x1b[0m')
32
34
  execSync('git init', { stdio: 'inherit' })
33
35
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "create-agent",
3
3
  "type": "module",
4
- "version": "0.0.20",
4
+ "version": "0.0.21",
5
5
  "description": "create an agent",
6
6
  "scripts": {
7
7
  "test": "node --test"
@@ -27,4 +27,4 @@
27
27
  "dependencies": {
28
28
  "nostr-tools": "^2.10.4"
29
29
  }
30
- }
30
+ }
@@ -0,0 +1,67 @@
1
+ import { test, describe } from 'node:test'
2
+ import assert from 'node:assert'
3
+ import { execSync, execFileSync } from 'child_process'
4
+ import fs from 'fs'
5
+ import os from 'os'
6
+ import path from 'path'
7
+
8
+ const bin = path.resolve('bin/create-agent.js')
9
+ const git = (cwd, args) => execSync(`git ${args}`, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
10
+ const run = (cwd) => execFileSync('node', [bin], { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] })
11
+ const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'create-agent-'))
12
+
13
+ describe('create-agent CLI', () => {
14
+ test('in a subfolder of a repo, makes the subfolder its own repo', () => {
15
+ const parent = tmp()
16
+ git(parent, 'init -q')
17
+ const sub = path.join(parent, 'project')
18
+ fs.mkdirSync(sub)
19
+
20
+ run(sub)
21
+
22
+ assert.ok(fs.existsSync(path.join(sub, '.git')), 'subfolder should get its own .git')
23
+ assert.ok(fs.existsSync(path.join(sub, 'agent.did.json')))
24
+ assert.match(git(sub, 'config --local nostr.privkey'), /^[0-9a-f]{64}$/)
25
+ assert.throws(() => git(parent, 'config --local nostr.privkey'), 'parent config must not hold the key')
26
+ })
27
+
28
+ test('two subfolders of one repo keep separate keys', () => {
29
+ const parent = tmp()
30
+ git(parent, 'init -q')
31
+ const a = path.join(parent, 'a')
32
+ const b = path.join(parent, 'b')
33
+ fs.mkdirSync(a)
34
+ fs.mkdirSync(b)
35
+
36
+ run(a)
37
+ const keyA = git(a, 'config --local nostr.privkey')
38
+ run(b)
39
+
40
+ assert.strictEqual(git(a, 'config --local nostr.privkey'), keyA, "b must not overwrite a's key")
41
+ assert.notStrictEqual(git(b, 'config --local nostr.privkey'), keyA)
42
+ })
43
+
44
+ test('at the top of an existing repo, uses that repo', () => {
45
+ const dir = tmp()
46
+ git(dir, 'init -q')
47
+ const before = fs.readdirSync(path.join(dir, '.git')).length
48
+
49
+ run(dir)
50
+
51
+ assert.match(git(dir, 'config --local nostr.privkey'), /^[0-9a-f]{64}$/)
52
+ assert.ok(fs.readdirSync(path.join(dir, '.git')).length >= before)
53
+ })
54
+
55
+ test('outside any repo, initialises one', () => {
56
+ const dir = tmp()
57
+ run(dir)
58
+ assert.ok(fs.existsSync(path.join(dir, '.git')))
59
+ assert.match(git(dir, 'config --local nostr.privkey'), /^[0-9a-f]{64}$/)
60
+ })
61
+
62
+ test('refuses to run twice in the same folder', () => {
63
+ const dir = tmp()
64
+ run(dir)
65
+ assert.throws(() => run(dir), /already exists/)
66
+ })
67
+ })