Skip to content

Commit

Permalink
feat: add a plugin loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Mar 27, 2023
1 parent b6b579e commit f7bbd41
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions bin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env node

import fs from 'fs'
import path from 'path'
import os from 'os'
import sade from 'sade'
import open from 'open'
import { getPkg } from './lib.js'
import { getClient, getPkg } from './lib.js'
import {
accessClaim,
authorize,
Expand Down Expand Up @@ -163,4 +166,20 @@ Run \`$ w3 --help\` for more info.
}
})

cli.parse(process.argv)
async function loadPlugins () {
try {
const dir = path.join(os.homedir(), '.w3cli', 'plugins')
const files = await fs.promises.readdir(dir)
if (!files.length) return

const client = await getClient()
for (const file of files) {
const module = await import(path.join(dir, file, 'index.js'))
await module.plugin(cli, client)
}
} catch (err) {
if (err.code !== 'ENOENT') throw err
}
}

loadPlugins().then(() => cli.parse(process.argv))

0 comments on commit f7bbd41

Please sign in to comment.