-
Notifications
You must be signed in to change notification settings - Fork 970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encapsulate runtime-dependent code #3454
Conversation
@@ -79,7 +78,6 @@ module.exports = new Command("deploy") | |||
} | |||
}) | |||
.before(checkValidTargetFilters) | |||
.before(checkFunctionsSDKVersion) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This check was redundant with other functions deploy code. It's now in deploy/functions/runtimes/node/index.ts:Delegate#validate
@@ -116,18 +115,6 @@ export function memoryOptionDisplayName(option: MemoryOptions): string { | |||
|
|||
export const SCHEDULED_FUNCTION_LABEL = Object.freeze({ deployment: "firebase-schedule" }); | |||
|
|||
/** Supported runtimes for new Cloud Functions. */ | |||
export type Runtime = "nodejs10" | "nodejs12" | "nodejs14"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
de-duplciated in deploy/functions/runtimes
clc.bold("npm i --save firebase-functions@latest") + | ||
" in the functions folder."; | ||
|
||
function functionsSDKTooOld(sourceDir: string, minRange: string): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check moved to versioning.ts
@@ -49,41 +46,6 @@ export function functionIdsAreValid(functions: { id: string }[]): void { | |||
} | |||
} | |||
|
|||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to deploy/functions/runtimes/node/validate.ts
let SDKVersionStub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
cjsonStub = sandbox.stub(cjson, "load"); | ||
warningSpy = sandbox.spy(utils, "logWarning"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These parts of tests are moved to /test/deploy/functions/runtimes/node/versioning.spec.ts
|
||
const cjson = require("cjson"); | ||
|
||
describe("validate", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests ripped from test/deploy/functions/validate.spec.ts
Node JS code is now behind /deploy/functions/runtimes/node. The rest of the codebase now only interacts with the runtime through a RuntimeDelegate interface exposed in /deploy/functions/runtimes. All Runtime specific code from backend.ts (and functions API code + pacakge.json parser) has been deduplicated in runtimes/index.ts The following files/functions have been renamed: checkFirebaseSDKVersions.ts -> deploy/functions/runtimes/node/versioning.ts deploy/functions/discovery/jsexports/* -> deploy/functions/runtimes/node deploy/functions/checkRuntimeDependencies.ts -> deploy/functions/ensureCloudBuildEnabled.ts deploy/functions/validate.ts -> deploy/functions/runtimes/node/validate.ts (Node methods only) Some code has been refactored to be more coehsive or to remove redundancies: 1. Multiple places loaded the current firebase-functions SDK. This is now called by the RuntimeDelegate and passed where it is needed 2. Context no longer needs anything to do with runtimes. All Runtime-dependent code is handled in prepare 3. prepareFunctionsUplaod did a _lot_ more than just pacakaging source. It was previously getting the backend (and storing it in context so it could be reused), getting env vars, getting runtime config, etc. It now just uploads source and everything else is in prepare direclty. 4. The min-SDK version check has been moved from getRuntimeChoice() to a versioning.ts:checkFunctionsSDKVersion, which always did other checks. This saved an exec to npm. Some small fixes/improvements along the way: 1. The log line we print when a runtime is not specified now tells customers to use the latest nodejs runtime 2. We warn customers of breaking changes for any breaking change, not just 0.x to 1.x.
180a16d
to
5a947b3
Compare
src/deploy/functions/args.ts
Outdated
@@ -2,6 +2,7 @@ import { ReadStream } from "fs"; | |||
|
|||
import * as backend from "./backend"; | |||
import * as gcfV2 from "../../gcp/cloudfunctionsv2"; | |||
import * as runtimes from "./runtimes"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, with a few test and logging nits.
): void { | ||
const packageJsonFile = path.join(sourceDir, "package.json"); | ||
if (!fsutils.fileExistsSync(packageJsonFile)) { | ||
const msg = `No npm package found in functions source directory. Please run 'npm init' inside ${sourceDirName}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this error message should recommend running npm init, for 2 reasons:
- I think the most common cause of this would be having an incorrect functions.source listed in firebase.json
- In general, we should push people toward using firebase init over npm init.
I'd suggest changing this toNo npm package found in functions source directory '${sourceDirName}'
await expect(checkRuntimeDependencies("test-project", runtime)).to.eventually.be.rejected; | ||
}); | ||
}); | ||
await expect(ensureCloudBuildEnabled("test-project")).to.eventually.be.rejected; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit since you weren't really changing this code: This probably should be calling rejectedWith() and asserting that we throw an error that mentions billing. Same applies for the permissions error test
it("Should give an upgrade warning", () => { | ||
latestVersion.returns("5.0.1"); | ||
versioning.checkFunctionsSDKVersion("5.0.0"); | ||
expect(warningSpy).to.have.been.calledWith(sinon.match("Please upgrade")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit - This test could also assert that the breaking change warning isn't shown
Node JS code is now behind /deploy/functions/runtimes/node. The rest of the codebase now only interacts with the runtime through a RuntimeDelegate interface exposed in /deploy/functions/runtimes. All Runtime specific code from backend.ts (and functions API code + pacakge.json parser) has been deduplicated in runtimes/index.ts The following files/functions have been renamed: checkFirebaseSDKVersions.ts -> deploy/functions/runtimes/node/versioning.ts deploy/functions/discovery/jsexports/* -> deploy/functions/runtimes/node deploy/functions/checkRuntimeDependencies.ts -> deploy/functions/ensureCloudBuildEnabled.ts deploy/functions/validate.ts -> deploy/functions/runtimes/node/validate.ts (Node methods only) Some code has been refactored to be more coehsive or to remove redundancies: 1. Multiple places loaded the current firebase-functions SDK. This is now called by the RuntimeDelegate and passed where it is needed 2. Context no longer needs anything to do with runtimes. All Runtime-dependent code is handled in prepare 3. prepareFunctionsUplaod did a _lot_ more than just pacakaging source. It was previously getting the backend (and storing it in context so it could be reused), getting env vars, getting runtime config, etc. It now just uploads source and everything else is in prepare direclty. 4. The min-SDK version check has been moved from getRuntimeChoice() to a versioning.ts:checkFunctionsSDKVersion, which always did other checks. This saved an exec to npm. Some small fixes/improvements along the way: 1. The log line we print when a runtime is not specified now tells customers to use the latest nodejs runtime 2. We warn customers of breaking changes for any breaking change, not just 0.x to 1.x.
Node JS code is now behind /deploy/functions/runtimes/node. The rest of the codebase now only interacts with the runtime through a RuntimeDelegate interface exposed in /deploy/functions/runtimes.
The following files/functions have been renamed:
checkFirebaseSDKVersions.ts
deploy/functions/runtimes/node/versioning.ts
deploy/functions/discovery/jsexports/*
deploy/functions/runtimes/node
deploy/functions/checkRuntimeDependencies.ts
deploy/functions/ensureCloudBuildEnabled.ts
deploy/functions/validate.ts
deploy/functions/runtimes/node/validate.ts
(Node methods only)Some code has been refactored to be more coehsive or to remove
redundancies:
prepareFunctionsUplaod
did a lot more than just packaging source. It was previously getting the backend (and storing it in context so it could be reused), getting env vars, getting runtime config, etc. It now just uploads source and everything else is inprepare
directly.checkFunctionsSDKVersion
inversioning.ts
, which always did other checks. This saved an exec tonpm
.Some small fixes/improvements along the way: