@dhruv2mars/mdv 0.0.17 → 0.0.19

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.
@@ -32,6 +32,12 @@ export function packageManagerHintFromEnv(env = process.env) {
32
32
  return null;
33
33
  }
34
34
 
35
+ export function shouldInstallBinary({ binExists, installedVersion, packageVersion }) {
36
+ if (!binExists) return true;
37
+ if (typeof packageVersion !== 'string' || packageVersion.length === 0) return false;
38
+ return installedVersion !== packageVersion;
39
+ }
40
+
35
41
  function parseIntEnv(value, fallback, min, max) {
36
42
  const parsed = Number.parseInt(String(value ?? ''), 10);
37
43
  if (!Number.isFinite(parsed)) return fallback;
package/bin/install.js CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  packageManagerHintFromEnv,
28
28
  parseChecksumForAsset,
29
29
  resolveReleaseAssetBundle,
30
+ shouldInstallBinary,
30
31
  shouldUseFallbackUrl
31
32
  } from './install-lib.js';
32
33
 
@@ -44,13 +45,16 @@ const backoffMs = tuning.backoffMs;
44
45
  const backoffJitterMs = tuning.backoffJitterMs;
45
46
  const debugEnabled = process.env.MDV_INSTALL_DEBUG === '1';
46
47
  const installStartedAt = Date.now();
48
+ const version = pkgVersion();
49
+ const installedVersion = readInstalledVersion(metaPath);
47
50
 
48
51
  if (process.env.MDV_SKIP_DOWNLOAD === '1') process.exit(0);
49
- if (existsSync(dest)) process.exit(0);
52
+ if (!shouldInstallBinary({ binExists: existsSync(dest), installedVersion, packageVersion: version })) {
53
+ process.exit(0);
54
+ }
50
55
 
51
56
  mkdirSync(binDir, { recursive: true });
52
57
 
53
- const version = pkgVersion();
54
58
  const asset = assetNameFor();
55
59
  const checksumsAsset = checksumsAssetNameFor();
56
60
  const cachePaths = cachePathsFor(installRoot, version, asset, checksumsAsset);
@@ -116,6 +120,17 @@ function pkgVersion() {
116
120
  }
117
121
  }
118
122
 
123
+ function readInstalledVersion(path) {
124
+ if (!existsSync(path)) return null;
125
+ try {
126
+ const meta = JSON.parse(readFileSync(path, 'utf8'));
127
+ const version = meta?.version;
128
+ return typeof version === 'string' && version.length > 0 ? version : null;
129
+ } catch {
130
+ return null;
131
+ }
132
+ }
133
+
119
134
  function download(url, outPath) {
120
135
  return downloadWithRedirects(url, outPath, 0);
121
136
  }
package/bin/mdv-lib.js CHANGED
@@ -2,11 +2,13 @@ import { existsSync, readFileSync } from 'node:fs';
2
2
  import { homedir } from 'node:os';
3
3
  import { join } from 'node:path';
4
4
  import { spawnSync } from 'node:child_process';
5
- import { packageManagerHintFromEnv } from './install-lib.js';
5
+ import { packageManagerHintFromEnv, shouldInstallBinary } from './install-lib.js';
6
6
 
7
7
  const PACKAGE_NAME = '@dhruv2mars/mdv@latest';
8
8
  const SUPPORTED_PMS = new Set(['bun', 'pnpm', 'yarn', 'npm']);
9
9
 
10
+ export { shouldInstallBinary };
11
+
10
12
  export function binNameForPlatform(platform = process.platform) {
11
13
  return platform === 'win32' ? 'mdv.exe' : 'mdv';
12
14
  }
@@ -15,6 +17,10 @@ export function resolveInstallRoot(env = process.env, home = homedir()) {
15
17
  return env.MDV_INSTALL_ROOT || join(home, '.mdv');
16
18
  }
17
19
 
