Skip to content

Commit

Permalink
feat: user addons in preference
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Jun 10, 2021
1 parent 8558043 commit 194544e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
18 changes: 15 additions & 3 deletions addons/settings/renderer/settings-line.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
v-else-if="isSimpleObject"
v-model="model"
:with-keys="spec.type === 'map'"
:pinned="spec.recommendations"
:pinned="recommendations"
>
<template #note="{ item }">
<template v-if="hasNotes">
Expand Down Expand Up @@ -146,10 +146,21 @@ export default {
return props.spec.key === 'terminal.addon.includes'
})
const recommendationsRef = computed(() => {
const recommendations = props.spec.recommendations
if (props.spec.key === 'terminal.addon.includes') {
return [
...recommendations!,
...commas.app.getUserAddons(),
]
}
return recommendations
})
function getNote(item: EditorEntryItem) {
if (props.spec.key === 'terminal.addon.includes') {
const { manifest } = commas.app.getAddonInfo(item.entry.value)
return manifest ? manifest.description : ''
const info = commas.app.getAddonInfo(item.entry.value)
return info?.manifest?.description ?? ''
}
return undefined
}
Expand Down Expand Up @@ -235,6 +246,7 @@ export default {
isCustomized: isCustomizedRef,
isChanged: isChangedRef,
hasNotes: hasNotesRef,
recommendations: recommendationsRef,
getNote,
toggle,
reset,
Expand Down
25 changes: 16 additions & 9 deletions api/modules/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,24 @@ function cloneAPI(api: Object, name: string) {

const userDataPath = isPackaged()
? getPath('userData')
: path.resolve(__dirname, '../../userData')
: path.resolve(__dirname, isMainProcess() ? '../../userdata' : '../userdata')

interface AddonInfo {
entry: string,
manifest: any,
type: 'builtin' | 'user',
}

const discoveredAddons: Record<string, AddonInfo> = {}
async function discoverAddons() {
const paths = [
path.join(userDataPath, 'addons'),
path.join(__dirname, isMainProcess() ? '../../addons' : '../addons'),
{ type: 'user' as const, base: path.join(userDataPath, 'addons') },
{ type: 'builtin' as const, base: path.join(__dirname, isMainProcess() ? '../../addons' : '../addons') },
]
for (const basePath of paths) {
for (const { type, base } of paths) {
let dirents: fs.Dirent[] = []
try {
dirents = await fs.promises.readdir(basePath, { withFileTypes: true })
dirents = await fs.promises.readdir(base, { withFileTypes: true })
} catch {
// ignore
}
Expand All @@ -80,20 +81,25 @@ async function discoverAddons() {
.filter(name => !discoveredAddons[name])
for (const name of directories) {
try {
const manifest = __non_webpack_require__(path.join(basePath, name, 'commas.json'))
const entry = path.join(basePath, name, 'index.js')
discoveredAddons[name] = { entry, manifest }
const manifest = __non_webpack_require__(path.join(base, name, 'commas.json'))
const entry = path.join(base, name, 'index.js')
discoveredAddons[name] = { type, entry, manifest }
} catch {
// continue
}
}
}
}

function getAddonInfo(name: string) {
function getAddonInfo(name: string): AddonInfo | undefined {
return discoveredAddons[name]
}

function getUserAddons() {
return Object.keys(discoveredAddons)
.filter(name => discoveredAddons[name].type === 'user')
}

const loadedAddons: string[] = []
function loadAddon(name: string, api: object) {
// Reserved names
Expand Down Expand Up @@ -135,6 +141,7 @@ export {
cloneAPI,
discoverAddons,
getAddonInfo,
getUserAddons,
loadAddon,
unloadAddon,
unloadAddons,
Expand Down

0 comments on commit 194544e

Please sign in to comment.