diffx-js 0.5.4 → 0.5.8

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/examples.js DELETED
@@ -1,468 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Examples demonstrating diffx-js usage
5
- * Shows various use cases and integration patterns
6
- */
7
-
8
- const { spawn } = require('child_process');
9
- const fs = require('fs');
10
- const path = require('path');
11
- const os = require('os');
12
-
13
- // Colors for output
14
- const colors = {
15
- green: '\x1b[32m',
16
- red: '\x1b[31m',
17
- yellow: '\x1b[33m',
18
- blue: '\x1b[34m',
19
- cyan: '\x1b[36m',
20
- magenta: '\x1b[35m',
21
- reset: '\x1b[0m'
22
- };
23
-
24
- function log(message, color = 'reset') {
25
- console.log(`${colors[color]}${message}${colors.reset}`);
26
- }
27
-
28
- function header(message) {
29
- log(`\n${message}`, 'cyan');
30
- log('='.repeat(message.length), 'cyan');
31
- }
32
-
33
- function example(title, description) {
34
- log(`\n${title}`, 'yellow');
35
- log(` ${description}`, 'blue');
36
- }
37
-
38
- function code(command) {
39
- log(` $ ${command}`, 'green');
40
- }
41
-
42
- function output(text) {
43
- log(` ${text}`, 'magenta');
44
- }
45
-
46
- async function runDiffx(args) {
47
- return new Promise((resolve, reject) => {
48
- const child = spawn('node', [path.join(__dirname, 'index.js'), ...args], {
49
- stdio: ['pipe', 'pipe', 'pipe']
50
- });
51
-
52
- let stdout = '';
53
- let stderr = '';
54
-
55
- child.stdout.on('data', (data) => {
56
- stdout += data.toString();
57
- });
58
-
59
- child.stderr.on('data', (data) => {
60
- stderr += data.toString();
61
- });
62
-
63
- child.on('close', (code) => {
64
- resolve({ code, stdout, stderr });
65
- });
66
-
67
- child.on('error', (err) => {
68
- reject(err);
69
- });
70
- });
71
- }
72
-
73
- async function runExamples() {
74
- header('diffx-js Usage Examples');
75
-
76
- // Create temporary directory for examples
77
- const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'diffx-examples-'));
78
- const oldCwd = process.cwd();
79
- process.chdir(tempDir);
80
-
81
- try {
82
- // Example 1: Basic JSON comparison
83
- header('1. Basic JSON Configuration Comparison');
84
-
85
- const config1 = {
86
- app: {
87
- name: "my-app",
88
- version: "1.0.0",
89
- database: {
90
- host: "localhost",
91
- port: 5432,
92
- ssl: false
93
- }
94
- },
95
- features: ["auth", "logging"]
96
- };
97
-
98
- const config2 = {
99
- app: {
100
- name: "my-app",
101
- version: "1.1.0",
102
- database: {
103
- host: "prod-db.example.com",
104
- port: 5432,
105
- ssl: true
106
- }
107
- },
108
- features: ["auth", "logging", "metrics"]
109
- };
110
-
111
- fs.writeFileSync('config_v1.json', JSON.stringify(config1, null, 2));
112
- fs.writeFileSync('config_v2.json', JSON.stringify(config2, null, 2));
113
-
114
- example(
115
- 'Application Configuration Migration',
116
- 'Compare two versions of app configuration to see what changed'
117
- );
118
- code('diffx config_v1.json config_v2.json');
119
-
120
- const result1 = await runDiffx(['config_v1.json', 'config_v2.json']);
121
- output(result1.stdout);
122
-
123
- // Example 2: YAML CI/CD pipeline changes
124
- header('2. CI/CD Pipeline Configuration Changes');
125
-
126
- const pipeline1 = `name: CI
127
- on:
128
- push:
129
- branches: [main]
130
- pull_request:
131
- branches: [main]
132
- jobs:
133
- test:
134
- runs-on: ubuntu-latest
135
- steps:
136
- - uses: actions/checkout@v3
137
- - uses: actions/setup-node@v3
138
- with:
139
- node-version: 16
140
- - run: npm test`;
141
-
142
- const pipeline2 = `name: CI
143
- on:
144
- push:
145
- branches: [main, develop]
146
- pull_request:
147
- branches: [main, develop]
148
- jobs:
149
- test:
150
- runs-on: ubuntu-latest
151
- strategy:
152
- matrix:
153
- node-version: [16, 18, 20]
154
- steps:
155
- - uses: actions/checkout@v4
156
- - uses: actions/setup-node@v4
157
- with:
158
- node-version: \${{ matrix.node-version }}
159
- - run: npm ci
160
- - run: npm test`;
161
-
162
- fs.writeFileSync('ci_old.yml', pipeline1);
163
- fs.writeFileSync('ci_new.yml', pipeline2);
164
-
165
- example(
166
- 'GitHub Actions Workflow Evolution',
167
- 'See how CI pipeline evolved to support multiple Node.js versions'
168
- );
169
- code('diffx ci_old.yml ci_new.yml');
170
-
171
- const result2 = await runDiffx(['ci_old.yml', 'ci_new.yml']);
172
- output(result2.stdout);
173
-
174
- // Example 3: JSON output for automation
175
- header('3. Machine-Readable Output for Automation');
176
-
177
- example(
178
- 'JSON Output for CI/CD Integration',
179
- 'Generate structured output for automated processing'
180
- );
181
- code('diffx config_v1.json config_v2.json --output json');
182
-
183
- const result3 = await runDiffx(['config_v1.json', 'config_v2.json', '--output', 'json']);
184
- try {
185
- const jsonOutput = JSON.parse(result3.stdout);
186
- output(JSON.stringify(jsonOutput, null, 2));
187
- } catch (e) {
188
- output(result3.stdout);
189
- }
190
-
191
- // Example 4: API Schema Evolution
192
- header('4. API Schema Version Comparison');
193
-
194
- const apiV1 = {
195
- openapi: "3.0.0",
196
- info: {
197
- title: "User API",
198
- version: "1.0.0"
199
- },
200
- paths: {
201
- "/users": {
202
- get: {
203
- responses: {
204
- "200": {
205
- content: {
206
- "application/json": {
207
- schema: {
208
- type: "array",
209
- items: {
210
- properties: {
211
- id: { type: "integer" },
212
- name: { type: "string" },
213
- email: { type: "string" }
214
- }
215
- }
216
- }
217
- }
218
- }
219
- }
220
- }
221
- }
222
- }
223
- }
224
- };
225
-
226
- const apiV2 = {
227
- openapi: "3.0.0",
228
- info: {
229
- title: "User API",
230
- version: "2.0.0"
231
- },
232
- paths: {
233
- "/users": {
234
- get: {
235
- responses: {
236
- "200": {
237
- content: {
238
- "application/json": {
239
- schema: {
240
- type: "array",
241
- items: {
242
- properties: {
243
- id: { type: "integer" },
244
- name: { type: "string" },
245
- email: { type: "string" },
246
- created_at: { type: "string", format: "date-time" },
247
- is_active: { type: "boolean" }
248
- }
249
- }
250
- }
251
- }
252
- }
253
- }
254
- }
255
- }
256
- },
257
- "/users/{id}": {
258
- get: {
259
- parameters: [
260
- {
261
- name: "id",
262
- in: "path",
263
- required: true,
264
- schema: { type: "integer" }
265
- }
266
- ]
267
- }
268
- }
269
- }
270
- };
271
-
272
- fs.writeFileSync('api_v1.json', JSON.stringify(apiV1, null, 2));
273
- fs.writeFileSync('api_v2.json', JSON.stringify(apiV2, null, 2));
274
-
275
- example(
276
- 'OpenAPI Schema Breaking Changes Detection',
277
- 'Identify API changes that may break client compatibility'
278
- );
279
- code('diffx api_v1.json api_v2.json');
280
-
281
- const result4 = await runDiffx(['api_v1.json', 'api_v2.json']);
282
- output(result4.stdout);
283
-
284
- // Example 5: Environment configuration
285
- header('5. Environment Configuration Drift Detection');
286
-
287
- const prodConfig = {
288
- database: {
289
- host: "prod-db.company.com",
290
- port: 5432,
291
- pool_size: 20,
292
- ssl: true,
293
- timeout: 30000
294
- },
295
- cache: {
296
- redis_url: "redis://prod-cache.company.com:6379",
297
- ttl: 3600
298
- },
299
- api: {
300
- rate_limit: 1000,
301
- cors_origins: ["https://app.company.com"],
302
- debug: false
303
- }
304
- };
305
-
306
- const stagingConfig = {
307
- database: {
308
- host: "staging-db.company.com",
309
- port: 5432,
310
- pool_size: 10,
311
- ssl: true,
312
- timeout: 30000
313
- },
314
- cache: {
315
- redis_url: "redis://staging-cache.company.com:6379",
316
- ttl: 1800
317
- },
318
- api: {
319
- rate_limit: 100,
320
- cors_origins: ["https://staging.company.com", "http://localhost:3000"],
321
- debug: true
322
- }
323
- };
324
-
325
- fs.writeFileSync('prod.json', JSON.stringify(prodConfig, null, 2));
326
- fs.writeFileSync('staging.json', JSON.stringify(stagingConfig, null, 2));
327
-
328
- example(
329
- 'Production vs Staging Configuration Audit',
330
- 'Verify configuration differences between environments'
331
- );
332
- code('diffx prod.json staging.json --output yaml');
333
-
334
- const result5 = await runDiffx(['prod.json', 'staging.json', '--output', 'yaml']);
335
- output(result5.stdout);
336
-
337
- // Example 6: Package.json dependency changes
338
- header('6. Package Dependencies Change Tracking');
339
-
340
- const pkg1 = {
341
- name: "my-project",
342
- version: "1.0.0",
343
- dependencies: {
344
- "express": "^4.18.0",
345
- "lodash": "^4.17.21",
346
- "axios": "^0.27.0"
347
- },
348
- devDependencies: {
349
- "jest": "^28.0.0",
350
- "eslint": "^8.0.0"
351
- }
352
- };
353
-
354
- const pkg2 = {
355
- name: "my-project",
356
- version: "1.1.0",
357
- dependencies: {
358
- "express": "^4.19.0",
359
- "lodash": "^4.17.21",
360
- "axios": "^1.0.0",
361
- "helmet": "^6.0.0"
362
- },
363
- devDependencies: {
364
- "jest": "^29.0.0",
365
- "eslint": "^8.0.0",
366
- "prettier": "^2.8.0"
367
- }
368
- };
369
-
370
- fs.writeFileSync('package_old.json', JSON.stringify(pkg1, null, 2));
371
- fs.writeFileSync('package_new.json', JSON.stringify(pkg2, null, 2));
372
-
373
- example(
374
- 'Dependency Update Audit',
375
- 'Track package dependency changes for security and compatibility'
376
- );
377
- code('diffx package_old.json package_new.json');
378
-
379
- const result6 = await runDiffx(['package_old.json', 'package_new.json']);
380
- output(result6.stdout);
381
-
382
- // Example 7: Integration with Node.js scripts
383
- header('7. Integration with Node.js Applications');
384
-
385
- example(
386
- 'Programmatic Usage in Node.js',
387
- 'Use diffx within your Node.js applications for automated config validation'
388
- );
389
-
390
- log('\nExample Node.js Integration:', 'yellow');
391
- const nodeExample = `
392
- const { spawn } = require('child_process');
393
-
394
- async function checkConfigChanges(oldConfig, newConfig) {
395
- return new Promise((resolve, reject) => {
396
- const diffx = spawn('npx', ['diffx', oldConfig, newConfig, '--output', 'json']);
397
-
398
- let output = '';
399
- diffx.stdout.on('data', (data) => {
400
- output += data.toString();
401
- });
402
-
403
- diffx.on('close', (code) => {
404
- if (code === 0) {
405
- try {
406
- const changes = JSON.parse(output);
407
- resolve(changes);
408
- } catch (e) {
409
- reject(e);
410
- }
411
- } else {
412
- reject(new Error(\`diffx failed with code \${code}\`));
413
- }
414
- });
415
- });
416
- }
417
-
418
- // Usage
419
- checkConfigChanges('config_v1.json', 'config_v2.json')
420
- .then(changes => {
421
- console.log(\`Found \${changes.length} changes:\`);
422
- changes.forEach(change => {
423
- console.log(\`- \${change.path}: \${change.change_type}\`);
424
- });
425
- })
426
- .catch(console.error);`;
427
-
428
- output(nodeExample);
429
-
430
- log('\nUse Cases:', 'cyan');
431
- log(' • Configuration drift detection in DevOps pipelines', 'blue');
432
- log(' • API schema validation in CI/CD', 'blue');
433
- log(' • Environment parity checking', 'blue');
434
- log(' • Dependency audit automation', 'blue');
435
- log(' • Infrastructure as Code validation', 'blue');
436
- log(' • Database schema migration verification', 'blue');
437
-
438
- log('\nTips for Better Results:', 'cyan');
439
- log(' • Use --output json for programmatic processing', 'blue');
440
- log(' • Combine with jq for advanced JSON manipulation', 'blue');
441
- log(' • Set up automated alerts for critical changes', 'blue');
442
- log(' • Version control your configuration files', 'blue');
443
- log(' • Use in pre-commit hooks for validation', 'blue');
444
-
445
- log('\nMore Information:', 'green');
446
- log(' • Documentation: https://github.com/kako-jun/diffx/tree/main/docs', 'blue');
447
- log(' • Issues: https://github.com/kako-jun/diffx/issues', 'blue');
448
- log(' • npm package: https://www.npmjs.com/package/diffx-js', 'blue');
449
-
450
- } catch (error) {
451
- log(`\nError running examples: ${error.message}`, 'red');
452
- } finally {
453
- // Cleanup
454
- process.chdir(oldCwd);
455
- try {
456
- fs.rmSync(tempDir, { recursive: true, force: true });
457
- } catch (cleanupErr) {
458
- log(`Cleanup warning: ${cleanupErr.message}`, 'yellow');
459
- }
460
- }
461
- }
462
-
463
- // Run examples if called directly
464
- if (require.main === module) {
465
- runExamples();
466
- }
467
-
468
- module.exports = { runExamples };