From c0044939b9143a4fbe91ac069fdce381a3eac943 Mon Sep 17 00:00:00 2001 From: Kelvin Luck Date: Wed, 10 May 2023 22:38:29 +0100 Subject: [PATCH] feat: Make sync-pnpm watch mode optional Watch mode is selected by passing the `--watch` argument. We now explicitly pass this argument when we want to watch (e.g. when we have `start`ed) and we don't when we just need to sync once (e.g. when linting or running tests on CI) --- docs-app/package.json | 2 +- internal/sync-pnpm/bin/sync-pnpm.js | 4 +++- internal/sync-pnpm/index.js | 11 ++++++++++- test-app/package.json | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs-app/package.json b/docs-app/package.json index dc0ec6d0..ccd4af4f 100644 --- a/docs-app/package.json +++ b/docs-app/package.json @@ -21,7 +21,7 @@ "lint:hbs:fix": "ember-template-lint . --fix --no-error-on-unmatched-pattern", "lint:js:fix": "eslint . --fix", "lint:types": "glint", - "start": "concurrently 'ember serve' 'npm:_syncPnpm' --names 'docs-app serve,docs-app sync built package'", + "start": "concurrently 'ember serve' 'pnpm sync-pnpm --watch' --names 'docs-app serve,docs-app sync built package'", "test:ember": "ember test", "_syncPnpm": "pnpm sync-pnpm" }, diff --git a/internal/sync-pnpm/bin/sync-pnpm.js b/internal/sync-pnpm/bin/sync-pnpm.js index fe153f21..5bb3e0b2 100755 --- a/internal/sync-pnpm/bin/sync-pnpm.js +++ b/internal/sync-pnpm/bin/sync-pnpm.js @@ -1,3 +1,5 @@ import syncPnpm from '../index.js'; -await syncPnpm(); +let watchMode = process.argv.find((arg) => arg === '--watch') !== undefined; + +await syncPnpm({ watchMode }); diff --git a/internal/sync-pnpm/index.js b/internal/sync-pnpm/index.js index 5292e184..e8b162ff 100755 --- a/internal/sync-pnpm/index.js +++ b/internal/sync-pnpm/index.js @@ -17,7 +17,7 @@ const syncDir = './dist'; const DEBOUNCE_INTERVAL = 50; -export default async function syncPnpm(dir = process.cwd()) { +export default async function syncPnpm({ dir = process.cwd(), watchMode = false }) { const root = await findRoot(dir); const ownPackageJson = await readJson(join(dir, 'package.json')); const ownDependencies = [ @@ -48,6 +48,14 @@ export default async function syncPnpm(dir = process.cwd()) { } } + if (!watchMode) { + for (const [syncFrom, syncTo] of Object.entries(paths)) { + syncDependency(syncFrom, syncTo); + } + + return; + } + let fromPaths = Object.keys(paths); let watcher = new Watcher(fromPaths); @@ -74,6 +82,7 @@ export default async function syncPnpm(dir = process.cwd()) { await syncDependency(foundFromPath, paths[foundFromPath]); } } + setTimeout(handleDirtyPaths, DEBOUNCE_INTERVAL); } diff --git a/test-app/package.json b/test-app/package.json index d555c53e..4eae90ba 100644 --- a/test-app/package.json +++ b/test-app/package.json @@ -21,7 +21,7 @@ "lint:types": "glint", "lint:prettier": "prettier -c .", "lint:prettier:fix": "prettier -w .", - "start": "concurrently 'ember serve' 'npm:_syncPnpm' --names 'test-app serve,test-app sync built package'", + "start": "concurrently 'ember serve' 'sync-pnpm --watch' --names 'test-app serve,test-app sync built package'", "test": "ember test", "test:ember": "ember test", "typecheck": "pnpm tsc -v; pnpm glint --version; pnpm lint:types",