2021-02-18 00:55:59 +01:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
2021-02-16 20:34:58 +01:00
|
|
|
import * as fs from 'fs'
|
|
|
|
import minimist from 'minimist'
|
|
|
|
import {exit} from 'process'
|
2022-02-14 16:38:45 +01: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:55:08 +02:00
|
|
|
import {Board} from '../../webapp/src/blocks/board'
|
2021-08-06 09:40:02 +02:00
|
|
|
import {IPropertyOption, 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-10-05 01:08:29 +02:00
|
|
|
import {createCheckboxBlock} from '../../webapp/src/blocks/checkboxBlock'
|
2021-02-16 20:34:58 +01:00
|
|
|
import {Trello} from './trello'
|
|
|
|
import {Utils} from './utils'
|
|
|
|
|
|
|
|
// HACKHACK: To allow Utils.CreateGuid to work
|
|
|
|
(global.window as any) = {}
|
|
|
|
|
2021-02-22 19:54:41 +01:00
|
|
|
const optionColors = [
|
|
|
|
// 'propColorDefault',
|
|
|
|
'propColorGray',
|
|
|
|
'propColorBrown',
|
|
|
|
'propColorOrange',
|
|
|
|
'propColorYellow',
|
|
|
|
'propColorGreen',
|
|
|
|
'propColorBlue',
|
|
|
|
'propColorPurple',
|
|
|
|
'propColorPink',
|
|
|
|
'propColorRed',
|
|
|
|
]
|
|
|
|
let optionColorIndex = 0
|
|
|
|
|
2021-02-16 20:34:58 +01:00
|
|
|
function main() {
|
|
|
|
const args: minimist.ParsedArgs = minimist(process.argv.slice(2))
|
|
|
|
|
|
|
|
const inputFile = args['i']
|
|
|
|
const outputFile = args['o'] || 'archive.focalboard'
|
|
|
|
|
|
|
|
if (!inputFile) {
|
|
|
|
showHelp()
|
|
|
|
}
|
|
|
|
|
2021-02-18 00:55:59 +01:00
|
|
|
if (!fs.existsSync(inputFile)) {
|
|
|
|
console.error(`File not found: ${inputFile}`)
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
|
2021-02-16 20:34:58 +01:00
|
|
|
// Read input
|
|
|
|
const inputData = fs.readFileSync(inputFile, 'utf-8')
|
|
|
|
const input = JSON.parse(inputData) as Trello
|
|
|
|
|
|
|
|
// Convert
|
2022-04-01 04:55:08 +02:00
|
|
|
const [boards, blocks] = convert(input)
|
2021-02-16 20:34:58 +01:00
|
|
|
|
|
|
|
// Save output
|
2021-03-02 22:21:55 +01:00
|
|
|
// TODO: Stream output
|
2022-04-01 04:55:08 +02:00
|
|
|
const outputData = ArchiveUtils.buildBlockArchive(boards, blocks)
|
2021-02-16 20:34:58 +01:00
|
|
|
fs.writeFileSync(outputFile, outputData)
|
|
|
|
|
|
|
|
console.log(`Exported to ${outputFile}`)
|
|
|
|
}
|
|
|
|
|
2022-04-01 04:55:08 +02:00
|
|
|
function convert(input: Trello): [Board[], Block[]] {
|
|
|
|
const boards: Board[] = []
|
2021-08-06 09:40:02 +02:00
|
|
|
const blocks: Block[] = []
|
2021-02-16 20:34:58 +01:00
|
|
|
|
|
|
|
// Board
|
2021-08-06 09:40:02 +02:00
|
|
|
const board = createBoard()
|
2021-02-16 20:34:58 +01:00
|
|
|
console.log(`Board: ${input.name}`)
|
|
|
|
board.title = input.name
|
2022-03-22 15:24:34 +01:00
|
|
|
board.description = input.desc
|
2021-02-16 20:34:58 +01:00
|
|
|
|
|
|
|
// Convert lists (columns) to a Select property
|
|
|
|
const optionIdMap = new Map<string, string>()
|
|
|
|
const options: IPropertyOption[] = []
|
|
|
|
input.lists.forEach(list => {
|
|
|
|
const optionId = Utils.createGuid()
|
|
|
|
optionIdMap.set(list.id, optionId)
|
2021-02-22 19:54:41 +01:00
|
|
|
const color = optionColors[optionColorIndex % optionColors.length]
|
|
|
|
optionColorIndex += 1
|
2021-02-16 20:34:58 +01:00
|
|
|
const option: IPropertyOption = {
|
|
|
|
id: optionId,
|
|
|
|
value: list.name,
|
2021-02-22 19:54:41 +01:00
|
|
|
color,
|
2021-02-16 20:34:58 +01:00
|
|
|
}
|
|
|
|
options.push(option)
|
|
|
|
})
|
|
|
|
|
|
|
|
const cardProperty: IPropertyTemplate = {
|
|
|
|
id: Utils.createGuid(),
|
|
|
|
name: 'List',
|
|
|
|
type: 'select',
|
|
|
|
options
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
board.cardProperties = [cardProperty]
|
2022-04-01 04:55:08 +02:00
|
|
|
boards.push(board)
|
2021-02-16 20:34:58 +01:00
|
|
|
|
|
|
|
// Board view
|
2021-08-06 09:40:02 +02:00
|
|
|
const view = createBoardView()
|
2021-02-16 20:34:58 +01:00
|
|
|
view.title = 'Board View'
|
2021-08-06 09:40:02 +02:00
|
|
|
view.fields.viewType = 'board'
|
2022-04-01 04:55:08 +02:00
|
|
|
view.boardId = board.id
|
2021-02-16 20:34:58 +01:00
|
|
|
view.parentId = board.id
|
|
|
|
blocks.push(view)
|
|
|
|
|
|
|
|
// Cards
|
|
|
|
input.cards.forEach(card => {
|
|
|
|
console.log(`Card: ${card.name}`)
|
|
|
|
|
2021-08-06 09:40:02 +02:00
|
|
|
const outCard = createCard()
|
2021-02-16 20:34:58 +01:00
|
|
|
outCard.title = card.name
|
2022-04-01 04:55:08 +02:00
|
|
|
outCard.boardId = board.id
|
2021-02-16 20:34:58 +01:00
|
|
|
outCard.parentId = board.id
|
|
|
|
|
|
|
|
// Map lists to Select property options
|
|
|
|
if (card.idList) {
|
|
|
|
const optionId = optionIdMap.get(card.idList)
|
|
|
|
if (optionId) {
|
2021-08-06 09:40:02 +02:00
|
|
|
outCard.fields.properties[cardProperty.id] = optionId
|
2021-02-16 20:34:58 +01:00
|
|
|
} else {
|
|
|
|
console.warn(`Invalid idList: ${card.idList} for card: ${card.name}`)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.warn(`Missing idList for card: ${card.name}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks.push(outCard)
|
|
|
|
|
|
|
|
if (card.desc) {
|
|
|
|
// console.log(`\t${card.desc}`)
|
2021-08-06 09:40:02 +02:00
|
|
|
const text = createTextBlock()
|
2021-02-16 20:34:58 +01:00
|
|
|
text.title = card.desc
|
2022-04-01 04:55:08 +02:00
|
|
|
text.boardId = board.id
|
2021-02-16 20:34:58 +01:00
|
|
|
text.parentId = outCard.id
|
|
|
|
blocks.push(text)
|
|
|
|
|
2021-08-06 09:40:02 +02:00
|
|
|
outCard.fields.contentOrder = [text.id]
|
2021-02-16 20:34:58 +01:00
|
|
|
}
|
2021-10-05 01:08:29 +02:00
|
|
|
|
|
|
|
// Add Checklists
|
|
|
|
if (card.idChecklists && card.idChecklists.length > 0) {
|
|
|
|
card.idChecklists.forEach(checklistID => {
|
|
|
|
const lookup = input.checklists.find(e => e.id === checklistID)
|
|
|
|
if (lookup) {
|
|
|
|
lookup.checkItems.forEach(trelloCheckBox=> {
|
|
|
|
const checkBlock = createCheckboxBlock()
|
|
|
|
checkBlock.title = trelloCheckBox.name
|
|
|
|
if (trelloCheckBox.state === 'complete') {
|
|
|
|
checkBlock.fields.value = true
|
|
|
|
} else {
|
|
|
|
checkBlock.fields.value = false
|
|
|
|
}
|
2022-04-01 04:55:08 +02:00
|
|
|
checkBlock.boardId = board.id
|
2021-10-05 01:08:29 +02:00
|
|
|
checkBlock.parentId = outCard.id
|
|
|
|
blocks.push(checkBlock)
|
|
|
|
|
|
|
|
outCard.fields.contentOrder.push(checkBlock.id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-16 20:34:58 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
console.log('')
|
|
|
|
console.log(`Found ${input.cards.length} card(s).`)
|
|
|
|
|
2022-04-01 04:55:08 +02:00
|
|
|
return [boards, blocks]
|
2021-02-16 20:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function showHelp() {
|
|
|
|
console.log('import -i <input.json> -o [output.focalboard]')
|
2021-02-18 00:55:59 +01:00
|
|
|
exit(1)
|
2021-02-16 20:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main()
|