Skip to content

Instantly share code, notes, and snippets.

@davidgtonge
Created February 1, 2018 14:38
Show Gist options
  • Save davidgtonge/cc3c86f0e7d5ed8bbe7081a58dd548d9 to your computer and use it in GitHub Desktop.
Save davidgtonge/cc3c86f0e7d5ed8bbe7081a58dd548d9 to your computer and use it in GitHub Desktop.
replace
const replace = require("replace-in-file")
const files = "./test/**/*.js"
const opts1 = {
files,
from: /import\s([a-zA-Z]*)\sfrom\s("[a-z\.\/\_\-\@]*")/g,
to: (match, p1, p2) => `const ${p1} = require(${p2})`,
}
const opts2 = {
files,
from: /import\s"([a-z\.\/\_\-\@]*)"\s*\n/g,
to: (match, p1) => `require(${p1})`,
}
const opts3 = {
files,
from: /import\s(\{[a-zA-Z\,\s]*})\sfrom\s("[a-z\.\/\_\-\@]*")/g,
to: (match, p1, p2) => `const ${p1} = require(${p2})`,
}
Promise.all([replace(opts1), replace(opts2), replace(opts3)])
.then(changes => {
console.log(changes)
})
.catch(error => {
console.error("Error occurred:", error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment