mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
refactor(create-discord-bot): replace deps with built-in apis (#10971)
This commit is contained in:
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
// eslint-disable-next-line n/shebang
|
// eslint-disable-next-line n/shebang
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
|
import { styleText } from 'node:util';
|
||||||
import { Option, program } from 'commander';
|
import { Option, program } from 'commander';
|
||||||
import picocolors from 'picocolors';
|
|
||||||
import prompts from 'prompts';
|
import prompts from 'prompts';
|
||||||
import validateProjectName from 'validate-npm-package-name';
|
import validateProjectName from 'validate-npm-package-name';
|
||||||
import packageJSON from '../package.json' assert { type: 'json' };
|
import packageJSON from '../package.json' assert { type: 'json' };
|
||||||
@@ -34,7 +34,7 @@ program
|
|||||||
.version(packageJSON.version)
|
.version(packageJSON.version)
|
||||||
.description('Create a basic discord.js bot.')
|
.description('Create a basic discord.js bot.')
|
||||||
.argument('[directory]', 'What is the name of the directory you want to create this project in?')
|
.argument('[directory]', 'What is the name of the directory you want to create this project in?')
|
||||||
.usage(`${picocolors.green('<directory>')}`)
|
.usage(`${styleText('green', '<directory>')}`)
|
||||||
.action((directory) => {
|
.action((directory) => {
|
||||||
projectDirectory = directory;
|
projectDirectory = directory;
|
||||||
})
|
})
|
||||||
@@ -68,13 +68,16 @@ if (!projectDirectory) {
|
|||||||
const errors = [];
|
const errors = [];
|
||||||
|
|
||||||
for (const error of [...(validationResult.errors ?? []), ...(validationResult.warnings ?? [])]) {
|
for (const error of [...(validationResult.errors ?? []), ...(validationResult.warnings ?? [])]) {
|
||||||
errors.push(picocolors.red(`- ${error}`));
|
errors.push(styleText('red', `- ${error}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
return picocolors.red(
|
return styleText(
|
||||||
`Cannot create a project named ${picocolors.yellow(
|
'red',
|
||||||
|
`Cannot create a project named ${styleText(
|
||||||
|
'yellow',
|
||||||
`"${directory}"`,
|
`"${directory}"`,
|
||||||
)} due to npm naming restrictions.\n\nErrors:\n${errors.join('\n')}\n\n${picocolors.red(
|
)} due to npm naming restrictions.\n\nErrors:\n${errors.join('\n')}\n\n${styleText(
|
||||||
|
'red',
|
||||||
'\nSee https://docs.npmjs.com/cli/configuring-npm/package-json for more details.',
|
'\nSee https://docs.npmjs.com/cli/configuring-npm/package-json for more details.',
|
||||||
)}}`,
|
)}}`,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -51,8 +51,6 @@
|
|||||||
"funding": "https://github.com/discordjs/discord.js?sponsor",
|
"funding": "https://github.com/discordjs/discord.js?sponsor",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"commander": "^13.1.0",
|
"commander": "^13.1.0",
|
||||||
"fast-glob": "^3.3.3",
|
|
||||||
"picocolors": "^1.1.1",
|
|
||||||
"prompts": "^2.4.2",
|
"prompts": "^2.4.2",
|
||||||
"validate-npm-package-name": "^6.0.0"
|
"validate-npm-package-name": "^6.0.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import type { ExecException } from 'node:child_process';
|
import type { ExecException } from 'node:child_process';
|
||||||
import { cp, stat, mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
|
import { cp, glob, mkdir, stat, readdir, readFile, writeFile } from 'node:fs/promises';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import { URL } from 'node:url';
|
import { URL } from 'node:url';
|
||||||
import glob from 'fast-glob';
|
import { styleText } from 'node:util';
|
||||||
import picocolors from 'picocolors';
|
|
||||||
import type { PackageManager } from './helpers/packageManager.js';
|
import type { PackageManager } from './helpers/packageManager.js';
|
||||||
import { install, isNodePackageManager } from './helpers/packageManager.js';
|
import { install, isNodePackageManager } from './helpers/packageManager.js';
|
||||||
import { GUIDE_URL } from './util/constants.js';
|
import { GUIDE_URL } from './util/constants.js';
|
||||||
@@ -35,15 +34,16 @@ export async function createDiscordBot({ directory, installPackages, typescript,
|
|||||||
// If the directory is actually a file or if it's not empty, throw an error.
|
// If the directory is actually a file or if it's not empty, throw an error.
|
||||||
if (!directoryStats.isDirectory() || (await readdir(root)).length > 0) {
|
if (!directoryStats.isDirectory() || (await readdir(root)).length > 0) {
|
||||||
console.error(
|
console.error(
|
||||||
picocolors.red(
|
styleText(
|
||||||
`The directory ${picocolors.yellow(`"${directoryName}"`)} is either not a directory or is not empty.`,
|
'red',
|
||||||
|
`The directory ${styleText('yellow', `"${directoryName}"`)} is either not a directory or is not empty.`,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
console.error(picocolors.red(`Please specify an empty directory.`));
|
console.error(styleText('red', `Please specify an empty directory.`));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Creating ${directoryName} in ${picocolors.green(root)}.`);
|
console.log(`Creating ${directoryName} in ${styleText('green', root)}.`);
|
||||||
const deno = packageManager === 'deno';
|
const deno = packageManager === 'deno';
|
||||||
await cp(new URL(`../template/${deno ? 'Deno' : typescript ? 'TypeScript' : 'JavaScript'}`, import.meta.url), root, {
|
await cp(new URL(`../template/${deno ? 'Deno' : typescript ? 'TypeScript' : 'JavaScript'}`, import.meta.url), root, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
@@ -80,8 +80,8 @@ export async function createDiscordBot({ directory, installPackages, typescript,
|
|||||||
});
|
});
|
||||||
await writeFile('./.vscode/settings.json', newVSCodeSettings);
|
await writeFile('./.vscode/settings.json', newVSCodeSettings);
|
||||||
|
|
||||||
const globStream = glob.stream('./src/**/*.ts');
|
const globIterator = glob('./src/**/*.ts');
|
||||||
for await (const file of globStream) {
|
for await (const file of globIterator) {
|
||||||
const newData = await readFile(file, { encoding: 'utf8' }).then((str) =>
|
const newData = await readFile(file, { encoding: 'utf8' }).then((str) =>
|
||||||
str.replaceAll('[REPLACE_IMPORT_EXT]', typescript && !isNodePackageManager(packageManager) ? 'ts' : 'js'),
|
str.replaceAll('[REPLACE_IMPORT_EXT]', typescript && !isNodePackageManager(packageManager) ? 'ts' : 'js'),
|
||||||
);
|
);
|
||||||
@@ -109,15 +109,15 @@ export async function createDiscordBot({ directory, installPackages, typescript,
|
|||||||
console.log();
|
console.log();
|
||||||
const err = error as ExecException;
|
const err = error as ExecException;
|
||||||
if (err.signal === 'SIGINT') {
|
if (err.signal === 'SIGINT') {
|
||||||
console.log(picocolors.red('Installation aborted.'));
|
console.log(styleText('red', 'Installation aborted.'));
|
||||||
} else {
|
} else {
|
||||||
console.error(picocolors.red('Installation failed.'));
|
console.error(styleText('red', 'Installation failed.'));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log();
|
console.log();
|
||||||
console.log(picocolors.green('All done! Be sure to read through the discord.js guide for help on your journey.'));
|
console.log(styleText('green', 'All done! Be sure to read through the discord.js guide for help on your journey.'));
|
||||||
console.log(`Link: ${picocolors.cyan(GUIDE_URL)}`);
|
console.log(`Link: ${styleText('cyan', GUIDE_URL)}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { execSync } from 'node:child_process';
|
import { execSync } from 'node:child_process';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import picocolors from 'picocolors';
|
import { styleText } from 'node:util';
|
||||||
import { DEFAULT_PACKAGE_MANAGER, NODE_PACKAGE_MANAGERS } from '../util/constants.js';
|
import { DEFAULT_PACKAGE_MANAGER, NODE_PACKAGE_MANAGERS } from '../util/constants.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,7 +36,8 @@ export function resolvePackageManager(): PackageManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.error(
|
console.error(
|
||||||
picocolors.yellow(
|
styleText(
|
||||||
|
'yellow',
|
||||||
`Detected an unsupported package manager (${npmConfigUserAgent}). Falling back to ${DEFAULT_PACKAGE_MANAGER}.`,
|
`Detected an unsupported package manager (${npmConfigUserAgent}). Falling back to ${DEFAULT_PACKAGE_MANAGER}.`,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
@@ -937,12 +937,6 @@ importers:
|
|||||||
commander:
|
commander:
|
||||||
specifier: ^13.1.0
|
specifier: ^13.1.0
|
||||||
version: 13.1.0
|
version: 13.1.0
|
||||||
fast-glob:
|
|
||||||
specifier: ^3.3.3
|
|
||||||
version: 3.3.3
|
|
||||||
picocolors:
|
|
||||||
specifier: ^1.1.1
|
|
||||||
version: 1.1.1
|
|
||||||
prompts:
|
prompts:
|
||||||
specifier: ^2.4.2
|
specifier: ^2.4.2
|
||||||
version: 2.4.2
|
version: 2.4.2
|
||||||
|
|||||||
Reference in New Issue
Block a user