Skip to content

Commit

Permalink
Manually require and export all util scripts
Browse files Browse the repository at this point in the history
* Fixes #466

Signed-off-by: Marvin A. Ruder <signed@mruder.dev>
  • Loading branch information
marvinruder authored and jsumners committed Oct 4, 2023
1 parent 9fb7330 commit 8ee55cc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
37 changes: 23 additions & 14 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
'use strict'

const { readdirSync } = require('fs')
const { basename } = require('path')

const files = readdirSync(__dirname).filter(f => f.endsWith('.test.js') === false)
for (const file of files) {
if (file === 'index.js') continue

const kebabName = basename(file, '.js')
const snakeName = kebabName.split('-').map((part, idx) => {
if (idx === 0) return part
return part[0].toUpperCase() + part.slice(1)
}).join('')

module.exports[snakeName] = require(`./${kebabName}.js`)
module.exports = {
buildSafeSonicBoom: require('./build-safe-sonic-boom.js'),
createDate: require('./create-date.js'),
deleteLogProperty: require('./delete-log-property.js'),
filterLog: require('./filter-log.js'),
formatTime: require('./format-time.js'),
getPropertyValue: require('./get-property-value.js'),
handleCustomLevelsNamesOpts: require('./handle-custom-levels-names-opts.js'),
handleCustomLevelsOpts: require('./handle-custom-levels-opts.js'),
interpretConditionals: require('./interpret-conditionals.js'),
isObject: require('./is-object.js'),
isValidDate: require('./is-valid-date.js'),
joinLinesWithIndentation: require('./join-lines-with-indentation.js'),
noop: require('./noop.js'),
parseFactoryOptions: require('./parse-factory-options.js'),
prettifyErrorLog: require('./prettify-error-log.js'),
prettifyError: require('./prettify-error.js'),
prettifyLevel: require('./prettify-level.js'),
prettifyMessage: require('./prettify-message.js'),
prettifyMetadata: require('./prettify-metadata.js'),
prettifyObject: require('./prettify-object.js'),
prettifyTime: require('./prettify-time.js'),
splitPropertyKey: require('./split-property-key.js')
}

// The remainder of this file consists of jsdoc blocks that are difficult to
Expand Down
37 changes: 37 additions & 0 deletions lib/utils/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const tap = require('tap')
const index = require('./index.js')
const { readdirSync } = require('fs')
const { basename } = require('path')

tap.test(
'index exports exactly all non-test files excluding itself',
async t => {
// Read all files in the `util` directory
const files = readdirSync(__dirname)

for (const file of files) {
const kebabName = basename(file, '.js')
const snakeName = kebabName.split('-').map((part, idx) => {
if (idx === 0) return part
return part[0].toUpperCase() + part.slice(1)
}).join('')

if (file.endsWith('.test.js') === false && file !== 'index.js') {
// We expect all files to be exported except…
t.ok(index[snakeName], `exports ${snakeName}`)
} else {
// …test files and the index file itself – those must not be exported
t.notOk(index[snakeName], `does not export ${snakeName}`)
}

// Remove the exported file from the index object
delete index[snakeName]
}

// Now the index is expected to be empty, as nothing else should be
// exported from it
t.same(index, {}, 'does not export anything else')
}
)

0 comments on commit 8ee55cc

Please sign in to comment.