Skip to content
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

Dynamically validate & set engines.node in package.json on firebase init functions #3894

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
21 changes: 19 additions & 2 deletions src/init/features/functions/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var path = require("path");

var npmDependencies = require("./npm-dependencies");
var { prompt } = require("../../../prompt");
var utils = require("../../../utils");
const { isValidRuntime } = require("../../../deploy/functions/runtimes/index");

var TEMPLATE_ROOT = path.resolve(__dirname, "../../../../templates/init/functions/javascript/");
var INDEX_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, "index.js"), "utf8");
Expand Down Expand Up @@ -33,12 +35,12 @@ module.exports = function (setup, config) {
if (setup.functions.lint) {
_.set(setup, "config.functions.predeploy", ['npm --prefix "$RESOURCE_DIR" run lint']);
return config
.askWriteProjectFile("functions/package.json", PACKAGE_LINTING_TEMPLATE)
.askWriteProjectFile("functions/package.json", getPackage(true))
.then(function () {
config.askWriteProjectFile("functions/.eslintrc.js", ESLINT_TEMPLATE);
});
}
return config.askWriteProjectFile("functions/package.json", PACKAGE_NO_LINTING_TEMPLATE);
return config.askWriteProjectFile("functions/package.json", getPackage(false));
})
.then(function () {
return config.askWriteProjectFile("functions/index.js", INDEX_TEMPLATE);
Expand All @@ -50,3 +52,18 @@ module.exports = function (setup, config) {
return npmDependencies.askInstallDependencies(setup.functions, config);
});
};

function getPackage(useLint) {
const nodeEngineVersion = utils.getNodeVersionString();
if (!isValidRuntime(`nodejs${nodeEngineVersion}`)) {
utils.logWarning(`Node ${nodeEngineVersion} is no longer supported in Google Cloud Functions.`);
utils.logWarning(
"See https://firebase.google.com/docs/functions/manage-functions for more details"
);
}

if (useLint) {
return PACKAGE_LINTING_TEMPLATE.replace(/{{NODE_VERSION}}/g, nodeEngineVersion);
}
return PACKAGE_NO_LINTING_TEMPLATE.replace(/{{NODE_VERSION}}/g, nodeEngineVersion);
}
21 changes: 19 additions & 2 deletions src/init/features/functions/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var path = require("path");

var npmDependencies = require("./npm-dependencies");
var { prompt } = require("../../../prompt");
var utils = require("../../../utils");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are only using getNodeVersionString, I think it make sense to do something like

var { getNodeVersionString}  = require("../../../utils");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intend to use logWarning() from utils to warn users of deprecated Node versions (like this code below):

if (!options.hashAlgo) {
utils.logWarning("No hash algorithm specified. Password users cannot be imported.");
return { valid: true };
}

In that case, should I keep it that way?

const { isValidRuntime } = require("../../../deploy/functions/runtimes/index");

var TEMPLATE_ROOT = path.resolve(__dirname, "../../../../templates/init/functions/typescript/");
var PACKAGE_LINTING_TEMPLATE = fs.readFileSync(
Expand Down Expand Up @@ -38,13 +40,13 @@ module.exports = function (setup, config) {
'npm --prefix "$RESOURCE_DIR" run build',
]);
return config
.askWriteProjectFile("functions/package.json", PACKAGE_LINTING_TEMPLATE)
.askWriteProjectFile("functions/package.json", getPackage(true))
.then(function () {
return config.askWriteProjectFile("functions/.eslintrc.js", ESLINT_TEMPLATE);
});
}
_.set(setup, "config.functions.predeploy", 'npm --prefix "$RESOURCE_DIR" run build');
return config.askWriteProjectFile("functions/package.json", PACKAGE_NO_LINTING_TEMPLATE);
return config.askWriteProjectFile("functions/package.json", getPackage(false));
})
.then(function () {
return config.askWriteProjectFile("functions/tsconfig.json", TSCONFIG_TEMPLATE);
Expand All @@ -64,3 +66,18 @@ module.exports = function (setup, config) {
return npmDependencies.askInstallDependencies(setup.functions, config);
});
};

function getPackage(useLint) {
const nodeEngineVersion = utils.getNodeVersionString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do we ever check that the user's node version is one supported by Google Cloud Functions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taeold I was thinking of using isDeprecatedRuntime() to check for deprecated node version and utils.logWarning() to display the warning message. Something like this:

if (isDeprecatedRuntime(`nodejs${nodeEngineVersion}`)) {
    utils.logWarning("Your Node version is not supported in Google Cloud Functions.");
}

If it's good enough, I'll go ahead and update the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼 Tiny bug I'd watch out for is that something like nodejs13, which is not supported by Google Cloud Functions, may pass the isDeprecatedRuntime() check. You might to throw in isValidRuntime check somewhere in the code.

if (!isValidRuntime(`nodejs${nodeEngineVersion}`)) {
utils.logWarning(`Node ${nodeEngineVersion} is no longer supported in Google Cloud Functions.`);
utils.logWarning(
"See https://firebase.google.com/docs/functions/manage-functions for more details"
);
}

if (useLint) {
return PACKAGE_LINTING_TEMPLATE.replace(/{{NODE_VERSION}}/g, nodeEngineVersion);
}
return PACKAGE_NO_LINTING_TEMPLATE.replace(/{{NODE_VERSION}}/g, nodeEngineVersion);
}
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,14 @@ export function thirtyDaysFromNow(): Date {
return new Date(Date.now() + THIRTY_DAYS_IN_MILLISECONDS);
}

/**
* Get the current version of Node.js engine from `process.version`.
* @return Node.js major release version
*/
export function getNodeVersionString(): string {
return process.versions.node.split(".")[0];
}

/**
* See:
* https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
Expand Down
2 changes: 1 addition & 1 deletion templates/init/functions/javascript/package.lint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
"node": "{{NODE_VERSION}}"
},
"main": "index.js",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion templates/init/functions/javascript/package.nolint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
"node": "{{NODE_VERSION}}"
},
"main": "index.js",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion templates/init/functions/typescript/package.lint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
"node": "{{NODE_VERSION}}"
},
"main": "lib/index.js",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion templates/init/functions/typescript/package.nolint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
"node": "{{NODE_VERSION}}"
},
"main": "lib/index.js",
"dependencies": {
Expand Down