Skip to content

Commit

Permalink
Merge pull request #176 from Financial-Times/nodemon-plugin
Browse files Browse the repository at this point in the history
CPP-782 Add nodemon plugin
  • Loading branch information
ivomurrell authored Mar 8, 2022
2 parents 5ea3bfd + f56a8ba commit f138885
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 9 deletions.
4 changes: 2 additions & 2 deletions core/sandbox/.toolkitrc.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
plugins:
- '@dotcom-tool-kit/frontend-app'
- '@dotcom-tool-kit/node'
- '@dotcom-tool-kit/nodemon'
- '@dotcom-tool-kit/next-router'
- '@dotcom-tool-kit/prettier'
- '@dotcom-tool-kit/lint-staged-npm'
- '@dotcom-tool-kit/jest'

hooks:
run:local:
- Node
- Nodemon
- NextRouter

options:
Expand Down
11 changes: 5 additions & 6 deletions core/sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
"@dotcom-tool-kit/circleci-heroku": "file:../../plugins/circleci-heroku",
"@dotcom-tool-kit/eslint": "file:../../plugins/eslint",
"@dotcom-tool-kit/frontend-app": "file:../../plugins/frontend-app",
"@dotcom-tool-kit/jest": "file:../../plugins/jest",
"@dotcom-tool-kit/lint-staged": "file:../../plugins/lint-staged",
"@dotcom-tool-kit/lint-staged-npm": "file:../../plugins/lint-staged-npm",
"@dotcom-tool-kit/mocha": "file:../../plugins/mocha",
"@dotcom-tool-kit/jest": "file:../../plugins/jest",
"@dotcom-tool-kit/n-test": "file:../../plugins/n-test",
"@dotcom-tool-kit/next-router": "file:../../plugins/next-router",
"@dotcom-tool-kit/nodemon": "file:../../plugins/nodemon",
"@dotcom-tool-kit/npm": "file:../../plugins/npm",
"@dotcom-tool-kit/prettier": "file:../../plugins/prettier",
"@dotcom-tool-kit/upload-assets-to-s3": "file:../../plugins/upload-assets-to-s3",
"dotcom-tool-kit": "file:../cli"
"dotcom-tool-kit": "file:../cli",
"nodemon": "^2.0.15"
},
"volta": {
"extends": "../../package.json"
},
"dependencies": {
"@dotcom-tool-kit/next-router": "file:../../plugins/next-router",
"@dotcom-tool-kit/node": "file:../../plugins/node"
},
"husky": {
"hooks": {
"pre-commit": "dotcom-tool-kit git:precommit"
Expand Down
7 changes: 6 additions & 1 deletion lib/logger/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ export function hookConsole(logger: Logger, processName: string): () => void {

// This function hooks winston into the stdout and stderr of child processes
// that we have spawned forked. Useful for when you need to invoke a CLI tool.
export function hookFork(logger: Logger, process: string, child: ChildProcess, logStdErr = true): void {
export function hookFork(
logger: Logger,
process: string,
child: Pick<ChildProcess, 'stdout' | 'stderr'>,
logStdErr = true
): void {
function hookStream(stream: Readable, level: string) {
stream.setEncoding('utf8')
stream
Expand Down
2 changes: 2 additions & 0 deletions lib/types/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import type { UploadAssetsToS3Options } from './schema/upload-assets-to-s3'
import type { VaultOptions } from './schema/vault'
import type { WebpackOptions } from './schema/webpack'
import type { NodeOptions } from './schema/node'
import type { NodemonOptions } from './schema/nodemon'
import type { NextRouterOptions } from './schema/next-router'
import type { PrettierOptions } from './schema/prettier'
import type { LintStagedNpmOptions } from './schema/lint-staged-npm'
Expand All @@ -77,6 +78,7 @@ export type Options = {
'@dotcom-tool-kit/vault'?: VaultOptions
'@dotcom-tool-kit/webpack'?: WebpackOptions
'@dotcom-tool-kit/node'?: NodeOptions
'@dotcom-tool-kit/nodemon'?: NodemonOptions
'@dotcom-tool-kit/next-router'?: NextRouterOptions
'@dotcom-tool-kit/prettier'?: PrettierOptions
'@dotcom-tool-kit/lint-staged-npm'?: LintStagedNpmOptions
Expand Down
9 changes: 9 additions & 0 deletions lib/types/src/schema/nodemon.ts
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
5 changes: 5 additions & 0 deletions plugins/nodemon/.toolkitrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins:
- '@dotcom-tool-kit/vault'

hooks:
'run:local': Nodemon
36 changes: 36 additions & 0 deletions plugins/nodemon/package.json
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"
}
}
3 changes: 3 additions & 0 deletions plugins/nodemon/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Nodemon from './tasks/nodemon'

export const tasks = [Nodemon]
72 changes: 72 additions & 0 deletions plugins/nodemon/src/tasks/nodemon.ts
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))
}
}
22 changes: 22 additions & 0 deletions plugins/nodemon/tsconfig.json
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/**/*"]
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
},
{
"path": "plugins/jest"
},
{
"path": "plugins/nodemon"
}
]
}

0 comments on commit f138885

Please sign in to comment.