Skip to content

Commit

Permalink
feat: add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli committed Oct 12, 2020
1 parent 5e2fb1b commit 29158d4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "GPL-3.0-only",
"dependencies": {
"deepmerge": "4.2.2",
"diff": "^4.0.2",
"json2md": "1.9.0",
"yaml": "1.10.0",
"yargs": "16.0.3"
Expand Down
47 changes: 46 additions & 1 deletion src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const path = require('path')

const deepMerge = require('deepmerge')
const diff = require('diff')
const json2md = require('json2md')
const yaml = require('yaml')
const yargs = require('yargs')
Expand All @@ -16,11 +17,17 @@ const argv = yargs
description: 'Path to a DargStack stack project',
type: 'string'
})
.option('validate', {
alias: 'v',
description: 'Flag that enabled validation only',
type: 'boolean'
})
.help()
.alias('help', 'h')
.argv

const projectPath = argv.path || process.cwd()
const validate = argv.validate || false

const stackDevelopmentPath = path.join(projectPath, 'src', 'development', 'stack.yml')
const stackProductionPath = path.join(projectPath, 'src', 'production', 'production.yml')
Expand Down Expand Up @@ -177,4 +184,42 @@ const mdjson = [
})
]

console.log(json2md(mdjson))
const md = json2md(mdjson)

if (validate) {
const readmePath = path.join(projectPath, 'README.md')
let readme

if (fs.existsSync(readmePath)) {
readme = fs.readFileSync(readmePath, 'utf8')
} else {
console.error('README.md file not found!')
process.exit(1)
}

const difference = diff.diffLines(md + '\n', readme)

if (difference.length > 1) {
difference.forEach((part) => {
let color = part.added ? 'green'
: part.removed ? 'red' : 'grey'

switch (color) {
case 'green':
color = '[32m'
break
case 'red':
color = '[31m'
break
default:
color = '[0m'
}

process.stderr.write('\x1b' + color + part.value + '\x1b[0m')
})

process.exit(difference.length)
}
} else {
console.log(md)
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ define-properties@^1.1.2, define-properties@^1.1.3:
dependencies:
object-keys "^1.0.12"

diff@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

doctrine@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa"
Expand Down

0 comments on commit 29158d4

Please sign in to comment.