Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
fix: check for updates before run
Browse files Browse the repository at this point in the history
  • Loading branch information
phuctm97 committed Jun 13, 2022
1 parent 05fe4bd commit 025b1ea
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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"
}
Expand Down
42 changes: 34 additions & 8 deletions 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<void> {
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)
Expand All @@ -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);
});
27 changes: 25 additions & 2 deletions yarn.lock
Expand Up @@ -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==
Expand Down Expand Up @@ -4478,13 +4478,28 @@ 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"
integrity sha512-6gkSb4U6aWJB4SF2ZvLb76yCBjcvufXBqvvEx1HbmKPkutswjW1xNVRY0+daljIYRbogN7O0etYSlbiaEQyMyw==
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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 025b1ea

Please sign in to comment.