mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-26 23:49:51 +00:00
CLI: Support src directory config
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'create-fumadocs-app': patch
|
||||
---
|
||||
|
||||
Support src directory config
|
||||
@@ -15,6 +15,7 @@ export interface Options {
|
||||
tailwindcss: boolean;
|
||||
packageManager: PackageManager;
|
||||
|
||||
useSrcDir?: boolean;
|
||||
installDeps?: boolean;
|
||||
initializeGit?: boolean;
|
||||
eslint?: boolean;
|
||||
@@ -29,28 +30,71 @@ export async function create(options: Options): Promise<void> {
|
||||
} = options;
|
||||
const projectName = path.basename(options.outputDir);
|
||||
const dest = path.resolve(cwd, options.outputDir);
|
||||
await copy(path.join(sourceDir, `template/+shared`), dest, (name) => {
|
||||
switch (name) {
|
||||
case 'example.gitignore':
|
||||
return '.gitignore';
|
||||
default:
|
||||
return name;
|
||||
}
|
||||
});
|
||||
|
||||
await copy(path.join(sourceDir, `template/${options.template}`), dest);
|
||||
log('Configured Typescript');
|
||||
function defaultRename(file: string): string {
|
||||
file = file.replace('example.gitignore', '.gitignore');
|
||||
|
||||
if (!options.useSrcDir) {
|
||||
return file;
|
||||
}
|
||||
|
||||
for (const dir of ['app', 'lib']) {
|
||||
const relative = path.relative(path.join(dest, dir), file);
|
||||
|
||||
if (!relative.startsWith('../')) {
|
||||
return path.join(dest, 'src', dir, relative);
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
await copy(path.join(sourceDir, `template/+shared`), dest, defaultRename);
|
||||
|
||||
await copy(
|
||||
path.join(sourceDir, `template/${options.template}`),
|
||||
dest,
|
||||
defaultRename,
|
||||
);
|
||||
|
||||
if (options.tailwindcss) {
|
||||
await copy(path.join(sourceDir, `template/+tailwindcss`), dest);
|
||||
await copy(
|
||||
path.join(sourceDir, `template/+tailwindcss`),
|
||||
dest,
|
||||
defaultRename,
|
||||
);
|
||||
log('Configured Tailwind CSS');
|
||||
}
|
||||
|
||||
if (options.eslint) {
|
||||
await copy(path.join(sourceDir, `template/+eslint`), dest);
|
||||
await copy(path.join(sourceDir, `template/+eslint`), dest, defaultRename);
|
||||
log('Configured ESLint');
|
||||
}
|
||||
|
||||
if (options.useSrcDir) {
|
||||
const tsconfigPath = path.join(dest, 'tsconfig.json');
|
||||
const content = (await fs.readFile(tsconfigPath)).toString();
|
||||
|
||||
const config = JSON.parse(content);
|
||||
|
||||
if (config.compilerOptions?.paths) {
|
||||
Object.assign(config.compilerOptions.paths, {
|
||||
'@/*': ['./src/*'],
|
||||
});
|
||||
}
|
||||
|
||||
await fs.writeFile(tsconfigPath, JSON.stringify(config, null, 2));
|
||||
|
||||
if (options.tailwindcss) {
|
||||
const cssPath = path.join(dest, 'src/app/global.css');
|
||||
|
||||
await fs.writeFile(
|
||||
cssPath,
|
||||
(await fs.readFile(cssPath)).toString().replace('../', '../../'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const packageJson = createPackageJson(projectName, options);
|
||||
await fs.writeFile(path.join(dest, 'package.json'), packageJson);
|
||||
|
||||
@@ -87,7 +131,7 @@ async function copy(
|
||||
|
||||
await Promise.all(
|
||||
files.map((file) =>
|
||||
copy(path.join(from, file), path.join(to, rename(file))),
|
||||
copy(path.join(from, file), rename(path.join(to, file))),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
@@ -155,11 +199,10 @@ function createPackageJson(projectName: string, options: Options): string {
|
||||
}
|
||||
|
||||
if (options.eslint) {
|
||||
packageJson.devDependencies = {
|
||||
...packageJson.devDependencies,
|
||||
Object.assign(packageJson.devDependencies, {
|
||||
eslint: '^8',
|
||||
'eslint-config-next': versionPkg.dependencies.next,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return JSON.stringify(packageJson, undefined, 2);
|
||||
|
||||
@@ -37,12 +37,22 @@ async function main(): Promise<void> {
|
||||
message: 'Choose a content source',
|
||||
initialValue: 'fuma-docs-mdx' as Template,
|
||||
options: [
|
||||
{ value: 'fuma-docs-mdx', label: 'Fumadocs MDX' },
|
||||
{
|
||||
value: 'fuma-docs-mdx',
|
||||
label: 'Fumadocs MDX',
|
||||
hint: 'recommended',
|
||||
},
|
||||
{ value: 'content-collections', label: 'Content Collections' },
|
||||
],
|
||||
}),
|
||||
src: () =>
|
||||
confirm({ message: 'Use `/src` directory?', initialValue: false }),
|
||||
tailwindcss: () => confirm({ message: 'Use Tailwind CSS for styling?' }),
|
||||
eslint: () => confirm({ message: 'Add default ESLint configuration?' }),
|
||||
eslint: () =>
|
||||
confirm({
|
||||
message: 'Add default ESLint configuration?',
|
||||
initialValue: false,
|
||||
}),
|
||||
installDeps: () =>
|
||||
confirm({
|
||||
message: `Do you want to install packages automatically? (detected as ${manager})`,
|
||||
@@ -92,9 +102,10 @@ async function main(): Promise<void> {
|
||||
outputDir: dest,
|
||||
installDeps: options.installDeps,
|
||||
eslint: options.eslint,
|
||||
useSrcDir: options.src,
|
||||
|
||||
log: (message) => {
|
||||
log.info(message);
|
||||
info.message(message);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"paths": {
|
||||
"@/.source": ["./.source/index.ts"],
|
||||
"@/*": ["./*"]
|
||||
},
|
||||
"plugins": [
|
||||
|
||||
Reference in New Issue
Block a user