-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from Financial-Times/nodemon-plugin
CPP-782 Add nodemon plugin
- Loading branch information
Showing
11 changed files
with
165 additions
and
9 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
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,9 @@ | ||
import { SchemaOutput } from '../schema' | ||
|
||
export const NodemonSchema = { | ||
entry: 'string?', | ||
configPath: 'string?' | ||
} as const | ||
export type NodemonOptions = SchemaOutput<typeof NodemonSchema> | ||
|
||
export const Schema = NodemonSchema |
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,5 @@ | ||
plugins: | ||
- '@dotcom-tool-kit/vault' | ||
|
||
hooks: | ||
'run:local': Nodemon |
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,36 @@ | ||
{ | ||
"name": "@dotcom-tool-kit/nodemon", | ||
"version": "0.0.0-development", | ||
"description": "", | ||
"main": "lib", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "FT.com Platforms Team <platforms-team.customer-products@ft.com>", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@dotcom-tool-kit/error": "file:../../lib/error", | ||
"@dotcom-tool-kit/state": "file:../../lib/state", | ||
"@dotcom-tool-kit/types": "file:../../lib/types", | ||
"@dotcom-tool-kit/vault": "file:../../lib/vault", | ||
"get-port": "^5.1.1" | ||
}, | ||
"peerDependencies": { | ||
"nodemon": "2.x" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/financial-times/dotcom-tool-kit.git", | ||
"directory": "plugins/nodemon" | ||
}, | ||
"bugs": "https://github.com/financial-times/dotcom-tool-kit/issues", | ||
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/nodemon", | ||
"files": [ | ||
"/lib", | ||
".toolkitrc.yml" | ||
], | ||
"devDependencies": { | ||
"@types/nodemon": "^1.19.1" | ||
} | ||
} |
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,3 @@ | ||
import Nodemon from './tasks/nodemon' | ||
|
||
export const tasks = [Nodemon] |
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,72 @@ | ||
import { ToolKitError } from '@dotcom-tool-kit/error' | ||
import { hookFork, styles } from '@dotcom-tool-kit/logger' | ||
import { Task } from '@dotcom-tool-kit/types' | ||
import { NodemonOptions, NodemonSchema } from '@dotcom-tool-kit/types/lib/schema/nodemon' | ||
import { VaultEnvVars } from '@dotcom-tool-kit/vault' | ||
import getPort from 'get-port' | ||
import nodemon from 'nodemon' | ||
import { Readable } from 'stream' | ||
|
||
export default class Nodemon extends Task<typeof NodemonSchema> { | ||
static description = '' | ||
|
||
static defaultOptions: NodemonOptions = { | ||
entry: './server/app.js' | ||
} | ||
|
||
async run(): Promise<void> { | ||
const { entry, configPath } = this.options | ||
const vault = new VaultEnvVars(this.logger, { | ||
environment: 'development' | ||
}) | ||
|
||
const vaultEnv = await vault.get() | ||
const port = | ||
Number(process.env.PORT) || | ||
(await getPort({ | ||
port: [3001, 3002, 3003] | ||
})) | ||
|
||
if (!entry) { | ||
const error = new ToolKitError( | ||
`the ${styles.task('Nodemon')} task requires an ${styles.option('entry')} option` | ||
) | ||
error.details = `this is the entrypoint for your app, e.g. ${styles.filepath('server/app.js')}` | ||
throw error | ||
} | ||
|
||
this.logger.verbose('starting the child nodemon process...') | ||
|
||
const env = { | ||
...vaultEnv, | ||
PORT: port.toString(), | ||
...process.env | ||
} | ||
nodemon({ script: entry, env, stdout: false, configFile: configPath }) | ||
nodemon.on('readable', () => { | ||
// These fields aren't specified in the type declaration for some reason | ||
const { stdout, stderr } = (nodemon as unknown) as { stdout: Readable; stderr: Readable } | ||
hookFork(this.logger, entry, { stdout, stderr }) | ||
}) | ||
const nodemonLogger = this.logger.child({ process: 'nodemon' }) | ||
nodemon.on('log', (msg) => { | ||
function nodemonToWinstonLogLevel(level: string): string { | ||
switch (level) { | ||
case 'log': | ||
return 'debug' | ||
case 'info': | ||
case 'detail': | ||
return 'verbose' | ||
case 'fail': | ||
case 'error': | ||
return 'error' | ||
case 'status': | ||
default: | ||
return 'info' | ||
} | ||
} | ||
nodemonLogger.log(nodemonToWinstonLogLevel(msg.type), msg.message) | ||
}) | ||
await new Promise((resolve) => nodemon.on('start', resolve)) | ||
} | ||
} |
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,22 @@ | ||
{ | ||
"extends": "../../tsconfig.settings.json", | ||
"compilerOptions": { | ||
"outDir": "lib", | ||
"rootDir": "src" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../../lib/types" | ||
}, | ||
{ | ||
"path": "../../lib/error" | ||
}, | ||
{ | ||
"path": "../../lib/logger" | ||
}, | ||
{ | ||
"path": "../../lib/options" | ||
} | ||
], | ||
"include": ["src/**/*"] | ||
} |
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 |
---|---|---|
|
@@ -93,6 +93,9 @@ | |
}, | ||
{ | ||
"path": "plugins/jest" | ||
}, | ||
{ | ||
"path": "plugins/nodemon" | ||
} | ||
] | ||
} |