forked from firebase/firebase-tools
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for project specific .env files (firebase#3904)
- Loading branch information
Showing
7 changed files
with
229 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- `firebase deploy --only extensions` now supports project specifc .env files. When deploying to multiple projects, param values that are different between projects can be put in `extensions/${extensionInstanceId}.env.${projectIdOrAlias}` and common param values can be put in `extensions/${extensionInstanceId}.env`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as path from "path"; | ||
import { logger } from "../../logger"; | ||
|
||
import { readEnvFile } from "../../extensions/paramHelper"; | ||
import { FirebaseError } from "../../error"; | ||
|
||
const ENV_DIRECTORY = "extensions"; | ||
|
||
/** | ||
* readParams gets the params for an extension instance from the `extensions` folder, | ||
* checking for project specific env files, then falling back to generic env files. | ||
* This checks the following locations & if a param is defined in multiple places, it prefers | ||
* whichever is higher on this list: | ||
* - extensions/{instanceId}.env.{projectID} | ||
* - extensions/{instanceId}.env.{projectNumber} | ||
* - extensions/{instanceId}.env.{projectAlias} | ||
* - extensions/{instanceId}.env | ||
*/ | ||
export function readParams(args: { | ||
projectDir: string; | ||
projectId: string; | ||
projectNumber: string; | ||
aliases: string[]; | ||
instanceId: string; | ||
}): Record<string, string> { | ||
const filesToCheck = [ | ||
`${args.instanceId}.env`, | ||
...args.aliases.map((alias) => `${args.instanceId}.env.${alias}`), | ||
`${args.instanceId}.env.${args.projectNumber}`, | ||
`${args.instanceId}.env.${args.projectId}`, | ||
]; | ||
let noFilesFound = true; | ||
const combinedParams = {}; | ||
for (const fileToCheck of filesToCheck) { | ||
try { | ||
const params = readParamsFile(args.projectDir, fileToCheck); | ||
logger.debug(`Successfully read params from ${fileToCheck}`); | ||
noFilesFound = false; | ||
Object.assign(combinedParams, params); | ||
} catch (err) { | ||
logger.debug(`${err}`); | ||
} | ||
} | ||
if (noFilesFound) { | ||
throw new FirebaseError(`No params file found for ${args.instanceId}`); | ||
} | ||
return combinedParams; | ||
} | ||
|
||
function readParamsFile(projectDir: string, fileName: string): Record<string, string> { | ||
const paramPath = path.join(projectDir, ENV_DIRECTORY, fileName); | ||
const params = readEnvFile(paramPath); | ||
return params as Record<string, string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { expect } from "chai"; | ||
import * as sinon from "sinon"; | ||
|
||
import * as params from "../../../deploy/extensions/params"; | ||
import * as paramHelper from "../../../extensions/paramHelper"; | ||
|
||
describe("readParams", () => { | ||
let readEnvFileStub: sinon.SinonStub; | ||
const testProjectDir = "test"; | ||
const testProjectId = "my-project"; | ||
const testProjectNumber = "123456"; | ||
const testInstanceId = "extensionId"; | ||
|
||
beforeEach(() => { | ||
readEnvFileStub = sinon.stub(paramHelper, "readEnvFile").returns({}); | ||
}); | ||
|
||
afterEach(() => { | ||
readEnvFileStub.restore(); | ||
}); | ||
|
||
it("should read from generic .env file", () => { | ||
readEnvFileStub | ||
.withArgs("test/extensions/extensionId.env") | ||
.returns({ param: "otherValue", param2: "value2" }); | ||
|
||
expect( | ||
params.readParams({ | ||
projectDir: testProjectDir, | ||
instanceId: testInstanceId, | ||
projectId: testProjectId, | ||
projectNumber: testProjectNumber, | ||
aliases: [], | ||
}) | ||
).to.deep.equal({ param: "otherValue", param2: "value2" }); | ||
}); | ||
|
||
it("should read from project id .env file", () => { | ||
readEnvFileStub | ||
.withArgs("test/extensions/extensionId.env.my-project") | ||
.returns({ param: "otherValue", param2: "value2" }); | ||
|
||
expect( | ||
params.readParams({ | ||
projectDir: testProjectDir, | ||
instanceId: testInstanceId, | ||
projectId: testProjectId, | ||
projectNumber: testProjectNumber, | ||
aliases: [], | ||
}) | ||
).to.deep.equal({ param: "otherValue", param2: "value2" }); | ||
}); | ||
|
||
it("should read from project number .env file", () => { | ||
readEnvFileStub | ||
.withArgs("test/extensions/extensionId.env.123456") | ||
.returns({ param: "otherValue", param2: "value2" }); | ||
|
||
expect( | ||
params.readParams({ | ||
projectDir: testProjectDir, | ||
instanceId: testInstanceId, | ||
projectId: testProjectId, | ||
projectNumber: testProjectNumber, | ||
aliases: [], | ||
}) | ||
).to.deep.equal({ param: "otherValue", param2: "value2" }); | ||
}); | ||
|
||
it("should read from an alias .env file", () => { | ||
readEnvFileStub | ||
.withArgs("test/extensions/extensionId.env.prod") | ||
.returns({ param: "otherValue", param2: "value2" }); | ||
|
||
expect( | ||
params.readParams({ | ||
projectDir: testProjectDir, | ||
instanceId: testInstanceId, | ||
projectId: testProjectId, | ||
projectNumber: testProjectNumber, | ||
aliases: ["prod"], | ||
}) | ||
).to.deep.equal({ param: "otherValue", param2: "value2" }); | ||
}); | ||
|
||
it("should prefer values from project specific env files", () => { | ||
readEnvFileStub | ||
.withArgs("test/extensions/extensionId.env.my-project") | ||
.returns({ param: "value" }); | ||
readEnvFileStub | ||
.withArgs("test/extensions/extensionId.env") | ||
.returns({ param: "otherValue", param2: "value2" }); | ||
|
||
expect( | ||
params.readParams({ | ||
projectDir: testProjectDir, | ||
instanceId: testInstanceId, | ||
projectId: testProjectId, | ||
projectNumber: testProjectNumber, | ||
aliases: [], | ||
}) | ||
).to.deep.equal({ param: "value", param2: "value2" }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters