Skip to content

Commit c145526

Browse files
authored
fix(create-sanity): fixes cli bin path (#302)
1 parent e210c45 commit c145526

File tree

6 files changed

+58
-40
lines changed

6 files changed

+58
-40
lines changed

packages/create-sanity/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
#!/usr/bin/env node
22
import {spawn} from 'node:child_process'
3+
import {readFile} from 'node:fs/promises'
34
import {join} from 'node:path'
5+
import {fileURLToPath} from 'node:url'
46

57
import {moduleResolve} from 'import-meta-resolve'
68

79
const args = process.argv.slice(2)
810

911
let cliBin
1012
try {
11-
const cliPkgDir = await moduleResolve('@sanity/cli/package.json', import.meta.url)
12-
const cliDir = join(cliPkgDir.pathname, '..')
13-
cliBin = join(cliDir, 'bin', 'run.js')
13+
const cliPkgDir = fileURLToPath(await moduleResolve('@sanity/cli/package.json', import.meta.url))
14+
const cliDir = join(cliPkgDir, '..')
15+
16+
// Read the package.json file and extract the bin path
17+
const pJson = await readFile(cliPkgDir, 'utf8')
18+
const pkgJson = JSON.parse(pJson)
19+
const binPath = pkgJson.bin?.['sanity']
20+
if (!binPath) {
21+
throw new Error('Failed to resolve `@sanity/cli` package')
22+
}
23+
24+
cliBin = join(cliDir, binPath)
1425
} catch (err) {
1526
throw new Error('Failed to resolve `@sanity/cli` package', {cause: err})
1627
}

packages/create-sanity/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@
2828
],
2929
"scripts": {
3030
"publint": "publint",
31-
"test": "node test.js"
31+
"test": "vitest"
3232
},
3333
"dependencies": {
3434
"@sanity/cli": "^5.4.0",
3535
"import-meta-resolve": "^4.1.0"
3636
},
3737
"devDependencies": {
38-
"publint": "catalog:"
38+
"publint": "catalog:",
39+
"vitest": "catalog:"
3940
},
4041
"engines": {
4142
"node": ">=20"

packages/create-sanity/test.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env node
2-
import {strict as assert} from 'node:assert'
32
import {spawn} from 'node:child_process'
43
import {join} from 'node:path'
5-
import {test} from 'node:test'
4+
5+
import {expect, test} from 'vitest'
66

77
const createSanityScript = join(import.meta.dirname, 'index.js')
88

@@ -41,19 +41,12 @@ function runCreateSanity(args = [], env = {}) {
4141
test('create-sanity --help returns exit code 0 and help text', async () => {
4242
const result = await runCreateSanity(['--help'])
4343

44-
assert.equal(result.code, 0, 'Exit code should be 0 for --help')
45-
assert.match(result.output, /help|usage|options/i, 'Output should contain help-related text')
46-
assert.match(
44+
expect(result.code, 'Exit code should be 0 for --help').toBe(0)
45+
expect(result.output, 'Output should contain help-related text').toMatch(/help|usage|options/i)
46+
expect(
4747
result.output,
48-
/init/i,
4948
"Output should mention init command since that's what create-sanity runs",
50-
)
51-
})
52-
53-
test('create-sanity with invalid flag returns non-zero exit code', async () => {
54-
const result = await runCreateSanity(['--invalid-flag-that-does-not-exist'])
55-
56-
assert.notEqual(result.code, 0, 'Exit code should be non-zero for invalid flags')
49+
).toMatch(/init/i)
5750
})
5851

5952
test('create-sanity passes through arguments to sanity init', async () => {
@@ -62,16 +55,16 @@ test('create-sanity passes through arguments to sanity init', async () => {
6255

6356
// Since create-sanity runs `sanity init --from-create --help`,
6457
// the help should be for the init command
65-
assert.equal(result.code, 0, 'Should successfully pass through --help to init command')
66-
assert.match(result.output, /init/i, 'Should show init command help')
58+
expect(result.code, 'Should successfully pass through --help to init command').toBe(0)
59+
expect(result.output, 'Should show init command help').toMatch(/init/i)
6760
})
6861

6962
test('create-sanity adds --from-create flag', async () => {
7063
// We can't easily test this directly without mocking, but we can verify
7164
// that the script runs without error when called properly
7265
const result = await runCreateSanity(['--help'])
7366

74-
assert.equal(result.code, 0, 'Script should run successfully')
67+
expect(result.code, 'Script should run successfully').toBe(0)
7568
// The --from-create flag should be added internally but we can't directly observe it
7669
// without more complex mocking. The fact that it runs successfully indicates
7770
// the flag is being added correctly.
@@ -82,49 +75,52 @@ test('create-sanity handles multiple arguments', async () => {
8275
const result = await runCreateSanity(['--help', '--json'])
8376

8477
// Should still return help (--help takes precedence) but with exit code 0
85-
assert.equal(result.code, 0, 'Should handle multiple arguments correctly')
86-
assert.match(result.output, /help|usage|options/i, 'Should still show help output')
78+
expect(result.code, 'Should handle multiple arguments correctly').toBe(0)
79+
expect(result.output, 'Should still show help output').toMatch(/help|usage|options/i)
8780
})
8881

89-
test('create-sanity script is executable', async () => {
82+
/**
83+
* Below tests are skipped since they are new features in the `@sanity/cli` package.
84+
* We should re-enable them when the `@sanity/cli` package is updated to include the new features.
85+
*/
86+
87+
test.skip('create-sanity with invalid flag returns non-zero exit code', async () => {
88+
const result = await runCreateSanity(['--invalid-flag-that-does-not-exist'])
89+
90+
expect(result.code, 'Exit code should be non-zero for invalid flags').not.toBe(0)
91+
})
92+
93+
test.skip('create-sanity script is executable', async () => {
9094
// Test that the script can be run directly
9195
const result = await runCreateSanity([])
9296

9397
// Even without arguments, the script should run and delegate to sanity init
9498
// It might show help or prompt for input, but shouldn't crash
95-
assert.equal(typeof result.code, 'number', 'Should return a numeric exit code')
99+
expect(typeof result.code, 'Should return a numeric exit code').toBe('number')
96100
})
97101

98-
test('should reference `npm create sanity@latest` in help text, not `sanity init`', async () => {
102+
test.skip('should reference `npm create sanity@latest` in help text, not `sanity init`', async () => {
99103
const result = await runCreateSanity(['--help'])
100104

101-
assert.match(
102-
result.output,
105+
expect(result.output, 'Should reference `npm create sanity` in help text').toMatch(
103106
/npm create sanity@latest/i,
104-
'Should reference `npm create sanity` in help text',
105107
)
106-
assert.doesNotMatch(
107-
result.output,
108+
expect(result.output, 'Should not reference `sanity init` in help text').not.toMatch(
108109
/sanity init/i,
109-
'Should not reference `sanity init` in help text',
110110
)
111111
})
112112

113113
// strictly speaking this is testing the `@sanity/cli` module, since this is determined
114114
// there - but we want to ensure we pass on environment variables etc
115-
test('should reference `pnpm create sanity@latest` in help text if pnpm is used ', async () => {
115+
test.skip('should reference `pnpm create sanity@latest` in help text if pnpm is used ', async () => {
116116
const result = await runCreateSanity(['--help'], {
117117
npm_config_user_agent: 'pnpm/10.7.1 npm/? node/v22.14.0 darwin arm64',
118118
})
119119

120-
assert.match(
121-
result.output,
120+
expect(result.output, 'Should reference `pnpm create sanity` in help text').toMatch(
122121
/pnpm create sanity@latest/i,
123-
'Should reference `pnpm create sanity` in help text',
124122
)
125-
assert.doesNotMatch(
126-
result.output,
123+
expect(result.output, 'Should not reference `sanity init` in help text').not.toMatch(
127124
/sanity init/i,
128-
'Should not reference `sanity init` in help text',
129125
)
130126
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {defineConfig} from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['test.js'],
6+
},
7+
})

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export default defineConfig({
3030
},
3131
// Add explicit exclude for test execution
3232
exclude: ['**/node_modules/**', '**/dist/**', '**/tmp/**', '**/.git/**'],
33-
projects: ['packages/@sanity/cli', 'packages/@sanity/cli-core'],
33+
projects: ['packages/@sanity/cli', 'packages/@sanity/cli-core', 'packages/create-sanity'],
3434
},
3535
})

0 commit comments

Comments
 (0)