feat(create-docusaurus): allow using local folder as template (#3458)

* feat(docusaurus-init): search the local ./templates folder during doc init

* Add docs

Co-authored-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
Afshin Mehrabani 2021-11-15 04:37:22 +00:00 committed by GitHub
parent 11f9a54a32
commit 54d0755493
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View file

@ -63,6 +63,7 @@ function createTemplateChoices(templates: string[]) {
return [ return [
...templates.map((template) => makeNameAndValueChoice(template)), ...templates.map((template) => makeNameAndValueChoice(template)),
makeNameAndValueChoice('Git repository'), makeNameAndValueChoice('Git repository'),
makeNameAndValueChoice('Local template'),
]; ];
} }
@ -176,13 +177,37 @@ export default async function init(
'Enter a repository URL from GitHub, Bitbucket, GitLab, or any other public repo.\n(e.g: https://github.com/ownerName/repoName.git)', 'Enter a repository URL from GitHub, Bitbucket, GitLab, or any other public repo.\n(e.g: https://github.com/ownerName/repoName.git)',
}); });
template = repoPrompt.gitRepoUrl; template = repoPrompt.gitRepoUrl;
} else if (template === 'Local template') {
const dirPrompt = await prompts({
type: 'text',
name: 'templateDir',
validate: (dir?: string) => {
if (dir) {
const fullDir = path.resolve(process.cwd(), dir);
if (fs.existsSync(fullDir)) {
return true;
}
return chalk.red(
`The path ${chalk.magenta(fullDir)} does not exist.`,
);
}
return chalk.red('Please enter a valid path.');
},
message:
'Enter a local folder path, relative to the current working directory.',
});
template = dirPrompt.templateDir;
}
if (!template) {
throw new Error('Template should not be empty');
} }
console.log(` console.log(`
${chalk.cyan('Creating new Docusaurus project...')} ${chalk.cyan('Creating new Docusaurus project...')}
`); `);
if (template && isValidGitRepoUrl(template)) { if (isValidGitRepoUrl(template)) {
console.log(`Cloning Git template ${chalk.cyan(template)}...`); console.log(`Cloning Git template ${chalk.cyan(template)}...`);
if ( if (
shell.exec(`git clone --recursive ${template} ${dest}`, {silent: true}) shell.exec(`git clone --recursive ${template} ${dest}`, {silent: true})
@ -190,7 +215,7 @@ ${chalk.cyan('Creating new Docusaurus project...')}
) { ) {
throw new Error(chalk.red(`Cloning Git template ${template} failed!`)); throw new Error(chalk.red(`Cloning Git template ${template} failed!`));
} }
} else if (template && templates.includes(template)) { } else if (templates.includes(template)) {
// Docusaurus templates. // Docusaurus templates.
if (useTS) { if (useTS) {
if (!hasTS(template)) { if (!hasTS(template)) {
@ -208,6 +233,14 @@ ${chalk.cyan('Creating new Docusaurus project...')}
); );
throw err; throw err;
} }
} else if (fs.existsSync(path.resolve(process.cwd(), template))) {
const templateDir = path.resolve(process.cwd(), template);
try {
await fs.copy(templateDir, dest);
} catch (err) {
console.log(`Copying local template ${templateDir} failed!`);
throw err;
}
} else { } else {
throw new Error('Invalid template.'); throw new Error('Invalid template.');
} }

View file

@ -34,6 +34,8 @@ npx create-docusaurus@latest website classic
If you do not specify `name` or `template`, it will prompt you for them. We recommend the `classic` template so that you can get started quickly, and it contains features found in Docusaurus 1. The `classic` template contains `@docusaurus/preset-classic` which includes standard documentation, a blog, custom pages, and a CSS framework (with dark mode support). You can get up and running extremely quickly with the classic template and customize things later on when you have gained more familiarity with Docusaurus. If you do not specify `name` or `template`, it will prompt you for them. We recommend the `classic` template so that you can get started quickly, and it contains features found in Docusaurus 1. The `classic` template contains `@docusaurus/preset-classic` which includes standard documentation, a blog, custom pages, and a CSS framework (with dark mode support). You can get up and running extremely quickly with the classic template and customize things later on when you have gained more familiarity with Docusaurus.
The `template` also accepts a git repo URL or a local file path, with the latter evaluated relative to the current working directory. The repo/folder content will be copied to the site directory.
**[FB-Only]:** If you are setting up a new Docusaurus website for a Facebook open source project, use the `facebook` template instead, which comes with some useful Facebook-specific defaults: **[FB-Only]:** If you are setting up a new Docusaurus website for a Facebook open source project, use the `facebook` template instead, which comes with some useful Facebook-specific defaults:
```bash ```bash