diffx-js 0.3.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.
- package/README.md +47 -0
- package/README_ja.md +47 -0
- package/bin/diffx +0 -0
- package/index.js +37 -0
- package/package.json +16 -0
- package/scripts/download-binary.js +123 -0
package/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# diffx-npm
|
2
|
+
|
3
|
+
A Node.js wrapper for the `diffx` CLI tool.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```bash
|
8
|
+
npm install diffx-js
|
9
|
+
```
|
10
|
+
|
11
|
+
This will automatically download the appropriate `diffx` binary for your system from GitHub Releases.
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
```javascript
|
16
|
+
const { runDiffx } = require('diffx-npm');
|
17
|
+
|
18
|
+
async function main() {
|
19
|
+
// Compare two JSON files
|
20
|
+
let result = await runDiffx(["file1.json", "file2.json"]);
|
21
|
+
|
22
|
+
if (result.code === 0) {
|
23
|
+
console.log("No differences found.");
|
24
|
+
} else {
|
25
|
+
console.log("Differences found:");
|
26
|
+
console.log(result.stdout);
|
27
|
+
}
|
28
|
+
|
29
|
+
// You can pass any arguments supported by the diffx CLI
|
30
|
+
result = await runDiffx(["file1.yaml", "file2.yaml", "--output", "json"]);
|
31
|
+
console.log(result.stdout);
|
32
|
+
}
|
33
|
+
|
34
|
+
main();
|
35
|
+
```
|
36
|
+
|
37
|
+
## Development
|
38
|
+
|
39
|
+
To link for local development:
|
40
|
+
|
41
|
+
```bash
|
42
|
+
npm link
|
43
|
+
```
|
44
|
+
|
45
|
+
## License
|
46
|
+
|
47
|
+
This project is licensed under the MIT License.
|
package/README_ja.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# diffx-npm
|
2
|
+
|
3
|
+
`diffx` CLIツールのNode.jsラッパー
|
4
|
+
|
5
|
+
## インストール
|
6
|
+
|
7
|
+
```bash
|
8
|
+
npm install diffx-js
|
9
|
+
```
|
10
|
+
|
11
|
+
これにより、GitHub Releasesからお使いのシステムに適した `diffx` バイナリが自動的にダウンロードされます。
|
12
|
+
|
13
|
+
## 使い方
|
14
|
+
|
15
|
+
```javascript
|
16
|
+
const { runDiffx } = require('diffx-npm');
|
17
|
+
|
18
|
+
async function main() {
|
19
|
+
// 2つのJSONファイルを比較
|
20
|
+
let result = await runDiffx(["file1.json", "file2.json"]);
|
21
|
+
|
22
|
+
if (result.code === 0) {
|
23
|
+
console.log("違いはありません。");
|
24
|
+
} else {
|
25
|
+
console.log("違いが見つかりました:");
|
26
|
+
console.log(result.stdout);
|
27
|
+
}
|
28
|
+
|
29
|
+
// diffx CLIでサポートされている任意の引数を渡すことができます
|
30
|
+
result = await runDiffx(["file1.yaml", "file2.yaml", "--output", "json"]);
|
31
|
+
console.log(result.stdout);
|
32
|
+
}
|
33
|
+
|
34
|
+
main();
|
35
|
+
```
|
36
|
+
|
37
|
+
## 開発
|
38
|
+
|
39
|
+
ローカル開発用にリンクするには:
|
40
|
+
|
41
|
+
```bash
|
42
|
+
npm link
|
43
|
+
```
|
44
|
+
|
45
|
+
## ライセンス
|
46
|
+
|
47
|
+
このプロジェクトはMITライセンスの下でライセンスされています。
|
package/bin/diffx
ADDED
File without changes
|
package/index.js
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
const { spawn } = require('child_process');
|
4
|
+
const path = require('path');
|
5
|
+
const fs = require('fs');
|
6
|
+
|
7
|
+
// Determine the platform-specific binary name
|
8
|
+
let binaryName = 'diffx';
|
9
|
+
if (process.platform === 'win32') {
|
10
|
+
binaryName = 'diffx.exe';
|
11
|
+
}
|
12
|
+
|
13
|
+
// Construct the path to the binary
|
14
|
+
// In a real scenario, this would involve downloading the binary
|
15
|
+
// For now, we assume it's in a 'bin' directory relative to this script
|
16
|
+
const binaryPath = path.join(__dirname, 'bin', binaryName);
|
17
|
+
|
18
|
+
// Check if the binary exists
|
19
|
+
if (!fs.existsSync(binaryPath)) {
|
20
|
+
console.error(`Error: Binary not found at ${binaryPath}`);
|
21
|
+
console.error('Please ensure diffx is properly installed or built for your platform.');
|
22
|
+
process.exit(1);
|
23
|
+
}
|
24
|
+
|
25
|
+
// Spawn the diffx process with arguments
|
26
|
+
const child = spawn(binaryPath, process.argv.slice(2), {
|
27
|
+
stdio: 'inherit',
|
28
|
+
});
|
29
|
+
|
30
|
+
child.on('close', (code) => {
|
31
|
+
process.exit(code);
|
32
|
+
});
|
33
|
+
|
34
|
+
child.on('error', (err) => {
|
35
|
+
console.error(`Failed to start diffx: ${err.message}`);
|
36
|
+
process.exit(1);
|
37
|
+
});
|
package/package.json
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
{
|
2
|
+
"name": "diffx-js",
|
3
|
+
"version": "0.3.0",
|
4
|
+
"description": "A CLI tool for structured diffing of JSON, YAML, and TOML files.",
|
5
|
+
"keywords": ["diff", "json", "yaml", "toml", "cli", "structured", "configuration"],
|
6
|
+
"main": "index.js",
|
7
|
+
"bin": {
|
8
|
+
"diffx": "./index.js"
|
9
|
+
},
|
10
|
+
"scripts": {
|
11
|
+
"postinstall": "node scripts/download-binary.js",
|
12
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
13
|
+
},
|
14
|
+
"author": "",
|
15
|
+
"license": "MIT"
|
16
|
+
}
|
@@ -0,0 +1,123 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
const fs = require('fs');
|
4
|
+
const path = require('path');
|
5
|
+
const https = require('https');
|
6
|
+
const { execSync } = require('child_process');
|
7
|
+
|
8
|
+
const DIFFX_VERSION = '0.3.0';
|
9
|
+
const BINARY_DIR = path.join(__dirname, '..', 'bin');
|
10
|
+
|
11
|
+
function getPlatform() {
|
12
|
+
const platform = process.platform;
|
13
|
+
const arch = process.arch;
|
14
|
+
|
15
|
+
if (platform === 'win32') {
|
16
|
+
return 'diffx-windows-x86_64.zip';
|
17
|
+
} else if (platform === 'darwin') {
|
18
|
+
if (arch === 'arm64') {
|
19
|
+
return 'diffx-macos-aarch64.tar.gz';
|
20
|
+
} else {
|
21
|
+
return 'diffx-macos-x86_64.tar.gz';
|
22
|
+
}
|
23
|
+
} else if (platform === 'linux') {
|
24
|
+
return 'diffx-linux-x86_64.tar.gz';
|
25
|
+
} else {
|
26
|
+
throw new Error(`Unsupported platform: ${platform}-${arch}`);
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
function downloadFile(url, dest) {
|
31
|
+
return new Promise((resolve, reject) => {
|
32
|
+
console.log(`Downloading diffx binary from ${url}...`);
|
33
|
+
const file = fs.createWriteStream(dest);
|
34
|
+
|
35
|
+
https.get(url, (response) => {
|
36
|
+
if (response.statusCode === 302 || response.statusCode === 301) {
|
37
|
+
// Follow redirect
|
38
|
+
downloadFile(response.headers.location, dest).then(resolve).catch(reject);
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
|
42
|
+
if (response.statusCode !== 200) {
|
43
|
+
reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`));
|
44
|
+
return;
|
45
|
+
}
|
46
|
+
|
47
|
+
response.pipe(file);
|
48
|
+
|
49
|
+
file.on('finish', () => {
|
50
|
+
file.close();
|
51
|
+
resolve();
|
52
|
+
});
|
53
|
+
|
54
|
+
file.on('error', (err) => {
|
55
|
+
fs.unlink(dest, () => {}); // Delete the file async
|
56
|
+
reject(err);
|
57
|
+
});
|
58
|
+
}).on('error', (err) => {
|
59
|
+
reject(err);
|
60
|
+
});
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
async function extractArchive(archivePath, extractDir) {
|
65
|
+
if (archivePath.endsWith('.zip')) {
|
66
|
+
// Use unzip for Windows
|
67
|
+
if (process.platform === 'win32') {
|
68
|
+
execSync(`powershell -command "Expand-Archive -Path '${archivePath}' -DestinationPath '${extractDir}' -Force"`, { stdio: 'inherit' });
|
69
|
+
} else {
|
70
|
+
execSync(`unzip -o "${archivePath}" -d "${extractDir}"`, { stdio: 'inherit' });
|
71
|
+
}
|
72
|
+
} else if (archivePath.endsWith('.tar.gz')) {
|
73
|
+
execSync(`tar -xzf "${archivePath}" -C "${extractDir}"`, { stdio: 'inherit' });
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
async function main() {
|
78
|
+
try {
|
79
|
+
// Skip download if binary already exists
|
80
|
+
const binaryName = process.platform === 'win32' ? 'diffx.exe' : 'diffx';
|
81
|
+
const binaryPath = path.join(BINARY_DIR, binaryName);
|
82
|
+
|
83
|
+
if (fs.existsSync(binaryPath)) {
|
84
|
+
console.log('diffx binary already exists, skipping download.');
|
85
|
+
return;
|
86
|
+
}
|
87
|
+
|
88
|
+
const platformFile = getPlatform();
|
89
|
+
const downloadUrl = `https://github.com/kako-jun/diffx/releases/download/v${DIFFX_VERSION}/${platformFile}`;
|
90
|
+
|
91
|
+
// Create bin directory
|
92
|
+
if (!fs.existsSync(BINARY_DIR)) {
|
93
|
+
fs.mkdirSync(BINARY_DIR, { recursive: true });
|
94
|
+
}
|
95
|
+
|
96
|
+
// Download archive
|
97
|
+
const archivePath = path.join(BINARY_DIR, platformFile);
|
98
|
+
await downloadFile(downloadUrl, archivePath);
|
99
|
+
|
100
|
+
console.log('Extracting binary...');
|
101
|
+
await extractArchive(archivePath, BINARY_DIR);
|
102
|
+
|
103
|
+
// Clean up archive
|
104
|
+
fs.unlinkSync(archivePath);
|
105
|
+
|
106
|
+
// Make binary executable on Unix systems
|
107
|
+
if (process.platform !== 'win32') {
|
108
|
+
fs.chmodSync(binaryPath, '755');
|
109
|
+
}
|
110
|
+
|
111
|
+
console.log(`✅ diffx binary installed successfully at ${binaryPath}`);
|
112
|
+
|
113
|
+
} catch (error) {
|
114
|
+
console.error('❌ Failed to download diffx binary:', error.message);
|
115
|
+
console.error('You may need to install diffx manually from: https://github.com/kako-jun/diffx/releases');
|
116
|
+
// Don't fail the installation, just warn
|
117
|
+
process.exit(0);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
if (require.main === module) {
|
122
|
+
main();
|
123
|
+
}
|