reciple 10.0.5 → 10.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/{_@oxc-project_runtime@0.110.0 → _@oxc-project_runtime@0.108.0}/helpers/decorate.mjs +1 -1
- package/dist/bin/commands/build.d.mts +1 -6
- package/dist/bin/commands/build.mjs +3 -2
- package/dist/bin/commands/build.mjs.map +1 -1
- package/dist/bin/commands/create.mjs +2 -3
- package/dist/bin/commands/create.mjs.map +1 -1
- package/dist/bin/commands/createModule.d.mts +0 -1
- package/dist/bin/commands/createModule.mjs +3 -2
- package/dist/bin/commands/createModule.mjs.map +1 -1
- package/dist/bin/commands/start.d.mts +0 -1
- package/dist/bin/commands/start.mjs +26 -25
- package/dist/bin/commands/start.mjs.map +1 -1
- package/dist/bin/commands/startSharding.mjs +21 -20
- package/dist/bin/commands/startSharding.mjs.map +1 -1
- package/dist/classes/cli/CLI.d.mts +1 -0
- package/dist/classes/cli/CLI.mjs +9 -1
- package/dist/classes/cli/CLI.mjs.map +1 -1
- package/dist/classes/cli/ConfigReader.d.mts +1 -4
- package/dist/classes/cli/ConfigReader.mjs +3 -8
- package/dist/classes/cli/ConfigReader.mjs.map +1 -1
- package/dist/classes/client/ModuleLoader.mjs +18 -18
- package/dist/classes/client/ModuleLoader.mjs.map +1 -1
- package/dist/classes/managers/ModuleManager.mjs +1 -1
- package/dist/classes/modules/PostconditionModule.mjs +1 -1
- package/dist/classes/modules/PreconditionModule.mjs +1 -1
- package/dist/classes/modules/commands/ContextMenuCommandModule.mjs +1 -1
- package/dist/classes/modules/commands/MessageCommandModule.mjs +1 -1
- package/dist/classes/modules/commands/SlashCommandModule.mjs +1 -1
- package/dist/classes/templates/ModuleTemplateBuilder.mjs +19 -19
- package/dist/classes/templates/ModuleTemplateBuilder.mjs.map +1 -1
- package/dist/classes/templates/TemplateBuilder.d.mts +1 -3
- package/dist/classes/templates/TemplateBuilder.mjs +30 -38
- package/dist/classes/templates/TemplateBuilder.mjs.map +1 -1
- package/dist/classes/validation/BaseModuleValidator.d.mts +10 -10
- package/dist/classes/validation/CommandModuleValidator.d.mts +17 -17
- package/dist/classes/validation/EventModuleValidator.d.mts +10 -10
- package/dist/classes/validation/PreconditionModule.d.mts +7 -7
- package/dist/package.mjs +1 -1
- package/package.json +1 -1
|
@@ -30,16 +30,13 @@ var ConfigReader = class ConfigReader {
|
|
|
30
30
|
this.filepath = filepath;
|
|
31
31
|
}
|
|
32
32
|
async read(options) {
|
|
33
|
-
const originalPath = process.cwd();
|
|
34
|
-
process.chdir(path.dirname(this.filepath));
|
|
35
33
|
const { module } = await unrun({
|
|
36
34
|
...options,
|
|
37
35
|
debug: isDebugging(),
|
|
38
36
|
preset: "bundle-require",
|
|
39
37
|
path: this.filepath
|
|
40
38
|
});
|
|
41
|
-
|
|
42
|
-
if (!module || !module.client || !options?.ignoreInstanceCheck && !(module.client instanceof Client)) throw new RecipleError(`exported client is not an instance of ${colors.cyan("Client")} from ${colors.green("\"@reciple/core\"")}.`);
|
|
39
|
+
if (!module || !module.client) throw new RecipleError(`exported client is not an instance of ${colors.cyan("Client")} from ${colors.green("\"@reciple/core\"")}.`);
|
|
43
40
|
this._client = module.client;
|
|
44
41
|
this._config = module.config;
|
|
45
42
|
this._build = module.build;
|
|
@@ -63,9 +60,7 @@ var ConfigReader = class ConfigReader {
|
|
|
63
60
|
}
|
|
64
61
|
static async find(options) {
|
|
65
62
|
const filenames = ConfigReader.configFilenames.filter((f) => !options?.lang || f.endsWith(options.lang));
|
|
66
|
-
const
|
|
67
|
-
const directories = [cwd];
|
|
68
|
-
directories.push(...options?.directories ?? [path.join(cwd, ".config")]);
|
|
63
|
+
const directories = options?.directories ?? [".", ".config"];
|
|
69
64
|
for (const directory of directories) {
|
|
70
65
|
if (!(await stat(directory).catch(() => void 0))?.isDirectory()) continue;
|
|
71
66
|
const file = (await readdir(directory)).find((f) => filenames.includes(f));
|
|
@@ -78,7 +73,7 @@ var ConfigReader = class ConfigReader {
|
|
|
78
73
|
async function getProjectLang(cwd) {
|
|
79
74
|
const hasTsConfig = !!await resolveTSConfig(cwd, { try: true });
|
|
80
75
|
const configLangIsTypescript = !!await ConfigReader.find({
|
|
81
|
-
cwd,
|
|
76
|
+
directories: [cwd, path.join(cwd, ".config")],
|
|
82
77
|
lang: "ts"
|
|
83
78
|
});
|
|
84
79
|
return hasTsConfig || configLangIsTypescript ? "ts" : "js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConfigReader.mjs","names":[],"sources":["../../../src/classes/cli/ConfigReader.ts"],"sourcesContent":["import type { ModuleLoader } from '../client/ModuleLoader.js';\nimport type { ModuleManager } from '../managers/ModuleManager.js';\nimport type { EventListeners } from '../client/EventListeners.js';\nimport type { Logger } from '@prtty/print';\nimport { CLI } from './CLI.js';\nimport { Client, RecipleError, type Config } from '@reciple/core';\nimport type { BuildConfig } from '../../helpers/types.js';\nimport type { UserConfig as TsdownConfig } from 'tsdown';\nimport path from 'node:path';\nimport { mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';\nimport { resolveTSConfig } from 'pkg-types';\nimport { unrun, type Options as UnrunOptions } from 'unrun';\nimport { colors } from '@prtty/prtty';\nimport type { ShardingManagerOptions } from 'discord.js';\nimport { isDebugging } from '@reciple/utils';\n\ndeclare module \"@reciple/core\" {\n interface Config {\n token?: string;\n modules?: ModuleLoader.Config;\n logger?: Logger|Logger.Options;\n }\n\n interface Client {\n readonly modules: ModuleManager;\n readonly moduleLoader: ModuleLoader;\n readonly eventListeners: EventListeners;\n readonly cli: CLI;\n logger: Logger;\n }\n}\n\nexport class ConfigReader {\n private _client: Client|null = null;\n private _config: Config|null = null;\n private _build: BuildConfig|null = null;\n private _sharding: ShardingManagerOptions|null = null;\n\n get client() {\n if (!this._client) throw new RecipleError('client is not yet loaded from config.');\n return this._client;\n }\n\n get config() {\n return this._config ?? {};\n }\n\n get build() {\n return ConfigReader.normalizeTsdownConfig({\n overrides: this._build ?? {}\n });\n }\n\n get sharding() {\n return this._sharding;\n }\n\n constructor(public readonly filepath: string) {}\n\n public async read(options?: Omit<UnrunOptions, 'path'>
|
|
1
|
+
{"version":3,"file":"ConfigReader.mjs","names":[],"sources":["../../../src/classes/cli/ConfigReader.ts"],"sourcesContent":["import type { ModuleLoader } from '../client/ModuleLoader.js';\nimport type { ModuleManager } from '../managers/ModuleManager.js';\nimport type { EventListeners } from '../client/EventListeners.js';\nimport type { Logger } from '@prtty/print';\nimport { CLI } from './CLI.js';\nimport { Client, RecipleError, type Config } from '@reciple/core';\nimport type { BuildConfig } from '../../helpers/types.js';\nimport type { UserConfig as TsdownConfig } from 'tsdown';\nimport path from 'node:path';\nimport { mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';\nimport { resolveTSConfig } from 'pkg-types';\nimport { unrun, type Options as UnrunOptions } from 'unrun';\nimport { colors } from '@prtty/prtty';\nimport type { ShardingManagerOptions } from 'discord.js';\nimport { isDebugging } from '@reciple/utils';\n\ndeclare module \"@reciple/core\" {\n interface Config {\n token?: string;\n modules?: ModuleLoader.Config;\n logger?: Logger|Logger.Options;\n }\n\n interface Client {\n readonly modules: ModuleManager;\n readonly moduleLoader: ModuleLoader;\n readonly eventListeners: EventListeners;\n readonly cli: CLI;\n logger: Logger;\n }\n}\n\nexport class ConfigReader {\n private _client: Client|null = null;\n private _config: Config|null = null;\n private _build: BuildConfig|null = null;\n private _sharding: ShardingManagerOptions|null = null;\n\n get client() {\n if (!this._client) throw new RecipleError('client is not yet loaded from config.');\n return this._client;\n }\n\n get config() {\n return this._config ?? {};\n }\n\n get build() {\n return ConfigReader.normalizeTsdownConfig({\n overrides: this._build ?? {}\n });\n }\n\n get sharding() {\n return this._sharding;\n }\n\n constructor(public readonly filepath: string) {}\n\n public async read(options?: Omit<UnrunOptions, 'path'>): Promise<ConfigReader> {\n const { module } = await unrun<Config>({\n ...options,\n debug: isDebugging(),\n preset: 'bundle-require',\n path: this.filepath\n });\n\n if (!module || !module.client) {\n throw new RecipleError(`exported client is not an instance of ${colors.cyan('Client')} from ${colors.green('\"@reciple/core\"')}.`);\n }\n\n this._client = module.client;\n this._config = module.config;\n this._build = module.build;\n this._sharding = module.sharding ?? null;\n\n return this;\n }\n\n public async create(options: Omit<ConfigReader.CreateOptions, 'path'>): Promise<ConfigReader> {\n const exists = await ConfigReader.exists(this.filepath);\n\n if (exists && options.throwIfExists === true) {\n throw new RecipleError(`Config file already exists at ${colors.green(path.relative(process.cwd(), this.filepath))}.`);\n }\n\n if (!exists || exists && options.overwrite !== false) {\n await mkdir(path.dirname(this.filepath), { recursive: true });\n await writeFile(this.filepath, await ConfigReader.getDefaultContent(options.type));\n }\n\n return options.readOptions !== false ? this.read(options.readOptions) : this;\n }\n\n public static async exists(file: string): Promise<boolean> {\n return await stat(file).then(s => s.isFile()).catch(() => false);\n }\n\n public static async create(options: ConfigReader.CreateOptions): Promise<ConfigReader> {\n return new ConfigReader(options.path).create(options);\n }\n\n public static async find(options?: ConfigReader.FindOptions): Promise<string|null> {\n const filenames = ConfigReader.configFilenames.filter(f => !options?.lang || f.endsWith(options.lang));\n const directories = options?.directories ?? ['.', '.config'];\n\n for (const directory of directories) {\n const stats = await stat(directory).catch(() => undefined);\n\n if (!stats?.isDirectory()) continue;\n\n const file = (await readdir(directory)).find(f => filenames.includes(f));\n if (file) return path.join(directory, file);\n }\n\n return null;\n }\n}\n\nexport namespace ConfigReader {\n export interface CreateOptions {\n path: string;\n overwrite?: boolean;\n throwIfExists?: boolean;\n type: LangType;\n readOptions?: Omit<UnrunOptions, 'path'>|false;\n }\n\n export interface FindOptions {\n lang?: LangType;\n directories?: string[];\n }\n\n export async function getProjectLang(cwd: string): Promise<LangType> {\n const hasTsConfig = !!await resolveTSConfig(cwd, { try: true });\n const configLangIsTypescript = !!(await ConfigReader.find({\n directories: [cwd, path.join(cwd, '.config')],\n lang: 'ts'\n }));\n\n return hasTsConfig || configLangIsTypescript ? 'ts' : 'js';\n }\n\n export type LangType = 'ts'|'js';\n\n export const defaultConfigPath = {\n ts: path.join(CLI.root, 'assets/config', `reciple.config.ts`),\n js: path.join(CLI.root, 'assets/config', `reciple.config.js`)\n };\n\n export async function getDefaultContent(type: LangType): Promise<string> {\n const filepath = ConfigReader.defaultConfigPath[type];\n const content = await readFile(filepath, 'utf-8');\n return content;\n }\n\n export const configFilenames = [\n 'reciple.config.ts',\n 'reciple.config.mts',\n 'reciple.config.js',\n 'reciple.config.mjs'\n ];\n\n export function createConfigFilename(type: LangType, esm: boolean = false): string {\n return `reciple.config.${esm ? 'm' : ''}${type}`;\n }\n\n export function normalizeTsdownConfig({ type, overrides }: { type?: LangType; overrides?: BuildConfig; } = {}): TsdownConfig {\n return {\n entry: [`./src/**/*.{ts,tsx,js,jsx}`],\n outDir: './modules',\n tsconfig: `./${type ?? 'ts'}config.json`,\n external: [],\n noExternal: [],\n sourcemap: true,\n treeshake: true,\n clean: true,\n ...overrides,\n watch: false,\n platform: 'node',\n format: 'esm',\n unbundle: true,\n skipNodeModulesBundle: true\n };\n }\n}\n"],"mappings":";;;;;;;;;;AAgCA,IAAa,eAAb,MAAa,aAAa;CACtB,AAAQ,UAAuB;CAC/B,AAAQ,UAAuB;CAC/B,AAAQ,SAA2B;CACnC,AAAQ,YAAyC;CAEjD,IAAI,SAAS;AACT,MAAI,CAAC,KAAK,QAAS,OAAM,IAAI,aAAa,wCAAwC;AAClF,SAAO,KAAK;;CAGhB,IAAI,SAAS;AACT,SAAO,KAAK,WAAW,EAAE;;CAG7B,IAAI,QAAQ;AACR,SAAO,aAAa,sBAAsB,EACtC,WAAW,KAAK,UAAU,EAAE,EAC/B,CAAC;;CAGN,IAAI,WAAW;AACX,SAAO,KAAK;;CAGhB,YAAY,AAAgB,UAAkB;EAAlB;;CAE5B,MAAa,KAAK,SAA6D;EAC3E,MAAM,EAAE,WAAW,MAAM,MAAc;GACnC,GAAG;GACH,OAAO,aAAa;GACpB,QAAQ;GACR,MAAM,KAAK;GACd,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,MAAM,QAAQ,UAAU,EAAE,MAAK,MAAK,UAAU,SAAS,EAAE,CAAC;AACxE,OAAI,KAAM,QAAO,KAAK,KAAK,WAAW,KAAK;;AAG/C,SAAO;;;;CAkBJ,eAAe,eAAe,KAAgC;EACjE,MAAM,cAAc,CAAC,CAAC,MAAM,gBAAgB,KAAK,EAAE,KAAK,MAAM,CAAC;EAC/D,MAAM,yBAAyB,CAAC,CAAE,MAAM,aAAa,KAAK;GACtD,aAAa,CAAC,KAAK,KAAK,KAAK,KAAK,UAAU,CAAC;GAC7C,MAAM;GACT,CAAC;AAEF,SAAO,eAAe,yBAAyB,OAAO;;;mCAKzB;EAC7B,IAAI,KAAK,KAAK,IAAI,MAAM,iBAAiB,oBAAoB;EAC7D,IAAI,KAAK,KAAK,IAAI,MAAM,iBAAiB,oBAAoB;EAChE;CAEM,eAAe,kBAAkB,MAAiC;EACrE,MAAM,WAAW,aAAa,kBAAkB;AAEhD,SADgB,MAAM,SAAS,UAAU,QAAQ;;;iCAItB;EAC3B;EACA;EACA;EACA;EACH;CAEM,SAAS,qBAAqB,MAAgB,MAAe,OAAe;AAC/E,SAAO,kBAAkB,MAAM,MAAM,KAAK;;;CAGvC,SAAS,sBAAsB,EAAE,MAAM,cAA6D,EAAE,EAAgB;AACzH,SAAO;GACH,OAAO,CAAC,6BAA6B;GACrC,QAAQ;GACR,UAAU,KAAK,QAAQ,KAAK;GAC5B,UAAU,EAAE;GACZ,YAAY,EAAE;GACd,WAAW;GACX,WAAW;GACX,OAAO;GACP,GAAG;GACH,OAAO;GACP,UAAU;GACV,QAAQ;GACR,UAAU;GACV,uBAAuB;GAC1B"}
|
|
@@ -11,14 +11,14 @@ import { CommandModuleValidator } from "../validation/CommandModuleValidator.mjs
|
|
|
11
11
|
import { EventModuleValidator } from "../validation/EventModuleValidator.mjs";
|
|
12
12
|
import { PreconditionModuleValidator } from "../validation/PreconditionModule.mjs";
|
|
13
13
|
import { PostconditionModuleValidator } from "../validation/PostconditionModule.mjs";
|
|
14
|
-
import path
|
|
14
|
+
import path from "node:path";
|
|
15
15
|
import { recursiveDefaults } from "@reciple/utils";
|
|
16
16
|
import { mkdir, readdir, stat } from "node:fs/promises";
|
|
17
17
|
import { colors } from "@prtty/prtty";
|
|
18
18
|
import { CommandType, RecipleError } from "@reciple/core";
|
|
19
19
|
import "discord.js";
|
|
20
20
|
import micromatch from "micromatch";
|
|
21
|
-
import { globby
|
|
21
|
+
import { globby, isDynamicPattern } from "globby";
|
|
22
22
|
import { EventEmitter } from "node:events";
|
|
23
23
|
|
|
24
24
|
//#region src/classes/client/ModuleLoader.ts
|
|
@@ -33,19 +33,19 @@ var ModuleLoader = class ModuleLoader extends EventEmitter {
|
|
|
33
33
|
const modulePaths = await ModuleLoader.scanForModulePaths(this.client.config?.modules);
|
|
34
34
|
const modules = [];
|
|
35
35
|
this.emit("modulesResolving", modulePaths);
|
|
36
|
-
for (const path of modulePaths) try {
|
|
37
|
-
this.emit("moduleResolving", path);
|
|
38
|
-
const resolved = await ModuleLoader.resolveModuleFromPath(path);
|
|
36
|
+
for (const path$1 of modulePaths) try {
|
|
37
|
+
this.emit("moduleResolving", path$1);
|
|
38
|
+
const resolved = await ModuleLoader.resolveModuleFromPath(path$1);
|
|
39
39
|
Object.assign(resolved, {
|
|
40
40
|
client: this.client,
|
|
41
|
-
__$filepath: path
|
|
41
|
+
__$filepath: path$1
|
|
42
42
|
});
|
|
43
43
|
modules.push(resolved);
|
|
44
44
|
this.emit("moduleResolved", resolved);
|
|
45
45
|
} catch (error) {
|
|
46
46
|
if (ignoreErrors) continue;
|
|
47
47
|
this.emitOrThrow("moduleResolveError", new RecipleError({
|
|
48
|
-
message: `Failed to load module: ${colors.cyan(path)}`,
|
|
48
|
+
message: `Failed to load module: ${colors.cyan(path$1)}`,
|
|
49
49
|
cause: error
|
|
50
50
|
}));
|
|
51
51
|
}
|
|
@@ -58,7 +58,7 @@ var ModuleLoader = class ModuleLoader extends EventEmitter {
|
|
|
58
58
|
let directories = [];
|
|
59
59
|
for (const directory of config?.directories ?? []) {
|
|
60
60
|
if (isDynamicPattern(directory, { cwd })) {
|
|
61
|
-
const matches = await globby
|
|
61
|
+
const matches = await globby(directory, {
|
|
62
62
|
cwd,
|
|
63
63
|
ignore: config?.ignore,
|
|
64
64
|
onlyDirectories: true,
|
|
@@ -68,7 +68,7 @@ var ModuleLoader = class ModuleLoader extends EventEmitter {
|
|
|
68
68
|
scanned.push(...matches);
|
|
69
69
|
continue;
|
|
70
70
|
}
|
|
71
|
-
scanned.push(path
|
|
71
|
+
scanned.push(path.join(cwd, directory));
|
|
72
72
|
}
|
|
73
73
|
for (const directory of scanned) {
|
|
74
74
|
if (!await stat(directory).catch(() => void 0) && config?.createDirectories !== false) await mkdir(directory, { recursive: true });
|
|
@@ -86,7 +86,7 @@ var ModuleLoader = class ModuleLoader extends EventEmitter {
|
|
|
86
86
|
matchBase: true,
|
|
87
87
|
dot: true
|
|
88
88
|
});
|
|
89
|
-
files = files.map((f) => path
|
|
89
|
+
files = files.map((f) => path.join(directory, f));
|
|
90
90
|
for (const file of files) {
|
|
91
91
|
if (config?.filter ? !await config?.filter(file) : ModuleLoader.fileTypes.every((type) => !file.endsWith(`.${type}`))) continue;
|
|
92
92
|
modules.push(file);
|
|
@@ -97,7 +97,7 @@ var ModuleLoader = class ModuleLoader extends EventEmitter {
|
|
|
97
97
|
}
|
|
98
98
|
static async resolveModuleFromPath(filepath, options) {
|
|
99
99
|
if (!await stat(filepath).catch(() => void 0)) throw new RecipleError(`Module not found: ${filepath}`);
|
|
100
|
-
const data = recursiveDefaults(await import(`file://${path
|
|
100
|
+
const data = recursiveDefaults(await import(`file://${path.resolve(options?.cwd ?? process.cwd(), filepath)}`));
|
|
101
101
|
if (BaseModule.isModule(data)) return data;
|
|
102
102
|
if (data && "moduleType" in data) switch (data?.moduleType) {
|
|
103
103
|
case ModuleType.Command:
|
|
@@ -125,10 +125,10 @@ var ModuleLoader = class ModuleLoader extends EventEmitter {
|
|
|
125
125
|
return BaseModule.from(data);
|
|
126
126
|
}
|
|
127
127
|
static async resolveSourceDirectories(options) {
|
|
128
|
-
const dir = path
|
|
129
|
-
const root = path
|
|
130
|
-
const out = path
|
|
131
|
-
return options.directories.map((directory) => path
|
|
128
|
+
const dir = path.isAbsolute(options.baseUrl) ? options.baseUrl : path.resolve(path.join(options.cwd ?? process.cwd(), options.baseUrl));
|
|
129
|
+
const root = path.resolve(path.join(dir, options.rootDir));
|
|
130
|
+
const out = path.resolve(path.join(dir, options.outDir));
|
|
131
|
+
return options.directories.map((directory) => path.resolve(directory).replace(out, root));
|
|
132
132
|
}
|
|
133
133
|
emitOrThrow(event, error) {
|
|
134
134
|
if (this.client.listenerCount(event) > 0) return this.emit(event, error);
|
|
@@ -136,15 +136,15 @@ var ModuleLoader = class ModuleLoader extends EventEmitter {
|
|
|
136
136
|
}
|
|
137
137
|
};
|
|
138
138
|
(function(_ModuleLoader) {
|
|
139
|
-
let globby = _ModuleLoader.globby = null;
|
|
139
|
+
let globby$1 = _ModuleLoader.globby = null;
|
|
140
140
|
_ModuleLoader.fileTypes = [
|
|
141
141
|
"js",
|
|
142
142
|
"mjs",
|
|
143
143
|
"jsx"
|
|
144
144
|
];
|
|
145
145
|
async function getGlobby() {
|
|
146
|
-
if (globby) return globby;
|
|
147
|
-
return globby = await import("globby");
|
|
146
|
+
if (globby$1) return globby$1;
|
|
147
|
+
return globby$1 = await import("globby");
|
|
148
148
|
}
|
|
149
149
|
_ModuleLoader.getGlobby = getGlobby;
|
|
150
150
|
})(ModuleLoader || (ModuleLoader = {}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ModuleLoader.mjs","names":["globby","path"],"sources":["../../../src/classes/client/ModuleLoader.ts"],"sourcesContent":["import { type Awaitable } from 'discord.js';\nimport path from 'node:path';\nimport { mkdir, readdir, stat } from 'node:fs/promises';\nimport micromatch from 'micromatch';\nimport { globby, isDynamicPattern } from 'globby';\nimport { CommandType, RecipleError, type Client } from '@reciple/core';\nimport type { AnyModule, AnyModuleData } from '../../helpers/types.js';\nimport { recursiveDefaults } from '@reciple/utils';\nimport { BaseModule } from '../modules/BaseModule.js';\nimport { BaseModuleValidator } from '../validation/BaseModuleValidator.js';\nimport { ModuleType } from '../../helpers/constants.js';\nimport { PostconditionModule } from '../modules/PostconditionModule.js';\nimport { PreconditionModule } from '../modules/PreconditionModule.js';\nimport { EventModule } from '../modules/events/EventModule.js';\nimport { MessageCommandModule } from '../modules/commands/MessageCommandModule.js';\nimport { SlashCommandModule } from '../modules/commands/SlashCommandModule.js';\nimport { ContextMenuCommandModule } from '../modules/commands/ContextMenuCommandModule.js';\nimport { CommandModuleValidator } from '../validation/CommandModuleValidator.js';\nimport { EventModuleValidator } from '../validation/EventModuleValidator.js';\nimport { PreconditionModuleValidator } from '../validation/PreconditionModule.js';\nimport { PostconditionModuleValidator } from '../validation/PostconditionModule.js';\nimport type { Logger } from '@prtty/print';\nimport { EventEmitter } from 'node:events';\nimport { colors } from '@prtty/prtty';\n\nexport class ModuleLoader extends EventEmitter<ModuleLoader.Events> {\n public readonly logger: Logger;\n\n constructor(public readonly client: Client) {\n super();\n\n this.logger = this.client.logger.clone({\n label: 'ModuleLoader'\n });\n }\n\n public async findModules(ignoreErrors: boolean = false): Promise<AnyModule[]> {\n const modulePaths = await ModuleLoader.scanForModulePaths(this.client.config?.modules);\n const modules: AnyModule[] = [];\n\n this.emit('modulesResolving', modulePaths);\n\n for (const path of modulePaths) {\n try {\n this.emit('moduleResolving', path);\n const resolved = await ModuleLoader.resolveModuleFromPath(path);\n\n Object.assign(resolved, { client: this.client, __$filepath: path });\n modules.push(resolved);\n\n this.emit('moduleResolved', resolved);\n } catch (error) {\n if (ignoreErrors) continue;\n\n this.emitOrThrow('moduleResolveError', new RecipleError({\n message: `Failed to load module: ${colors.cyan(path)}`,\n cause: error\n }));\n }\n }\n\n this.emit('modulesResolved', modules);\n return modules;\n }\n\n public static async scanForDirectories(config?: Pick<ModuleLoader.Config, 'directories'|'ignore'> & { cwd?: string; createDirectories?: boolean; }) {\n const cwd = config?.cwd ?? process.cwd();\n\n let scanned: string[] = [];\n let directories: string[] = [];\n\n for (const directory of config?.directories ?? []) {\n if (isDynamicPattern(directory, { cwd })) {\n const matches = await globby(directory, {\n cwd,\n ignore: config?.ignore,\n onlyDirectories: true,\n baseNameMatch: true,\n absolute: true\n });\n\n scanned.push(...matches);\n continue;\n }\n\n scanned.push(path.join(cwd, directory));\n }\n\n for (const directory of scanned) {\n const stats = await stat(directory).catch(() => undefined);\n\n if (!stats && config?.createDirectories !== false) {\n await mkdir(directory, { recursive: true });\n }\n\n directories.push(directory);\n }\n\n return directories;\n }\n\n public static async scanForModulePaths(config?: ModuleLoader.Config & { cwd?: string; createDirectories?: boolean; }): Promise<string[]> {\n const directories = await ModuleLoader.scanForDirectories(config);\n\n let modules: string[] = [];\n\n for (const directory of directories) {\n let files = await readdir(directory);\n\n if (config?.ignore?.length) {\n files = micromatch.not(files, config.ignore, {\n cwd: directory,\n matchBase: true,\n dot: true\n });\n }\n\n files = files.map(f => path.join(directory, f));\n\n for (const file of files) {\n if (config?.filter ? !(await config?.filter(file)) : ModuleLoader.fileTypes.every(type => !file.endsWith(`.${type}`))) continue;\n modules.push(file);\n }\n }\n\n if (config?.sort) modules.sort(config.sort);\n\n return modules;\n }\n\n public static async resolveModuleFromPath(filepath: string, options?: { cwd?: string; }): Promise<AnyModule> {\n const stats = await stat(filepath).catch(() => undefined);\n if (!stats) throw new RecipleError(`Module not found: ${filepath}`);\n\n const data = recursiveDefaults<AnyModule|AnyModuleData|undefined>(await import(`file://${path.resolve(options?.cwd ?? process.cwd(), filepath)}`));\n if (BaseModule.isModule(data)) return data;\n\n if (data && 'moduleType' in data) {\n switch (data?.moduleType) {\n case ModuleType.Command:\n CommandModuleValidator.isValid(data);\n switch (data.type) {\n case CommandType.Message: return MessageCommandModule.from(data);\n case CommandType.Slash: return SlashCommandModule.from(data);\n case CommandType.ContextMenu: return ContextMenuCommandModule.from(data);\n default: throw new RecipleError(`Unknown command type from module: ${colors.cyan(filepath)}`);\n }\n case ModuleType.Event:\n EventModuleValidator.isValid(data);\n return EventModule.from(data);\n case ModuleType.Precondition:\n PreconditionModuleValidator.isValid(data);\n return PreconditionModule.from(data);\n case ModuleType.Postcondition:\n PostconditionModuleValidator.isValid(data);\n return PostconditionModule.from(data);\n default:\n BaseModuleValidator.isValid(data);\n return BaseModule.from(data);\n }\n }\n\n BaseModuleValidator.isValid(data);\n return BaseModule.from(data);\n }\n\n public static async resolveSourceDirectories(options: ModuleLoader.ResolveSourceDirectoryOptions): Promise<string[]> {\n const dir = path.isAbsolute(options.baseUrl) ? options.baseUrl : path.resolve(path.join(options.cwd ?? process.cwd(), options.baseUrl));\n\n const root = path.resolve(path.join(dir, options.rootDir));\n const out = path.resolve(path.join(dir, options.outDir));\n\n return options.directories.map(directory => path.resolve(directory).replace(out, root));\n }\n\n private emitOrThrow<K extends keyof Pick<ModuleLoader.Events, 'moduleResolveError'>>(event: K, error: RecipleError) {\n if (this.client.listenerCount(event) > 0) {\n // @ts-expect-error\n return this.emit(event, error);\n }\n\n throw error;\n }\n}\n\nexport namespace ModuleLoader {\n export let globby: typeof import('globby')|null = null;\n\n export const fileTypes = [\n 'js',\n 'mjs',\n 'jsx'\n ];\n\n export interface Config {\n directories?: string[];\n ignore?: string[];\n filter?: (filepath: string) => Awaitable<boolean>;\n sort?: (a: string, b: string) => number;\n }\n\n export interface Events {\n moduleResolveError: [error: RecipleError];\n moduleResolved: [module: AnyModule];\n moduleResolving: [filepath: string];\n modulesResolved: [modules: AnyModule[]];\n modulesResolving: [files: string[]];\n }\n\n export interface ResolveSourceDirectoryOptions {\n directories: string[];\n baseUrl: string;\n rootDir: string;\n outDir: string;\n cwd?: string;\n }\n\n export async function getGlobby(): Promise<typeof import('globby')> {\n if (globby) return globby;\n\n return globby = await import('globby');\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,IAAa,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],"sourcesContent":["import { type Awaitable } from 'discord.js';\nimport path from 'node:path';\nimport { mkdir, readdir, stat } from 'node:fs/promises';\nimport micromatch from 'micromatch';\nimport { globby, isDynamicPattern } from 'globby';\nimport { CommandType, RecipleError, type Client } from '@reciple/core';\nimport type { AnyModule, AnyModuleData } from '../../helpers/types.js';\nimport { recursiveDefaults } from '@reciple/utils';\nimport { BaseModule } from '../modules/BaseModule.js';\nimport { BaseModuleValidator } from '../validation/BaseModuleValidator.js';\nimport { ModuleType } from '../../helpers/constants.js';\nimport { PostconditionModule } from '../modules/PostconditionModule.js';\nimport { PreconditionModule } from '../modules/PreconditionModule.js';\nimport { EventModule } from '../modules/events/EventModule.js';\nimport { MessageCommandModule } from '../modules/commands/MessageCommandModule.js';\nimport { SlashCommandModule } from '../modules/commands/SlashCommandModule.js';\nimport { ContextMenuCommandModule } from '../modules/commands/ContextMenuCommandModule.js';\nimport { CommandModuleValidator } from '../validation/CommandModuleValidator.js';\nimport { EventModuleValidator } from '../validation/EventModuleValidator.js';\nimport { PreconditionModuleValidator } from '../validation/PreconditionModule.js';\nimport { PostconditionModuleValidator } from '../validation/PostconditionModule.js';\nimport type { Logger } from '@prtty/print';\nimport { EventEmitter } from 'node:events';\nimport { colors } from '@prtty/prtty';\n\nexport class ModuleLoader extends EventEmitter<ModuleLoader.Events> {\n public readonly logger: Logger;\n\n constructor(public readonly client: Client) {\n super();\n\n this.logger = this.client.logger.clone({\n label: 'ModuleLoader'\n });\n }\n\n public async findModules(ignoreErrors: boolean = false): Promise<AnyModule[]> {\n const modulePaths = await ModuleLoader.scanForModulePaths(this.client.config?.modules);\n const modules: AnyModule[] = [];\n\n this.emit('modulesResolving', modulePaths);\n\n for (const path of modulePaths) {\n try {\n this.emit('moduleResolving', path);\n const resolved = await ModuleLoader.resolveModuleFromPath(path);\n\n Object.assign(resolved, { client: this.client, __$filepath: path });\n modules.push(resolved);\n\n this.emit('moduleResolved', resolved);\n } catch (error) {\n if (ignoreErrors) continue;\n\n this.emitOrThrow('moduleResolveError', new RecipleError({\n message: `Failed to load module: ${colors.cyan(path)}`,\n cause: error\n }));\n }\n }\n\n this.emit('modulesResolved', modules);\n return modules;\n }\n\n public static async scanForDirectories(config?: Pick<ModuleLoader.Config, 'directories'|'ignore'> & { cwd?: string; createDirectories?: boolean; }) {\n const cwd = config?.cwd ?? process.cwd();\n\n let scanned: string[] = [];\n let directories: string[] = [];\n\n for (const directory of config?.directories ?? []) {\n if (isDynamicPattern(directory, { cwd })) {\n const matches = await globby(directory, {\n cwd,\n ignore: config?.ignore,\n onlyDirectories: true,\n baseNameMatch: true,\n absolute: true\n });\n\n scanned.push(...matches);\n continue;\n }\n\n scanned.push(path.join(cwd, directory));\n }\n\n for (const directory of scanned) {\n const stats = await stat(directory).catch(() => undefined);\n\n if (!stats && config?.createDirectories !== false) {\n await mkdir(directory, { recursive: true });\n }\n\n directories.push(directory);\n }\n\n return directories;\n }\n\n public static async scanForModulePaths(config?: ModuleLoader.Config & { cwd?: string; createDirectories?: boolean; }): Promise<string[]> {\n const directories = await ModuleLoader.scanForDirectories(config);\n\n let modules: string[] = [];\n\n for (const directory of directories) {\n let files = await readdir(directory);\n\n if (config?.ignore?.length) {\n files = micromatch.not(files, config.ignore, {\n cwd: directory,\n matchBase: true,\n dot: true\n });\n }\n\n files = files.map(f => path.join(directory, f));\n\n for (const file of files) {\n if (config?.filter ? !(await config?.filter(file)) : ModuleLoader.fileTypes.every(type => !file.endsWith(`.${type}`))) continue;\n modules.push(file);\n }\n }\n\n if (config?.sort) modules.sort(config.sort);\n\n return modules;\n }\n\n public static async resolveModuleFromPath(filepath: string, options?: { cwd?: string; }): Promise<AnyModule> {\n const stats = await stat(filepath).catch(() => undefined);\n if (!stats) throw new RecipleError(`Module not found: ${filepath}`);\n\n const data = recursiveDefaults<AnyModule|AnyModuleData|undefined>(await import(`file://${path.resolve(options?.cwd ?? process.cwd(), filepath)}`));\n if (BaseModule.isModule(data)) return data;\n\n if (data && 'moduleType' in data) {\n switch (data?.moduleType) {\n case ModuleType.Command:\n CommandModuleValidator.isValid(data);\n switch (data.type) {\n case CommandType.Message: return MessageCommandModule.from(data);\n case CommandType.Slash: return SlashCommandModule.from(data);\n case CommandType.ContextMenu: return ContextMenuCommandModule.from(data);\n default: throw new RecipleError(`Unknown command type from module: ${colors.cyan(filepath)}`);\n }\n case ModuleType.Event:\n EventModuleValidator.isValid(data);\n return EventModule.from(data);\n case ModuleType.Precondition:\n PreconditionModuleValidator.isValid(data);\n return PreconditionModule.from(data);\n case ModuleType.Postcondition:\n PostconditionModuleValidator.isValid(data);\n return PostconditionModule.from(data);\n default:\n BaseModuleValidator.isValid(data);\n return BaseModule.from(data);\n }\n }\n\n BaseModuleValidator.isValid(data);\n return BaseModule.from(data);\n }\n\n public static async resolveSourceDirectories(options: ModuleLoader.ResolveSourceDirectoryOptions): Promise<string[]> {\n const dir = path.isAbsolute(options.baseUrl) ? options.baseUrl : path.resolve(path.join(options.cwd ?? process.cwd(), options.baseUrl));\n\n const root = path.resolve(path.join(dir, options.rootDir));\n const out = path.resolve(path.join(dir, options.outDir));\n\n return options.directories.map(directory => path.resolve(directory).replace(out, root));\n }\n\n private emitOrThrow<K extends keyof Pick<ModuleLoader.Events, 'moduleResolveError'>>(event: K, error: RecipleError) {\n if (this.client.listenerCount(event) > 0) {\n // @ts-expect-error\n return this.emit(event, error);\n }\n\n throw error;\n }\n}\n\nexport namespace ModuleLoader {\n export let globby: typeof import('globby')|null = null;\n\n export const fileTypes = [\n 'js',\n 'mjs',\n 'jsx'\n ];\n\n export interface Config {\n directories?: string[];\n ignore?: string[];\n filter?: (filepath: string) => Awaitable<boolean>;\n sort?: (a: string, b: string) => number;\n }\n\n export interface Events {\n moduleResolveError: [error: RecipleError];\n moduleResolved: [module: AnyModule];\n moduleResolving: [filepath: string];\n modulesResolved: [modules: AnyModule[]];\n modulesResolving: [files: string[]];\n }\n\n export interface ResolveSourceDirectoryOptions {\n directories: string[];\n baseUrl: string;\n rootDir: string;\n outDir: string;\n cwd?: string;\n }\n\n export async function getGlobby(): Promise<typeof import('globby')> {\n if (globby) return globby;\n\n return globby = await import('globby');\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,IAAa,eAAb,MAAa,qBAAqB,aAAkC;CAChE,AAAgB;CAEhB,YAAY,AAAgB,QAAgB;AACxC,SAAO;EADiB;AAGxB,OAAK,SAAS,KAAK,OAAO,OAAO,MAAM,EACnC,OAAO,gBACV,CAAC;;CAGN,MAAa,YAAY,eAAwB,OAA6B;EAC1E,MAAM,cAAc,MAAM,aAAa,mBAAmB,KAAK,OAAO,QAAQ,QAAQ;EACtF,MAAM,UAAuB,EAAE;AAE/B,OAAK,KAAK,oBAAoB,YAAY;AAE1C,OAAK,MAAMA,UAAQ,YACf,KAAI;AACA,QAAK,KAAK,mBAAmBA,OAAK;GAClC,MAAM,WAAW,MAAM,aAAa,sBAAsBA,OAAK;AAE/D,UAAO,OAAO,UAAU;IAAE,QAAQ,KAAK;IAAQ,aAAaA;IAAM,CAAC;AACnE,WAAQ,KAAK,SAAS;AAEtB,QAAK,KAAK,kBAAkB,SAAS;WAChC,OAAO;AACZ,OAAI,aAAc;AAElB,QAAK,YAAY,sBAAsB,IAAI,aAAa;IACpD,SAAS,0BAA0B,OAAO,KAAKA,OAAK;IACpD,OAAO;IACV,CAAC,CAAC;;AAIX,OAAK,KAAK,mBAAmB,QAAQ;AACrC,SAAO;;CAGX,aAAoB,mBAAmB,QAA6G;EAChJ,MAAM,MAAM,QAAQ,OAAO,QAAQ,KAAK;EAExC,IAAI,UAAoB,EAAE;EAC1B,IAAI,cAAwB,EAAE;AAE9B,OAAK,MAAM,aAAa,QAAQ,eAAe,EAAE,EAAE;AAC/C,OAAI,iBAAiB,WAAW,EAAE,KAAK,CAAC,EAAE;IACtC,MAAM,UAAU,MAAM,OAAO,WAAW;KACpC;KACA,QAAQ,QAAQ;KAChB,iBAAiB;KACjB,eAAe;KACf,UAAU;KACb,CAAC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YAA8C;AAChE,MAAIA,SAAQ,QAAOA;AAEnB,SAAO,WAAS,MAAM,OAAO"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseModule } from "../modules/BaseModule.mjs";
|
|
2
2
|
import { ModuleType } from "../../helpers/constants.mjs";
|
|
3
|
-
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.
|
|
3
|
+
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs";
|
|
4
4
|
import { BaseManager, RecipleError } from "@reciple/core";
|
|
5
5
|
import { mix } from "ts-mixer";
|
|
6
6
|
import { EventEmitter } from "node:events";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseModule } from "./BaseModule.mjs";
|
|
2
2
|
import { ModuleType } from "../../helpers/constants.mjs";
|
|
3
|
-
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.
|
|
3
|
+
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs";
|
|
4
4
|
import { CommandPostcondition } from "@reciple/core";
|
|
5
5
|
import { hasMixin, mix } from "ts-mixer";
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseModule } from "./BaseModule.mjs";
|
|
2
2
|
import { ModuleType } from "../../helpers/constants.mjs";
|
|
3
|
-
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.
|
|
3
|
+
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs";
|
|
4
4
|
import { CommandPrecondition } from "@reciple/core";
|
|
5
5
|
import { hasMixin, mix } from "ts-mixer";
|
|
6
6
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseModule } from "../BaseModule.mjs";
|
|
2
2
|
import { ModuleType } from "../../../helpers/constants.mjs";
|
|
3
|
-
import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.
|
|
3
|
+
import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs";
|
|
4
4
|
import { ContextMenuCommand } from "@reciple/core";
|
|
5
5
|
import { DiscordSnowflake } from "@sapphire/snowflake";
|
|
6
6
|
import { hasMixin, mix } from "ts-mixer";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseModule } from "../BaseModule.mjs";
|
|
2
2
|
import { ModuleType } from "../../../helpers/constants.mjs";
|
|
3
|
-
import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.
|
|
3
|
+
import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs";
|
|
4
4
|
import { MessageCommand, MessageCommandBuilder } from "@reciple/core";
|
|
5
5
|
import { DiscordSnowflake } from "@sapphire/snowflake";
|
|
6
6
|
import { hasMixin, mix } from "ts-mixer";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseModule } from "../BaseModule.mjs";
|
|
2
2
|
import { ModuleType } from "../../../helpers/constants.mjs";
|
|
3
|
-
import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.
|
|
3
|
+
import { __decorate } from "../../../_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs";
|
|
4
4
|
import { SlashCommand } from "@reciple/core";
|
|
5
5
|
import { DiscordSnowflake } from "@sapphire/snowflake";
|
|
6
6
|
import { hasMixin, mix } from "ts-mixer";
|
|
@@ -71,11 +71,11 @@ var ModuleTemplateBuilder = class ModuleTemplateBuilder {
|
|
|
71
71
|
if (!template) {
|
|
72
72
|
const templateName = typeof options?.template === "string" ? options.template : void 0;
|
|
73
73
|
const templates = await ModuleTemplateBuilder.resolveModuleTemplates(this.typescript ? "ts" : "js");
|
|
74
|
-
const selectedTemplate = this.defaultAll ? templateName ? templates.find((template) => template.name === templateName) : templates.at(0) : await select({
|
|
74
|
+
const selectedTemplate = this.defaultAll ? templateName ? templates.find((template$1) => template$1.name === templateName) : templates.at(0) : await select({
|
|
75
75
|
message: "Select a module template",
|
|
76
|
-
options: templates.map((template) => ({
|
|
77
|
-
label: template.name,
|
|
78
|
-
value: template
|
|
76
|
+
options: templates.map((template$1) => ({
|
|
77
|
+
label: template$1.name,
|
|
78
|
+
value: template$1
|
|
79
79
|
}))
|
|
80
80
|
});
|
|
81
81
|
if (isCancel(selectedTemplate)) throw new NotAnError("Operation cancelled");
|
|
@@ -102,9 +102,9 @@ var ModuleTemplateBuilder = class ModuleTemplateBuilder {
|
|
|
102
102
|
initialValue: defaultValue,
|
|
103
103
|
placeholder: defaultValue,
|
|
104
104
|
defaultValue,
|
|
105
|
-
validate: (value) => {
|
|
106
|
-
if (!value) return "Please enter a value";
|
|
107
|
-
if (value.includes(" ")) return "Please enter a value without spaces";
|
|
105
|
+
validate: (value$1) => {
|
|
106
|
+
if (!value$1) return "Please enter a value";
|
|
107
|
+
if (value$1.includes(" ")) return "Please enter a value without spaces";
|
|
108
108
|
}
|
|
109
109
|
});
|
|
110
110
|
break;
|
|
@@ -162,9 +162,9 @@ var ModuleTemplateBuilder = class ModuleTemplateBuilder {
|
|
|
162
162
|
});
|
|
163
163
|
const selectedDirectory = this.defaultAll ? directories.at(0) : await select({
|
|
164
164
|
message: "Select a directory",
|
|
165
|
-
options: directories.map((directory) => ({
|
|
166
|
-
label: path.relative(cwd, directory),
|
|
167
|
-
value: directory
|
|
165
|
+
options: directories.map((directory$1) => ({
|
|
166
|
+
label: path.relative(cwd, directory$1),
|
|
167
|
+
value: directory$1
|
|
168
168
|
}))
|
|
169
169
|
});
|
|
170
170
|
if (isCancel(selectedDirectory)) throw new NotAnError("Operation cancelled");
|
|
@@ -203,15 +203,15 @@ var ModuleTemplateBuilder = class ModuleTemplateBuilder {
|
|
|
203
203
|
}
|
|
204
204
|
};
|
|
205
205
|
(function(_ModuleTemplateBuilder) {
|
|
206
|
-
let Placeholder = /* @__PURE__ */ function(Placeholder) {
|
|
207
|
-
Placeholder["ModuleName"] = "$MODULE_NAME$";
|
|
208
|
-
Placeholder["CommandName"] = "$COMMAND_NAME$";
|
|
209
|
-
Placeholder["CommandDescription"] = "$COMMAND_DESCRIPTION$";
|
|
210
|
-
Placeholder["CommandContextMenuType"] = "$COMMAND_CONTEXT_MENU_TYPE$";
|
|
211
|
-
Placeholder["EventEmitter"] = "$EVENT_EMITTER$";
|
|
212
|
-
Placeholder["EventName"] = "$EVENT_NAME$";
|
|
213
|
-
Placeholder["EventOnce"] = "$EVENT_ONCE$";
|
|
214
|
-
return Placeholder;
|
|
206
|
+
let Placeholder = /* @__PURE__ */ function(Placeholder$1) {
|
|
207
|
+
Placeholder$1["ModuleName"] = "$MODULE_NAME$";
|
|
208
|
+
Placeholder$1["CommandName"] = "$COMMAND_NAME$";
|
|
209
|
+
Placeholder$1["CommandDescription"] = "$COMMAND_DESCRIPTION$";
|
|
210
|
+
Placeholder$1["CommandContextMenuType"] = "$COMMAND_CONTEXT_MENU_TYPE$";
|
|
211
|
+
Placeholder$1["EventEmitter"] = "$EVENT_EMITTER$";
|
|
212
|
+
Placeholder$1["EventName"] = "$EVENT_NAME$";
|
|
213
|
+
Placeholder$1["EventOnce"] = "$EVENT_ONCE$";
|
|
214
|
+
return Placeholder$1;
|
|
215
215
|
}({});
|
|
216
216
|
_ModuleTemplateBuilder.Placeholder = Placeholder;
|
|
217
217
|
_ModuleTemplateBuilder.PlaceholderDefaultValues = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ModuleTemplateBuilder.mjs","names":[],"sources":["../../../src/classes/templates/ModuleTemplateBuilder.ts"],"sourcesContent":["import path from 'node:path';\nimport { CLI } from '../cli/CLI.js';\nimport { ModuleType } from '../../helpers/constants.js';\nimport { BaseModule } from '../modules/BaseModule.js';\nimport { MessageCommandModule } from '../modules/commands/MessageCommandModule.js';\nimport { SlashCommandModule } from '../modules/commands/SlashCommandModule.js';\nimport { ContextMenuCommandModule } from '../modules/commands/ContextMenuCommandModule.js';\nimport { ClientEventModule } from '../modules/events/ClientEventModule.js';\nimport { EventModule } from '../modules/events/EventModule.js';\nimport { RESTEventModule } from '../modules/events/RESTEventModule.js';\nimport { PostconditionModule } from '../modules/PostconditionModule.js';\nimport { PreconditionModule } from '../modules/PreconditionModule.js';\nimport { mkdir, readdir, readFile, writeFile } from 'node:fs/promises';\nimport { ConfigReader } from '../cli/ConfigReader.js';\nimport { confirm, intro, isCancel, outro, select, text } from '@clack/prompts';\nimport { NotAnError } from '../NotAnError.js';\nimport { ApplicationCommandType } from 'discord.js';\nimport { ModuleLoader } from '../client/ModuleLoader.js';\nimport { existsSync } from 'node:fs';\nimport { readTSConfig } from 'pkg-types';\nimport { colors } from '@prtty/prtty';\n\nexport class ModuleTemplateBuilder {\n public _directory?: string;\n\n public readonly cli: CLI;\n public readonly config: ConfigReader;\n\n public template?: ModuleTemplateBuilder.Data;\n public typescript?: boolean;\n public filename?: string;\n public defaultAll: boolean;\n\n get content() {\n return this.template?.content ?? '';\n }\n\n get directory() {\n return this._directory ?? process.cwd();\n }\n\n get filepath() {\n return path.join(this.directory, this.filename ?? '');\n }\n\n constructor(options: ModuleTemplateBuilder.Options) {\n this.cli = options.cli;\n this.config = options.config;\n this.template = options?.template;\n this.typescript = options?.typescript;\n this._directory = options?.directory;\n this.filename = options?.filename;\n this.defaultAll = options?.defaultAll ?? false;\n }\n\n public async init(): Promise<this> {\n intro(colors.bold().black().bgCyan(` ${this.cli.command.name()} create module - v${this.cli.build} `));\n return this;\n }\n\n public async setupLanguage(options?: ModuleTemplateBuilder.SetupLanguageOptions): Promise<this> {\n this.typescript = options?.typescript ?? this.typescript;\n\n if (!this.typescript) {\n const isTypeScriptDefault = (await ConfigReader.getProjectLang(this.directory)) === 'ts';\n const isTypeScript = this.defaultAll\n ? isTypeScriptDefault\n : await confirm({\n message: 'Would you like to use TypeScript?',\n active: 'Yes',\n inactive: 'No',\n initialValue: isTypeScriptDefault\n });\n\n if (isCancel(isTypeScript)) throw new NotAnError('Operation cancelled');\n this.typescript = isTypeScript;\n }\n\n return this;\n }\n\n public async setupTemplate(options?: ModuleTemplateBuilder.SetupTemplateOptions): Promise<this> {\n let template = typeof options?.template === 'object' ? options.template : undefined;\n\n if (!template) {\n const templateName = typeof options?.template === 'string' ? options.template : undefined;\n const templates = await ModuleTemplateBuilder.resolveModuleTemplates(this.typescript ? 'ts' : 'js');\n\n const selectedTemplate = this.defaultAll\n ? templateName ? templates.find(template => template.name === templateName) : templates.at(0)\n : await select({\n message: 'Select a module template',\n options: templates.map(template => ({\n label: template.name,\n value: template\n }))\n });\n\n if (isCancel(selectedTemplate)) throw new NotAnError('Operation cancelled');\n template = selectedTemplate;\n }\n\n this.template = template;\n if (!this.template) throw new NotAnError('No template selected');\n\n return this;\n }\n\n public async setupPlaceholders(): Promise<this> {\n const placeholders = Object.keys(ModuleTemplateBuilder.Placeholder).filter(k => !k.startsWith('$')) as (keyof typeof ModuleTemplateBuilder.Placeholder)[];\n\n for (const placeholderKey of placeholders) {\n if (!ModuleTemplateBuilder.hasPlaceholder(ModuleTemplateBuilder.Placeholder[placeholderKey], this.content)) continue;\n const placeholder = ModuleTemplateBuilder.Placeholder[placeholderKey];\n const defaultValue = ModuleTemplateBuilder.PlaceholderDefaultValues[placeholder];\n\n let value: string|symbol;\n\n if (this.defaultAll) {\n value = defaultValue;\n } else {\n switch (placeholder) {\n case ModuleTemplateBuilder.Placeholder.ModuleName:\n case ModuleTemplateBuilder.Placeholder.CommandName:\n case ModuleTemplateBuilder.Placeholder.EventName:\n value = this.defaultAll\n ? defaultValue\n : await text({\n message: `What would you like to name the ${placeholderKey.replace('Name', '').toLowerCase()}?`,\n initialValue: defaultValue,\n placeholder: defaultValue,\n defaultValue,\n validate: (value) => {\n if (!value) return 'Please enter a value';\n if (value.includes(' ')) return 'Please enter a value without spaces';\n }\n });\n break;\n case ModuleTemplateBuilder.Placeholder.CommandContextMenuType:\n const type = await select({\n message: 'Select a command context menu type',\n options: [\n { label: 'Message', value: ApplicationCommandType.Message },\n { label: 'User', value: ApplicationCommandType.User }\n ],\n initialValue: ApplicationCommandType.Message\n });\n\n value = isCancel(type) ? type : `${type}`;\n break;\n case ModuleTemplateBuilder.Placeholder.EventOnce:\n const once = await confirm({\n message: `Would you like to use the event once?`,\n active: 'Yes',\n inactive: 'No',\n initialValue: false\n });\n\n value = isCancel(once) ? once : `${once}`;\n break;\n case ModuleTemplateBuilder.Placeholder.EventEmitter:\n default:\n value = await text({\n message: `What would you like to use for the ${ModuleTemplateBuilder.toTextCase(placeholder)}?`,\n initialValue: defaultValue,\n placeholder: defaultValue,\n defaultValue\n });\n break;\n }\n }\n\n if (isCancel(value)) throw new NotAnError('Operation cancelled');\n if (!this.template) throw new NotAnError('No template selected');\n this.template.content = ModuleTemplateBuilder.removeExpectedErrorComments(this.content.replaceAll(placeholder, value));\n }\n\n return this;\n }\n\n public async setupDirectory(options?: ModuleTemplateBuilder.SetupDirectoryOptions): Promise<this> {\n let directory = options?.directory;\n\n if (!directory) {\n const cwd = process.cwd();\n const tsconfig = this.config.build.tsconfig\n ? await readTSConfig(typeof this.config.build.tsconfig == 'string' ? this.config.build.tsconfig : cwd, { try: true })\n : undefined;\n\n let directories = await ModuleLoader.scanForDirectories(this.config.config.modules);\n directories = await ModuleLoader.resolveSourceDirectories({\n directories,\n baseUrl: tsconfig?.compilerOptions?.baseUrl ?? '.',\n rootDir: tsconfig?.compilerOptions?.rootDir ?? 'src',\n outDir: tsconfig?.compilerOptions?.outDir ?? 'modules',\n cwd\n });\n\n const selectedDirectory = this.defaultAll\n ? directories.at(0)\n : await select({\n message: 'Select a directory',\n options: directories.map(directory => ({\n label: path.relative(cwd, directory),\n value: directory\n }))\n });\n\n if (isCancel(selectedDirectory)) throw new NotAnError('Operation cancelled');\n directory = selectedDirectory;\n }\n\n if (!directory) throw new NotAnError('No directory selected');\n this._directory = directory;\n\n return this;\n }\n\n public async setupFilename(options?: ModuleTemplateBuilder.SetupFilenameOptions): Promise<this> {\n let filename = options?.filename;\n\n if (!filename) {\n const defaultFilename = `${this.template?.name}.${this.typescript ? 'ts' : 'js'}`;\n const selectedFilename = this.defaultAll\n ? defaultFilename\n : await text({\n message: 'What would you like to name the module file?',\n initialValue: defaultFilename,\n placeholder: defaultFilename,\n defaultValue: defaultFilename,\n validate: (value) => {\n if (!value) return 'Please enter a value';\n if (existsSync(path.join(this.directory, value))) return 'File already exists';\n }\n });\n\n if (isCancel(selectedFilename)) throw new NotAnError('Operation cancelled');\n filename = selectedFilename;\n }\n\n if (!filename) throw new NotAnError('No filename selected');\n this.filename = filename;\n\n return this;\n }\n\n public async build({ overwrite, silent }: ModuleTemplateBuilder.BuildOptions = {}): Promise<this> {\n await mkdir(this.directory, { recursive: true });\n await writeFile(this.filepath, this.content, { flag: overwrite === false ? 'wx' : undefined });\n if (!silent) outro(`Module ${colors.green(this.template?.name!)} created in ${colors.cyan(path.relative(process.cwd(), this.filepath))}`);\n return this;\n }\n}\n\nexport namespace ModuleTemplateBuilder {\n export interface Options {\n cli: CLI;\n config: ConfigReader;\n template?: ModuleTemplateBuilder.Data;\n typescript?: boolean;\n directory?: string;\n filename?: string;\n defaultAll?: boolean;\n }\n\n export interface SetupLanguageOptions {\n typescript?: boolean;\n }\n\n export interface SetupTemplateOptions {\n template?: ModuleTemplateBuilder.Data|string;\n }\n\n export interface SetupDirectoryOptions {\n directory?: string;\n }\n\n export interface SetupFilenameOptions {\n filename?: string;\n }\n\n export interface BuildOptions {\n overwrite?: boolean;\n silent?: boolean;\n }\n\n export enum Placeholder {\n ModuleName = '$MODULE_NAME$',\n CommandName = '$COMMAND_NAME$',\n CommandDescription = '$COMMAND_DESCRIPTION$',\n CommandContextMenuType = '$COMMAND_CONTEXT_MENU_TYPE$',\n EventEmitter = '$EVENT_EMITTER$',\n EventName = '$EVENT_NAME$',\n EventOnce = '$EVENT_ONCE$'\n }\n\n export const PlaceholderDefaultValues: Record<Placeholder, string> = {\n [Placeholder.ModuleName]: 'MyModule',\n [Placeholder.CommandName]: 'my-command',\n [Placeholder.CommandDescription]: 'My command',\n [Placeholder.CommandContextMenuType]: `${ApplicationCommandType.Message}`,\n [Placeholder.EventEmitter]: 'null',\n [Placeholder.EventName]: 'my-event',\n [Placeholder.EventOnce]: 'false'\n }\n\n export const SourceDirectory = {\n js: path.join(CLI.root, './assets/modules/javascript'),\n ts: path.join(CLI.root, './assets/modules/typescript')\n };\n\n export const ModuleTypeClassName: Record<ModuleType, string[]> = {\n [ModuleType.Base]: [BaseModule.name],\n [ModuleType.Command]: [MessageCommandModule.name, SlashCommandModule.name, ContextMenuCommandModule.name],\n [ModuleType.Event]: [ClientEventModule.name, EventModule.name, RESTEventModule.name],\n [ModuleType.Postcondition]: [PostconditionModule.name],\n [ModuleType.Precondition]: [PreconditionModule.name]\n };\n\n export interface Data {\n src: string;\n name: string;\n content: string;\n moduleType: ModuleType|null;\n }\n\n export async function resolveModuleTemplates(source: keyof typeof SourceDirectory): Promise<ModuleTemplateBuilder.Data[]> {\n const src = ModuleTemplateBuilder.SourceDirectory[source];\n const files: string[] = await readdir(src);\n const templates: ModuleTemplateBuilder.Data[] = [];\n\n for (const file of files) {\n const filepath = path.join(src, file);\n const content = await readFile(filepath, 'utf-8');\n const moduleType = ModuleTemplateBuilder.getModuleTemplateContentType(content);\n\n templates.push({ src: filepath, name: path.parse(filepath).name, content, moduleType });\n }\n\n return templates;\n }\n\n export function getModuleTemplateContentType(content: string): ModuleType|null {\n const createExtendStatement = (name: string) => `export class ${Placeholder.ModuleName} extends ${name}`;\n\n for (const type of Object.keys(ModuleTypeClassName).map(Number) as ModuleType[]) {\n const statements = ModuleTypeClassName[type].map(createExtendStatement);\n\n for (const statement of statements) {\n if (content.includes(statement)) return type;\n }\n }\n\n return null;\n }\n\n export function hasPlaceholder(placeholder: Placeholder, content: string): boolean {\n return content.includes(placeholder);\n }\n\n export function removeExpectedErrorComments(content: string): string {\n const lines = content.split('\\n');\n\n return lines\n .filter(line => !line.trim().includes('// @ts-expect-error'))\n .join('\\n');\n }\n\n export function toTextCase(string: string): string {\n return String(string)\n .replace(/^[^A-Za-z0-9]*|[^A-Za-z0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S,UAAU,CAAE,QAAO;;AAIhD,SAAO;;;CAGJ,SAAS,eAAe,aAA0B,SAA0B;AAC/E,SAAO,QAAQ,SAAS,YAAY;;;CAGjC,SAAS,4BAA4B,SAAyB;AAGjE,SAFc,QAAQ,MAAM,KAAK,CAG5B,QAAO,SAAQ,CAAC,KAAK,MAAM,CAAC,SAAS,sBAAsB,CAAC,CAC5D,KAAK,KAAK;;;CAGZ,SAAS,WAAW,QAAwB;AAC/C,SAAO,OAAO,OAAO,CAChB,QAAQ,kCAAkC,GAAG,CAC7C,QAAQ,oBAAoB,GAAG,GAAG,MAAM,IAAI,MAAM,EAAE,aAAa,CAAC,CAClE,QAAQ,qBAAqB,IAAI,CACjC,aAAa"}
|
|
1
|
+
{"version":3,"file":"ModuleTemplateBuilder.mjs","names":["template","value","directory"],"sources":["../../../src/classes/templates/ModuleTemplateBuilder.ts"],"sourcesContent":["import path from 'node:path';\nimport { CLI } from '../cli/CLI.js';\nimport { ModuleType } from '../../helpers/constants.js';\nimport { BaseModule } from '../modules/BaseModule.js';\nimport { MessageCommandModule } from '../modules/commands/MessageCommandModule.js';\nimport { SlashCommandModule } from '../modules/commands/SlashCommandModule.js';\nimport { ContextMenuCommandModule } from '../modules/commands/ContextMenuCommandModule.js';\nimport { ClientEventModule } from '../modules/events/ClientEventModule.js';\nimport { EventModule } from '../modules/events/EventModule.js';\nimport { RESTEventModule } from '../modules/events/RESTEventModule.js';\nimport { PostconditionModule } from '../modules/PostconditionModule.js';\nimport { PreconditionModule } from '../modules/PreconditionModule.js';\nimport { mkdir, readdir, readFile, writeFile } from 'node:fs/promises';\nimport { ConfigReader } from '../cli/ConfigReader.js';\nimport { confirm, intro, isCancel, outro, select, text } from '@clack/prompts';\nimport { NotAnError } from '../NotAnError.js';\nimport { ApplicationCommandType } from 'discord.js';\nimport { ModuleLoader } from '../client/ModuleLoader.js';\nimport { existsSync } from 'node:fs';\nimport { readTSConfig } from 'pkg-types';\nimport { colors } from '@prtty/prtty';\n\nexport class ModuleTemplateBuilder {\n public _directory?: string;\n\n public readonly cli: CLI;\n public readonly config: ConfigReader;\n\n public template?: ModuleTemplateBuilder.Data;\n public typescript?: boolean;\n public filename?: string;\n public defaultAll: boolean;\n\n get content() {\n return this.template?.content ?? '';\n }\n\n get directory() {\n return this._directory ?? process.cwd();\n }\n\n get filepath() {\n return path.join(this.directory, this.filename ?? '');\n }\n\n constructor(options: ModuleTemplateBuilder.Options) {\n this.cli = options.cli;\n this.config = options.config;\n this.template = options?.template;\n this.typescript = options?.typescript;\n this._directory = options?.directory;\n this.filename = options?.filename;\n this.defaultAll = options?.defaultAll ?? false;\n }\n\n public async init(): Promise<this> {\n intro(colors.bold().black().bgCyan(` ${this.cli.command.name()} create module - v${this.cli.build} `));\n return this;\n }\n\n public async setupLanguage(options?: ModuleTemplateBuilder.SetupLanguageOptions): Promise<this> {\n this.typescript = options?.typescript ?? this.typescript;\n\n if (!this.typescript) {\n const isTypeScriptDefault = (await ConfigReader.getProjectLang(this.directory)) === 'ts';\n const isTypeScript = this.defaultAll\n ? isTypeScriptDefault\n : await confirm({\n message: 'Would you like to use TypeScript?',\n active: 'Yes',\n inactive: 'No',\n initialValue: isTypeScriptDefault\n });\n\n if (isCancel(isTypeScript)) throw new NotAnError('Operation cancelled');\n this.typescript = isTypeScript;\n }\n\n return this;\n }\n\n public async setupTemplate(options?: ModuleTemplateBuilder.SetupTemplateOptions): Promise<this> {\n let template = typeof options?.template === 'object' ? options.template : undefined;\n\n if (!template) {\n const templateName = typeof options?.template === 'string' ? options.template : undefined;\n const templates = await ModuleTemplateBuilder.resolveModuleTemplates(this.typescript ? 'ts' : 'js');\n\n const selectedTemplate = this.defaultAll\n ? templateName ? templates.find(template => template.name === templateName) : templates.at(0)\n : await select({\n message: 'Select a module template',\n options: templates.map(template => ({\n label: template.name,\n value: template\n }))\n });\n\n if (isCancel(selectedTemplate)) throw new NotAnError('Operation cancelled');\n template = selectedTemplate;\n }\n\n this.template = template;\n if (!this.template) throw new NotAnError('No template selected');\n\n return this;\n }\n\n public async setupPlaceholders(): Promise<this> {\n const placeholders = Object.keys(ModuleTemplateBuilder.Placeholder).filter(k => !k.startsWith('$')) as (keyof typeof ModuleTemplateBuilder.Placeholder)[];\n\n for (const placeholderKey of placeholders) {\n if (!ModuleTemplateBuilder.hasPlaceholder(ModuleTemplateBuilder.Placeholder[placeholderKey], this.content)) continue;\n const placeholder = ModuleTemplateBuilder.Placeholder[placeholderKey];\n const defaultValue = ModuleTemplateBuilder.PlaceholderDefaultValues[placeholder];\n\n let value: string|symbol;\n\n if (this.defaultAll) {\n value = defaultValue;\n } else {\n switch (placeholder) {\n case ModuleTemplateBuilder.Placeholder.ModuleName:\n case ModuleTemplateBuilder.Placeholder.CommandName:\n case ModuleTemplateBuilder.Placeholder.EventName:\n value = this.defaultAll\n ? defaultValue\n : await text({\n message: `What would you like to name the ${placeholderKey.replace('Name', '').toLowerCase()}?`,\n initialValue: defaultValue,\n placeholder: defaultValue,\n defaultValue,\n validate: (value) => {\n if (!value) return 'Please enter a value';\n if (value.includes(' ')) return 'Please enter a value without spaces';\n }\n });\n break;\n case ModuleTemplateBuilder.Placeholder.CommandContextMenuType:\n const type = await select({\n message: 'Select a command context menu type',\n options: [\n { label: 'Message', value: ApplicationCommandType.Message },\n { label: 'User', value: ApplicationCommandType.User }\n ],\n initialValue: ApplicationCommandType.Message\n });\n\n value = isCancel(type) ? type : `${type}`;\n break;\n case ModuleTemplateBuilder.Placeholder.EventOnce:\n const once = await confirm({\n message: `Would you like to use the event once?`,\n active: 'Yes',\n inactive: 'No',\n initialValue: false\n });\n\n value = isCancel(once) ? once : `${once}`;\n break;\n case ModuleTemplateBuilder.Placeholder.EventEmitter:\n default:\n value = await text({\n message: `What would you like to use for the ${ModuleTemplateBuilder.toTextCase(placeholder)}?`,\n initialValue: defaultValue,\n placeholder: defaultValue,\n defaultValue\n });\n break;\n }\n }\n\n if (isCancel(value)) throw new NotAnError('Operation cancelled');\n if (!this.template) throw new NotAnError('No template selected');\n this.template.content = ModuleTemplateBuilder.removeExpectedErrorComments(this.content.replaceAll(placeholder, value));\n }\n\n return this;\n }\n\n public async setupDirectory(options?: ModuleTemplateBuilder.SetupDirectoryOptions): Promise<this> {\n let directory = options?.directory;\n\n if (!directory) {\n const cwd = process.cwd();\n const tsconfig = this.config.build.tsconfig\n ? await readTSConfig(typeof this.config.build.tsconfig == 'string' ? this.config.build.tsconfig : cwd, { try: true })\n : undefined;\n\n let directories = await ModuleLoader.scanForDirectories(this.config.config.modules);\n directories = await ModuleLoader.resolveSourceDirectories({\n directories,\n baseUrl: tsconfig?.compilerOptions?.baseUrl ?? '.',\n rootDir: tsconfig?.compilerOptions?.rootDir ?? 'src',\n outDir: tsconfig?.compilerOptions?.outDir ?? 'modules',\n cwd\n });\n\n const selectedDirectory = this.defaultAll\n ? directories.at(0)\n : await select({\n message: 'Select a directory',\n options: directories.map(directory => ({\n label: path.relative(cwd, directory),\n value: directory\n }))\n });\n\n if (isCancel(selectedDirectory)) throw new NotAnError('Operation cancelled');\n directory = selectedDirectory;\n }\n\n if (!directory) throw new NotAnError('No directory selected');\n this._directory = directory;\n\n return this;\n }\n\n public async setupFilename(options?: ModuleTemplateBuilder.SetupFilenameOptions): Promise<this> {\n let filename = options?.filename;\n\n if (!filename) {\n const defaultFilename = `${this.template?.name}.${this.typescript ? 'ts' : 'js'}`;\n const selectedFilename = this.defaultAll\n ? defaultFilename\n : await text({\n message: 'What would you like to name the module file?',\n initialValue: defaultFilename,\n placeholder: defaultFilename,\n defaultValue: defaultFilename,\n validate: (value) => {\n if (!value) return 'Please enter a value';\n if (existsSync(path.join(this.directory, value))) return 'File already exists';\n }\n });\n\n if (isCancel(selectedFilename)) throw new NotAnError('Operation cancelled');\n filename = selectedFilename;\n }\n\n if (!filename) throw new NotAnError('No filename selected');\n this.filename = filename;\n\n return this;\n }\n\n public async build({ overwrite, silent }: ModuleTemplateBuilder.BuildOptions = {}): Promise<this> {\n await mkdir(this.directory, { recursive: true });\n await writeFile(this.filepath, this.content, { flag: overwrite === false ? 'wx' : undefined });\n if (!silent) outro(`Module ${colors.green(this.template?.name!)} created in ${colors.cyan(path.relative(process.cwd(), this.filepath))}`);\n return this;\n }\n}\n\nexport namespace ModuleTemplateBuilder {\n export interface Options {\n cli: CLI;\n config: ConfigReader;\n template?: ModuleTemplateBuilder.Data;\n typescript?: boolean;\n directory?: string;\n filename?: string;\n defaultAll?: boolean;\n }\n\n export interface SetupLanguageOptions {\n typescript?: boolean;\n }\n\n export interface SetupTemplateOptions {\n template?: ModuleTemplateBuilder.Data|string;\n }\n\n export interface SetupDirectoryOptions {\n directory?: string;\n }\n\n export interface SetupFilenameOptions {\n filename?: string;\n }\n\n export interface BuildOptions {\n overwrite?: boolean;\n silent?: boolean;\n }\n\n export enum Placeholder {\n ModuleName = '$MODULE_NAME$',\n CommandName = '$COMMAND_NAME$',\n CommandDescription = '$COMMAND_DESCRIPTION$',\n CommandContextMenuType = '$COMMAND_CONTEXT_MENU_TYPE$',\n EventEmitter = '$EVENT_EMITTER$',\n EventName = '$EVENT_NAME$',\n EventOnce = '$EVENT_ONCE$'\n }\n\n export const PlaceholderDefaultValues: Record<Placeholder, string> = {\n [Placeholder.ModuleName]: 'MyModule',\n [Placeholder.CommandName]: 'my-command',\n [Placeholder.CommandDescription]: 'My command',\n [Placeholder.CommandContextMenuType]: `${ApplicationCommandType.Message}`,\n [Placeholder.EventEmitter]: 'null',\n [Placeholder.EventName]: 'my-event',\n [Placeholder.EventOnce]: 'false'\n }\n\n export const SourceDirectory = {\n js: path.join(CLI.root, './assets/modules/javascript'),\n ts: path.join(CLI.root, './assets/modules/typescript')\n };\n\n export const ModuleTypeClassName: Record<ModuleType, string[]> = {\n [ModuleType.Base]: [BaseModule.name],\n [ModuleType.Command]: [MessageCommandModule.name, SlashCommandModule.name, ContextMenuCommandModule.name],\n [ModuleType.Event]: [ClientEventModule.name, EventModule.name, RESTEventModule.name],\n [ModuleType.Postcondition]: [PostconditionModule.name],\n [ModuleType.Precondition]: [PreconditionModule.name]\n };\n\n export interface Data {\n src: string;\n name: string;\n content: string;\n moduleType: ModuleType|null;\n }\n\n export async function resolveModuleTemplates(source: keyof typeof SourceDirectory): Promise<ModuleTemplateBuilder.Data[]> {\n const src = ModuleTemplateBuilder.SourceDirectory[source];\n const files: string[] = await readdir(src);\n const templates: ModuleTemplateBuilder.Data[] = [];\n\n for (const file of files) {\n const filepath = path.join(src, file);\n const content = await readFile(filepath, 'utf-8');\n const moduleType = ModuleTemplateBuilder.getModuleTemplateContentType(content);\n\n templates.push({ src: filepath, name: path.parse(filepath).name, content, moduleType });\n }\n\n return templates;\n }\n\n export function getModuleTemplateContentType(content: string): ModuleType|null {\n const createExtendStatement = (name: string) => `export class ${Placeholder.ModuleName} extends ${name}`;\n\n for (const type of Object.keys(ModuleTypeClassName).map(Number) as ModuleType[]) {\n const statements = ModuleTypeClassName[type].map(createExtendStatement);\n\n for (const statement of statements) {\n if (content.includes(statement)) return type;\n }\n }\n\n return null;\n }\n\n export function hasPlaceholder(placeholder: Placeholder, content: string): boolean {\n return content.includes(placeholder);\n }\n\n export function removeExpectedErrorComments(content: string): string {\n const lines = content.split('\\n');\n\n return lines\n .filter(line => !line.trim().includes('// @ts-expect-error'))\n .join('\\n');\n }\n\n export function toTextCase(string: string): string {\n return String(string)\n .replace(/^[^A-Za-z0-9]*|[^A-Za-z0-9]*$/g, '')\n .replace(/([a-z])([A-Z])/g, (m, a, b) => a + '_' + b.toLowerCase())\n .replace(/[^A-Za-z0-9]+|_+/g, ' ')\n .toLowerCase();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsBA,IAAa,wBAAb,MAAa,sBAAsB;CAC/B,AAAO;CAEP,AAAgB;CAChB,AAAgB;CAEhB,AAAO;CACP,AAAO;CACP,AAAO;CACP,AAAO;CAEP,IAAI,UAAU;AACV,SAAO,KAAK,UAAU,WAAW;;CAGrC,IAAI,YAAY;AACZ,SAAO,KAAK,cAAc,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,GAAG,MAAM,IAAI,MAAM,EAAE,aAAa,CAAC,CAClE,QAAQ,qBAAqB,IAAI,CACjC,aAAa"}
|
|
@@ -5,7 +5,7 @@ import { PackageManagerName } from "nypm";
|
|
|
5
5
|
|
|
6
6
|
//#region src/classes/templates/TemplateBuilder.d.ts
|
|
7
7
|
declare class TemplateBuilder {
|
|
8
|
-
|
|
8
|
+
originalCwd: string;
|
|
9
9
|
cli: CLI;
|
|
10
10
|
typescript?: boolean;
|
|
11
11
|
token?: string;
|
|
@@ -14,7 +14,6 @@ declare class TemplateBuilder {
|
|
|
14
14
|
packageJson?: PackageJsonBuilder;
|
|
15
15
|
packageManager?: PackageManagerName;
|
|
16
16
|
dependenciesInstalled: boolean;
|
|
17
|
-
get directory(): string;
|
|
18
17
|
get relativeDirectory(): string;
|
|
19
18
|
get packageJsonPath(): string;
|
|
20
19
|
get name(): string;
|
|
@@ -33,7 +32,6 @@ declare class TemplateBuilder {
|
|
|
33
32
|
declare namespace TemplateBuilder {
|
|
34
33
|
interface Options {
|
|
35
34
|
cli: CLI;
|
|
36
|
-
directory?: string;
|
|
37
35
|
typescript?: boolean;
|
|
38
36
|
packageManager?: PackageManagerName;
|
|
39
37
|
defaultAll?: boolean;
|