mirror of
https://github.com/sern-handler/cli
synced 2026-06-06 09:26:52 +00:00
Compare commits
6 Commits
feat/watch
...
v1.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bb212a3be | ||
|
|
2d6e65a1e6 | ||
|
|
eb53ecb638 | ||
|
|
303ac0280c | ||
|
|
3f994d6948 | ||
|
|
a5cb66828e |
21
CHANGELOG.md
21
CHANGELOG.md
@@ -2,6 +2,27 @@
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## [1.2.0](https://github.com/sern-handler/cli/compare/v1.1.0...v1.2.0) (2024-01-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* make application id optional, thanks [@trueharuu](https://github.com/trueharuu) ([#130](https://github.com/sern-handler/cli/issues/130)) ([2d6e65a](https://github.com/sern-handler/cli/commit/2d6e65a1e6073f605aab192b8cea33037a04af2c))
|
||||
|
||||
## [1.1.0](https://github.com/sern-handler/cli/compare/v1.0.3...v1.1.0) (2024-01-28)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* command clear ([#128](https://github.com/sern-handler/cli/issues/128)) ([303ac02](https://github.com/sern-handler/cli/commit/303ac0280c7c7c55f2670d49c9685b911670bc05))
|
||||
|
||||
## [1.0.3](https://github.com/sern-handler/cli/compare/v1.0.2...v1.0.3) (2023-12-25)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* intellisense for esm build ts ([#126](https://github.com/sern-handler/cli/issues/126)) ([a5cb668](https://github.com/sern-handler/cli/commit/a5cb66828eae47d3eac5b86c7e67c01dbed500d5))
|
||||
|
||||
## [1.0.2](https://github.com/sern-handler/cli/compare/v1.0.1...v1.0.2) (2023-11-04)
|
||||
|
||||
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@sern/cli",
|
||||
"version": "1.0.2",
|
||||
"version": "1.2.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@sern/cli",
|
||||
"version": "1.0.2",
|
||||
"version": "1.2.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@esbuild-kit/cjs-loader": "^2.4.2",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@sern/cli",
|
||||
"version": "1.0.2",
|
||||
"version": "1.2.0",
|
||||
"description": "Official CLI for @sern/handler",
|
||||
"exports": "./dist/index.js",
|
||||
"bin": {
|
||||
|
||||
@@ -54,19 +54,23 @@ export async function build(options: Record<string, any>) {
|
||||
}
|
||||
const sernConfig = await getConfig();
|
||||
let buildConfig: Partial<BuildOptions> = {};
|
||||
|
||||
const entryPoints = await glob(`./src/**/*{${validExtensions.join(',')}}`, {
|
||||
//for some reason, my ignore glob wasn't registering correctly'
|
||||
ignore: {
|
||||
ignored: (p) => p.name.endsWith('.d.ts'),
|
||||
},
|
||||
});
|
||||
|
||||
const buildConfigPath = resolve(options.project ?? 'sern.build.js');
|
||||
|
||||
const resolveBuildConfig = (path: string|undefined, language: string) => {
|
||||
if(language === 'javascript') {
|
||||
return path ?? resolve('jsconfig.json')
|
||||
}
|
||||
return path ?? resolve('tsconfig.json')
|
||||
}
|
||||
|
||||
const defaultBuildConfig = {
|
||||
defineVersion: true,
|
||||
format: options.format ?? 'esm',
|
||||
@@ -76,28 +80,23 @@ export async function build(options: Record<string, any>) {
|
||||
env: options.env ?? resolve('.env'),
|
||||
};
|
||||
if (pathExistsSync(buildConfigPath)) {
|
||||
try {
|
||||
buildConfig = {
|
||||
...defaultBuildConfig,
|
||||
...(await import('file:///' + buildConfigPath)).default,
|
||||
};
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
//throwable, buildConfigPath may not exist
|
||||
buildConfig = {
|
||||
...defaultBuildConfig,
|
||||
...(await import('file:///' + buildConfigPath)).default,
|
||||
};
|
||||
} else {
|
||||
buildConfig = defaultBuildConfig;
|
||||
console.log('No build config found, defaulting');
|
||||
}
|
||||
|
||||
let env = {} as Record<string, string>;
|
||||
configDotenv({ path: buildConfig.env, processEnv: env });
|
||||
|
||||
if (env.MODE && !env.NODE_ENV) {
|
||||
const modeButNotNodeEnvExists = env.MODE && !env.NODE_ENV;
|
||||
if (modeButNotNodeEnvExists) {
|
||||
console.warn('Use NODE_ENV instead of MODE');
|
||||
console.warn('MODE has no effect.');
|
||||
console.warn(`https://nodejs.dev/en/learn/nodejs-the-difference-between-development-and-production/`);
|
||||
console.warn(`https://nodejs.org/en/learn/getting-started/nodejs-the-difference-between-development-and-production`);
|
||||
}
|
||||
|
||||
if (env.NODE_ENV) {
|
||||
@@ -110,7 +109,7 @@ export async function build(options: Record<string, any>) {
|
||||
|
||||
try {
|
||||
let config = require(buildConfig.tsconfig!);
|
||||
config.extends && console.warn("Please ensure to extend the generated tsconfig")
|
||||
config.extends && console.warn("Extend the generated tsconfig")
|
||||
} catch(e) {
|
||||
console.warn("no tsconfig / jsconfig found");
|
||||
console.warn(`Please create a ${sernConfig.language === 'javascript' ? 'jsconfig.json' : 'tsconfig.json' }`);
|
||||
@@ -130,11 +129,11 @@ export async function build(options: Record<string, any>) {
|
||||
console.log(' ', magentaBright('env'), buildConfig.env);
|
||||
|
||||
const sernDir = resolve('.sern'),
|
||||
genDir = resolve(sernDir, 'generated'),
|
||||
ambientFilePath = resolve(sernDir, 'ambient.d.ts'),
|
||||
packageJsonPath = resolve('package.json'),
|
||||
sernTsConfigPath = resolve(sernDir, 'tsconfig.json'),
|
||||
packageJson = () => require(packageJsonPath);
|
||||
genDir = resolve(sernDir, 'generated'),
|
||||
ambientFilePath = resolve(sernDir, 'ambient.d.ts'),
|
||||
packageJsonPath = resolve('package.json'),
|
||||
sernTsConfigPath = resolve(sernDir, 'tsconfig.json'),
|
||||
packageJson = () => require(packageJsonPath);
|
||||
|
||||
if (!(await pathExists(genDir))) {
|
||||
console.log('Making .sern/generated dir, does not exist');
|
||||
|
||||
60
src/commands/command-clear.ts
Normal file
60
src/commands/command-clear.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import * as Rest from '../rest.js'
|
||||
import assert from 'node:assert'
|
||||
import dotenv from 'dotenv'
|
||||
import ora from 'ora';
|
||||
import type { CommandData, GuildId } from '../utilities/types.js';
|
||||
import { readFileSync, writeFile } from 'node:fs'
|
||||
import { resolve } from 'node:path'
|
||||
import prompts from 'prompts';
|
||||
|
||||
const getConfirmation = (args: Record<string,any> ) => {
|
||||
if(args.yes) {
|
||||
return args.yes
|
||||
} else {
|
||||
return prompts({
|
||||
type: 'confirm',
|
||||
name: 'confirmation',
|
||||
message: 'Are you sure you want to delete ALL your application commands?',
|
||||
initial: true
|
||||
}, { onCancel: () => (console.log("Cancelled operation ( ̄┰ ̄*)"), process.exit(1)) })
|
||||
.then(response => response.confirmation);
|
||||
}
|
||||
}
|
||||
export async function commandClear(args: Record<string,any>) {
|
||||
dotenv.configDotenv({ path: args.env || resolve('.env') })
|
||||
const token = process.env.token || process.env.DISCORD_TOKEN;
|
||||
assert(token, 'Could not find a token for this bot in .env or commandline. Do you have DISCORD_TOKEN in env?');
|
||||
|
||||
const confirmation = await getConfirmation(args);
|
||||
|
||||
if (confirmation) {
|
||||
const spin = ora({
|
||||
text: `Deleting ALL application commands...`,
|
||||
spinner: 'aesthetic',
|
||||
}).start();
|
||||
const rest = await Rest.create(token);
|
||||
let guildCommands: Record<GuildId, CommandData[]>
|
||||
try {
|
||||
guildCommands = JSON.parse(readFileSync('.sern/command-data-remote.json', 'utf-8'))
|
||||
await rest.updateGlobal([]);
|
||||
delete guildCommands.global
|
||||
for (const guildId in guildCommands) {
|
||||
await rest.putGuildCommands(guildId, []);
|
||||
}
|
||||
writeFile('.sern/command-data-remote.json', "{}", (err) => {
|
||||
if(err) {
|
||||
spin.fail("Error happened while writing to json:");
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
})
|
||||
spin.succeed();
|
||||
} catch(e) {
|
||||
spin.fail("Something went wrong. ");
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
console.log('Operation canceled. ( ̄┰ ̄*)');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,9 +14,11 @@ export function list() {
|
||||
const globalCommands = commands.global;
|
||||
|
||||
delete commands.global;
|
||||
console.log(bold('Global Commands'));
|
||||
for (const command of globalCommands) log(command);
|
||||
|
||||
if(globalCommands) {
|
||||
console.log(bold('Global Commands'));
|
||||
for (const command of globalCommands) log(command);
|
||||
}
|
||||
|
||||
console.log('\t');
|
||||
|
||||
for (const guildId in commands) {
|
||||
|
||||
@@ -15,7 +15,7 @@ export async function publish(commandDir: string | undefined, args: Partial<Publ
|
||||
args.import ??= [];
|
||||
|
||||
args.token && console.info('Token passed through command line');
|
||||
args.applicationId && console.info('applicationId passed through command line');
|
||||
args.applicationId && console.info(magentaBright('WARNING')+ ' This option is deprecated. Do not pass applicationId through command line');
|
||||
commandDir && console.info('Publishing with override path: ', commandDir);
|
||||
|
||||
const dotenvLocation = new URL('../node_modules/dotenv/config.js', rootPath),
|
||||
|
||||
@@ -10,7 +10,7 @@ import { once } from 'node:events';
|
||||
import * as Rest from './rest';
|
||||
import type { sernConfig } from './utilities/getConfig';
|
||||
import type { PublishableData, PublishableModule, Typeable } from './create-publish.d.ts';
|
||||
import { cyanBright, greenBright, redBright } from 'colorette';
|
||||
import { cyanBright, greenBright, magentaBright, redBright } from 'colorette';
|
||||
import ora from 'ora';
|
||||
|
||||
async function deriveFileInfo(dir: string, file: string) {
|
||||
@@ -163,8 +163,7 @@ const publishableData = modules.map(makePublishData),
|
||||
appid = process.env.applicationId || process.env.APPLICATION_ID;
|
||||
|
||||
assert(token, 'Could not find a token for this bot in .env or commandline. Do you have DISCORD_TOKEN in env?');
|
||||
assert(appid, 'Could not find an application id for this bot in .env or commandline. Do you have APPLICATION_ID in env?');
|
||||
|
||||
appid && console.warn(`${magentaBright('WARNING')}: APPLICATION_ID is not necessary anymore`);
|
||||
// partition globally published and guilded commands
|
||||
const [globalCommands, guildedCommands] = publishableData.reduce(
|
||||
([globals, guilded], module) => {
|
||||
@@ -181,7 +180,7 @@ const spin = ora(`Publishing ${cyanBright('Global')} commands`);
|
||||
|
||||
globalCommands.length && spin.start();
|
||||
|
||||
const rest = Rest.create(appid, token);
|
||||
const rest = await Rest.create(token);
|
||||
const res = await rest.updateGlobal(globalCommands);
|
||||
|
||||
let globalCommandsResponse: unknown;
|
||||
|
||||
12
src/index.ts
12
src/index.ts
@@ -43,12 +43,16 @@ program //
|
||||
.option('--appId [applicationId]')
|
||||
.argument('[path]', 'path with respect to current working directory that will locate all published files')
|
||||
.action(async (...args) => importDynamic('publish.js').then((m) => m.publish(...args)))
|
||||
)
|
||||
.addCommand(
|
||||
).addCommand(
|
||||
new Command('list') //
|
||||
.description('List all slash commands')
|
||||
.action(async (...args) => importDynamic('list.js').then((m) => m.list(...args)))
|
||||
);
|
||||
.action(async (...args) => importDynamic('list.js').then((m) => m.list(...args))))
|
||||
.addCommand(
|
||||
new Command('clear')
|
||||
.description('Clear and reset commands-data-remote.json and the api')
|
||||
.option('-y, --yes', "Say yes to all prompts")
|
||||
.option('-e, --env [path]', "Supply a path to a .env")
|
||||
.action(async (...args) => importDynamic('command-clear.js').then((m) => m.commandClear(...args))));
|
||||
|
||||
program
|
||||
.command('build')
|
||||
|
||||
14
src/rest.ts
14
src/rest.ts
@@ -11,12 +11,22 @@ const publishablesIntoJson = (ps: PublishableModule[]) =>
|
||||
4
|
||||
);
|
||||
|
||||
export const create = (appid: string, token: string) => {
|
||||
const globalURL = new URL(`${appid}/commands`, baseURL);
|
||||
export const create = async (token: string) => {
|
||||
const headers = {
|
||||
Authorization: 'Bot ' + token,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
let me;
|
||||
let appid: string;
|
||||
try {
|
||||
me = await fetch(new URL('@me', baseURL), { headers }).then(res => res.json());
|
||||
appid = me.id;
|
||||
} catch(e) {
|
||||
console.log("Something went wrong while trying to fetch your application:");
|
||||
throw e;
|
||||
}
|
||||
const globalURL = new URL(`${appid}/commands`, baseURL);
|
||||
|
||||
return {
|
||||
updateGlobal: (commands: PublishableModule[]) =>
|
||||
fetch(globalURL, {
|
||||
|
||||
2
src/types/config.d.ts
vendored
2
src/types/config.d.ts
vendored
@@ -13,7 +13,7 @@ export interface sernConfig {
|
||||
|
||||
export interface TheoreticalEnv {
|
||||
DISCORD_TOKEN: string;
|
||||
APPLICATION_ID: string;
|
||||
APPLICATION_ID?: string;
|
||||
MODE: 'PROD' | 'DEV';
|
||||
[name: string]: string;
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ const processEnvType = (env: NodeJS.ProcessEnv) => {
|
||||
const envBuilder = new StringWriter();
|
||||
|
||||
for (const key of entries) {
|
||||
envBuilder.tab();
|
||||
envBuilder.tab();
|
||||
envBuilder.envField(key);
|
||||
envBuilder.tab()
|
||||
.tab()
|
||||
.envField(key);
|
||||
}
|
||||
return envBuilder.build();
|
||||
};
|
||||
@@ -40,6 +40,8 @@ const writeTsConfig = async (format: 'cjs' | 'esm', configPath: string, fw: File
|
||||
const target = format === 'esm' ? { target: 'esnext' } : {};
|
||||
const sernTsConfig = {
|
||||
compilerOptions: {
|
||||
//module determines top level await. CJS doesn't have that abliity afaik
|
||||
module: format === 'cjs' ? 'node' : 'esnext',
|
||||
moduleResolution: 'node',
|
||||
strict: true,
|
||||
skipLibCheck: true,
|
||||
@@ -69,6 +71,7 @@ class StringWriter {
|
||||
return this;
|
||||
}
|
||||
envField(key: string) {
|
||||
//if env field has space or parens, wrap key in ""
|
||||
if (/\s|\(|\)/g.test(key)) {
|
||||
this.fileString += `"${key}": string`;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user