Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Last active July 21, 2021 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamieMason/09fd30cb666e85473e7859b949d76bb1 to your computer and use it in GitHub Desktop.
Save JamieMason/09fd30cb666e85473e7859b949d76bb1 to your computer and use it in GitHub Desktop.
Remove single line javascript comments between ES module import statements
const fs = require('fs')
const globby = require('globby')
globby
.sync(
['**/*.js', '!node_modules', '!.next', '!**/*.spec.js', '!**/*.test.js', '!**/integration/**'],
{
absolute: true,
},
)
.map((file) => {
return [file, fs.readFileSync(file, 'utf8')]
})
.filter(([file, contents]) => {
return contents.search(/^import /gm) !== -1 || contents.includes('require(')
})
.forEach(([file, contents]) => {
const lines = contents.split('\n')
const nextLines = lines
.map((line, i) => {
if (
(line.startsWith('//') || line === '') &&
lines.slice(i, lines.length).some((laterLine) => laterLine.startsWith('import'))
) {
return null
}
if (
line.startsWith('//') &&
lines.slice(i, lines.length).some((laterLine) => laterLine.includes('require('))
) {
return null
}
return line
})
.filter((line) => line !== null)
fs.writeFileSync(file, nextLines.join('\n'), 'utf8')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment