@joripspace/cli 0.4.2 → 0.5.1

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.
@@ -1,134 +1,134 @@
1
- const crypto = require('node:crypto');
2
- const fs = require('node:fs');
3
- const path = require('node:path');
4
-
5
- function installExecutableAtomically(sourcePath, targetPath, options = {}) {
6
- const source = path.resolve(sourcePath);
7
- const target = path.resolve(targetPath);
8
- const platform = options.platform || process.platform;
9
- const sourceStat = fs.lstatSync(source);
10
- if (!sourceStat.isFile() || sourceStat.isSymbolicLink()) {
11
- throw new Error('JoripSpace installer source must be a regular file.');
12
- }
13
-
14
- const directory = path.dirname(target);
15
- fs.mkdirSync(directory, { recursive: true });
16
- const before = executableSnapshot(target);
17
- const suffix = `${process.pid}-${Date.now()}-${crypto.randomBytes(8).toString('hex')}`;
18
- const temporary = path.join(directory, `.${path.basename(target)}.tmp-${suffix}`);
19
- let backup = '';
20
- let installed = false;
21
-
22
- try {
23
- fs.copyFileSync(source, temporary, fs.constants.COPYFILE_EXCL);
24
- fs.chmodSync(temporary, options.mode ?? 0o755);
25
- fsyncFile(temporary);
26
- const copied = fs.lstatSync(temporary);
27
- if (!copied.isFile() || copied.isSymbolicLink() || copied.size !== sourceStat.size) {
28
- throw new Error('JoripSpace installer temporary executable failed integrity checks.');
29
- }
30
- if (!sameSnapshot(before, executableSnapshot(target))) {
31
- throw new Error('Installed JoripSpace CLI changed during upgrade.');
32
- }
33
-
34
- if (platform === 'win32' && before) {
35
- backup = path.join(directory, `.${path.basename(target)}.previous-${suffix}`);
36
- fs.renameSync(target, backup);
37
- try {
38
- if (typeof options.beforePromote === 'function') options.beforePromote();
39
- fs.renameSync(temporary, target);
40
- } catch (error) {
41
- try {
42
- if (!fs.existsSync(target)) fs.renameSync(backup, target);
43
- } catch (rollbackError) {
44
- throw new Error(
45
- `JoripSpace CLI upgrade failed and the previous executable remains at ${backup}: ${messageOf(
46
- rollbackError
47
- )}`,
48
- { cause: error }
49
- );
50
- }
51
- backup = '';
52
- throw error;
53
- }
54
- } else {
55
- if (typeof options.beforePromote === 'function') options.beforePromote();
56
- fs.renameSync(temporary, target);
57
- }
58
- installed = true;
59
- fsyncDirectory(directory);
60
- if (backup) {
61
- try {
62
- fs.rmSync(backup, { force: true });
63
- backup = '';
64
- fsyncDirectory(directory);
65
- } catch {}
66
- }
67
- return { target, replaced: Boolean(before) };
68
- } finally {
69
- fs.rmSync(temporary, { force: true });
70
- if (installed && backup) {
71
- try {
72
- fs.rmSync(backup, { force: true });
73
- } catch {}
74
- }
75
- }
76
- }
77
-
78
- function executableSnapshot(filePath) {
79
- let stat;
80
- try {
81
- stat = fs.lstatSync(filePath);
82
- } catch (error) {
83
- if (error?.code === 'ENOENT') return null;
84
- throw error;
85
- }
86
- if (!stat.isFile() || stat.isSymbolicLink()) {
87
- throw new Error('Installed JoripSpace CLI target must be a regular file.');
88
- }
89
- return {
90
- dev: stat.dev,
91
- ino: stat.ino,
92
- size: stat.size,
93
- mtimeMs: stat.mtimeMs,
94
- sha256: crypto.createHash('sha256').update(fs.readFileSync(filePath)).digest('hex'),
95
- };
96
- }
97
-
98
- function sameSnapshot(left, right) {
99
- if (!left || !right) return left === right;
100
- return (
101
- left.dev === right.dev &&
102
- left.ino === right.ino &&
103
- left.size === right.size &&
104
- left.mtimeMs === right.mtimeMs &&
105
- left.sha256 === right.sha256
106
- );
107
- }
108
-
109
- function fsyncFile(filePath) {
110
- const descriptor = fs.openSync(filePath, 'r+');
111
- try {
112
- fs.fsyncSync(descriptor);
113
- } finally {
114
- fs.closeSync(descriptor);
115
- }
116
- }
117
-
118
- function fsyncDirectory(directory) {
119
- let descriptor;
120
- try {
121
- descriptor = fs.openSync(directory, 'r');
122
- fs.fsyncSync(descriptor);
123
- } catch (error) {
124
- if (!['EINVAL', 'ENOTSUP', 'EISDIR', 'EPERM'].includes(error?.code)) throw error;
125
- } finally {
126
- if (descriptor !== undefined) fs.closeSync(descriptor);
127
- }
128
- }
129
-
130
- function messageOf(error) {
131
- return error instanceof Error ? error.message : String(error);
132
- }
133
-
134
- module.exports = { installExecutableAtomically };
1
+ const crypto = require('node:crypto');
2
+ const fs = require('node:fs');
3
+ const path = require('node:path');
4
+
5
+ function installExecutableAtomically(sourcePath, targetPath, options = {}) {
6
+ const source = path.resolve(sourcePath);
7
+ const target = path.resolve(targetPath);
8
+ const platform = options.platform || process.platform;
9
+ const sourceStat = fs.lstatSync(source);
10
+ if (!sourceStat.isFile() || sourceStat.isSymbolicLink()) {
11
+ throw new Error('JoripSpace installer source must be a regular file.');
12
+ }
13
+
14
+ const directory = path.dirname(target);
15
+ fs.mkdirSync(directory, { recursive: true });
16
+ const before = executableSnapshot(target);
17
+ const suffix = `${process.pid}-${Date.now()}-${crypto.randomBytes(8).toString('hex')}`;
18
+ const temporary = path.join(directory, `.${path.basename(target)}.tmp-${suffix}`);
19
+ let backup = '';
20
+ let installed = false;
21
+
22
+ try {
23
+ fs.copyFileSync(source, temporary, fs.constants.COPYFILE_EXCL);
24
+ fs.chmodSync(temporary, options.mode ?? 0o755);
25
+ fsyncFile(temporary);
26
+ const copied = fs.lstatSync(temporary);
27
+ if (!copied.isFile() || copied.isSymbolicLink() || copied.size !== sourceStat.size) {
28
+ throw new Error('JoripSpace installer temporary executable failed integrity checks.');
29
+ }
30
+ if (!sameSnapshot(before, executableSnapshot(target))) {
31
+ throw new Error('Installed JoripSpace CLI changed during upgrade.');
32
+ }
33
+
34
+ if (platform === 'win32' && before) {
35
+ backup = path.join(directory, `.${path.basename(target)}.previous-${suffix}`);
36
+ fs.renameSync(target, backup);
37
+ try {
38
+ if (typeof options.beforePromote === 'function') options.beforePromote();
39
+ fs.renameSync(temporary, target);
40
+ } catch (error) {
41
+ try {
42
+ if (!fs.existsSync(target)) fs.renameSync(backup, target);
43
+ } catch (rollbackError) {
44
+ throw new Error(
45
+ `JoripSpace CLI upgrade failed and the previous executable remains at ${backup}: ${messageOf(
46
+ rollbackError
47
+ )}`,
48
+ { cause: error }
49
+ );
50
+ }
51
+ backup = '';
52
+ throw error;
53
+ }
54
+ } else {
55
+ if (typeof options.beforePromote === 'function') options.beforePromote();
56
+ fs.renameSync(temporary, target);
57
+ }
58
+ installed = true;
59
+ fsyncDirectory(directory);
60
+ if (backup) {
61
+ try {
62
+ fs.rmSync(backup, { force: true });
63
+ backup = '';
64
+ fsyncDirectory(directory);
65
+ } catch {}
66
+ }
67
+ return { target, replaced: Boolean(before) };
68
+ } finally {
69
+ fs.rmSync(temporary, { force: true });
70
+ if (installed && backup) {
71
+ try {
72
+ fs.rmSync(backup, { force: true });
73
+ } catch {}
74
+ }
75
+ }
76
+ }
77
+
78
+ function executableSnapshot(filePath) {
79
+ let stat;
80
+ try {
81
+ stat = fs.lstatSync(filePath);
82
+ } catch (error) {
83
+ if (error?.code === 'ENOENT') return null;
84
+ throw error;
85
+ }
86
+ if (!stat.isFile() || stat.isSymbolicLink()) {
87
+ throw new Error('Installed JoripSpace CLI target must be a regular file.');
88
+ }
89
+ return {
90
+ dev: stat.dev,
91
+ ino: stat.ino,
92
+ size: stat.size,
93
+ mtimeMs: stat.mtimeMs,
94
+ sha256: crypto.createHash('sha256').update(fs.readFileSync(filePath)).digest('hex'),
95
+ };
96
+ }
97
+
98
+ function sameSnapshot(left, right) {
99
+ if (!left || !right) return left === right;
100
+ return (
101
+ left.dev === right.dev &&
102
+ left.ino === right.ino &&
103
+ left.size === right.size &&
104
+ left.mtimeMs === right.mtimeMs &&
105
+ left.sha256 === right.sha256
106
+ );
107
+ }
108
+
109
+ function fsyncFile(filePath) {
110
+ const descriptor = fs.openSync(filePath, 'r+');
111
+ try {
112
+ fs.fsyncSync(descriptor);
113
+ } finally {
114
+ fs.closeSync(descriptor);
115
+ }
116
+ }
117
+
118
+ function fsyncDirectory(directory) {
119
+ let descriptor;
120
+ try {
121
+ descriptor = fs.openSync(directory, 'r');
122
+ fs.fsyncSync(descriptor);
123
+ } catch (error) {
124
+ if (!['EINVAL', 'ENOTSUP', 'EISDIR', 'EPERM'].includes(error?.code)) throw error;
125
+ } finally {
126
+ if (descriptor !== undefined) fs.closeSync(descriptor);
127
+ }
128
+ }
129
+
130
+ function messageOf(error) {
131
+ return error instanceof Error ? error.message : String(error);
132
+ }
133
+
134
+ module.exports = { installExecutableAtomically };