Skip to content

Commit a4acd54

Browse files
committed
fix: link-watch now works when the watch directory does not exist on startup
1 parent 762d9c0 commit a4acd54

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ npx @sanity/plugin-kit init sanity-plugin-testing-it-out
5757
cd sanity-plugin-testing-it-out
5858
npm run link-watch
5959

60-
# In another shell, link the plugin to your Sanity studio
60+
# In another shell, link the plugin to your Sanity studio using the command indicated by link-watch
6161
cd /path/to/my-studio
62-
yalc add --link sanity-plugin-testing-it-out && yarn install
62+
npx yalc add sanity-plugin-testing-it-out && npx yalc link sanity-plugin-testing-it-out && npm install
63+
6364
```
6465

6566
Now, configure the plugin in sanity.config.ts (or .js) in Sanity Studio:

src/actions/link-watch.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import path from 'path'
2525
import log from '../util/log'
2626
import {getPackage} from '../npm/package'
2727
import outdent from 'outdent'
28+
import {fileExists, mkdir} from '../util/files'
2829

2930
interface YalcWatchConfig {
3031
folder?: string
@@ -38,7 +39,7 @@ interface PackageJson {
3839

3940
export async function linkWatch({basePath}: {basePath: string}) {
4041
const packageJson: PackageJson = JSON.parse(
41-
fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')
42+
fs.readFileSync(path.join(basePath, 'package.json'), 'utf8')
4243
)
4344

4445
const watch: Required<YalcWatchConfig> = {
@@ -55,8 +56,16 @@ export async function linkWatch({basePath}: {basePath: string}) {
5556
//delay: 1000
5657
})
5758

59+
// ensure the folder exits so it can be watched
60+
const folder = path.join(basePath, watch.folder)
61+
if (!(await fileExists(folder))) {
62+
await mkdir(folder)
63+
}
64+
5865
const pkg = await getPackage({basePath, validate: false})
5966

67+
concurrently([watch.command])
68+
6069
nodemon
6170
.on('start', function () {
6271
log.info(
@@ -75,6 +84,4 @@ export async function linkWatch({basePath}: {basePath: string}) {
7584
log.info('Found changes in files:', chalk.magentaBright(files))
7685
log.info('Pushing new yalc package...')
7786
})
78-
79-
concurrently([watch.command])
8087
}

0 commit comments

Comments
 (0)