-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a watch mode that restart and empty the require cache when needed. Also adds the API types automatically generated and updated on changes. concerns #75 #51
- Loading branch information
Showing
25 changed files
with
631 additions
and
380 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
8 changes: 4 additions & 4 deletions
8
packages/whook-create/src/services/__snapshots__/createWhook.test.ts.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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,3 +1,3 @@ | ||
import { runServer } from '../src/index'; | ||
import { watchDevServer } from '../src/dev'; | ||
|
||
runServer(); | ||
watchDevServer(); |
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import chokidar from 'chokidar'; | ||
import dtsgenerator, { parseSchema } from 'dtsgenerator'; | ||
import path from 'path'; | ||
import crypto from 'crypto'; | ||
import { writeFileSync } from 'fs'; | ||
import type { Schema } from 'dtsgenerator'; | ||
import type { Knifecycle } from 'knifecycle'; | ||
import type { DelayService } from 'common-services'; | ||
|
||
let $instance: Knifecycle; | ||
let delay: DelayService; | ||
let delayPromise: Promise<void>; | ||
let hash: string; | ||
|
||
export async function watchDevServer() { | ||
await restartDevServer(); | ||
chokidar | ||
.watch(['**/*.ts', 'package*.json', ''], { | ||
ignored: ['node_modules', 'coverage', '*.d.ts'], | ||
ignoreInitial: true, | ||
}) | ||
.on('all', (_event, filePath) => { | ||
const absolutePath = path.join(process.cwd(), filePath); | ||
|
||
if (filePath.match(/package.*\.json/)) { | ||
for (let key in require.cache) { | ||
delete require.cache[key]; | ||
} | ||
} else { | ||
delete require.cache[absolutePath]; | ||
} | ||
|
||
if (delay) { | ||
if (!delayPromise) { | ||
delayPromise = delay.create(2000); | ||
restartDevServer(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
export async function restartDevServer() { | ||
if ($instance) { | ||
console.log( | ||
'info', | ||
'➡️ Changes detected : Will restart the server soon...', | ||
); | ||
await delayPromise; | ||
await $instance.destroy(); | ||
delayPromise = undefined; | ||
} | ||
|
||
const { runServer, prepareEnvironment, prepareServer } = await import('.'); | ||
|
||
const { | ||
PROJECT_SRC, | ||
$instance: _instance, | ||
delay: _delay, | ||
getOpenAPI, | ||
} = await runServer(prepareEnvironment, prepareServer, [ | ||
'PROJECT_SRC', | ||
'$instance', | ||
'delay', | ||
'getOpenAPI', | ||
]); | ||
|
||
$instance = _instance; | ||
delay = _delay; | ||
|
||
const response = await getOpenAPI({ | ||
authenticated: true, | ||
mutedMethods: ['options'], | ||
mutedParameters: [], | ||
}); | ||
const openAPIData = JSON.stringify(response.body); | ||
const newHash = crypto.createHash('md5').update(openAPIData).digest('hex'); | ||
|
||
if (hash !== newHash) { | ||
hash = newHash; | ||
console.log('info', '🦄 - API Changed : Generating API types...'); | ||
|
||
const schema = await parseSchema(response.body); | ||
const typesDefs = await dtsgenerator({ | ||
contents: [schema], | ||
}); | ||
|
||
writeFileSync(path.join(PROJECT_SRC, 'openAPISchema.d.ts'), typesDefs); | ||
} | ||
} |
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
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.