Skip to content

Commit

Permalink
feat: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Demian222 committed Jun 30, 2021
1 parent 7daf053 commit 68953b4
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 45 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ Tech radar generator

```
yarn add @qiwi/tech-radar
techradar --csv-path /path/to/csv --out-dir /radar --versions 1.00 --pathPrefix js
techradar --input /path/to/csv --out-dir /radar
```
or
```
npx @qiwi/tech-radar --csvPath /path/to/csv --outDir /radar --versions 1.00 --pathPrefix js
npx @qiwi/tech-radar --input /path/to/csv --outDir /radar
```

| Args | description |
|---|---
| csvPath | path to csv file
| csvPath | path to csv/json/yml file
| outDir |
| versions |
| pathPrefix |

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"csv-parse": "^4.12.0",
"fs-extra": "^10.0.0",
"html-minifier": "^4.0.0",
"js-yaml": "^4.1.0",
"meow": "^10.0.1"
}
}
14 changes: 3 additions & 11 deletions src/main/js/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,22 @@ import {generateTechRadar} from './index.js'
export const cli = meow(
`
Usage:
techradar --csvPath /path/to/csv --outDir /radar --versions 1.00 pathPrefix js
techradar --input /path/to/csv --outDir /radar
Options
--csvPath
--input
--outDir
--version
--pathPrefix
`,
{
importMeta: import.meta,
flags: {
csvPath: {
input: {
type: 'string',
isRequired: true,
},
outDir: {
type: 'string',
isRequired: true,
},
version: {
type: 'string',
},
pathPrefix: {
type: 'string',
},
},
},
)
Expand Down
4 changes: 2 additions & 2 deletions src/main/js/e11y/.eleventy.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = (config) => {
[assetsPath]: '/',
})

