mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
feat(create-discord-bot): allow certain files/folders to be included in the target directory (#11368)
* feat(create-discord-bot): allow empty git repositories
* feat(create-discord-bot): create a .gitignore for git repositories
* fix(create-discord-bot): correctly handle .git file edge case
* refactor(create-discord-bot): use next.js "is-folder-empty" utility
* refactor(create-discord-bot): combine checks
* revert: revert comment change
This reverts commit 2f8422e90d.
* feat(create-discord-bot): add .gitignore to templates
* docs(create-discord-bot): add source link to isFolderEmpty util
* docs(create-discord-bot): add @license JSDoc tag to license comment
---------
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import type { ExecException } from 'node:child_process';
|
||||
import { cp, mkdir, stat, readdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import { cp, mkdir, stat, readFile, writeFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { styleText } from 'node:util';
|
||||
import type { PackageManager } from './helpers/packageManager.js';
|
||||
import { install } from './helpers/packageManager.js';
|
||||
import { GUIDE_URL } from './util/constants.js';
|
||||
import { isFolderEmpty } from './util/isFolderEmpty.js';
|
||||
|
||||
interface Options {
|
||||
directory: string;
|
||||
@@ -31,7 +32,7 @@ export async function createDiscordBot({ directory, installPackages, typescript,
|
||||
});
|
||||
|
||||
// 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() || !isFolderEmpty(root, directoryName)) {
|
||||
console.error(
|
||||
styleText(
|
||||
'red',
|
||||
|
||||
78
packages/create-discord-bot/src/util/isFolderEmpty.ts
Normal file
78
packages/create-discord-bot/src/util/isFolderEmpty.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* @license MIT
|
||||
*
|
||||
* Copyright (c) 2025 Vercel, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
|
||||
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
|
||||
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Source: https://github.com/vercel/next.js/blob/7fb2aa908216fb3a910c7fa6d24524412b5af6e5/packages/create-next-app/helpers/is-folder-empty.ts
|
||||
*/
|
||||
import { lstatSync, readdirSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { styleText } from 'node:util';
|
||||
|
||||
export function isFolderEmpty(root: string, name: string): boolean {
|
||||
const validFiles = [
|
||||
'.DS_Store',
|
||||
'.git',
|
||||
'.gitattributes',
|
||||
'.gitignore',
|
||||
'.gitlab-ci.yml',
|
||||
'.hg',
|
||||
'.hgcheck',
|
||||
'.hgignore',
|
||||
'.idea',
|
||||
'.npmignore',
|
||||
'.travis.yml',
|
||||
'LICENSE',
|
||||
'Thumbs.db',
|
||||
'docs',
|
||||
'mkdocs.yml',
|
||||
'npm-debug.log',
|
||||
'yarn-debug.log',
|
||||
'yarn-error.log',
|
||||
'yarnrc.yml',
|
||||
'.yarn',
|
||||
];
|
||||
|
||||
const conflicts = readdirSync(root).filter(
|
||||
(file) =>
|
||||
!validFiles.includes(file) &&
|
||||
// Support IntelliJ IDEA-based editors
|
||||
!file.endsWith('.iml'),
|
||||
);
|
||||
|
||||
if (conflicts.length > 0) {
|
||||
console.log(`The directory ${styleText('green', name)} contains files that could conflict:`);
|
||||
console.log();
|
||||
for (const file of conflicts) {
|
||||
try {
|
||||
const stats = lstatSync(join(root, file));
|
||||
if (stats.isDirectory()) {
|
||||
console.log(` ${styleText('blue', file)}/`);
|
||||
} else {
|
||||
console.log(` ${file}`);
|
||||
}
|
||||
} catch {
|
||||
console.log(` ${file}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
console.log('Either try using a new directory name, or remove the files listed above.');
|
||||
console.log();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user