@whatalo/cli-kit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,164 @@
1
+ // src/version/check.ts
2
+ import { readFile, writeFile, mkdir } from "fs/promises";
3
+ import path2 from "path";
4
+ import chalk from "chalk";
5
+
6
+ // src/session/store.ts
7
+ import fs from "fs/promises";
8
+ import os from "os";
9
+ import path from "path";
10
+ function getSessionDir() {
11
+ return path.join(os.homedir(), ".whatalo");
12
+ }
13
+
14
+ // src/version/check.ts
15
+ var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
16
+ var REGISTRY_TIMEOUT_MS = 3e3;
17
+ function getCachePath() {
18
+ return path2.join(getSessionDir(), "version-check.json");
19
+ }
20
+ async function readCache() {
21
+ try {
22
+ const raw = await readFile(getCachePath(), "utf-8");
23
+ const cache = JSON.parse(raw);
24
+ const lastCheck = new Date(cache.lastCheck).getTime();
25
+ if (Date.now() - lastCheck < CHECK_INTERVAL_MS) {
26
+ return cache;
27
+ }
28
+ return null;
29
+ } catch {
30
+ return null;
31
+ }
32
+ }
33
+ async function writeCache(cache) {
34
+ try {
35
+ const dir = getSessionDir();
36
+ await mkdir(dir, { recursive: true, mode: 448 });
37
+ await writeFile(getCachePath(), JSON.stringify(cache, null, 2), "utf-8");
38
+ } catch {
39
+ }
40
+ }
41
+ async function fetchLatestVersion(packageName) {
42
+ try {
43
+ const res = await fetch(
44
+ `https://registry.npmjs.org/${packageName}/latest`,
45
+ { signal: AbortSignal.timeout(REGISTRY_TIMEOUT_MS) }
46
+ );
47
+ if (!res.ok) return null;
48
+ const data = await res.json();
49
+ return data.version ?? null;
50
+ } catch {
51
+ return null;
52
+ }
53
+ }
54
+ function isNewerVersion(current, latest) {
55
+ const parse = (v) => v.replace(/^v/, "").split(".").map(Number);
56
+ const c = parse(current);
57
+ const l = parse(latest);
58
+ for (let i = 0; i < 3; i++) {
59
+ if ((l[i] ?? 0) > (c[i] ?? 0)) return true;
60
+ if ((l[i] ?? 0) < (c[i] ?? 0)) return false;
61
+ }
62
+ return false;
63
+ }
64
+ function getUpgradeCommand() {
65
+ const pm = detectPackageManager();
66
+ switch (pm) {
67
+ case "pnpm":
68
+ return "pnpm add -g @whatalo/cli";
69
+ case "yarn":
70
+ return "yarn global add @whatalo/cli";
71
+ case "npm":
72
+ return "npm install -g @whatalo/cli";
73
+ }
74
+ }
75
+ function detectPackageManager() {
76
+ const userAgent = process.env.npm_config_user_agent ?? "";
77
+ if (userAgent.includes("pnpm")) return "pnpm";
78
+ if (userAgent.includes("yarn")) return "yarn";
79
+ if (userAgent.includes("npm")) return "npm";
80
+ return "pnpm";
81
+ }
82
+ function scheduleVersionCheck(currentVersion) {
83
+ const checkPromise = (async () => {
84
+ const cached = await readCache();
85
+ if (cached && cached.currentVersion === currentVersion) {
86
+ return cached.latestVersion !== currentVersion && isNewerVersion(currentVersion, cached.latestVersion) ? cached.latestVersion : null;
87
+ }
88
+ const latestVersion = await fetchLatestVersion("@whatalo/cli");
89
+ if (!latestVersion) return null;
90
+ await writeCache({
91
+ lastCheck: (/* @__PURE__ */ new Date()).toISOString(),
92
+ latestVersion,
93
+ currentVersion
94
+ });
95
+ return isNewerVersion(currentVersion, latestVersion) ? latestVersion : null;
96
+ })();
97
+ process.on("exit", () => {
98
+ checkPromise.then((latestVersion) => {
99
+ if (!latestVersion) return;
100
+ const upgradeCmd = getUpgradeCommand();
101
+ const box = [
102
+ "",
103
+ chalk.yellow("\u256D\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256E"),
104
+ chalk.yellow("\u2502 \u2502"),
105
+ chalk.yellow("\u2502") + ` Update available: ${chalk.dim(currentVersion)} ${chalk.yellow("\u2192")} ${chalk.green(latestVersion)} ` + chalk.yellow("\u2502"),
106
+ chalk.yellow("\u2502") + ` Run: ${chalk.cyan(upgradeCmd)} ` + chalk.yellow("\u2502"),
107
+ chalk.yellow("\u2502 \u2502"),
108
+ chalk.yellow("\u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256F"),
109
+ ""
110
+ ];
111
+ for (const line of box) {
112
+ process.stderr.write(line + "\n");
113
+ }
114
+ }).catch(() => {
115
+ });
116
+ });
117
+ }
118
+
119
+ // src/version/compatibility.ts
120
+ import { readFileSync } from "fs";
121
+ import path3 from "path";
122
+ function checkSdkCompatibility(cliVersion, projectDir) {
123
+ const sdkVersion = getSdkVersion(projectDir);
124
+ if (!sdkVersion) return null;
125
+ const cliMajor = parseMajor(cliVersion);
126
+ const sdkMajor = parseMajor(sdkVersion);
127
+ if (cliMajor !== sdkMajor) {
128
+ return `SDK version ${sdkVersion} may not be compatible with CLI ${cliVersion}. Run: pnpm update @whatalo/app-sdk`;
129
+ }
130
+ const cliMinor = parseMinor(cliVersion);
131
+ const sdkMinor = parseMinor(sdkVersion);
132
+ if (cliMinor - sdkMinor > 1) {
133
+ return `SDK version ${sdkVersion} is behind CLI ${cliVersion}. Run: pnpm update @whatalo/app-sdk`;
134
+ }
135
+ return null;
136
+ }
137
+ function getSdkVersion(projectDir) {
138
+ try {
139
+ const pkgPath = path3.join(
140
+ projectDir,
141
+ "node_modules",
142
+ "@whatalo",
143
+ "app-sdk",
144
+ "package.json"
145
+ );
146
+ const raw = readFileSync(pkgPath, "utf-8");
147
+ const pkg = JSON.parse(raw);
148
+ return pkg.version ?? null;
149
+ } catch {
150
+ return null;
151
+ }
152
+ }
153
+ function parseMajor(version) {
154
+ return parseInt(version.replace(/^v/, "").split(".")[0] ?? "0", 10);
155
+ }
156
+ function parseMinor(version) {
157
+ return parseInt(version.replace(/^v/, "").split(".")[1] ?? "0", 10);
158
+ }
159
+ export {
160
+ checkSdkCompatibility,
161
+ getUpgradeCommand,
162
+ isNewerVersion,
163
+ scheduleVersionCheck
164
+ };
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "@whatalo/cli-kit",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "description": "Shared CLI utilities for Whatalo plugin development tools",
7
+ "author": {
8
+ "name": "Bello Sanchez",
9
+ "email": "bello@whatalo.com",
10
+ "url": "https://github.com/bellopushon"
11
+ },
12
+ "homepage": "https://developers.whatalo.com",
13
+ "bugs": {
14
+ "email": "soporte@whatalo.com"
15
+ },
16
+ "keywords": [
17
+ "whatalo",
18
+ "cli",
19
+ "plugin",
20
+ "developer-tools"
21
+ ],
22
+ "main": "./dist/index.cjs",
23
+ "module": "./dist/index.mjs",
24
+ "types": "./dist/index.d.ts",
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "sideEffects": false,
29
+ "engines": {
30
+ "node": ">=18"
31
+ },
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "import": "./dist/index.mjs",
36
+ "require": "./dist/index.cjs"
37
+ },
38
+ "./session": {
39
+ "types": "./dist/session/index.d.ts",
40
+ "import": "./dist/session/index.mjs",
41
+ "require": "./dist/session/index.cjs"
42
+ },
43
+ "./output": {
44
+ "types": "./dist/output/index.d.ts",
45
+ "import": "./dist/output/index.mjs",
46
+ "require": "./dist/output/index.cjs"
47
+ },
48
+ "./http": {
49
+ "types": "./dist/http/index.d.ts",
50
+ "import": "./dist/http/index.mjs",
51
+ "require": "./dist/http/index.cjs"
52
+ },
53
+ "./config": {
54
+ "types": "./dist/config/index.d.ts",
55
+ "import": "./dist/config/index.mjs",
56
+ "require": "./dist/config/index.cjs"
57
+ },
58
+ "./tunnel": {
59
+ "types": "./dist/tunnel/index.d.ts",
60
+ "import": "./dist/tunnel/index.mjs",
61
+ "require": "./dist/tunnel/index.cjs"
62
+ },
63
+ "./version": {
64
+ "types": "./dist/version/index.d.ts",
65
+ "import": "./dist/version/index.mjs",
66
+ "require": "./dist/version/index.cjs"
67
+ }
68
+ },
69
+ "files": [
70
+ "dist",
71
+ "README.md",
72
+ "LICENSE"
73
+ ],
74
+ "scripts": {
75
+ "build": "tsup",
76
+ "dev": "tsup --watch",
77
+ "test": "vitest run",
78
+ "test:watch": "vitest",
79
+ "type-check": "tsc --noEmit",
80
+ "clean": "rm -rf dist"
81
+ },
82
+ "dependencies": {
83
+ "@iarna/toml": "^2.2.5",
84
+ "chalk": "^5.4.1",
85
+ "esbuild": "^0.27.3",
86
+ "zod": "^4.3.6"
87
+ },
88
+ "devDependencies": {
89
+ "@types/iarna__toml": "^2.0.5",
90
+ "@types/node": "^24.10.0",
91
+ "tsup": "^8.5.0",
92
+ "typescript": "^5.9.3",
93
+ "vitest": "^3.2.4"
94
+ }
95
+ }