diff --git a/local-cli/link/android/patches/makePackagePatch.js b/local-cli/link/android/patches/makePackagePatch.js index 054c2ac5649b79..a97349bd27662f 100644 --- a/local-cli/link/android/patches/makePackagePatch.js +++ b/local-cli/link/android/patches/makePackagePatch.js @@ -5,13 +5,46 @@ * LICENSE file in the root directory of this source tree. */ -const applyParams = require('./applyParams'); - -module.exports = function makePackagePatch(packageInstance, params, prefix) { - const processedInstance = applyParams(packageInstance, params, prefix); +const fs = require("fs") +const log = require('npmlog') +const applyParams = require("./applyParams") +function getPatchAndPattern(patch) { return { - pattern: 'new MainReactPackage()', - patch: ',\n ' + processedInstance, - }; + pattern: "new MainReactPackage()", + patch + } +} + +function applyPackagePatch( + packageInstance, + params, + prefix) { + const processedInstance = applyParams(packageInstance, params, prefix) + return getPatchAndPattern(',\n ' + processedInstance) +} + +function revokePackagePatch(file, + packageInstance, + params, + prefix) { + let processedInstance = applyParams(packageInstance, params, prefix).replace( + /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, + "\\$&" + ) + let fileStream = fs.readFileSync(file, "utf8") + + return getPatchAndPattern( + fileStream.match( + new RegExp( + `(,\\s*${processedInstance}|${processedInstance}\\s*,|\\s*${processedInstance}\\s*)`, + "g" + ) + ) || "" + ) +} + +module.exports = { + applyPackagePatch, + revokePackagePatch }; diff --git a/local-cli/link/android/registerNativeModule.js b/local-cli/link/android/registerNativeModule.js index 0ce77c4e6ed4da..727ff4d379a006 100644 --- a/local-cli/link/android/registerNativeModule.js +++ b/local-cli/link/android/registerNativeModule.js @@ -10,7 +10,7 @@ const makeStringsPatch = require('./patches/makeStringsPatch'); const makeSettingsPatch = require('./patches/makeSettingsPatch'); const makeBuildPatch = require('./patches/makeBuildPatch'); const makeImportPatch = require('./patches/makeImportPatch'); -const makePackagePatch = require('./patches/makePackagePatch'); +const { applyPackagePatch } = require('./patches/makePackagePatch'); module.exports = function registerNativeAndroidModule( name, @@ -30,7 +30,7 @@ module.exports = function registerNativeAndroidModule( applyPatch( projectConfig.mainFilePath, - makePackagePatch(androidConfig.packageInstance, params, name) + applyPackagePatch(androidConfig.packageInstance, params, name) ); applyPatch( diff --git a/local-cli/link/android/unregisterNativeModule.js b/local-cli/link/android/unregisterNativeModule.js index b6b8d166531841..f5a9f7b9d73d44 100644 --- a/local-cli/link/android/unregisterNativeModule.js +++ b/local-cli/link/android/unregisterNativeModule.js @@ -13,7 +13,7 @@ const makeSettingsPatch = require('./patches/makeSettingsPatch'); const makeBuildPatch = require('./patches/makeBuildPatch'); const makeStringsPatch = require('./patches/makeStringsPatch'); const makeImportPatch = require('./patches/makeImportPatch'); -const makePackagePatch = require('./patches/makePackagePatch'); +const { revokePackagePatch } = require('./patches/makePackagePatch'); module.exports = function unregisterNativeAndroidModule( name, @@ -41,7 +41,7 @@ module.exports = function unregisterNativeAndroidModule( revokePatch( projectConfig.mainFilePath, - makePackagePatch(androidConfig.packageInstance, params, name) + revokePackagePatch(projectConfig.mainFilePath, androidConfig.packageInstance, params, name) ); revokePatch( diff --git a/local-cli/link/unlink.js b/local-cli/link/unlink.js index 91c89ab7ec0844..61d6599d12438e 100644 --- a/local-cli/link/unlink.js +++ b/local-cli/link/unlink.js @@ -1,3 +1,4 @@ + /** * Copyright (c) 2015-present, Facebook, Inc. * @@ -48,7 +49,7 @@ const unlinkDependency = (platforms, project, dependency, packageName, otherDepe otherDependencies ); - log.info(`Platform '${platform}' module ${dependency.name} has been successfully unlinked`); + log.info(`Platform '${platform}' module ${packageName} has been successfully unlinked`); }); };