diff --git a/package.json b/package.json index 44f4e42..8283315 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "ts-loader": "^9.3.0", "tsconfig-paths-webpack-plugin": "^3.5.2", "typescript": "^4.7.3", + "update-check": "^1.5.4", "webpack": "^5.73.0", "webpack-cli": "^4.9.2" } diff --git a/src/index.ts b/src/index.ts index c1cf439..c7abcff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,17 +1,41 @@ import path from "path"; import fs from "fs/promises"; +import chalk from "chalk"; +import updateCheck from "update-check"; import { Command, Option } from "commander"; import create from "./create"; -async function index() { - const program = new Command(); - +async function getPkg() { const pkgJson = await fs.readFile( path.resolve(__dirname, "..", "package.json"), "utf8" ); - const pkg = JSON.parse(pkgJson); + return JSON.parse(pkgJson); +} + +async function checkForUpdates(): Promise { + const pkg = await getPkg(); + if (pkg.version === "0.0.0-SNAPSHOT") return; + const res = await updateCheck(pkg); + if (res?.latest) { + console.log( + `\n${chalk.yellow.bold( + `A new version of ${chalk.green.bold( + pkg.name + )} is available and required!` + )}\n\nRun ${chalk.cyan( + `yarn global add ${pkg.name}` + )} to update then try again.\n` + ); + process.exit(1); + } +} + +async function run() { + const program = new Command(); + + const pkg = await getPkg(); program .name(pkg.name) @@ -35,7 +59,9 @@ async function index() { await program.parseAsync(); } -index().catch((err) => { - console.error(err); - process.exit(1); -}); +checkForUpdates() + .then(run) + .catch((err) => { + console.error(err); + process.exit(1); + }); diff --git a/yarn.lock b/yarn.lock index a4c0b95..633a5ab 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4365,7 +4365,7 @@ randombytes@^2.1.0: dependencies: safe-buffer "^5.1.0" -rc@^1.2.8: +rc@^1.0.1, rc@^1.1.6, rc@^1.2.8: version "1.2.8" resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed" integrity sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw== @@ -4478,6 +4478,14 @@ redeyed@~2.1.0: dependencies: esprima "~4.0.0" +registry-auth-token@3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/registry-auth-token/-/registry-auth-token-3.3.2.tgz#851fd49038eecb586911115af845260eec983f20" + integrity sha512-JL39c60XlzCVgNrO+qq68FoNb56w/m7JYvGR2jT5iR1xBrUA3Mfx5Twk5rqTThPmQKMWydGmq8oFtDlxfrmxnQ== + dependencies: + rc "^1.1.6" + safe-buffer "^5.0.1" + registry-auth-token@^4.0.0: version "4.2.1" resolved "https://registry.yarnpkg.com/registry-auth-token/-/registry-auth-token-4.2.1.tgz#6d7b4006441918972ccd5fedcd41dc322c79b250" @@ -4485,6 +4493,13 @@ registry-auth-token@^4.0.0: dependencies: rc "^1.2.8" +registry-url@3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/registry-url/-/registry-url-3.1.0.tgz#3d4ef870f73dde1d77f0cf9a381432444e174942" + integrity sha512-ZbgR5aZEdf4UKZVBPYIgaglBmSF2Hi94s2PcIHhRGFjKYu+chjJdYfHn4rt3hB6eCKLJ8giVIIfgMa1ehDfZKA== + dependencies: + rc "^1.0.1" + repeat-string@^1.6.1: version "1.6.1" resolved "https://registry.yarnpkg.com/repeat-string/-/repeat-string-1.6.1.tgz#8dcae470e1c88abc2d600fff4a776286da75e637" @@ -4609,7 +4624,7 @@ rxjs@^7.5.5: dependencies: tslib "^2.1.0" -safe-buffer@^5.1.0, safe-buffer@~5.2.0: +safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@~5.2.0: version "5.2.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6" integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ== @@ -5283,6 +5298,14 @@ universalify@^2.0.0: resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717" integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ== +update-check@^1.5.4: + version "1.5.4" + resolved "https://registry.yarnpkg.com/update-check/-/update-check-1.5.4.tgz#5b508e259558f1ad7dbc8b4b0457d4c9d28c8743" + integrity sha512-5YHsflzHP4t1G+8WGPlvKbJEbAJGCgw+Em+dGR1KmBUbr1J36SJBqlHLjR7oob7sco5hWHGQVcr9B2poIVDDTQ== + dependencies: + registry-auth-token "3.3.2" + registry-url "3.1.0" + uri-js@^4.2.2: version "4.4.1" resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"