mirror of
https://github.com/sern-handler/cli
synced 2026-06-20 23:02:23 +00:00
add nonpromptable plugins install
This commit is contained in:
@@ -132,8 +132,6 @@ export async function build(options: Record<string, any>) {
|
||||
// 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<string, any>) {
|
||||
...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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<PluginData[]> {
|
||||
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<string, unknown>) {
|
||||
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'))}`);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import type { Choice, PromptObject } from 'prompts';
|
||||
import { fetch } from 'undici';
|
||||
|
||||
async function gimmechoices(): Promise<Choice[]> {
|
||||
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<PromptObject> {
|
||||
return {
|
||||
name: 'list',
|
||||
type: 'autocompleteMultiselect',
|
||||
message: 'What plugins do you want to install?',
|
||||
choices: await gimmechoices(),
|
||||
min: 1,
|
||||
};
|
||||
}
|
||||
|
||||
interface Data {
|
||||
name: string;
|
||||
link: string;
|
||||
}
|
||||
@@ -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<SernConfig> {
|
||||
const sernLocation = await findUp('sern.config.json');
|
||||
|
||||
Reference in New Issue
Block a user