Skip to content

Instantly share code, notes, and snippets.

@felixmc
Created July 10, 2017 23:42
Show Gist options
  • Save felixmc/e4c8c4216eb8360c14cecbb647c834db to your computer and use it in GitHub Desktop.
Save felixmc/e4c8c4216eb8360c14cecbb647c834db to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const path = require('path')
const exec = require('child_process').exec
const cmd = `ag "import .* from '" -G "${process.argv[2] || 'lib/'}" --nogroup | sed -e "s/:.*from//g" -e "s/'//g" | sed "s/^\\(.*\\.js\\) \\(.*\\)$/\\1 \\2/"`
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error('Error ocurred:', error)
} else {
const output = stdout.trim()
if (output.length === 0) process.exit(0)
const lines = stdout.trim().split('\n')
Promise.all(lines.map((line, index, lines) => {
const [requiringFile, modulePath] = line.trim().split(' ')
return isValidPath(requiringFile, modulePath)
// checkResolve(requiringFile, modulePath)
}))
.then(results => {
results.forEach(({ isValid, requiringFile, modulePath, fullModulePath }, index, lines) => {
if (!isValid) {
console.error(`\nBad import "${modulePath}" in "${requiringFile}"\n could not resolve "${fullModulePath}"`)
}
})
})
}
})
function isValidPath (requiringFile, modulePath) {
const requiringDir = path.join(requiringFile, '..')
const isModuleRelative = modulePath.startsWith('.')
const fullModulePath = isModuleRelative ? path.resolve(requiringDir, modulePath) : modulePath
const isTestsPath = modulePath.startsWith('tests/')
const isLoader = modulePath.endsWith('!')
const data = { requiringFile, modulePath, fullModulePath, isValid: isTestsPath || isLoader }
if (data.isValid) return Promise.resolve(data)
return new Promise((resolve, reject) => {
try {
require.resolve(fullModulePath)
data.isValid = true
resolve(data)
} catch (e) {
data.isValid = false
resolve(data)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment