Last active
March 21, 2018 18:21
-
-
Save swashcap/05ec9c53bd51a6379f23a1fe11577b02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* **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