|
| 1 | +import { execSync } from "child_process"; |
| 2 | +import { getVersion } from "./getT3Version.js"; |
| 3 | +import { logger } from "./logger.js"; |
| 4 | + |
| 5 | +export const renderVersionWarning = (npmVersion: string) => { |
| 6 | + const currentVersion = getVersion(); |
| 7 | + |
| 8 | + // console.log("current", currentVersion); |
| 9 | + // console.log("npm", npmVersion); |
| 10 | + |
| 11 | + if (currentVersion.includes("beta")) { |
| 12 | + logger.warn(" You are using a beta version of create-t3-app."); |
| 13 | + logger.warn(" Please report any bugs you encounter."); |
| 14 | + } else if (currentVersion.includes("next")) { |
| 15 | + logger.warn( |
| 16 | + " You are running create-t3-app with the @next tag which is no longer maintained.", |
| 17 | + ); |
| 18 | + logger.warn(" Please run the CLI with @latest instead."); |
| 19 | + } else if (currentVersion !== npmVersion) { |
| 20 | + logger.warn(" You are using an outdated version of create-t3-app."); |
| 21 | + logger.warn( |
| 22 | + " Your version:", |
| 23 | + currentVersion + ".", |
| 24 | + "Latest version in the npm registry:", |
| 25 | + npmVersion, |
| 26 | + ); |
| 27 | + logger.warn(" Please run the CLI with @latest to get the latest updates."); |
| 28 | + } |
| 29 | + console.log(""); |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 34 | + * |
| 35 | + * This source code is licensed under the MIT license found in the |
| 36 | + * LICENSE file in the root directory of this source tree. |
| 37 | + * https://github.com/facebook/create-react-app/blob/main/packages/create-react-app/LICENSE |
| 38 | + */ |
| 39 | +import https from "https"; |
| 40 | +function checkForLatestVersion(): Promise<string> { |
| 41 | + return new Promise((resolve, reject) => { |
| 42 | + https |
| 43 | + .get( |
| 44 | + "https://registry.npmjs.org/-/package/create-t3-app/dist-tags", |
| 45 | + (res) => { |
| 46 | + if (res.statusCode === 200) { |
| 47 | + let body = ""; |
| 48 | + res.on("data", (data) => (body += data)); |
| 49 | + res.on("end", () => { |
| 50 | + resolve(JSON.parse(body).latest); |
| 51 | + }); |
| 52 | + } else { |
| 53 | + reject(); |
| 54 | + } |
| 55 | + }, |
| 56 | + ) |
| 57 | + .on("error", () => { |
| 58 | + // logger.error("Unable to check for latest version."); |
| 59 | + reject(); |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +export const getNpmVersion = () => |
| 65 | + // `fetch` to the registry is faster than `npm view` so we try that first |
| 66 | + checkForLatestVersion().catch(() => { |
| 67 | + try { |
| 68 | + return execSync("npm view create-t3-app version").toString().trim(); |
| 69 | + } catch { |
| 70 | + return null; |
| 71 | + } |
| 72 | + }); |
0 commit comments