-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8a3ce9
commit 1e60941
Showing
20 changed files
with
465 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,91 @@ | ||
import chokidar from 'chokidar'; | ||
import { error } from '@internals/common/logger'; | ||
import { error, verbose } from '@internals/common/logger'; | ||
import { ChildProcess, spawn } from 'child_process'; | ||
import path from 'path'; | ||
import { CLI_DIR, ROOT_DIR } from '@internals/common/constants'; | ||
|
||
const TIMEOUT_LENGTH = 100; | ||
const TIMEOUT_LENGTH = 250; | ||
|
||
export const startWatch = (fn: () => Promise<unknown>, ...args: Parameters<typeof chokidar.watch>) => { | ||
const watcher = chokidar.watch(...args); | ||
class SpawnError extends Error { | ||
code: null | number; | ||
constructor(code: null | number) { | ||
super(`Exited with code ${code}`); | ||
this.code = code; | ||
} | ||
} | ||
|
||
export const spawnProcessThatInheritsStdioAndPreservesColor = (_cmd: string): [ChildProcess, Promise<void>] => { | ||
let resolve: () => void; | ||
let reject: (err: unknown) => void; | ||
const promise = new Promise<void>((_resolve, _reject) => { | ||
resolve = _resolve; | ||
reject = _reject; | ||
}); | ||
const cmd = _cmd.split(' '); | ||
const spawnedProcess = spawn(cmd[0], cmd.slice(1), { | ||
shell: true, | ||
stdio: 'inherit', | ||
}); | ||
spawnedProcess.on('close', (code) => { | ||
if (code !== 0) { | ||
reject(new SpawnError(code)); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
process.on('exit', () => { | ||
console.log('exiting, kill process', spawnedProcess.pid); | ||
spawnedProcess.kill(); | ||
}); | ||
return [ | ||
spawnedProcess, | ||
promise, | ||
]; | ||
}; | ||
|
||
const d = (now: number) => ((performance.now() - now) / 1000).toFixed(2); | ||
|
||
export const startWatch = ( | ||
cmd: string, | ||
paths: Parameters<typeof chokidar.watch>[0], | ||
options: Parameters<typeof chokidar.watch>[1], | ||
) => { | ||
const watcher = chokidar.watch([ | ||
...paths, | ||
path.join(CLI_DIR, 'src/**/*'), | ||
], options); | ||
let timer: NodeJS.Timeout; | ||
watcher.on('all', async () => { | ||
let last: number; | ||
let iterations = 0; | ||
let spawnedProcess: ChildProcess | undefined; | ||
watcher.on('all', async (event, file, stats) => { | ||
console.clear(); | ||
if (spawnedProcess) { | ||
verbose(`>> Killing previous spawned process ${spawnedProcess.pid}`); | ||
spawnedProcess.kill(); | ||
} | ||
clearTimeout(timer); | ||
timer = setTimeout(async () => { | ||
try { | ||
await fn(); | ||
} catch (err) { | ||
error(err); | ||
verbose(`Running command "${cmd.trim()}" in watch mode, iteration ${iterations++} at ${new Date().toLocaleTimeString()}, ${last ? `last run was ${d(last)}s ago` : 'no last run'}`); | ||
verbose(`>> Running because of event "${event}" on file "${file.split(ROOT_DIR).pop()}"`); | ||
const now = performance.now(); | ||
const [child, promise] = spawnProcessThatInheritsStdioAndPreservesColor(cmd); | ||
spawnedProcess = child; | ||
await promise.catch(err => { | ||
if (err instanceof SpawnError) { | ||
// if (err.code !== null) { | ||
// error(err.message); | ||
// } | ||
} else { | ||
error(err.message); | ||
} | ||
}); | ||
verbose(`Ran command "${cmd.trim()}" in watch mode, took ${d(now)}s`); | ||
spawnedProcess = undefined; | ||
} finally { | ||
last = performance.now(); | ||
} | ||
}, TIMEOUT_LENGTH); | ||
}); | ||
}; | ||
|
2 changes: 1 addition & 1 deletion
2
internals/cli/src/lib/commands/write/docs/api/_templates/source.md.t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<small className="gray">Defined in <a target="_blank" href="<%- url %>"><%- prettyFileName ->:<%- line %></a></small> | ||
<small className="gray">Defined in <a target="_blank" href="<%- url %>"><%- prettyFileName %>:<%- line %></a></small> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
internals/cli/src/lib/commands/write/docs/api/get-definitions/get-definitions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.