Skip to content

Commit

Permalink
fix: link-watch now works when the watch directory does not exist on …
Browse files Browse the repository at this point in the history
…startup
  • Loading branch information
snorrees committed Nov 14, 2022
1 parent 762d9c0 commit a4acd54
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ npx @sanity/plugin-kit init sanity-plugin-testing-it-out
cd sanity-plugin-testing-it-out
npm run link-watch

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

```

Now, configure the plugin in sanity.config.ts (or .js) in Sanity Studio:
Expand Down
13 changes: 10 additions & 3 deletions src/actions/link-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import path from 'path'
import log from '../util/log'
import {getPackage} from '../npm/package'
import outdent from 'outdent'
import {fileExists, mkdir} from '../util/files'

interface YalcWatchConfig {
folder?: string
Expand All @@ -38,7 +39,7 @@ interface PackageJson {

export async function linkWatch({basePath}: {basePath: string}) {
const packageJson: PackageJson = JSON.parse(
fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')
fs.readFileSync(path.join(basePath, 'package.json'), 'utf8')
)

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

// ensure the folder exits so it can be watched
const folder = path.join(basePath, watch.folder)
if (!(await fileExists(folder))) {
await mkdir(folder)
}

const pkg = await getPackage({basePath, validate: false})

concurrently([watch.command])

nodemon
.on('start', function () {
log.info(
Expand All @@ -75,6 +84,4 @@ export async function linkWatch({basePath}: {basePath: string}) {
log.info('Found changes in files:', chalk.magentaBright(files))
log.info('Pushing new yalc package...')
})

concurrently([watch.command])
}

0 comments on commit a4acd54

Please sign in to comment.