2021-02-22 20:58:50 +02:00
|
|
|
import csv from 'csvtojson'
|
|
|
|
import * as fs from 'fs'
|
|
|
|
import minimist from 'minimist'
|
|
|
|
import path from 'path'
|
|
|
|
import {exit} from 'process'
|
2022-02-14 17:38:45 +02:00
|
|
|
import {ArchiveUtils} from '../util/archive'
|
2021-08-06 09:40:02 +02:00
|
|
|
import {Block} from '../../webapp/src/blocks/block'
|
2022-04-01 04:41:13 +02:00
|
|
|
import {Board} from '../../webapp/src/blocks/board'
|
2021-08-06 09:40:02 +02:00
|
|
|
import {IPropertyTemplate, createBoard} from '../../webapp/src/blocks/board'
|
|
|
|
import {createBoardView} from '../../webapp/src/blocks/boardView'
|
|
|
|
import {createCard} from '../../webapp/src/blocks/card'
|
|
|
|
import {createTextBlock} from '../../webapp/src/blocks/textBlock'
|
2021-02-22 20:58:50 +02:00
|
|
|
import {Utils} from './utils'
|
|
|
|
|
|
|
|
// HACKHACK: To allow Utils.CreateGuid to work
|
|
|
|
(global.window as any) = {}
|
|
|
|
|
|
|
|
let markdownFolder: string
|
|
|
|
|
|
|
|
const optionColors = [
|
|
|
|
// 'propColorDefault',
|
|
|
|
'propColorGray',
|
|
|
|
'propColorBrown',
|
|
|
|
'propColorOrange',
|
|
|
|
'propColorYellow',
|
|
|
|
'propColorGreen',
|
|
|
|
'propColorBlue',
|
|
|
|
'propColorPurple',
|
|
|
|
'propColorPink',
|
|
|
|
'propColorRed',
|
|
|
|
]
|
|
|
|
let optionColorIndex = 0
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const args: minimist.ParsedArgs = minimist(process.argv.slice(2))
|
|
|
|
|
|
|
|
const inputFolder = args['i']
|
2022-10-19 21:33:06 +02:00
|
|
|
const outputFile = args['o'] || 'archive.boardarchive'
|
2021-02-22 20:58:50 +02:00
|
|
|
|
|
|
|
if (!inputFolder) {
|
|
|
|
showHelp()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync(inputFolder)){
|
|
|
|
console.log(`Folder not found: ${inputFolder}`)
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
const inputFile = getCsvFilePath(inputFolder)
|
|
|
|
if (!inputFile) {
|
|
|
|
console.log(`.csv file not found in folder: ${inputFolder}`)
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`inputFile: ${inputFile}`)
|
|
|
|
|
|
|
|
// Read input
|
|
|
|
const input = await csv().fromFile(inputFile)
|
|
|
|
|
|
|
|
console.log(`Read ${input.length} rows.`)
|
|
|
|
|
|
|
|
console.log(input)
|
|
|
|
|
|
|
|
const basename = path.basename(inputFile, '.csv')
|
|
|
|
const components = basename.split(' ')
|
|
|
|
components.pop()
|
|
|
|
const title = components.join(' ')
|
|
|
|
|
|
|
|
console.log(`title: ${title}`)
|
|
|
|
|
|
|
|
markdownFolder = path.join(inputFolder, basename)
|
|
|
|
|
|
|
|
// Convert
|
2022-04-01 04:41:13 +02:00
|
|
|
const [boards, blocks] = convert(input, title)
|
2021-02-22 20:58:50 +02:00
|
|
|
|
|
|
|
// Save output
|
2021-03-02 23:21:55 +02:00
|
|
|
// TODO: Stream output
|
2022-04-01 04:41:13 +02:00
|
|
|
const outputData = ArchiveUtils.buildBlockArchive(boards, blocks)
|
2021-02-22 20:58:50 +02:00
|
|
|
fs.writeFileSync(outputFile, outputData)
|
|
|
|
|
|
|
|
console.log(`Exported to ${outputFile}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCsvFilePath(inputFolder: string): string | undefined {
|
|
|
|
const files = fs.readdirSync(inputFolder)
|
|
|
|
const file = files.find(o => path.extname(o).toLowerCase() === '.csv')
|
|
|
|
|
|
|
|
return file ? path.join(inputFolder, file) : undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMarkdown(cardTitle: string): string | undefined {
|
2021-08-06 09:40:02 +02:00
|
|
|
if (!fs.existsSync(markdownFolder)){ return undefined}
|
2021-02-22 20:58:50 +02:00
|
|
|
const files = fs.readdirSync(markdownFolder)
|
|
|
|
const file = files.find((o) => {
|
|
|
|
const basename = path.basename(o)
|
|
|
|
const components = basename.split(' ')
|
|
|
|
const fileCardTitle = components.slice(0, components.length-1).join(' ')
|
|
|
|
if (fileCardTitle === cardTitle) {
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
const filePath = path.join(markdownFolder, file)
|
|
|
|
const markdown = fs.readFileSync(filePath, 'utf-8')
|
|
|
|
|
|
|
|
// TODO: Remove header from markdown, which repets card title and properties
|
|
|
|
return markdown
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColumns(input: any[]) {
|
|
|
|
const row = input[0]
|
|
|
|
const keys = Object.keys(row)
|
|
|
|
// The first key (column) is the card title
|
|
|
|
return keys.slice(1)
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:41:13 +02:00
|
|
|
function convert(input: any[], title: string): [Board[], Block[]] {
|
|
|
|
const boards: Board[] = []
|
2021-08-06 09:40:02 +02:00
|
|
|
const blocks: Block[] = []
|
2021-02-22 20:58:50 +02:00
|
|
|
|
|
|
|
// Board
|
2021-08-06 09:40:02 +02:00
|
|
|
const board = createBoard()
|
2021-02-22 20:58:50 +02:00
|
|
|
console.log(`Board: ${title}`)
|
|
|
|
board.title = title
|
|
|
|
|
|
|
|
// Each column is a card property
|
|
|
|
const columns = getColumns(input)
|
|
|
|
columns.forEach(column => {
|
|
|
|
const cardProperty: IPropertyTemplate = {
|
|
|
|
id: Utils.createGuid(),
|
|
|
|
name: column,
|
|
|
|
type: 'select',
|
|
|
|
options: []
|
|
|
|
}
|
2022-03-22 16:24:34 +02:00
|
|
|
board.cardProperties.push(cardProperty)
|
2021-02-22 20:58:50 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Set all column types to select
|
|
|
|
// TODO: Detect column type
|
2022-04-01 04:41:13 +02:00
|
|
|
boards.push(board)
|
2021-02-22 20:58:50 +02:00
|
|
|
|
|
|
|
// Board view
|
2021-08-06 09:40:02 +02:00
|
|
|
const view = createBoardView()
|
2021-02-22 20:58:50 +02:00
|
|
|
view.title = 'Board View'
|
2021-08-06 09:40:02 +02:00
|
|
|
view.fields.viewType = 'board'
|
2022-04-01 04:41:13 +02:00
|
|
|
view.boardId = board.id
|
2021-02-22 20:58:50 +02:00
|
|
|
view.parentId = board.id
|
|
|
|
blocks.push(view)
|
|
|
|
|
|
|
|
// Cards
|
|
|
|
input.forEach(row => {
|
|
|
|
const keys = Object.keys(row)
|
|
|
|
console.log(keys)
|
|
|
|
if (keys.length < 1) {
|
|
|
|
console.error(`Expected at least one column`)
|
2021-03-02 23:21:55 +02:00
|
|
|
return blocks
|
2021-02-22 20:58:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const titleKey = keys[0]
|
|
|
|
const title = row[titleKey]
|
|
|
|
|
|
|
|
console.log(`Card: ${title}`)
|
|
|
|
|
2021-08-06 09:40:02 +02:00
|
|
|
const outCard = createCard()
|
2021-02-22 20:58:50 +02:00
|
|
|
outCard.title = title
|
2022-04-01 04:41:13 +02:00
|
|
|
outCard.boardId = board.id
|
2021-02-22 20:58:50 +02:00
|
|
|
outCard.parentId = board.id
|
|
|
|
|
|
|
|
// Card properties, skip first key which is the title
|
|
|
|
for (const key of keys.slice(1)) {
|
|
|
|
const value = row[key]
|
|
|
|
if (!value) {
|
|
|
|
// Skip empty values
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
const cardProperty = board.cardProperties.find((o) => o.name === key)!
|
2021-02-22 20:58:50 +02:00
|
|
|
let option = cardProperty.options.find((o) => o.value === value)
|
|
|
|
if (!option) {
|
|
|
|
const color = optionColors[optionColorIndex % optionColors.length]
|
|
|
|
optionColorIndex += 1
|
|
|
|
option = {
|
|
|
|
id: Utils.createGuid(),
|
|
|
|
value,
|
|
|
|
color: color,
|
|
|
|
}
|
|
|
|
cardProperty.options.push(option)
|
|
|
|
}
|
|
|
|
|
2021-08-06 09:40:02 +02:00
|
|
|
outCard.fields.properties[cardProperty.id] = option.id
|
2021-02-22 20:58:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blocks.push(outCard)
|
|
|
|
|
|
|
|
// Card notes from markdown
|
|
|
|
const markdown = getMarkdown(title)
|
|
|
|
if (markdown) {
|
|
|
|
console.log(`Markdown: ${markdown.length} bytes`)
|
2021-08-06 09:40:02 +02:00
|
|
|
const text = createTextBlock()
|
2021-02-22 20:58:50 +02:00
|
|
|
text.title = markdown
|
2022-04-01 04:41:13 +02:00
|
|
|
text.boardId = board.id
|
2021-02-22 20:58:50 +02:00
|
|
|
text.parentId = outCard.id
|
|
|
|
blocks.push(text)
|
|
|
|
|
2021-08-06 09:40:02 +02:00
|
|
|
outCard.fields.contentOrder = [text.id]
|
2021-02-22 20:58:50 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
console.log('')
|
|
|
|
console.log(`Found ${input.length} card(s).`)
|
|
|
|
|
2022-04-01 04:41:13 +02:00
|
|
|
return [boards, blocks]
|
2021-02-22 20:58:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function showHelp() {
|
2022-10-19 21:33:06 +02:00
|
|
|
console.log('import -i <input.json> -o [output.boardarchive]')
|
2021-02-22 20:58:50 +02:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|