Skip to content

Commit

Permalink
✨ watch & run clean logs & tune log level (#399)
Browse files Browse the repository at this point in the history
* ✨ FIX: dep

* ✨ NEW: watch with clean logs

* 👌 UPDATE: defaults
  • Loading branch information
jycouet authored Oct 15, 2023
1 parent 2f050ec commit 7b0edf3
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-turkeys-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-watch-and-run': minor
---

remove quiet args in favor of logs to have a more granular control.
5 changes: 5 additions & 0 deletions .changeset/tall-bulldogs-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kitql/helpers': patch
---

fix dep (esm-env)
39 changes: 39 additions & 0 deletions packages/helpers/src/lib/stry/stry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,45 @@ describe('kitql - helper - stry', () => {
}"
`)
})

it('should handle array', () => {
const obj = {
data: {
usersList: [
{ name: 'Bruce Willis', id: 'pagination-query-offset-variables:1' },
{ name: 'Samuel Jackson', id: 'pagination-query-offset-variables:2' },
{ name: 'Morgan Freeman', id: 'pagination-query-offset-variables:3' },
{ name: 'Tom Hanks', id: 'pagination-query-offset-variables:4' },
],
},
}

const result = stry(obj)
expect(result).toMatchInlineSnapshot(`
"{
\\"data\\": {
\\"usersList\\": [
{
\\"id\\": \\"pagination-query-offset-variables:1\\",
\\"name\\": \\"Bruce Willis\\"
},
{
\\"id\\": \\"pagination-query-offset-variables:2\\",
\\"name\\": \\"Samuel Jackson\\"
},
{
\\"id\\": \\"pagination-query-offset-variables:3\\",
\\"name\\": \\"Morgan Freeman\\"
},
{
\\"id\\": \\"pagination-query-offset-variables:4\\",
\\"name\\": \\"Tom Hanks\\"
}
]
}
}"
`)
})
})

describe('kitql - helper - stry0', () => {
Expand Down
53 changes: 33 additions & 20 deletions packages/vite-plugin-watch-and-run/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export type Options = {
watchKind?: WatchKind[]

/**
* Don't print anything extra to the console when an event is trigger
* Tune what you want to print to the console. By default, everything.
*/
quiet?: boolean
logs?: LogType[]

/**
* run command (npm run gen for example!)
Expand Down Expand Up @@ -57,13 +57,14 @@ export type Options = {

export const kindWithPath = ['add', 'addDir', 'change', 'unlink', 'unlinkDir'] as const
export type KindWithPath = (typeof kindWithPath)[number]
export type LogType = 'trigger' | 'streamData' | 'streamError' | 'end'
export const kindWithoutPath = ['all', 'error', 'raw', 'ready'] as const
export type KindWithoutPath = (typeof kindWithoutPath)[number]
export type WatchKind = KindWithPath | KindWithoutPath

export type StateDetail = {
kind: WatchKind[]
quiet: boolean
logs: LogType[]
run: string | ((server: ViteDevServer) => void | Promise<void>)
delay: number
isRunning: boolean
Expand All @@ -88,7 +89,7 @@ function checkConf(params: Options[]) {
delay: paramRow.delay ?? 300,
isRunning: false,
name: paramRow.name,
quiet: Boolean(paramRow.quiet),
logs: paramRow.logs ?? (['trigger', 'streamData', 'streamError', 'end'] as LogType[]),
watch: paramRow.watch,
shell: paramRow.shell ?? true,
watchFile: paramRow.watchFile,
Expand Down Expand Up @@ -161,17 +162,17 @@ async function watcher(
info.isRunning = true

// print the message
if (!info.quiet) {
let message = `${green('✔')} Watch ${cyan(watchKind)}`
if (info.logs.includes('trigger')) {
let message = [`Watch ${cyan(watchKind)}`]
if (info.watch && absolutePath) {
message += green(' ' + absolutePath.replaceAll(process.cwd(), ''))
message.push(green(absolutePath.replaceAll(process.cwd(), '')))
}
if (typeof info.run === 'string') {
message += ` and run ${green(info.run)} `
message.push(`and run ${green(info.run)}`)
}
message += ` ${cyan(info.delay + 'ms')}`
message.push(`${cyan(info.delay + 'ms')}`)

log.info(message)
log.success(message.join(' '))
}

// Run after a delay
Expand All @@ -197,20 +198,32 @@ async function watcher(
const child = spawn(info.run, [], { shell: info.shell })

//spit stdout to screen
child.stdout.on('data', data => {
process.stdout.write(formatLog(data.toString(), info.name ?? ''))
})
if (info.logs.includes('streamData')) {
child.stdout.on('data', data => {
process.stdout.write(formatLog(data.toString(), info.name ?? ''))
})
}

//spit stderr to screen
child.stderr.on('data', data => {
process.stdout.write(formatLog(data.toString(), info.name ?? ''))
})
if (info.logs.includes('streamError')) {
child.stderr.on('data', data => {
process.stdout.write(formatLog(data.toString(), info.name ?? ''))
})
}

child.on('close', code => {
if (code === 0) {
log.info(`${green('✔')} finished ${green('successfully')}`)
} else {
log.error(`finished with some ${red('errors')}`)
if (info.logs.includes('end')) {
const message = [`Finished`]
if (info.name) {
message.push(`${magenta(info.name)}`)
}
if (code === 0) {
message.push(green('successfully'))
log.success(message.join(' '))
} else {
message.push(`with some ${red('errors')}!`)
log.error(message.join(' '))
}
}
info.isRunning = false
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ describe('vite-plugin-watch-and-run', () => {
"change",
"unlink",
],
"logs": [
"trigger",
"streamData",
"streamError",
"end",
],
"name": undefined,
"quiet": false,
"run": "npm run gen",
"shell": true,
"watch": "**/*.(gql|graphql)",
Expand Down
17 changes: 15 additions & 2 deletions packages/vite-plugin-watch-and-run/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import path from 'path'
import { sveltekit } from '@sveltejs/kit/vite'
import { defineConfig } from 'vitest/config'
import { defineConfig } from 'vite'
import watchAndRun from './src/lib/index.js'

export default defineConfig({
plugins: [sveltekit()],
plugins: [
sveltekit(),
// demo
watchAndRun([
{
name: 'Yop OK',
run: 'echo coucou 👋',
watch: path.resolve('src/**/*.svelte'),
},
]),
watchAndRun([{ name: 'Yop NOK', run: 'exit(1)', watch: path.resolve('src/**/*.svelte') }]),
],
test: {
include: ['src/**/*.{test,spec}.{js,ts}'],
},
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"noImplicitReturns": true,
"noUnusedLocals": false,
"resolveJsonModule": true,
"skipLibCheck": true
"skipLibCheck": true,
"paths": {
"@kitql/helper": ["packages/helper/src/index"]
}
},
"include": ["packages"],
"exclude": ["**/dist", "**/temp"]
Expand Down

1 comment on commit 7b0edf3

@vercel
Copy link

@vercel vercel bot commented on 7b0edf3 Oct 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kitql – ./

kitql-git-main-jycouet.vercel.app
kitql.dev
kitql.vercel.app
kitql-jycouet.vercel.app
www.kitql.dev

Please sign in to comment.