diff --git a/README.md b/README.md index e4f825e..894cef5 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ it also copies any files you have suffixed with the name of the environemnt for Once you have a initial `environment-rules.json` file you can change between the environments using `--env.use.ENV_NAME` +You can also create environment file each platform like `environment-rules.android.json` or `environment-rules.ios.json` + for example for ios: `tns run ios --env.use.staging` @@ -39,12 +41,12 @@ a basic environment-rules.json file is generated for you it looks like this: { name: "staging", packageId: "org.nativescript.appName.staging", - copyRule: "(.*\\.staging\\..*)" + copyRules: "(.*\\.staging\\..*)" }, { name: "release", packageId: "org.nativescript.appName.release", - copyRule: "(.*\\.release\\..*)" + copyRules: "(.*\\.release\\..*)" } ] } diff --git a/lib/env-switcher.common.js b/lib/env-switcher.common.js index daad173..0f26508 100644 --- a/lib/env-switcher.common.js +++ b/lib/env-switcher.common.js @@ -25,7 +25,8 @@ class EnvSwitcherCommon { } } readRules() { - const ruleFile = path.join(this.projectData.projectDir, "environment-rules.json"); + + const ruleFile = this.getProjectRules(); if (fs.existsSync(ruleFile)) { this.logger.debug("Environment Rules found, reading contents"); return JSON.parse(fs.readFileSync(ruleFile).toString()); @@ -40,9 +41,12 @@ class EnvSwitcherCommon { let dirName = inputFolder.indexOf(this.projectData.$projectHelper.cachedProjectDir) < 0 ? path.join(this.projectData.$projectHelper.cachedProjectDir, inputFolder) : inputFolder; const dir = fs.readdirSync(dirName); const getNewFilename = function (file) { + + // If file name is separated by mulitple dots then only remove environment name + // Like File Name en.default.staging.json let fileNameParts = file.split("."); - let ext = fileNameParts[fileNameParts.length - 1]; - let fileName = fileNameParts[0]; + let ext = fileNameParts[fileNameParts.length - 1] + let fileName = fileNameParts.splice(0, fileNameParts.length - 2).join("."); return `${fileName}.${ext}`; }; const testForRelease = file => { @@ -58,7 +62,6 @@ class EnvSwitcherCommon { const newFileName = getNewFilename(file); const newFilePath = path.join(inputFolder, newFileName); if (!this.doesSourceMatchDestination(filePath, newFilePath)) { - fs.writeFileSync(newFilePath, fileContents); } else { @@ -116,8 +119,21 @@ class EnvSwitcherCommon { throw e; } } + + // Check if there is different file for different environment other wise return default environment-rules file + getProjectRules() { + const fileName = "environment-rules." + this.platformData.platformNameLowerCase + ".json"; + const projectRules = path.join(this.projectData.projectDir, fileName); + if (fs.existsSync(projectRules)) { + return projectRules; + } else { + const defaultFileName = "environment-rules.json"; + return path.join(this.projectData.projectDir, defaultFileName); + } + } + maybeCreateEnvironmentRules() { - const projectRules = path.join(this.projectData.projectDir, 'environment-rules.json'); + const projectRules = this.getProjectRules(); if (!fs.existsSync(projectRules)) { this.logger.info('Environment Rules file does not exist, creating a basic one now.'); const environmentRules = {