diff --git a/packages/create-next-app/helpers/examples.ts b/packages/create-next-app/helpers/examples.ts index 43cec7ac8f224..2b0dad8d5c336 100644 --- a/packages/create-next-app/helpers/examples.ts +++ b/packages/create-next-app/helpers/examples.ts @@ -22,7 +22,7 @@ export function getRepoInfo( const filePath = examplePath ? examplePath.replace(/^\//, '') : file.join('/') // If examplePath is available, the branch name takes the entire path const branch = examplePath - ? `${_branch}/${file.join('/')}`.replace(new RegExp(`/${filePath}$`), '') + ? `${_branch}/${file.join('/')}`.replace(new RegExp(`/${filePath}|/$`), '') : _branch if (username && name && branch && t === 'tree') { diff --git a/test/integration/create-next-app/index.test.js b/test/integration/create-next-app/index.test.js index 94af494af937b..80efaca4de1d3 100644 --- a/test/integration/create-next-app/index.test.js +++ b/test/integration/create-next-app/index.test.js @@ -81,4 +81,77 @@ describe('create next app', () => { fs.existsSync(path.join(cwd, projectName, '.gitignore')) ).toBeTruthy() }) + + it('should allow example with GitHub URL', async () => { + const projectName = 'github-app' + const res = await run( + projectName, + '--example', + 'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages' + ) + + expect(res.exitCode).toBe(0) + expect( + fs.existsSync(path.join(cwd, projectName, 'package.json')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, 'pages/index.js')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, 'pages/about.js')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, '.gitignore')) + ).toBeTruthy() + }) + + it('should allow example with GitHub URL and example-path', async () => { + const projectName = 'github-example-path' + const res = await run( + projectName, + '--example', + 'https://github.com/zeit/next-learn-demo/tree/master', + '--example-path', + '1-navigate-between-pages' + ) + + expect(res.exitCode).toBe(0) + expect( + fs.existsSync(path.join(cwd, projectName, 'package.json')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, 'pages/index.js')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, 'pages/about.js')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, '.gitignore')) + ).toBeTruthy() + }) + + it('should use --example-path over the file path in the GitHub URL', async () => { + const projectName = 'github-example-path-2' + const res = await run( + projectName, + '--example', + 'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages', + '--example-path', + '1-navigate-between-pages' + ) + + expect(res.exitCode).toBe(0) + expect( + fs.existsSync(path.join(cwd, projectName, 'package.json')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, 'pages/index.js')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, 'pages/about.js')) + ).toBeTruthy() + expect( + fs.existsSync(path.join(cwd, projectName, '.gitignore')) + ).toBeTruthy() + }) })