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 [
...templates.map((template) => makeNameAndValueChoice(template)),
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)',
});
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(`
${chalk.cyan('Creating new Docusaurus project...')}
`);
if (template && isValidGitRepoUrl(template)) {
if (isValidGitRepoUrl(template)) {
console.log(`Cloning Git template ${chalk.cyan(template)}...`);
if (
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!`));
}
} else if (template && templates.includes(template)) {
} else if (templates.includes(template)) {
// Docusaurus templates.
if (useTS) {
if (!hasTS(template)) {
@ -208,6 +233,14 @@ ${chalk.cyan('Creating new Docusaurus project...')}
);
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 {
throw new Error('Invalid template.');
}