Skip to content

Commit

Permalink
feat: improved logging, added --log-level n
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Nov 16, 2023
1 parent e31dbdd commit 6e9f136
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
11 changes: 9 additions & 2 deletions lib/buildtime/RoutifyBuildtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { RNodeBuildtime } from './RNodeBuildtime.js'
import { createParallelHooksCollection } from 'hookar'
import { devHelperPlugin } from './plugins/devHelper/helper.js'
import { metaCapturePlugin } from './plugins/metaCapture/index.js'
import { log, logs } from './logMsgs.js'

/** @returns {Partial<RoutifyBuildtimeOptions>} */
const getDefaults = () => ({
Expand All @@ -28,6 +29,7 @@ const getDefaults = () => ({
resetFiles: ['_reset'],
fallbackFiles: ['_fallback'],
},
logLevel: 3,
routesDir: {
default: 'src/routes',
},
Expand Down Expand Up @@ -78,6 +80,7 @@ export class RoutifyBuildtime extends Routify {
options,
)

log.level = Number(config.logLevel)
this.options = config

// clear dir
Expand Down Expand Up @@ -125,24 +128,28 @@ export class RoutifyBuildtime extends Routify {
const tools = {
split: split(this.options.routifyDir),
hashObj,
log,
}

const resolvedPlugins = await resolvePlugins(this.options.plugins || [])
const plugins = normalizePlugins(resolvedPlugins)
this.plugins = sortPlugins(plugins)

logs.buildTimePluginsList(this.plugins)

for (const plugin of this.plugins)
if (plugin.options) this.options = await plugin.options(this.options)

for (const plugin of this.plugins)
if (plugin.build) await plugin.build({ instance, tools })

await this.on.buildComplete.run()
console.log(`[Routify 3] build triggered by ${trigger} (${Date.now() - time} ms)`)

logs.buildTime(trigger, time)
}

start() {
return this.build('initial')
return this.build('init')
}
}

Expand Down
43 changes: 43 additions & 0 deletions lib/buildtime/logMsgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createLogger } from 'consolite'

export const y = str => `\x1b[33m${str}\x1b[0m`
export const r = str => `\x1b[31m${str}\x1b[0m`
export const p = str => `\x1b[35m${str}\x1b[0m`
// light pink
export const lp = str => `\x1b[38;5;206m${str}\x1b[0m`
// light blue
export const lb = str => `\x1b[38;5;117m${str}\x1b[0m`
export const b = str => `\x1b[34m${str}\x1b[0m`
export const g = str => `\x1b[32m${str}\x1b[0m`
// cyan
export const c = str => `\x1b[36m${str}\x1b[0m`
// bold
export const bold = str => `\x1b[1m${str}\x1b[0m`

const map = {
// info: b('[info]'),
debug: c('[debug]'),
warn: y('[warn]'),
error: r('[error]'),
}

export const log = createLogger(level => lp('[Routify 3]') + (map[level] || ''))

export const logs = {
buildTime: (trigger, time) => {
if (trigger === 'init')
log.info(bold(`build completed (${Date.now() - time} ms)`))
else log.info(`rebuild triggered by ${trigger} (${Date.now() - time} ms)`)
},
buildTimePluginsList: plugins => {
log.debug('buildtime plugins:\r\n ' + g(plugins.map(p => p.name).join('\r\n ')))
},
metaKeysWarning: potentialConflicts => {
const warning = 'Unrecognized meta keys detected:'
const note =
'\r\nFix: Consider prefixing keys with _ or mynamespace_ to avoid future conflicts.\r\nHide: Suppress warnings by setting "ignoreMetaConflictWarnings" to true or an array of keys.'

const uniqueConflicts = [...new Set(potentialConflicts)].map(str => g(str))
log.warn([warning, ...uniqueConflicts].join('\r\n ') + lb(note))
},
}
8 changes: 3 additions & 5 deletions lib/buildtime/plugins/metaFromFile/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync } from 'fs'
import { writeFileIfDifferentSync } from '../../../buildtime/utils.js'
import { resolve } from 'path'
import { logs } from '../../logMsgs.js'

// todo should be used

Expand Down Expand Up @@ -28,6 +29,7 @@ const reservedMetaKeys = [
'name',
'inline',
'isDefault',
'history',
]

/**
Expand All @@ -48,9 +50,5 @@ export const printReservedWarnings = (meta, ignoreMetaConflictWarnings) => {
potentialConflicts.push(`- "${key}" found in "${node.file.path}"`)
})
})
if (potentialConflicts.length) {
const warning =
'[Routify] The following meta keys were not recognized as Routify options. To avoid conflicts with future features, consider prefixing the keys with either _ or mynamespace_ . To kill the warnings, you can set "ignoreMetaConflictWarnings" to true or an array of ignored keys.'
console.log(warning, ['', ...potentialConflicts].join('\r\n'))
}
if (potentialConflicts.length) logs.metaKeysWarning(potentialConflicts)
}
2 changes: 2 additions & 0 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ program
.description('build routes')
.option('-w --watch', 'rebuild routes on changes')
.option('-r --routifyDir <path>', 'output folder for routify temp files')
.option('-l, --logLevel <level>', 'log level', '3')
.option('-c, --clearRoutifyDir', 'clear routifyDir before build')
.option('-e --extensions <ext>', 'file extensions to build, comma separated.')
.option(' --noDynamicImports', 'disable code splitting of pages')
.option(' --ignore <glob>', 'files to ignore')
Expand Down
1 change: 1 addition & 0 deletions types/typedef.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* @prop {string|Object<string,string>} routesDir defaults to { default: 'src/routes' }
* @prop {string[]|boolean} ignoreMetaConflictWarnings
* @prop {object} filemapper
* @prop {1|2|3|4|5} logLevel defaults to 3
* @prop {string[]} filemapper.moduleFiles defaults to ['_module.svelte', '_reset.svelte']
* @prop {string[]} filemapper.resetFiles defaults to ['_reset.svelte']
* @prop {string[]} filemapper.fallbackFiles defaults to ['_reset.svelte']
Expand Down

0 comments on commit 6e9f136

Please sign in to comment.