diff --git a/lib/common/declarations.d.ts b/lib/common/declarations.d.ts index 4b27870564..252dd0bfd7 100644 --- a/lib/common/declarations.d.ts +++ b/lib/common/declarations.d.ts @@ -1242,11 +1242,6 @@ interface IResourceLoader { readJson(path: string): any; } -interface IPluginVariablesHelper { - getPluginVariableFromVarOption(variableName: string, configuration?: string): any; - simplifyYargsObject(obj: any, configuration?: string): any; -} - /** * Used for getting strings for informational/error messages. */ diff --git a/lib/common/plugin-variables-helper.ts b/lib/common/plugin-variables-helper.ts deleted file mode 100644 index 45800e10f2..0000000000 --- a/lib/common/plugin-variables-helper.ts +++ /dev/null @@ -1,90 +0,0 @@ -export class PluginVariablesHelper implements IPluginVariablesHelper { - constructor(private $options: IOptions) { } - - /** - * Checks if the specified pluginVariable exists in the --var option specified by user. - * The variable can be added to --var option for configuration or globally, for ex.: - * `--var.APP_ID myAppIdentifier` or `--var.debug.APP_ID myAppIdentifier`. - * NOTE: If the variable is added for specific configuration and globally, - * the value for the specified configuration will be used as it has higher priority. For ex.: - * `--var.APP_ID myAppIdentifier1 --var.debug.APP_ID myAppIdentifier2` will return myAppIdentifier2 for debug configuration - * and myAppIdentifier for release configuration. - * @param {string} variableName The name of the plugin variable. - * @param {string} configuration The configuration for which the variable will be used. - * @returns {any} The value of the plugin variable specified in --var or undefined. - */ - public getPluginVariableFromVarOption(variableName: string, configuration?: string): any { - let varOption = this.$options.var; - configuration = configuration ? configuration.toLowerCase() : undefined; - const lowerCasedVariableName = variableName.toLowerCase(); - if (varOption) { - let configVariableValue: string; - let generalVariableValue: string; - if (variableName.indexOf(".") !== -1) { - varOption = this.simplifyYargsObject(varOption, configuration); - } - _.each(varOption, (propValue: any, propKey: string) => { - if (propKey.toLowerCase() === configuration) { - _.each(propValue, (configPropValue: string, configPropKey: string) => { - if (configPropKey.toLowerCase() === lowerCasedVariableName) { - configVariableValue = configPropValue; - return false; - } - }); - } else if (propKey.toLowerCase() === lowerCasedVariableName) { - generalVariableValue = propValue; - } - }); - - const value = configVariableValue || generalVariableValue; - if (value) { - const obj = Object.create(null); - obj[variableName] = value.toString(); - return obj; - } - } - - return undefined; - } - - /** - * Converts complicated yargs object with many subobjects, to simplified one. - * Use it when the plugin variable contains dots ("."). In this case yargs treats them as inner object instead of propery name. - * For ex. '--var.debug.DATA.APP.ID testId' will be converted to {debug: {DATA: {APP: {ID: testId}}}}, while we need {debug: {DATA.APP.ID: testId}} - * '--var.DATA.APP.ID testId' will be converted to DATA: {APP: {ID: testId}}}, while we need {DATA.APP.ID: testId} - * @param {any} obj varObject created by yargs - * @param {string} configuration The configuration for which the plugin variable will be used. - * @return {any} Converted object if the obj paramater is of type object, otherwise - the object itself. - */ - public simplifyYargsObject(obj: any, configuration?: string): any { - if (obj && typeof (obj) === "object") { - const convertedObject: any = Object.create({}); - - _.each(obj, (propValue: any, propKey: string) => { - if (typeof (propValue) !== "object") { - convertedObject[propKey] = propValue; - return false; - } - - configuration = configuration ? configuration.toLowerCase() : undefined; - const innerObj = this.simplifyYargsObject(propValue, configuration); - - if (propKey.toLowerCase() === configuration) { - // for --var.debug.DATA.APP.ID testId - convertedObject[propKey] = innerObj; - } else { - // for --var.DATA.APP.ID testId - _.each(innerObj, (innerPropValue: any, innerPropKey: string) => { - convertedObject[`${propKey}.${innerPropKey}`] = innerPropValue; - }); - } - - }); - - return convertedObject; - } - - return obj; - } -} -$injector.register("pluginVariablesHelper", PluginVariablesHelper); diff --git a/lib/definitions/plugins.d.ts b/lib/definitions/plugins.d.ts index 8fce2600ba..46759de0dc 100644 --- a/lib/definitions/plugins.d.ts +++ b/lib/definitions/plugins.d.ts @@ -44,53 +44,6 @@ interface IPluginPlatformsData { android: string; } -interface IPluginVariablesService { - /** - * Saves plugin variables in project package.json file. - * @param {IPluginData} pluginData for the plugin. - * @param {string} projectDir: Specifies the directory of the project. - * @return {Promise} - */ - savePluginVariablesInProjectFile(pluginData: IPluginData, projectDir: string): Promise; - - /** - * Removes plugin variables from project package.json file. - * @param {string} pluginName Name of the plugin. - * @param {string} projectDir: Specifies the directory of the project. - * @return {void} - */ - removePluginVariablesFromProjectFile(pluginName: string, projectDir: string): void; - - /** - * Replaces all plugin variables with their corresponding values. - * @param {IPluginData} pluginData for the plugin. - * @param {pluginConfigurationFilePath} pluginConfigurationFilePath for the plugin. - * @param {string} projectDir: Specifies the directory of the project. - * @return {Promise} - */ - interpolatePluginVariables(pluginData: IPluginData, pluginConfigurationFilePath: string, projectDir: string): Promise; - - /** - * Replaces {nativescript.id} expression with the application identifier from package.json. - * @param {pluginConfigurationFilePath} pluginConfigurationFilePath for the plugin. - * @return {void} - */ - interpolateAppIdentifier(pluginConfigurationFilePath: string, projectIdentifier: string): void; - - /** - * Replaces both plugin variables and appIdentifier - */ - interpolate(pluginData: IPluginData, pluginConfigurationFilePath: string, projectDir: string, projectIdentifier: string): Promise; - - /** - * Returns the - * @param {string} pluginName for the plugin. - * @return {Promise} returns the changed plugin configuration file content. - */ - getPluginVariablePropertyName(pluginName: string): string; - -} - interface IPluginVariableData { defaultValue?: string; name?: string; diff --git a/lib/services/ios-project-service.ts b/lib/services/ios-project-service.ts index 65156eb9cc..13fbf48e38 100644 --- a/lib/services/ios-project-service.ts +++ b/lib/services/ios-project-service.ts @@ -43,7 +43,6 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ private $devicesService: Mobile.IDevicesService, private $mobileHelper: Mobile.IMobileHelper, private $hostInfo: IHostInfo, - private $pluginVariablesService: IPluginVariablesService, private $xcprojService: IXcprojService, private $iOSProvisionService: IOSProvisionService, private $pbxprojDomXcode: IPbxprojDomXcode, @@ -800,11 +799,6 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f await this.mergeInfoPlists(projectData, opts); await this.$iOSEntitlementsService.merge(projectData); await this.mergeProjectXcconfigFiles(projectData, opts); - for (const pluginData of await this.getAllInstalledPlugins(projectData)) { - await this.$pluginVariablesService.interpolatePluginVariables(pluginData, this.getPlatformData(projectData).configurationFilePath, projectData.projectDir); - } - - this.$pluginVariablesService.interpolateAppIdentifier(this.getPlatformData(projectData).configurationFilePath, projectData.projectIdentifiers.ios); } private getInfoPlistPath(projectData: IProjectData): string { diff --git a/lib/services/plugin-variables-service.ts b/lib/services/plugin-variables-service.ts deleted file mode 100644 index d73ed0a56d..0000000000 --- a/lib/services/plugin-variables-service.ts +++ /dev/null @@ -1,104 +0,0 @@ -import * as helpers from "./../common/helpers"; - -export class PluginVariablesService implements IPluginVariablesService { - private static PLUGIN_VARIABLES_KEY = "variables"; - - constructor(private $errors: IErrors, - private $pluginVariablesHelper: IPluginVariablesHelper, - private $projectDataService: IProjectDataService, - private $prompter: IPrompter, - private $fs: IFileSystem) { } - - public getPluginVariablePropertyName(pluginName: string): string { - return `${pluginName}-${PluginVariablesService.PLUGIN_VARIABLES_KEY}`; - } - - public async savePluginVariablesInProjectFile(pluginData: IPluginData, projectDir: string): Promise { - const values = Object.create(null); - await this.executeForAllPluginVariables(pluginData, async (pluginVariableData: IPluginVariableData) => { - const pluginVariableValue = await this.getPluginVariableValue(pluginVariableData); - this.ensurePluginVariableValue(pluginVariableValue, `Unable to find value for ${pluginVariableData.name} plugin variable from ${pluginData.name} plugin. Ensure the --var option is specified or the plugin variable has default value.`); - values[pluginVariableData.name] = pluginVariableValue; - }, projectDir); - - if (!_.isEmpty(values)) { - this.$projectDataService.setNSValue(projectDir, this.getPluginVariablePropertyName(pluginData.name), values); - } - } - - public removePluginVariablesFromProjectFile(pluginName: string, projectDir: string): void { - this.$projectDataService.removeNSProperty(projectDir, this.getPluginVariablePropertyName(pluginName)); - } - - public async interpolatePluginVariables(pluginData: IPluginData, pluginConfigurationFilePath: string, projectDir: string): Promise { - let pluginConfigurationFileContent = this.$fs.readText(pluginConfigurationFilePath); - await this.executeForAllPluginVariables(pluginData, async (pluginVariableData: IPluginVariableData) => { - this.ensurePluginVariableValue(pluginVariableData.value, `Unable to find the value for ${pluginVariableData.name} plugin variable into project package.json file. Verify that your package.json file is correct and try again.`); - pluginConfigurationFileContent = this.interpolateCore(pluginVariableData.name, pluginVariableData.value, pluginConfigurationFileContent); - }, projectDir); - - this.$fs.writeFile(pluginConfigurationFilePath, pluginConfigurationFileContent); - } - - public interpolateAppIdentifier(pluginConfigurationFilePath: string, projectIdentifier: string): void { - const pluginConfigurationFileContent = this.$fs.readText(pluginConfigurationFilePath); - const newContent = this.interpolateCore("nativescript.id", projectIdentifier, pluginConfigurationFileContent); - this.$fs.writeFile(pluginConfigurationFilePath, newContent); - } - - public async interpolate(pluginData: IPluginData, pluginConfigurationFilePath: string, projectDir: string, projectIdentifier: string): Promise { - await this.interpolatePluginVariables(pluginData, pluginConfigurationFilePath, projectDir); - this.interpolateAppIdentifier(pluginConfigurationFilePath, projectIdentifier); - } - - private interpolateCore(name: string, value: string, content: string): string { - return content.replace(new RegExp(`{${name}}`, "gi"), value); - } - - private ensurePluginVariableValue(pluginVariableValue: string, errorMessage: string): void { - if (!pluginVariableValue) { - this.$errors.failWithoutHelp(errorMessage); - } - } - - private async getPluginVariableValue(pluginVariableData: IPluginVariableData): Promise { - const pluginVariableName = pluginVariableData.name; - let value = this.$pluginVariablesHelper.getPluginVariableFromVarOption(pluginVariableName); - if (value) { - value = value[pluginVariableName]; - } else { - value = pluginVariableData.defaultValue; - if (!value && helpers.isInteractive()) { - const promptSchema = { - name: pluginVariableName, - type: "input", - message: `Enter value for ${pluginVariableName} variable:`, - validate: (val: string) => !!val ? true : 'Please enter a value!' - }; - const promptData = await this.$prompter.get([promptSchema]); - value = promptData[pluginVariableName]; - } - } - - return value; - } - - private async executeForAllPluginVariables(pluginData: IPluginData, action: (pluginVariableData: IPluginVariableData) => Promise, projectDir: string): Promise { - const pluginVariables = pluginData.pluginVariables; - const pluginVariablesNames = _.keys(pluginVariables); - await Promise.all(_.map(pluginVariablesNames, pluginVariableName => action(this.createPluginVariableData(pluginData, pluginVariableName, projectDir)))); - } - - private createPluginVariableData(pluginData: IPluginData, pluginVariableName: string, projectDir: string): IPluginVariableData { - const variableData = pluginData.pluginVariables[pluginVariableName]; - - variableData.name = pluginVariableName; - - const pluginVariableValues = this.$projectDataService.getNSValue(projectDir, this.getPluginVariablePropertyName(pluginData.name)); - variableData.value = pluginVariableValues ? pluginVariableValues[pluginVariableName] : undefined; - - return variableData; - } -} - -$injector.register("pluginVariablesService", PluginVariablesService); diff --git a/lib/services/plugins-service.ts b/lib/services/plugins-service.ts index 143da2d6d8..7fbf435a71 100644 --- a/lib/services/plugins-service.ts +++ b/lib/services/plugins-service.ts @@ -12,9 +12,6 @@ export class PluginsService implements IPluginsService { private get $platformsData(): IPlatformsData { return this.$injector.resolve("platformsData"); } - private get $pluginVariablesService(): IPluginVariablesService { - return this.$injector.resolve("pluginVariablesService"); - } private get $projectDataService(): IProjectDataService { return this.$injector.resolve("projectDataService"); } @@ -57,16 +54,6 @@ export class PluginsService implements IPluginsService { await this.executeForAllInstalledPlatforms(action, projectData); - try { - await this.$pluginVariablesService.savePluginVariablesInProjectFile(pluginData, projectData.projectDir); - } catch (err) { - // Revert package.json - this.$projectDataService.removeNSProperty(projectData.projectDir, this.$pluginVariablesService.getPluginVariablePropertyName(pluginData.name)); - await this.$packageManager.uninstall(plugin, PluginsService.NPM_CONFIG, projectData.projectDir); - - throw err; - } - this.$logger.out(`Successfully installed plugin ${realNpmPackageJson.name}.`); } else { await this.$packageManager.uninstall(realNpmPackageJson.name, { save: true }, projectData.projectDir); @@ -81,7 +68,6 @@ export class PluginsService implements IPluginsService { await platformData.platformProjectService.removePluginNativeCode(pluginData, projectData); }; - this.$pluginVariablesService.removePluginVariablesFromProjectFile(pluginName.toLowerCase(), projectData.projectDir); await this.executeForAllInstalledPlatforms(removePluginNativeCodeAction, projectData); await this.executeNpmCommand(PluginsService.UNINSTALL_COMMAND_NAME, pluginName, projectData); diff --git a/test/ios-project-service.ts b/test/ios-project-service.ts index 74468e6e6b..2efac35129 100644 --- a/test/ios-project-service.ts +++ b/test/ios-project-service.ts @@ -21,8 +21,6 @@ import { LoggingLevels } from "../lib/common/mobile/logging-levels"; import { DeviceDiscovery } from "../lib/common/mobile/mobile-core/device-discovery"; import { IOSDeviceDiscovery } from "../lib/common/mobile/mobile-core/ios-device-discovery"; import { AndroidDeviceDiscovery } from "../lib/common/mobile/mobile-core/android-device-discovery"; -import { PluginVariablesService } from "../lib/services/plugin-variables-service"; -import { PluginVariablesHelper } from "../lib/common/plugin-variables-helper"; import { Utils } from "../lib/common/utils"; import { CocoaPodsService } from "../lib/services/cocoapods-service"; import { PackageManager } from "../lib/package-manager"; @@ -113,8 +111,6 @@ function createTestInjector(projectPath: string, projectName: string, xCode?: IX checkIfXcodeprojIsRequired: () => ({}) }); testInjector.register("iosDeviceOperations", {}); - testInjector.register("pluginVariablesService", PluginVariablesService); - testInjector.register("pluginVariablesHelper", PluginVariablesHelper); testInjector.register("pluginsService", { getAllInstalledPlugins: (): string[] => [] }); diff --git a/test/plugin-variables-service.ts b/test/plugin-variables-service.ts deleted file mode 100644 index 86657459e3..0000000000 --- a/test/plugin-variables-service.ts +++ /dev/null @@ -1,372 +0,0 @@ -import { assert } from "chai"; -import { Errors } from "../lib/common/errors"; -import { FileSystem } from "../lib/common/file-system"; -import { HostInfo } from "../lib/common/host-info"; -import { Options } from "../lib/options"; -import { PluginVariablesHelper } from "../lib/common/plugin-variables-helper"; -import { PluginVariablesService } from "../lib/services/plugin-variables-service"; -import { ProjectData } from "../lib/project-data"; -import { ProjectDataService } from "../lib/services/project-data-service"; -import { ProjectHelper } from "../lib/common/project-helper"; -import { StaticConfig } from "../lib/config"; -import { MessagesService } from "../lib/common/services/messages-service"; -import { Yok } from '../lib/common/yok'; -import { SettingsService } from "../lib/common/test/unit-tests/stubs"; -import { DevicePlatformsConstants } from "../lib/common/mobile/device-platforms-constants"; -import * as stubs from './stubs'; -import * as path from "path"; -import * as temp from "temp"; -temp.track(); - -function createTestInjector(): IInjector { - const testInjector = new Yok(); - - testInjector.register("messagesService", MessagesService); - testInjector.register("errors", Errors); - testInjector.register("fs", FileSystem); - testInjector.register("hostInfo", HostInfo); - testInjector.register("logger", stubs.LoggerStub); - testInjector.register("options", Options); - testInjector.register("pluginVariablesHelper", PluginVariablesHelper); - testInjector.register("pluginVariablesService", PluginVariablesService); - testInjector.register("projectData", ProjectData); - testInjector.register("projectDataService", ProjectDataService); - testInjector.register("projectHelper", ProjectHelper); - testInjector.register("prompter", { - get: () => { - const errors: IErrors = testInjector.resolve("errors"); - errors.fail("$prompter.get function shouldn't be called!"); - } - }); - testInjector.register("staticConfig", StaticConfig); - testInjector.register("settingsService", SettingsService); - testInjector.register("devicePlatformsConstants", DevicePlatformsConstants); - testInjector.register("androidResourcesMigrationService", { - hasMigrated: () => true - }); - - return testInjector; -} - -async function createProjectFile(testInjector: IInjector): Promise { - const tempFolder = temp.mkdirSync("pluginVariablesService"); - - const options = testInjector.resolve("options"); - options.path = tempFolder; - - const projectData = { - "name": "myProject", - "nativescript": { - id: { android: "", ios: ""} - } - }; - testInjector.resolve("fs").writeJson(path.join(tempFolder, "package.json"), projectData); - - return tempFolder; -} - -function createPluginData(pluginVariables: any): IPluginData { - const pluginData = { - name: "myTestPlugin", - version: "", - fullPath: "", - isPlugin: true, - moduleInfo: "", - platformsData: { - ios: "", - android: "" - }, - pluginVariables: pluginVariables, - pluginPlatformsFolderPath: (platform: string) => "" - }; - - return pluginData; -} - -describe("Plugin Variables service", () => { - let testInjector: IInjector; - beforeEach(() => { - testInjector = createTestInjector(); - }); - - describe("plugin add when the console is non interactive", () => { - beforeEach(() => { - const helpers = require("./../lib/common/helpers"); - helpers.isInteractive = () => false; - }); - it("fails when no --var option and no default value are specified", async () => { - await createProjectFile(testInjector); - - const pluginVariables = { "MY_TEST_PLUGIN_VARIABLE": {} }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const projectData: IProjectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - - const expectedError = `Unable to find value for MY_TEST_PLUGIN_VARIABLE plugin variable from ${pluginData.name} plugin. Ensure the --var option is specified or the plugin variable has default value.`; - let actualError: string = null; - - try { - await pluginVariablesService.savePluginVariablesInProjectFile(pluginData, projectData.projectDir); - } catch (err) { - actualError = err.message; - } - - assert.equal(expectedError, actualError); - }); - it("does not fail when --var option is specified", async () => { - await createProjectFile(testInjector); - - const pluginVariableValue = "myAppId"; - testInjector.resolve("options").var = { - "MY_APP_ID": pluginVariableValue - }; - - const pluginVariables = { "MY_APP_ID": {} }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const projectData: IProjectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - await pluginVariablesService.savePluginVariablesInProjectFile(pluginData, projectData.projectDir); - - const fs = testInjector.resolve("fs"); - const staticConfig: IStaticConfig = testInjector.resolve("staticConfig"); - - const projectFileContent = fs.readJson(path.join(projectData.projectDir, "package.json")); - assert.equal(pluginVariableValue, projectFileContent[staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][`${pluginData.name}-variables`]["MY_APP_ID"]); - }); - it("does not fail when default value is specified", async () => { - await createProjectFile(testInjector); - - const defaultPluginValue = "myDefaultValue"; - const pluginVariables = { "MY_TEST_PLUGIN_VARIABLE": { defaultValue: defaultPluginValue } }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const projectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - - await pluginVariablesService.savePluginVariablesInProjectFile(pluginData, projectData.projectDir); - - const fs = testInjector.resolve("fs"); - const staticConfig: IStaticConfig = testInjector.resolve("staticConfig"); - - const projectFileContent = fs.readJson(path.join(projectData.projectDir, "package.json")); - assert.equal(defaultPluginValue, projectFileContent[staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][`${pluginData.name}-variables`]["MY_TEST_PLUGIN_VARIABLE"]); - }); - }); - - describe("plugin add when the console is interactive", () => { - beforeEach(() => { - const helpers = require("./../lib/common/helpers"); - helpers.isInteractive = () => true; - }); - it("prompt for plugin variable value when no --var option and no default value are specified", async () => { - await createProjectFile(testInjector); - - const pluginVariableValue = "testAppURL"; - const prompter = testInjector.resolve("prompter"); - prompter.get = async () => ({ "APP_URL": pluginVariableValue }); - - const pluginVariables = { "APP_URL": {} }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const projectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - await pluginVariablesService.savePluginVariablesInProjectFile(pluginData, projectData.projectDir); - - const fs = testInjector.resolve("fs"); - const staticConfig: IStaticConfig = testInjector.resolve("staticConfig"); - - const projectFileContent = fs.readJson(path.join(projectData.projectDir, "package.json")); - assert.equal(pluginVariableValue, projectFileContent[staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][`${pluginData.name}-variables`]["APP_URL"]); - }); - it("does not prompt for plugin variable value when default value is specified", async () => { - await createProjectFile(testInjector); - - const defaultPluginValue = "myAppNAme"; - const pluginVariables = { "APP_NAME": { defaultValue: defaultPluginValue } }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const projectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - - await pluginVariablesService.savePluginVariablesInProjectFile(pluginData, projectData.projectDir); - - const fs = testInjector.resolve("fs"); - const staticConfig: IStaticConfig = testInjector.resolve("staticConfig"); - - const projectFileContent = fs.readJson(path.join(projectData.projectDir, "package.json")); - assert.equal(defaultPluginValue, projectFileContent[staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][`${pluginData.name}-variables`]["APP_NAME"]); - }); - it("does not prompt for plugin variable value when --var option is specified", async () => { - await createProjectFile(testInjector); - - const pluginVariableValue = "pencho.goshko"; - testInjector.resolve("options").var = { - "USERNAME": pluginVariableValue - }; - - const pluginVariables = { "USERNAME": {} }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const projectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - await pluginVariablesService.savePluginVariablesInProjectFile(pluginData, projectData.projectDir); - - const fs = testInjector.resolve("fs"); - const staticConfig: IStaticConfig = testInjector.resolve("staticConfig"); - - const projectFileContent = fs.readJson(path.join(projectData.projectDir, "package.json")); - assert.equal(pluginVariableValue, projectFileContent[staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][`${pluginData.name}-variables`]["USERNAME"]); - }); - }); - - describe("plugin interpolation", () => { - it("fails when the plugin value is undefined", async () => { - const tempFolder = await createProjectFile(testInjector); - - const pluginVariables = { "MY_VAR": {} }; - const pluginData = createPluginData(pluginVariables); - - const fs: IFileSystem = testInjector.resolve("fs"); - const filePath = path.join(tempFolder, "myfile"); - fs.writeFile(filePath, ""); - - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const projectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - - const expectedError = "Unable to find the value for MY_VAR plugin variable into project package.json file. Verify that your package.json file is correct and try again."; - let error: string = null; - try { - await pluginVariablesService.interpolatePluginVariables(pluginData, filePath, projectData.projectDir); - } catch (err) { - error = err.message; - } - - assert.equal(error, expectedError); - }); - - it("interpolates correctly plugin variable value", async () => { - const tempFolder = await createProjectFile(testInjector); - - const projectData: IProjectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - const fs: IFileSystem = testInjector.resolve("fs"); - - // Write plugin variables values to package.json file - const packageJsonFilePath = path.join(projectData.projectDir, "package.json"); - const data = fs.readJson(packageJsonFilePath); - data["nativescript"]["myTestPlugin-variables"] = { - "FB_APP_NAME": "myFacebookAppName" - }; - fs.writeJson(packageJsonFilePath, data); - - const pluginVariables = { "FB_APP_NAME": {} }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const pluginConfigurationFileContent = '' + - '' + - '' + - '' + - '' + - ''; - const filePath = path.join(tempFolder, "myfile"); - fs.writeFile(filePath, pluginConfigurationFileContent); - - await pluginVariablesService.interpolatePluginVariables(pluginData, filePath, projectData.projectDir); - - const result = fs.readText(filePath); - const expectedResult = '' + - '' + - '' + - '' + - '' + - ''; - - assert.equal(result, expectedResult); - }); - - it("interpolates correctly case sensive plugin variable value", async () => { - const tempFolder = await createProjectFile(testInjector); - - const projectData: IProjectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - const fs: IFileSystem = testInjector.resolve("fs"); - - // Write plugin variables values to package.json file - const packageJsonFilePath = path.join(projectData.projectDir, "package.json"); - const data = fs.readJson(packageJsonFilePath); - data["nativescript"]["myTestPlugin-variables"] = { - "FB_APP_NAME": "myFacebookAppName" - }; - fs.writeJson(packageJsonFilePath, data); - - const pluginVariables = { "FB_APP_NAME": {} }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const pluginConfigurationFileContent = '' + - '' + - '' + - '' + - '' + - ''; - const filePath = path.join(tempFolder, "myfile"); - fs.writeFile(filePath, pluginConfigurationFileContent); - - await pluginVariablesService.interpolatePluginVariables(pluginData, filePath, projectData.projectDir); - - const result = fs.readText(filePath); - const expectedResult = '' + - '' + - '' + - '' + - '' + - ''; - - assert.equal(result, expectedResult); - }); - - it("interpolates correctly more than one plugin variables values", async () => { - const tempFolder = await createProjectFile(testInjector); - - const projectData: IProjectData = testInjector.resolve("projectData"); - projectData.initializeProjectData(); - const fs: IFileSystem = testInjector.resolve("fs"); - - const packageJsonFilePath = path.join(projectData.projectDir, "package.json"); - const data = fs.readJson(packageJsonFilePath); - data["nativescript"]["myTestPlugin-variables"] = { - "FB_APP_NAME": "myFacebookAppName", - "FB_APP_URL": "myFacebookAppURl" - }; - fs.writeJson(packageJsonFilePath, data); - - const pluginVariables = { "FB_APP_NAME": {}, "FB_APP_URL": {} }; - const pluginData = createPluginData(pluginVariables); - const pluginVariablesService: IPluginVariablesService = testInjector.resolve("pluginVariablesService"); - const pluginConfigurationFileContent = '' + - '' + - '' + - '' + - '' + - '' + - ''; - const filePath = path.join(tempFolder, "myfile"); - fs.writeFile(filePath, pluginConfigurationFileContent); - - await pluginVariablesService.interpolatePluginVariables(pluginData, filePath, projectData.projectDir); - - const result = fs.readText(filePath); - const expectedResult = '' + - '' + - '' + - '' + - '' + - '' + - ''; - - assert.equal(result, expectedResult); - }); - }); -}); diff --git a/test/update.ts b/test/update.ts index 65b162c48a..80eb387f02 100644 --- a/test/update.ts +++ b/test/update.ts @@ -47,7 +47,6 @@ function createTestInjector( return "1.0.0"; } }); - testInjector.register("pluginVariablesService", {}); testInjector.register("platformService", { getInstalledPlatforms: function(): string[] { return installedPlatforms;