diff options
author | 2022-09-20 14:30:13 +0200 | |
---|---|---|
committer | 2022-09-20 08:30:13 -0400 | |
commit | c84d85ba4d85f250d87bbc98c74665992f6c2768 (patch) | |
tree | bcca0ad05692783f028f7c80c19954e8915bd27c /packages/create-astro/src/index.ts | |
parent | f2b515d0b48e8023ee28013e75e01fc8f058e010 (diff) | |
download | astro-c84d85ba4d85f250d87bbc98c74665992f6c2768.tar.gz astro-c84d85ba4d85f250d87bbc98c74665992f6c2768.tar.zst astro-c84d85ba4d85f250d87bbc98c74665992f6c2768.zip |
Add support for running `create-astro` in cloned empty git repository (#4805)
Diffstat (limited to 'packages/create-astro/src/index.ts')
-rw-r--r-- | packages/create-astro/src/index.ts | 54 |
1 files changed, 48 insertions, 6 deletions
diff --git a/packages/create-astro/src/index.ts b/packages/create-astro/src/index.ts index 186650d9e..ee548f59d 100644 --- a/packages/create-astro/src/index.ts +++ b/packages/create-astro/src/index.ts @@ -43,6 +43,46 @@ function isEmpty(dirPath: string) { return !fs.existsSync(dirPath) || fs.readdirSync(dirPath).length === 0; } +// Some existing files and directories can be safely ignored when checking if a directory is a valid project directory. +// https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/create-react-app/createReactApp.js#L907-L934 +const VALID_PROJECT_DIRECTORY_SAFE_LIST = [ + '.DS_Store', + '.git', + '.gitattributes', + '.gitignore', + '.gitlab-ci.yml', + '.hg', + '.hgcheck', + '.hgignore', + '.idea', + '.npmignore', + '.travis.yml', + '.yarn', + '.yarnrc.yml', + 'docs', + 'LICENSE', + 'mkdocs.yml', + 'Thumbs.db', + /\.iml$/, + /^npm-debug\.log/, + /^yarn-debug\.log/, + /^yarn-error\.log/, +]; + +function isValidProjectDirectory(dirPath: string) { + if (!fs.existsSync(dirPath)) { + return true; + } + + const conflicts = fs.readdirSync(dirPath).filter((content) => { + return !VALID_PROJECT_DIRECTORY_SAFE_LIST.some((safeContent) => { + return typeof safeContent === 'string' ? content === safeContent : safeContent.test(content); + }); + }); + + return conflicts.length === 0; +} + const { version } = JSON.parse( fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8') ); @@ -59,7 +99,7 @@ export async function main() { let cwd = args['_'][2] as string; - if (cwd && isEmpty(cwd)) { + if (cwd && isValidProjectDirectory(cwd)) { let acknowledgeProjectDir = ora({ color: 'green', text: `Using ${bold(cwd)} as project directory.`, @@ -67,10 +107,10 @@ export async function main() { acknowledgeProjectDir.succeed(); } - if (!cwd || !isEmpty(cwd)) { + if (!cwd || !isValidProjectDirectory(cwd)) { const notEmptyMsg = (dirPath: string) => `"${bold(dirPath)}" is not empty!`; - if (!isEmpty(cwd)) { + if (!isValidProjectDirectory(cwd)) { let rejectProjectDir = ora({ color: 'red', text: notEmptyMsg(cwd) }); rejectProjectDir.fail(); } @@ -81,7 +121,7 @@ export async function main() { message: 'Where would you like to create your new project?', initial: './my-astro-site', validate(value) { - if (!isEmpty(value)) { + if (!isValidProjectDirectory(value)) { return notEmptyMsg(value); } return true; @@ -143,8 +183,10 @@ export async function main() { // degit does not return an error when an invalid template is provided, as such we need to handle this manually // It's not very pretty, but to the user eye, we just return a nice message and nothing weird happened - if (isEmpty(cwd)) { - fs.rmdirSync(cwd); + if (isValidProjectDirectory(cwd)) { + if (isEmpty(cwd)) { + fs.rmdirSync(cwd); + } throw new Error(`Error: The provided template (${cyan(options.template)}) does not exist`); } } catch (err: any) { |