Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mosanden committed Sep 14, 2023
0 parents commit 7d76fff
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 0 deletions.
172 changes: 172 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# IntelliJ based IDEs
.idea
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# compare-yaml

This CLI tool can compare the values or structure of two YAML files. It will output the keys/values of the first file that differ from the second file.

## Install

```bash
wget https://github.com/mosanden/compare-yaml/releases/latest/download/compare-yaml -O /usr/bin/compare-yaml &&\
chmod +x /usr/bin/compare-yaml
```

## Usage

To compare the structure:

```bash
compare-yaml struct path/to/first/file path/to/second/file
```

To compare the values:

```bash
compare-yaml values path/to/first/file path/to/second/file
```

## Develop

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run compare-yaml.js
```

To build:

```bash
bun build --compile compare-yaml.js --outfile=compare-yaml
```

This project was created using `bun init` in bun v1.0.1. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file added bun.lockb
Binary file not shown.
91 changes: 91 additions & 0 deletions compare-yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const { program } = require("commander");
const yaml = require("js-yaml");
const fs = require("fs");

program
.name("compare-yaml")
.description("Compare the structure or values of two YAML files.")
.version("0.1.0");

program
.command("struct")
.description(
"Output those keys of the first YAML document that are not contained in the second one"
)
.argument("<first document>", "path to first YAML file")
.argument("<second document>", "path to second YAML file")
.action((doc1Name, doc2Name, _) => {
try {
const doc1 = yaml.load(fs.readFileSync(doc1Name, "utf8"));
const doc2 = yaml.load(fs.readFileSync(doc2Name, "utf8"));
console.log(objectsHaveSameKeysDeep("", doc1, doc2));
} catch (e) {
console.log(e);
}
});

program
.command("values")
.description(
"Output those key-value pairs of the first document that differ from the second document"
)
.argument("<first document>", "path to first YAML file")
.argument("<second document>", "path to second YAML file")
.option(
"-c, --only-common",
"exclude values that only appear in one of the two documents from comparison"
)
.action((doc1Name, doc2Name, options) => {
const onlyCommon = options.onlyCommon ? true : false;
try {
const doc1 = yaml.load(fs.readFileSync(doc1Name, "utf8"));
const doc2 = yaml.load(fs.readFileSync(doc2Name, "utf8"));
console.log(objectsHaveSameValuesDeep("", doc1, doc2, onlyCommon));
} catch (e) {
console.log(e);
}
});

function objectsHaveSameKeysDeep(parent, a, b) {
const aKeys = Object.keys(a).sort();
const bKeys = Object.keys(b).sort();
if (JSON.stringify(aKeys) !== JSON.stringify(bKeys)) {
for (const key of aKeys) {
if (!(key in b)) console.log(parent + "." + key);
}
}
for (const key of aKeys) {
if (
typeof a[key] === "object" &&
a[key] !== null &&
typeof b[key] === "object" &&
b[key] !== null
)
objectsHaveSameKeysDeep(parent + "." + key, a[key], b[key]);
}
}

function objectsHaveSameValuesDeep(parent, a, b, onlyCommon) {
const aKeys = Object.keys(a).sort();
for (const key of aKeys) {
if (
(!onlyCommon || (onlyCommon && key in b)) &&
a[key] !== b[key] &&
typeof a[key] !== "object"
) {
console.log(parent + "." + key + " = " + a[key]);
}
}

for (const key of aKeys) {
if (
typeof a[key] === "object" &&
a[key] !== null &&
typeof b[key] === "object" &&
b[key] !== null
)
objectsHaveSameValuesDeep(parent + "." + key, a[key], b[key], onlyCommon);
}
}

program.parse();
22 changes: 22 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"moduleDetection": "force",
"allowImportingTsExtensions": true,
"noEmit": true,
"composite": true,
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "preserve",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"types": [
"bun-types" // add Bun global
]
}
}
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "compare-yaml",
"module": "compare-yaml.js",
"type": "module",
"devDependencies": {
"bun-types": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"commander": "^11.0.0",
"fs": "^0.0.1-security",
"js-yaml": "^4.1.0"
}
}

0 comments on commit 7d76fff

Please sign in to comment.