From 402ef67f944333cb4c8fd9bf8f983e247f8fe26a Mon Sep 17 00:00:00 2001 From: Stoyan Stratev Date: Mon, 14 May 2018 15:19:04 +0300 Subject: [PATCH] fix: error when installing plugin with custom project configuration in nsconfig.json --- src/hooks/before-prepare.js | 2 +- src/hooks/utils.js | 18 +++++++++--------- src/scripts/postinstall.js | 16 ++++++++++++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/hooks/before-prepare.js b/src/hooks/before-prepare.js index f40057b..d2d7609 100755 --- a/src/hooks/before-prepare.js +++ b/src/hooks/before-prepare.js @@ -9,7 +9,7 @@ module.exports = function ($logger, $projectData) { utils.setLogger(_log); if (utils.targetsAndroid($projectData.projectDir)) { - utils.addIfNecessary($projectData.platformsDir); + utils.addIfNecessary($projectData.platformsDir, $projectData.appResourcesDirectoryPath); } function _log (str) { diff --git a/src/hooks/utils.js b/src/hooks/utils.js index a79aa20..3e7e983 100755 --- a/src/hooks/utils.js +++ b/src/hooks/utils.js @@ -27,13 +27,13 @@ function checkForGoogleServicesJson(projectDir, resourcesDir) { } } -function addOnPluginInstall(platformsDir) { +function addOnPluginInstall(platformsDir, resourcesDir) { if (buildGradleExists(platformsDir)) { - addIfNecessary(platformsDir); + addIfNecessary(platformsDir, resourcesDir); } } -function addIfNecessary(platformsDir) { +function addIfNecessary(platformsDir, resourcesDir) { _amendBuildGradle(platformsDir, function(pluginImported, pluginApplied, fileContents) { if (!pluginImported) { fileContents.projectFileContents = _addPluginImport(fileContents.projectFileContents); @@ -49,7 +49,7 @@ function addIfNecessary(platformsDir) { return fileContents; }); - _copyGoogleServices(platformsDir); + _copyGoogleServices(resourcesDir, platformsDir); } function removeIfPresent(platformsDir) { @@ -80,8 +80,8 @@ var _versionRegExp = '[^\'"]+'; var _pluginImportName = 'com.google.gms:google-services'; var _pluginApplicationName = 'com.google.gms.google-services'; -function _copyGoogleServices(platformsDir) { - var srcServicesFile = path.join(platformsDir, '..', 'app', 'App_Resources', 'Android', 'google-services.json'); +function _copyGoogleServices(resourcesDir, platformsDir) { + var srcServicesFile = path.join(resourcesDir, 'Android', 'google-services.json'); var dstServicesFile = path.join(platformsDir, 'android', 'app', 'google-services.json'); if (fs.existsSync(srcServicesFile) && !fs.existsSync(dstServicesFile) && fs.existsSync(path.join(platformsDir, 'android', 'app'))) { // try to copy google-services config file to platform app directory @@ -130,15 +130,15 @@ function _removePluginApplication(buildGradleContents) { function _addPluginImport(buildGradleContents) { var androidGradle = 'com.android.tools.build:gradle'; var insertBeforeDoubleQuotes = 'classpath "' + androidGradle; - var insertBeforeSingleQoutes = 'classpath \'' + androidGradle; + var insertBeforeSingleQuotes = 'classpath \'' + androidGradle; var quoteToInsert = '"' var matchedString = insertBeforeDoubleQuotes; var ind = buildGradleContents.indexOf(insertBeforeDoubleQuotes); if (ind === -1) { - ind = buildGradleContents.indexOf(insertBeforeSingleQoutes); + ind = buildGradleContents.indexOf(insertBeforeSingleQuotes); quoteToInsert = '\''; - matchedString = insertBeforeSingleQoutes; + matchedString = insertBeforeSingleQuotes; } if (ind === -1) { diff --git a/src/scripts/postinstall.js b/src/scripts/postinstall.js index b46c9c0..0180c16 100644 --- a/src/scripts/postinstall.js +++ b/src/scripts/postinstall.js @@ -6,6 +6,18 @@ var projDir = hook.findProjectDir(); hook.postinstall(); if (projDir) { - utils.checkForGoogleServicesJson(projDir, path.join(projDir, 'app', 'App_Resources')); - utils.addOnPluginInstall(path.join(projDir, 'platforms')); + var resourcesDir; + + try { + var globalPath = require('child_process').execSync('npm root -g').toString().trim(); + var tns = require(path.join(globalPath, 'nativescript')); + var project = tns.projectDataService.getProjectData(projDir); + resourcesDir = project.appResourcesDirectoryPath; + } catch (exc) { + console.log('Push plugin cannot find project root. Project will be initialized during build.'); + } + if (resourcesDir) { + utils.checkForGoogleServicesJson(projDir, resourcesDir); + utils.addOnPluginInstall(path.join(projDir, 'platforms'), resourcesDir); + } }