From d49224562704563bfada04c7e709a9f0cb2dcb68 Mon Sep 17 00:00:00 2001 From: fatme Date: Wed, 5 Jun 2019 17:15:53 +0300 Subject: [PATCH] fix: make the hooks compatible with CLI v6.0.0 release --- lib/after-prepare.ts | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/after-prepare.ts b/lib/after-prepare.ts index 2be32bf..4d0866e 100644 --- a/lib/after-prepare.ts +++ b/lib/after-prepare.ts @@ -1,16 +1,30 @@ import * as path from "path"; import * as fs from "fs"; -module.exports = function (hookArgs, $platformsData, $testExecutionService) { - const bundle = hookArgs && hookArgs.appFilesUpdaterOptions && hookArgs.appFilesUpdaterOptions.bundle; - if($testExecutionService && $testExecutionService.platform && !bundle) { - let platformData = $platformsData.getPlatformData($testExecutionService.platform), - projectFilesPath = path.join(platformData.appDestinationDirectoryPath, "app"), - packageJsonPath = path.join(projectFilesPath, 'package.json'), - packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString()); +function isCLIVersionLowerThan6($injector) { + try { + const $staticConfig = $injector.resolve('$staticConfig'); + const version = $staticConfig && $staticConfig.version; + const majorVersion = (version || '').split('.')[0]; + return !majorVersion || +majorVersion < 6; + } catch (err) { + return false; + } +} + +module.exports = function (hookArgs, $injector, $testExecutionService) { + if (isCLIVersionLowerThan6($injector)) { + const bundle = hookArgs && hookArgs.appFilesUpdaterOptions && hookArgs.appFilesUpdaterOptions.bundle; + if($testExecutionService && $testExecutionService.platform && !bundle) { + const $platformsData = $injector.resolve("platformsData"); + let platformData = $platformsData.getPlatformData($testExecutionService.platform), + projectFilesPath = path.join(platformData.appDestinationDirectoryPath, "app"), + packageJsonPath = path.join(projectFilesPath, 'package.json'), + packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString()); - // When test command is used in ns-cli, we should change the entry point of the application - packageJson.main = "./tns_modules/nativescript-unit-test-runner/app.js"; - fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson)); + // When test command is used in ns-cli, we should change the entry point of the application + packageJson.main = "./tns_modules/nativescript-unit-test-runner/app.js"; + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson)); + } } -} +} \ No newline at end of file