From 1990b3e158d54f60f6735b5629e6decef27e8ce0 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Mon, 20 May 2024 17:08:47 -0500 Subject: [PATCH] add nonpromptable plugins install --- src/commands/build.ts | 5 ++-- src/commands/plugins.ts | 55 ++++++++++++++++++++++++++++++-------- src/index.ts | 2 +- src/prompts/plugin.ts | 31 --------------------- src/utilities/getConfig.ts | 14 ---------- 5 files changed, 47 insertions(+), 60 deletions(-) delete mode 100644 src/prompts/plugin.ts diff --git a/src/commands/build.ts b/src/commands/build.ts index 05fb2f6..e99f208 100644 --- a/src/commands/build.ts +++ b/src/commands/build.ts @@ -132,8 +132,6 @@ export async function build(options: Record) { // glob(`**/*`, { withFileTypes: true, ignore: { ignored: p => p.isDirectory() }, cwd: "./src/events/" }) // ]) - - //https://esbuild.github.io/content-types/#tsconfig-json const ctx = await esbuild.context({ entryPoints, @@ -141,10 +139,11 @@ export async function build(options: Record) { ...defaultEsbuild(buildConfig.format!, buildConfig.tsconfig), dropLabels: [buildConfig.mode === 'production' ? '__DEV__' : '__PROD__', ...buildConfig.dropLabels!], }); + + await ctx.rebuild() if(options.watch) { await ctx.watch() } else { - await ctx.rebuild() await ctx.dispose() } } diff --git a/src/commands/plugins.ts b/src/commands/plugins.ts index 4bc526b..cff9712 100644 --- a/src/commands/plugins.ts +++ b/src/commands/plugins.ts @@ -2,12 +2,13 @@ import { greenBright } from 'colorette'; import fs from 'fs'; import prompt from 'prompts'; import { fetch } from 'undici'; -import { pluginsQ } from '../prompts/plugin.js'; import { fromCwd } from '../utilities/fromCwd.js'; import esbuild from 'esbuild'; -import { getLang } from '../utilities/getConfig.js'; +import { getConfig } from '../utilities/getConfig.js'; +import type { PromptObject } from 'prompts'; import { resolve } from 'path'; import { require } from '../utilities/require.js'; + interface PluginData { description: string; hash: string; @@ -15,25 +16,57 @@ interface PluginData { author: string[]; link: string; example: string; - version: '1.0.0'; + version: string; } +const link = `https://raw.githubusercontent.com/sern-handler/awesome-plugins/main/pluginlist.json`; +export async function fetchPluginData(): Promise { + return fetch(link) + .then(res => res.json()) + .then(data => (data as PluginData[])) + .catch(() => []) +} + +export function pluginsQ(choices: PluginData[]): PromptObject[] { + return [{ + name: 'list', + type: 'autocompleteMultiselect', + message: 'What plugins do you want to install?', + choices: choices.map(e => ({ title: e.name, value: e })), + min: 1, + }]; +} /** * Installs plugins to project */ -export async function plugins() { - const e: PluginData[] = (await prompt([await pluginsQ()])).list; - if (!e) process.exit(1); - - const lang = await getLang(); - for await (const plgData of e) { +export async function plugins(args: string[], opts: Record) { + const plugins = await fetchPluginData(); + let selectedPlugins : PluginData[]; + if(args.length) { + const normalizedArgs = args.map(str => str.toLowerCase()) + console.log("Trying to find plugins to install..."); + const results = plugins.reduce((acc, cur) => { + if(normalizedArgs.includes(cur.name.toLowerCase())) { + return [...acc, cur] + } + return acc; + }, [] as PluginData[]); + selectedPlugins = results; + } else { + selectedPlugins = (await prompt(pluginsQ(plugins))).list; + } + if (!selectedPlugins.length) { + process.exit(1); + } + const { language } = await getConfig(); + for await (const plgData of selectedPlugins) { const pluginText = await download(plgData.link); const dir = fromCwd('/src/plugins'); const linkNoExtension = `${process.cwd()}/src/plugins/${plgData.name}`; if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } - if (lang === 'typescript') { + if (language === 'typescript') { fs.writeFileSync(linkNoExtension + '.ts', pluginText); } else { const { type = undefined } = require(resolve('package.json')); @@ -54,7 +87,7 @@ export async function plugins() { } } - const pluginNames = e.map((data) => { + const pluginNames = selectedPlugins.map((data) => { return 'Installed ' + data.name + ' ' + 'from ' + data.author.join(','); }); console.log(`Successfully downloaded plugin(s):\n${greenBright(pluginNames.join('\n'))}`); diff --git a/src/index.ts b/src/index.ts index e60e155..290c769 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ program program .command('plugins') .description('Install plugins from https://github.com/sern-handler/awesome-plugins') - .option('-n --name', 'Name of plugin') + .argument('[names...]', 'Names of plugins to install') .action((...args) => importDynamic('plugins.js').then((m) => m.plugins(...args))); program diff --git a/src/prompts/plugin.ts b/src/prompts/plugin.ts deleted file mode 100644 index d94efbc..0000000 --- a/src/prompts/plugin.ts +++ /dev/null @@ -1,31 +0,0 @@ -import type { Choice, PromptObject } from 'prompts'; -import { fetch } from 'undici'; - -async function gimmechoices(): Promise { - const link = `https://raw.githubusercontent.com/sern-handler/awesome-plugins/main/pluginlist.json`; - - const resp = await fetch(link).catch(() => null); - if (!resp) return [{ title: 'No plugins found!', value: '', disabled: true }]; - const data = (await resp.json()) as Data[]; - const choices = data.map((e) => ({ - title: e.name, - value: e, - })); - - return choices; -} - -export async function pluginsQ(): Promise { - return { - name: 'list', - type: 'autocompleteMultiselect', - message: 'What plugins do you want to install?', - choices: await gimmechoices(), - min: 1, - }; -} - -interface Data { - name: string; - link: string; -} diff --git a/src/utilities/getConfig.ts b/src/utilities/getConfig.ts index aa3bba8..6e163ae 100644 --- a/src/utilities/getConfig.ts +++ b/src/utilities/getConfig.ts @@ -1,21 +1,7 @@ import { findUp } from 'find-up'; import { readFile } from 'node:fs/promises'; import assert from 'node:assert'; -/** - * It finds the sern.config.json file, reads it, and returns the language property - * @returns The language of the project. - */ -export async function getLang(): Promise<'typescript' | 'javascript'> { - const sernLocation = await findUp('sern.config.json'); - if (!sernLocation) throw new Error("Can't find sern.config.json"); - - const output = JSON.parse(await readFile(sernLocation, 'utf8')); - - if (!output) throw new Error("Can't read your sern.config.json."); - - return output.language; -} export async function getConfig(): Promise { const sernLocation = await findUp('sern.config.json');