20
+ export function resolveInstallMetaPath(env = process.env, home = homedir()) {
21
+ return join(resolveInstallRoot(env, home), 'install-meta.json');
22
+ }
23
+
18
24
  export function resolveInstalledBin(env = process.env, platform = process.platform, home = homedir()) {
19
25
  const installRoot = resolveInstallRoot(env, home);
20
26
  const binName = binNameForPlatform(platform);
@@ -32,8 +38,8 @@ function updateArgsFor(pm) {
32
38
  return ['install', '-g', PACKAGE_NAME];
33
39
  }
34
40
 
35
- function readInstallMeta(installRoot) {
36
- const path = join(installRoot, 'install-meta.json');
41
+ export function readInstallMeta(env = process.env, home = homedir()) {
42
+ const path = resolveInstallMetaPath(env, home);
37
43
  if (!existsSync(path)) return null;
38
44
  try {
39
45
  return JSON.parse(readFileSync(path, 'utf8'));
@@ -42,6 +48,11 @@ function readInstallMeta(installRoot) {
42
48
  }
43
49
  }
44
50
 
51
+ export function resolveInstalledVersion(env = process.env, home = homedir()) {
52
+ const version = readInstallMeta(env, home)?.version;
53
+ return typeof version === 'string' && version.length > 0 ? version : null;
54
+ }
55
+
45
56
  function isSupportedPm(pm) {
46
57
  return typeof pm === 'string' && SUPPORTED_PMS.has(pm);
47
58
  }
@@ -82,8 +93,7 @@ export function detectInstalledPackageManager(probe = defaultProbe, preferred =
82
93
  }
83
94
 
84
95
  export function resolveUpdateCommand(env = process.env) {
85
- const installRoot = resolveInstallRoot(env);
86
- const metaPm = readInstallMeta(installRoot)?.packageManager;
96
+ const metaPm = readInstallMeta(env)?.packageManager;
87
97
  const envPm = packageManagerHintFromEnv(env);
88
98
  const hintPm = isSupportedPm(metaPm) ? metaPm : (isSupportedPm(envPm) ? envPm : null);
89
99
  const detectedPm = env === process.env && !hintPm
package/bin/mdv.js CHANGED
@@ -1,11 +1,13 @@
1
1
  #!/usr/bin/env node
2
- import { existsSync } from 'node:fs';
2
+ import { existsSync, readFileSync } from 'node:fs';
3
3
  import { spawnSync } from 'node:child_process';
4
4
  import { join } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
6
  import {
7
+ resolveInstalledVersion,
7
8
  resolveInstalledBin,
8
9
  resolveUpdateCommand,
10
+ shouldInstallBinary,
9
11
  shouldRunUpdateCommand
10
12
  } from './mdv-lib.js';
11
13
 
@@ -21,12 +23,24 @@ const envBin = process.env.MDV_BIN;
21
23
  if (envBin) run(envBin, args);
22
24
 
23
25
  const installedBin = resolveInstalledBin(process.env, process.platform);
26
+ const packageVersion = readPackageVersion();
27
+ const binExists = existsSync(installedBin);
28
+ const installedVersion = resolveInstalledVersion(process.env);
24
29
 
25
- if (!existsSync(installedBin)) {
30
+ if (shouldInstallBinary({ binExists, installedVersion, packageVersion })) {
26
31
  const here = fileURLToPath(new URL('.', import.meta.url));
27
32
  const installer = join(here, 'install.js');
28
33
  const res = spawnSync(process.execPath, [installer], { stdio: 'inherit', env: process.env });
29
- if (res.status !== 0 || !existsSync(installedBin)) {
34
+ const existsAfterInstall = existsSync(installedBin);
35
+ const installedVersionAfterInstall = resolveInstalledVersion(process.env);
36
+ if (
37
+ res.status !== 0 ||
38
+ shouldInstallBinary({
39
+ binExists: existsAfterInstall,
40
+ installedVersion: installedVersionAfterInstall,
41
+ packageVersion
42
+ })
43
+ ) {
30
44
  console.error('mdv: install missing. try reinstall: npm i -g @dhruv2mars/mdv');
31
45
  process.exit(1);
32
46
  }
@@ -38,3 +52,14 @@ function run(bin, binArgs) {
38
52
  const res = spawnSync(bin, binArgs, { stdio: 'inherit' });
39
53
  process.exit(res.status ?? 1);
40
54
  }
55
+
56
+ function readPackageVersion() {
57
+ try {
58
+ const here = fileURLToPath(new URL('.', import.meta.url));
59
+ const pkg = JSON.parse(readFileSync(join(here, '..', 'package.json'), 'utf8'));
60
+ const version = pkg?.version;
61
+ return typeof version === 'string' && version.length > 0 ? version : '';
62
+ } catch {
63
+ return '';
64
+ }
65
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dhruv2mars/mdv",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "description": "Terminal-first markdown visualizer/editor",
5
5
  "type": "module",
6
6
  "bin": {