Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* **Warning!** This script modifies files. Use with caution, in a version
* controlled directory, etc.
*
* Replace partially resolved `import` or `require` paths with fully resolved
* paths using Node.js's internal `require` mechanism. Use:
*
* ```shell
* find . -type f -name '*.js' -exec node resolve-imports.js {} \;
* ```
*/
var fs = require('fs'),
path = require('path'),
patt = /(@walmart\/react-native-shared-library\/[^'"]*)/g,
filename = process.argv[2]
if (!filename) {
throw new Error('No file passed!')
}
filename = !path.isAbsolute(filename) ? filename : path.resolve(__dirname, filename),
fs.stat(filename, function (err) {
if (err) throw err
fs.readFile(filename, 'utf8', function (err2, data) {
if (err2) throw err2
var matches = data.match(patt),
newData = data
if (matches) {
matches
// Ensure the uniques
.reduce(function (memo, importFile) {
return memo.indexOf(importFile) >= 0
? memo
: memo.concat(importFile)
}, [])
// Mutate `newData`
.forEach(function (importFile) {
newData = newData.replace(
new RegExp(importFile, 'g'),
require.resolve(importFile).replace(path.join(__dirname, 'node_modules/'), '')
)
})
}
fs.writeFile(filename, newData, function (err3) {
if (err3) throw err3
console.log('' + filename)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.