forked from t3-oss/create-t3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderVersionWarning.ts
76 lines (70 loc) · 2.3 KB
/
renderVersionWarning.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
import { getVersion } from "./getT3Version.js";
import { logger } from "./logger.js";
import { execSync } from "child_process";
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the LICENSE file in the root
* directory of this source tree.
* https://github.com/facebook/create-react-app/blob/main/packages/create-react-app/LICENSE
*/
import https from "https";
export const renderVersionWarning = (npmVersion: string) => {
const currentVersion = getVersion();
// console.log("current", currentVersion);
// console.log("npm", npmVersion);
if (currentVersion.includes("beta")) {
logger.warn(" You are using a beta version of create-t3-app.");
logger.warn(" Please report any bugs you encounter.");
} else if (currentVersion.includes("next")) {
logger.warn(
" You are running create-t3-app with the @next tag which is no longer maintained.",
);
logger.warn(" Please run the CLI with @latest instead.");
} else if (currentVersion !== npmVersion) {
logger.warn(" You are using an outdated version of create-t3-app.");
logger.warn(
" Your version:",
currentVersion + ".",
"Latest version in the npm registry:",
npmVersion,
);
logger.warn(" Please run the CLI with @latest to get the latest updates.");
}
console.log("");
};
type DistTagsBody = {
latest: string;
};
function checkForLatestVersion(): Promise<string> {
return new Promise((resolve, reject) => {
https
.get(
"https://registry.npmjs.org/-/package/create-t3-app/dist-tags",
(res) => {
if (res.statusCode === 200) {
let body = "";
res.on("data", (data) => (body += data));
res.on("end", () => {
resolve((JSON.parse(body) as DistTagsBody).latest);
});
} else {
reject();
}
},
)
.on("error", () => {
// logger.error("Unable to check for latest version.");
reject();
});
});
}
export const getNpmVersion = () =>
// `fetch` to the registry is faster than `npm view` so we try that first
checkForLatestVersion().catch(() => {
try {
return execSync("npm view create-t3-app version").toString().trim();
} catch {
return null;
}
});