@code0-tech/cli 0.0.0 → 0.0.5
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.
- package/README.md +19 -0
- package/bin/codezero.js +16 -0
- package/lib/extract.js +67 -0
- package/lib/install.js +122 -0
- package/package.json +37 -6
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# codezero
|
|
2
|
+
|
|
3
|
+
`codezero` — a CLI setup wizard for running [CodeZero](https://codezero.build) locally.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install -g @code0-tech/cli
|
|
7
|
+
codezero
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
This package is a Node.js wrapper: on install it downloads the prebuilt `codezero` binary
|
|
11
|
+
for your OS/arch from the matching [GitHub release](https://github.com/code0-tech/hydra/releases)
|
|
12
|
+
and extracts it with `tar-stream`/`yauzl`. If the postinstall download is skipped (e.g. `pnpm`
|
|
13
|
+
without `trustedDependencies`, Deno, or `--ignore-scripts`), the wrapper downloads the binary
|
|
14
|
+
lazily on first run instead.
|
|
15
|
+
|
|
16
|
+
Supported platforms: Linux x64, macOS x64/arm64, Windows x64.
|
|
17
|
+
|
|
18
|
+
Source, issues, and other install methods (Homebrew, Cargo, prebuilt archives): see the
|
|
19
|
+
[hydra repository](https://github.com/code0-tech/hydra).
|
package/bin/codezero.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const { spawnSync } = require('child_process');
|
|
5
|
+
const { ensureBinary } = require('../lib/install.js');
|
|
6
|
+
|
|
7
|
+
ensureBinary()
|
|
8
|
+
.then((binPath) => {
|
|
9
|
+
const result = spawnSync(binPath, process.argv.slice(2), { stdio: 'inherit' });
|
|
10
|
+
if (result.error) throw result.error;
|
|
11
|
+
process.exit(result.status === null ? 1 : result.status);
|
|
12
|
+
})
|
|
13
|
+
.catch((err) => {
|
|
14
|
+
console.error(`codezero: ${err.message}`);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
});
|
package/lib/extract.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const zlib = require('zlib');
|
|
4
|
+
const { Readable } = require('stream');
|
|
5
|
+
const tar = require('tar-stream');
|
|
6
|
+
const yauzl = require('yauzl');
|
|
7
|
+
|
|
8
|
+
// Pulls a single named file out of the gzip-compressed tarballs the release
|
|
9
|
+
// workflow produces.
|
|
10
|
+
function extractTarGz(buffer, fileName) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const extract = tar.extract();
|
|
13
|
+
let found = null;
|
|
14
|
+
|
|
15
|
+
extract.on('entry', (header, stream, next) => {
|
|
16
|
+
if (header.name !== fileName) {
|
|
17
|
+
stream.resume();
|
|
18
|
+
return next();
|
|
19
|
+
}
|
|
20
|
+
const chunks = [];
|
|
21
|
+
stream.on('data', (c) => chunks.push(c));
|
|
22
|
+
stream.on('end', () => {
|
|
23
|
+
found = Buffer.concat(chunks);
|
|
24
|
+
next();
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
extract.on('finish', () => {
|
|
29
|
+
if (!found) reject(new Error(`"${fileName}" not found in tar archive`));
|
|
30
|
+
else resolve(found);
|
|
31
|
+
});
|
|
32
|
+
extract.on('error', reject);
|
|
33
|
+
|
|
34
|
+
Readable.from(zlib.gunzipSync(buffer)).pipe(extract);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Pulls a single named file out of the zip archives the release workflow
|
|
39
|
+
// produces.
|
|
40
|
+
function extractZip(buffer, fileName) {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
yauzl.fromBuffer(buffer, { lazyEntries: true }, (err, zipfile) => {
|
|
43
|
+
if (err) return reject(err);
|
|
44
|
+
|
|
45
|
+
zipfile.on('error', reject);
|
|
46
|
+
zipfile.on('end', () => reject(new Error(`"${fileName}" not found in zip archive`)));
|
|
47
|
+
|
|
48
|
+
zipfile.on('entry', (entry) => {
|
|
49
|
+
if (entry.fileName !== fileName) {
|
|
50
|
+
zipfile.readEntry();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
zipfile.openReadStream(entry, (err, stream) => {
|
|
54
|
+
if (err) return reject(err);
|
|
55
|
+
const chunks = [];
|
|
56
|
+
stream.on('data', (c) => chunks.push(c));
|
|
57
|
+
stream.on('end', () => resolve(Buffer.concat(chunks)));
|
|
58
|
+
stream.on('error', reject);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
zipfile.readEntry();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = { extractTarGz, extractZip };
|
package/lib/install.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const https = require('https');
|
|
7
|
+
const crypto = require('crypto');
|
|
8
|
+
|
|
9
|
+
const { extractTarGz, extractZip } = require('./extract.js');
|
|
10
|
+
|
|
11
|
+
const pkg = require('../package.json');
|
|
12
|
+
|
|
13
|
+
const REPO = 'code0-tech/hydra';
|
|
14
|
+
|
|
15
|
+
// Mirrors the `asset` names produced by .github/workflows/release.yml.
|
|
16
|
+
function releaseAsset() {
|
|
17
|
+
const platform = os.platform();
|
|
18
|
+
const arch = os.arch();
|
|
19
|
+
|
|
20
|
+
if (platform === 'linux' && arch === 'x64') return { name: 'codezero-linux-x86_64', ext: 'tar.gz' };
|
|
21
|
+
if (platform === 'darwin' && arch === 'x64') return { name: 'codezero-macos-x86_64', ext: 'tar.gz' };
|
|
22
|
+
if (platform === 'darwin' && arch === 'arm64') return { name: 'codezero-macos-arm64', ext: 'tar.gz' };
|
|
23
|
+
if (platform === 'win32' && arch === 'x64') return { name: 'codezero-windows-x86_64', ext: 'zip' };
|
|
24
|
+
|
|
25
|
+
throw new Error(
|
|
26
|
+
`codezero: no prebuilt binary for platform "${platform}" (${arch}). ` +
|
|
27
|
+
'Install from source instead: https://github.com/code0-tech/hydra#install'
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function binaryName() {
|
|
32
|
+
return os.platform() === 'win32' ? 'codezero.exe' : 'codezero';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function binaryDir() {
|
|
36
|
+
return path.join(__dirname, '..', 'bin');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function binaryPath() {
|
|
40
|
+
return path.join(binaryDir(), binaryName());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function get(url, redirects = 5) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
https
|
|
46
|
+
.get(url, { headers: { 'User-Agent': 'codezero-npm-installer' } }, (res) => {
|
|
47
|
+
if (
|
|
48
|
+
res.statusCode >= 300 &&
|
|
49
|
+
res.statusCode < 400 &&
|
|
50
|
+
res.headers.location &&
|
|
51
|
+
redirects > 0
|
|
52
|
+
) {
|
|
53
|
+
res.resume();
|
|
54
|
+
resolve(get(res.headers.location, redirects - 1));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (res.statusCode !== 200) {
|
|
58
|
+
res.resume();
|
|
59
|
+
reject(new Error(`request to ${url} failed with status ${res.statusCode}`));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const chunks = [];
|
|
63
|
+
res.on('data', (c) => chunks.push(c));
|
|
64
|
+
res.on('end', () => resolve(Buffer.concat(chunks)));
|
|
65
|
+
res.on('error', reject);
|
|
66
|
+
})
|
|
67
|
+
.on('error', reject);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function verifyChecksum(tag, assetName, data) {
|
|
72
|
+
try {
|
|
73
|
+
const checksums = (
|
|
74
|
+
await get(`https://github.com/${REPO}/releases/download/${tag}/checksums.txt`)
|
|
75
|
+
).toString('utf8');
|
|
76
|
+
const line = checksums.split('\n').find((l) => l.trim().endsWith(assetName));
|
|
77
|
+
if (!line) return;
|
|
78
|
+
const expected = line.trim().split(/\s+/)[0];
|
|
79
|
+
const actual = crypto.createHash('sha256').update(data).digest('hex');
|
|
80
|
+
if (expected !== actual) {
|
|
81
|
+
throw new Error(`checksum mismatch for ${assetName}: expected ${expected}, got ${actual}`);
|
|
82
|
+
}
|
|
83
|
+
} catch (err) {
|
|
84
|
+
if (err.message.startsWith('checksum mismatch')) throw err;
|
|
85
|
+
console.warn(`codezero: could not verify checksum (${err.message}), continuing anyway`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function ensureBinary() {
|
|
90
|
+
const dest = binaryPath();
|
|
91
|
+
if (fs.existsSync(dest)) return dest;
|
|
92
|
+
|
|
93
|
+
const { name, ext } = releaseAsset();
|
|
94
|
+
const tag = `v${pkg.version}`;
|
|
95
|
+
const assetName = `${name}.${ext}`;
|
|
96
|
+
const url = `https://github.com/${REPO}/releases/download/${tag}/${assetName}`;
|
|
97
|
+
|
|
98
|
+
console.log(`codezero: downloading ${assetName} from release ${tag}...`);
|
|
99
|
+
const archive = await get(url);
|
|
100
|
+
await verifyChecksum(tag, assetName, archive);
|
|
101
|
+
|
|
102
|
+
const data = ext === 'zip' ? await extractZip(archive, binaryName()) : await extractTarGz(archive, binaryName());
|
|
103
|
+
|
|
104
|
+
fs.mkdirSync(binaryDir(), { recursive: true });
|
|
105
|
+
const tmp = `${dest}.download`;
|
|
106
|
+
fs.writeFileSync(tmp, data);
|
|
107
|
+
if (os.platform() !== 'win32') fs.chmodSync(tmp, 0o755);
|
|
108
|
+
fs.renameSync(tmp, dest);
|
|
109
|
+
|
|
110
|
+
return dest;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = { ensureBinary, binaryPath };
|
|
114
|
+
|
|
115
|
+
if (require.main === module) {
|
|
116
|
+
ensureBinary()
|
|
117
|
+
.then(() => console.log('codezero: binary ready'))
|
|
118
|
+
.catch((err) => {
|
|
119
|
+
console.warn(`codezero: postinstall download failed (${err.message}).`);
|
|
120
|
+
console.warn('codezero: it will be downloaded on first run instead.');
|
|
121
|
+
});
|
|
122
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@code0-tech/cli",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "A CLI setup wizard for running CodeZero locally",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
6
|
+
"homepage": "https://codezero.build",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/code0-tech/hydra.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"codezero",
|
|
13
|
+
"cli",
|
|
14
|
+
"setup"
|
|
15
|
+
],
|
|
16
|
+
"bin": {
|
|
17
|
+
"codezero": "bin/codezero.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"bin/codezero.js",
|
|
21
|
+
"lib/install.js",
|
|
22
|
+
"lib/extract.js"
|
|
23
|
+
],
|
|
9
24
|
"scripts": {
|
|
10
|
-
"
|
|
25
|
+
"postinstall": "node lib/install.js || exit 0"
|
|
26
|
+
},
|
|
27
|
+
"os": [
|
|
28
|
+
"darwin",
|
|
29
|
+
"linux",
|
|
30
|
+
"win32"
|
|
31
|
+
],
|
|
32
|
+
"cpu": [
|
|
33
|
+
"x64",
|
|
34
|
+
"arm64"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"tar-stream": "^3.2.2",
|
|
41
|
+
"yauzl": "^3.4.0"
|
|
11
42
|
}
|
|
12
43
|
}
|