Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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\\..*)"
}
]
}
Expand Down
26 changes: 21 additions & 5 deletions lib/env-switcher.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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 => {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand Down