-
Notifications
You must be signed in to change notification settings - Fork 956
/
versioning.ts
116 lines (106 loc) · 3.52 KB
/
versioning.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as _ from "lodash";
import * as clc from "colorette";
import * as semver from "semver";
import * as spawn from "cross-spawn";
import * as utils from "../../../../utils";
import { logger } from "../../../../logger";
import { track } from "../../../../track";
interface NpmListResult {
name: string;
dependencies: {
"firebase-functions": {
version: string;
from: string;
resolved: string;
};
};
}
interface NpmShowResult {
"dist-tags": {
latest: string;
};
}
const MIN_SDK_VERSION = "2.0.0";
export const FUNCTIONS_SDK_VERSION_TOO_OLD_WARNING =
clc.bold(clc.yellow("functions: ")) +
"You must have a " +
clc.bold("firebase-functions") +
" version that is at least 2.0.0. Please run " +
clc.bold("npm i --save firebase-functions@latest") +
" in the functions folder.";
/**
* Returns the version of firebase-functions SDK specified by package.json and package-lock.json.
* @param sourceDir Source directory of functions code
* @return version string (e.g. "3.1.2"), or void if firebase-functions is not in package.json
* or if we had trouble getting the version.
*/
export function getFunctionsSDKVersion(sourceDir: string): string | void {
try {
const child = spawn.sync("npm", ["list", "firebase-functions", "--json=true"], {
cwd: sourceDir,
encoding: "utf8",
});
if (child.error) {
logger.debug("getFunctionsSDKVersion encountered error:", child.error.stack);
return;
}
const output: NpmListResult = JSON.parse(child.stdout);
return _.get(output, ["dependencies", "firebase-functions", "version"]);
} catch (e: any) {
logger.debug("getFunctionsSDKVersion encountered error:", e);
return;
}
}
export function getLatestSDKVersion(): string | undefined {
const child = spawn.sync("npm", ["show", "firebase-functions", "--json=true"], {
encoding: "utf8",
});
if (child.error) {
logger.debug(
"checkFunctionsSDKVersion was unable to fetch information from NPM",
child.error.stack
);
return;
}
const output: NpmShowResult = JSON.parse(child.stdout);
if (_.isEmpty(output)) {
return;
}
return _.get(output, ["dist-tags", "latest"]);
}
/**
* Checks if firebase-functions SDK is not the latest version in NPM, and prints update notice if it is outdated.
* If it is unable to do the check, it does nothing.
* @param sourceDir the location of the customer's source code.
*/
export function checkFunctionsSDKVersion(currentVersion: string): void {
try {
if (semver.lt(currentVersion, MIN_SDK_VERSION)) {
void track("functions_runtime_notices", "functions_sdk_too_old");
utils.logWarning(FUNCTIONS_SDK_VERSION_TOO_OLD_WARNING);
}
// N.B. We must use exports.getLatestSDKVersion so that the method dynamic and we can stub in tests.
const latest = exports.getLatestSDKVersion();
if (!latest) {
return;
}
if (semver.eq(currentVersion, latest)) {
return;
}
utils.logWarning(
clc.bold(clc.yellow("functions: ")) +
"package.json indicates an outdated version of firebase-functions. Please upgrade using " +
clc.bold("npm install --save firebase-functions@latest") +
" in your functions directory."
);
if (semver.major(currentVersion) < semver.major(latest)) {
utils.logWarning(
clc.bold(clc.yellow("functions: ")) +
"Please note that there will be breaking changes when you upgrade."
);
}
} catch (e: any) {
logger.debug("checkFunctionsSDKVersion encountered error:", e);
return;
}
}