Created
March 3, 2023 05:37
-
-
Save naosim/e8c70fc1e4979fcae2d281152d3ef08f to your computer and use it in GitHub Desktop.
ConvertToMarkdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tableToMarkdown(table) { | |
var lines = [ | |
['カラム名', '型', 'PK', 'UNIQUE', 'NOT NULL', '備考'], | |
]; | |
lines.push(new Array(lines[0].length).fill('---')); | |
// if(table.tableNameComment.length > 0) { | |
// lines = [table.tableNameComment, ...lines]; | |
// } | |
table.columns | |
.map(v => [v.columnName, v.type, v.isPk ? '◯' : '', v.isUnique ? '◯' : '', v.isNotNull ? '◯' : '', v.comment]) | |
.forEach(v => lines.push(v)); | |
const tableText = lines.map(v => v.join('|')).join('\n'); | |
if(table.tableNameComment.length > 0) { | |
return table.tableNameComment + '\n' + tableText; | |
} | |
return tableText; | |
} | |
function output(map, nest) { | |
nest = nest || 1; | |
if(map.table) { // テーブル出力 | |
return tableToMarkdown(map.table) + '\n\nfile: ' + map.file + '\n'; | |
} | |
var header = '###########'.slice(0, nest); | |
return Object.keys(map).map(key => { | |
var text = header + ' ' + key + '\n' + output(map[key], nest + 1); | |
return text; | |
}).join('\n'); | |
} | |
export function convertToMarkdown(dbTable) { | |
var map = {}; | |
dbTable | |
// .map(v => analyzeDBTableScript(v)) | |
.forEach(v => { | |
var m = map; | |
var segs = v.file.split('/'); | |
segs.forEach(s => { | |
if(!m[s]) { | |
m[s] = {} | |
} | |
m = m[s]; | |
}); | |
m.file = v.file; | |
m.table = v.table; | |
}) | |
return output(map); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs' | |
import process from 'process'; | |
import {convertToMarkdown} from './convertToMarkdown.mjs' | |
// console.log(process.argv); | |
var json = JSON.parse(fs.readFileSync(process.argv[2], 'utf8').trim()); | |
console.log(convertToMarkdown(json)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment