diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c844db6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Tradle + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/cmd.js b/cmd.js index 7613b35..ae3790c 100755 --- a/cmd.js +++ b/cmd.js @@ -219,7 +219,7 @@ function installShims ({ modules, overwrite }, done) { installLine += ' ' }) - fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), function (err) { + fs.writeFile(pkgPath, prettify(pkg), function (err) { if (err) throw err if (installLine.trim() === BASE_INSTALL_LINE) { @@ -274,79 +274,73 @@ function hackPackageJSONs (modules, done) { function fixPackageJSON (modules, file, overwrite) { if (file.split(path.sep).indexOf('react-native') >= 0) return - fs.readFile(path.resolve(file), { encoding: 'utf8' }, function (err, contents) { - if (err) throw err + var contents = fs.readFileSync(path.resolve(file), { encoding: 'utf8' }) - // var browser = pick(baseBrowser, modules) - var pkgJson - try { - pkgJson = JSON.parse(contents) - } catch (err) { - console.warn('failed to parse', file) - return - } + // var browser = pick(baseBrowser, modules) + var pkgJson + try { + pkgJson = JSON.parse(contents) + } catch (err) { + console.warn('failed to parse', file) + return + } - // if (shims[pkgJson.name]) { - // log('skipping', pkgJson.name) - // return - // } + // if (shims[pkgJson.name]) { + // log('skipping', pkgJson.name) + // return + // } - // if (pkgJson.name === 'readable-stream') debugger + // if (pkgJson.name === 'readable-stream') debugger - var orgBrowser = pkgJson['react-native'] || pkgJson.browser || pkgJson.browserify || {} - if (typeof orgBrowser === 'string') { - orgBrowser = {} - orgBrowser[pkgJson.main || 'index.js'] = pkgJson['react-native'] || pkgJson.browser || pkgJson.browserify - } + var orgBrowser = pkgJson['react-native'] || pkgJson.browser || pkgJson.browserify || {} + if (typeof orgBrowser === 'string') { + orgBrowser = {} + orgBrowser[pkgJson.main || 'index.js'] = pkgJson['react-native'] || pkgJson.browser || pkgJson.browserify + } - var depBrowser = extend({}, orgBrowser) - for (var p in browser) { - if (modules.indexOf(p) === -1) continue + var depBrowser = extend({}, orgBrowser) + for (var p in browser) { + if (modules.indexOf(p) === -1) continue - if (!(p in orgBrowser)) { - depBrowser[p] = browser[p] + if (!(p in orgBrowser)) { + depBrowser[p] = browser[p] + } else { + if (!overwrite && orgBrowser[p] !== browser[p]) { + log('not overwriting mapping', p, orgBrowser[p]) } else { - if (!overwrite && orgBrowser[p] !== browser[p]) { - log('not overwriting mapping', p, orgBrowser[p]) - } else { - depBrowser[p] = browser[p] - } + depBrowser[p] = browser[p] } } + } - modules.forEach(function (p) { - if (depBrowser[p] === false && browser[p] !== false) { - log('removing browser exclude', file, p) - delete depBrowser[p] - } - }) - - - const { main } = pkgJson - if (main) { - const alt = main.startsWith('./') ? main.slice(2) : './' + main - if (depBrowser[alt]) { - depBrowser[main] = depBrowser[alt] - log(`normalized "main" browser mapping in ${pkgJson.name}, fixed here: https://github.com/facebook/metro-bundler/pull/3`) - delete depBrowser[alt] - } + modules.forEach(function (p) { + if (depBrowser[p] === false && browser[p] !== false) { + log('removing browser exclude', file, p) + delete depBrowser[p] } + }) - if (pkgJson.name === 'constants-browserify') { - // otherwise react-native packager chokes for some reason - delete depBrowser.constants - } - if (!deepEqual(orgBrowser, depBrowser)) { - pkgJson.browser = pkgJson['react-native'] = depBrowser - delete pkgJson.browserify - fs.writeFile(file, JSON.stringify(pkgJson, null, 2), rethrow) + const { main } = pkgJson + if (typeof main === 'string') { + const alt = main.startsWith('./') ? main.slice(2) : './' + main + if (depBrowser[alt]) { + depBrowser[main] = depBrowser[alt] + log(`normalized "main" browser mapping in ${pkgJson.name}, fixed here: https://github.com/facebook/metro-bundler/pull/3`) + delete depBrowser[alt] } - }) -} + } + + if (pkgJson.name === 'constants-browserify') { + // otherwise react-native packager chokes for some reason + delete depBrowser.constants + } -function rethrow (err) { - if (err) throw err + if (!deepEqual(orgBrowser, depBrowser)) { + pkgJson.browser = pkgJson['react-native'] = depBrowser + delete pkgJson.browserify + fs.writeFileSync(file, prettify(pkgJson)) + } } function runHelp () { @@ -373,3 +367,7 @@ function runHelp () { function log () { console.log.apply(console, arguments) } + +function prettify (json) { + return JSON.stringify(json, null, 2) + '\n' +} diff --git a/package.json b/package.json index ea7e1e9..2b511af 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "rn-nodeify", "repository": { "type": "git", - "url": "https://github.com/mvayngrib/rn-nodeify.git" + "url": "https://github.com/tradle/rn-nodeify.git" }, - "version": "10.0.0", + "version": "10.3.0", "preferGlobal": true, "bin": "./cmd.js", "license": "MIT", diff --git a/pkg-hacks.js b/pkg-hacks.js index bbbdd3c..6ac639a 100644 --- a/pkg-hacks.js +++ b/pkg-hacks.js @@ -520,7 +520,9 @@ var hackers = [ hack: function (file, contents) { if (isInReactNative(file)) return - var fixed = contents.replace('process.version', '"' + process.version + '"') + var fixed = contents + .replace(/global\.process\.version/g, '"' + process.version + '"') + .replace(/process\.version/g, '"' + process.version + '"') return contents === fixed ? null : fixed } @@ -546,7 +548,7 @@ function body (fn) { } function prettify (json) { - return JSON.stringify(json, null, 2) + return JSON.stringify(json, null, 2) + '\n' } function isInReactNative (file) { diff --git a/readme.md b/readme.md index 80af834..4ba6ad9 100644 --- a/readme.md +++ b/readme.md @@ -30,6 +30,7 @@ rn-nodeify --install install node core shims (default: install all), fix the "browser" and "react-native" fields in the package.json's of dependencies --hack hack individual packages that are known to make the React Native packager choke +--yarn use yarn instead of npm ``` ### Examples @@ -70,12 +71,12 @@ Some shims may require linking libraries, be sure to run `react-native link` aft ### Example Apps / Workflows -* the [react-native-crypto](https://github.com/mvayngrib/react-native-crypto) package has an example workflow for using crypto in a React Native app +* the [react-native-crypto](https://github.com/tradle/react-native-crypto) package has an example workflow for using crypto in a React Native app * this [example React Native app](https://github.com/mvayngrib/adexample) shows how you can use [levelup](https://github.com/Level/levelup) in React Native ### Example Workflow -copied from [react-native-crypto](https://github.com/mvayngrib/react-native-crypto) +copied from [react-native-crypto](https://github.com/tradle/react-native-crypto) 1. Install and shim ```sh @@ -84,7 +85,7 @@ copied from [react-native-crypto](https://github.com/mvayngrib/react-native-cryp npm i --save react-native-randombytes react-native link react-native-randombytes # install latest rn-nodeify - npm i --save-dev mvayngrib/rn-nodeify + npm i --save-dev rn-nodeify@latest # install node core shims and recursively hack package.json files # in ./node_modules to add/update the "browser"/"react-native" field with relevant mappings ./node_modules/.bin/rn-nodeify --hack --install @@ -108,8 +109,7 @@ copied from [react-native-crypto](https://github.com/mvayngrib/react-native-cryp - when installing a package from git, the postinstall hook isn't triggered, run it manually instead (`npm run postinstall`) - restart the react-native packager after installing a module! - removing the packager cache helps as well sometimes (`rm -fr $TMPDIR/react-*`) -- rn-nodeify currently uses `npm` to install shims. PRs are welcome to make it compatible with `yarn` - use `npm@3`. `npm@5` has some issues that cause `node_modules` to disappear. See: - - https://github.com/mvayngrib/rn-nodeify/issues/42 + - https://github.com/tradle/rn-nodeify/issues/42 - https://github.com/infinitered/ignite/issues/1101 - https://github.com/npm/npm/issues/16839