config.addShortcode('makeBootScript', (title, settings, collections) => {
config.addShortcode('makeBootScript', (settings, collections) => {
const entries = collections
.map((entity) => ({
quadrant: entity.data.quadrant,
Expand All @@ -27,7 +27,7 @@ module.exports = (config) => {

const radarSettings = {
...settings,
title,
title: global.title,
entries,
}

Expand Down
33 changes: 15 additions & 18 deletions src/main/js/generateMdAssets.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import parse from 'csv-parse/lib/sync.js'
import fs from 'fs'
import fsExtra from 'fs-extra'
import path from 'path'

import { reader } from './reader.js'

const tplPath = path.resolve('src/main/tpl')

export const langAndFw = 'languages-and-frameworks'
Expand Down Expand Up @@ -55,23 +56,19 @@ moved: ${moved || '0'}
${description}`
}

export const generateMdAssets = ({ csvPath, tempDir }) => {
export const generateMdAssets = (filePath, tempDir) => {
const tempDirResolved = path.resolve(tempDir)
const csvPathResolved = path.resolve(csvPath)
fsExtra.copySync(tplPath, tempDirResolved)
const radarData = fs.readFileSync(csvPathResolved)
try {
const records = parse(radarData, { columns: true })
records.forEach(({ name, quadrant, ring, description, moved }) => {
try {
const entryFilePath = generatePath({ name, quadrant, tempDirResolved })
const content = generateMd({ ring, description, moved })
fs.writeFileSync(entryFilePath, content)
} catch (err) {
console.error(err)
}
})
} catch (err) {
console.error(err)
}
const radarDocument = reader(filePath)
radarDocument.data.forEach(({ name, quadrant, ring, description, moved }) => {
try {
const entryFilePath = generatePath({ name, quadrant, tempDirResolved })
const content = generateMd({ ring, description, moved })
fs.writeFileSync(entryFilePath, content)
} catch (err) {
console.error(err)
}
})
global.title = radarDocument.meta.title
global.legend = radarDocument.meta.legend
}
4 changes: 2 additions & 2 deletions src/main/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import path from 'path'

import { generateMdAssets } from './generateMdAssets.js'

export const generateTechRadar = async ({ csvPath, outDir }) => {
export const generateTechRadar = async ({ input, outDir }) => {
global.outDir = outDir
const tempDir = 'temp'
global.tempDir = tempDir
generateMdAssets({ csvPath, tempDir })
generateMdAssets(input, tempDir)
await generateStatics(tempDir, outDir)
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/js/reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import parse from 'csv-parse/lib/sync.js'
import fs from 'fs'
import yaml from 'js-yaml'
import path from 'path'

export const reader = (filePath) => {
if (filePath.includes('.csv', filePath.length - 4)) {
return csvReader(filePath)
}
if (filePath.includes('.json', filePath.length - 5)) {
return jsonReader(filePath)
}
if (filePath.includes('.yml', filePath.length - 5)) {
return yamlReader(filePath)
}
}

export const csvReader = (csvPath) => {
const csvPathResolved = path.resolve(csvPath)
const radarContents = fs.readFileSync(csvPathResolved, 'utf8')
const radarDocument = {
meta: {},
data: [],
}
radarContents.split('===').forEach((radarChunks) => {
const records = parse(radarChunks, {
columns: true,
skip_empty_lines: true,
})
const keys = Object.keys(records[0]).toString()

if (keys === 'name,quadrant,ring,description,moved') {
radarDocument.data = [...radarDocument.data, ...records]
}
if (keys === 'title') {
radarDocument.meta.title = records[0].title
}
if (keys === 'date') {
radarDocument.meta.date = records[0].date
}
if (keys === 'legend') {
radarDocument.meta.legend = records[0].legend
}
})
return radarDocument
}

export const jsonReader = (jsonPath) => {
const jsonPathResolved = path.resolve(jsonPath)
const fileData = fs.readFileSync(jsonPathResolved, 'utf8')
return JSON.parse(fileData)
}

export const yamlReader = (yamlPath) => {
const jsonPathResolved = path.resolve(yamlPath)
const yamlData = fs.readFileSync(jsonPathResolved, 'utf8')
return yaml.load(yamlData, 'utf8')
}
3 changes: 1 addition & 2 deletions src/main/tpl/index.njk
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
---
title: QIWI JavaScript Tech Radar — 2021.06
layout: page.njk
---

{% set bootScript %}
{% makeBootScript title, settins, collections.entries %}
{% makeBootScript settins, collections.entries %}
{% endset %}

<svg id="radar"></svg>
Expand Down
160 changes: 160 additions & 0 deletions src/test/js/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,163 @@ moved: 1
Статически типизированный ЖС",
}
`;

exports[`reader.js csvReader 1`] = `
Object {
"data": Array [
Object {
"description": "Статически типизированный ЖС",
"moved": "1",
"name": "TypeScript",
"quadrant": "language",
"ring": "Adopt",
},
Object {
"description": "",
"moved": "",
"name": "Nodejs",
"quadrant": "Platforms",
"ring": "Adopt",
},
Object {
"description": "Статический анализатор кода",
"moved": "0",
"name": "codeclimate",
"quadrant": "tools",
"ring": "Trial",
},
Object {
"description": "Унификации контракта интерфейсов различных слоев приложений",
"moved": "-1",
"name": "Гексагональная архитектура",
"quadrant": "Techniques",
"ring": "Assess",
},
],
"meta": Object {
"date": "2021-06-18",
"legend": "bla bla bla",
"title": "work it",
},
}
`;

exports[`reader.js jsonReader 1`] = `
Object {
"data": Array [
Object {
"description": "Статически типизированный ЖС",
"moved": "1",
"name": "TypeScript",
"quadrant": "language",
"ring": "Adopt",
},
Object {
"description": "",
"moved": "",
"name": "Nodejs",
"quadrant": "Platforms",
"ring": "Adopt",
},
Object {
"description": "Статический анализатор кода",
"moved": "0",
"name": "codeclimate",
"quadrant": "tools",
"ring": "Trial",
},
Object {
"description": "Унификации контракта интерфейсов различных слоев приложений",
"moved": "-1",
"name": "Гексагональная архитектура",
"quadrant": "Techniques",
"ring": "Assess",
},
],
"meta": Object {
"date": "2021-06-12",
"legend": "bla bla bla",
"title": "tech radar js",
},
}
`;

exports[`reader.js reader 1`] = `
Object {
"data": Array [
Object {
"description": "Статически типизированный ЖС",
"moved": "1",
"name": "TypeScript",
"quadrant": "language",
"ring": "Adopt",
},
Object {
"description": "",
"moved": "",
"name": "Nodejs",
"quadrant": "Platforms",
"ring": "Adopt",
},
Object {
"description": "Статический анализатор кода",
"moved": "0",
"name": "codeclimate",
"quadrant": "tools",
"ring": "Trial",
},
Object {
"description": "Унификации контракта интерфейсов различных слоев приложений",
"moved": "-1",
"name": "Гексагональная архитектура",
"quadrant": "Techniques",
"ring": "Assess",
},
],
"meta": Object {
"date": "2021-06-18",
"legend": "bla bla bla",
"title": "work it",
},
}
`;

exports[`reader.js yamlReader 1`] = `
Object {
"data": Array [
Object {
"description": "Статически типизированный ЖС",
"moved": 1,
"name": "TypeScript",
"quadrant": "language",
"ring": "Adopt",
},
Object {
"description": null,
"moved": null,
"name": "Nodejs",
"quadrant": "Platforms",
"ring": "Adopt",
},
Object {
"description": "Статический анализатор кода",
"moved": 0,
"name": "codeclimate",
"quadrant": "tools",
"ring": "Trial",
},
Object {
"description": "Унификации контракта интерфейсов различных слоев приложений",
"moved": -1,
"name": "Гексагональная архитектура",
"quadrant": "Techniques",
"ring": "Assess",
},
],
"meta": Object {
"date": 2021-06-12T00:00:00.000Z,
"legend": "bla bla bla",
"title": "tech radar js",
},
}
`;
Loading

0 comments on commit 68953b4

Please sign in to comment.