Created
July 18, 2020 19:23
-
-
Save colin969/0cf3cb94ff2bfd42cd9771761b0495a5 to your computer and use it in GitHub Desktop.
This file contains 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 * as flashpoint from 'flashpoint'; | |
import * as path from 'path'; | |
import * as fs from 'fs'; | |
export function activate(context: flashpoint.ExtensionContext) { | |
const registerSubscription = (d: flashpoint.Disposable) => { | |
flashpoint.registerDisposable(context.subscriptions, d); | |
}; | |
const setDevText = (text: string) => { | |
flashpoint.status.setStatus('devConsoleText', text); | |
}; | |
registerSubscription(flashpoint.commands.registerCommand( | |
'tag-exporter.export-tags', | |
async () => { | |
// Find file to save to | |
const defaultPath = path.join(flashpoint.config.flashpointPath, 'Data'); | |
await fs.promises.access(defaultPath, fs.constants.F_OK) | |
.then(() => { /** Exists, carry on */ }) | |
.catch((err) => fs.promises.mkdir(defaultPath)); | |
const filePath = await flashpoint.openSaveDialog({ | |
title: 'Export Tags', | |
defaultPath: path.join(defaultPath, 'exported_tags.json'), | |
filters: [{ | |
name: 'Tags file', | |
extensions: ['json'], | |
}] | |
}); | |
// Find tags to export | |
setDevText('Exporting tags, please wait...'); | |
const jsonTagsFile: TagsFile = { categories: [], tags: [] }; | |
try { | |
const allTagCategories = await flashpoint.tags.findTagCategories(); | |
jsonTagsFile.categories = allTagCategories; | |
const allTags = await flashpoint.tags.findTags(); | |
jsonTagsFile.tags = allTags.map(t => { | |
const primaryAlias = t.aliases.find(a => a.id === t.primaryAliasId); | |
const bareTag: BareTag = { | |
categoryId: t.categoryId || -1, | |
description: t.description, | |
primaryAlias: primaryAlias ? primaryAlias.name : 'ERROR', | |
aliases: t.aliases.map(a => a.name) | |
}; | |
return bareTag; | |
}); | |
await fs.promises.writeFile(filePath, JSON.stringify(jsonTagsFile, null, ' '), { encoding: 'utf8' }); | |
setDevText(`${allTags.length} Tags Exported to ${path.resolve(filePath)}`); | |
} catch (error) { | |
setDevText(`Failed to export tags\n${error}`); | |
} | |
} | |
)); | |
registerSubscription(flashpoint.commands.registerCommand( | |
'tag-exporter.import-tags', | |
() => { | |
} | |
)); | |
} | |
type BareTag = { | |
categoryId: number; | |
description?: string; | |
primaryAlias: string; | |
aliases: string[]; | |
} | |
type TagsFile = { | |
categories: flashpoint.TagCategory[]; | |
tags: BareTag[] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment