Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/create-next-app/@types/promisepipe.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'promisepipe'
1 change: 1 addition & 0 deletions packages/create-next-app/@types/update-check.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'update-check'
12 changes: 12 additions & 0 deletions packages/create-next-app/@types/validate-npm-package-name.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare module 'validate-npm-package-name' {
function validate(
projectName: string
): {
validForNewPackages: boolean
validForOldPackages: boolean
errors?: string[] | null
warnings?: string[] | null
}

export default validate
}
9 changes: 0 additions & 9 deletions packages/create-next-app/LICENSE

This file was deleted.

181 changes: 0 additions & 181 deletions packages/create-next-app/README.md

This file was deleted.

30 changes: 0 additions & 30 deletions packages/create-next-app/cli.js

This file was deleted.

132 changes: 132 additions & 0 deletions packages/create-next-app/create-app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import chalk from 'chalk'
import cpy from 'cpy'
import fs from 'fs'
import makeDir from 'make-dir'
import os from 'os'
import path from 'path'

import { downloadAndExtractExample, hasExample } from './helpers/examples'
import { install } from './helpers/install'
import { isFolderEmpty } from './helpers/is-folder-empty'
import { getOnline } from './helpers/is-online'
import { shouldUseYarn } from './helpers/should-use-yarn'

export async function createApp({
appPath,
useNpm,
example,
}: {
appPath: string
useNpm: boolean
example?: string
}) {
if (example) {
const found = await hasExample(example)
if (!found) {
console.error(
`Could not locate an example named ${chalk.red(
`"${example}"`
)}. Please check your spelling and try again.`
)
process.exit(1)
}
}

const root = path.resolve(appPath)
const appName = path.basename(root)

await makeDir(root)
if (!isFolderEmpty(root, appName)) {
process.exit(1)
}

const useYarn = useNpm ? false : shouldUseYarn()
const isOnline = !useYarn || (await getOnline())
const originalDirectory = process.cwd()

const displayedCommand = useYarn ? 'yarn' : 'npm'
console.log(`Creating a new Next.js app in ${chalk.green(root)}.`)
console.log()

await makeDir(root)
process.chdir(root)

if (example) {
console.log(
`Downloading files for example ${chalk.cyan(
example
)}. This might take a moment.`
)
console.log()
await downloadAndExtractExample(root, example)

console.log('Installing packages. This might take a couple of minutes.')
console.log()

await install(root, null, { useYarn, isOnline })
console.log()
} else {
const packageJson = {
name: appName,
version: '0.1.0',
private: true,
scripts: { dev: 'next dev', build: 'next build', start: 'next start' },
}
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify(packageJson, null, 2) + os.EOL
)

console.log(
`Installing ${chalk.cyan('react')}, ${chalk.cyan(
'react-dom'
)}, and ${chalk.cyan('next')} using ${displayedCommand}...`
)
console.log()

await install(root, ['react', 'react-dom', 'next'], { useYarn, isOnline })
console.log()

await cpy('**', root, {
parents: true,
cwd: path.join(__dirname, 'templates', 'default'),
rename: name => {
switch (name) {
case 'gitignore': {
return '.'.concat(name)
}
default: {
return name
}
}
},
})
}

let cdpath: string
if (path.join(originalDirectory, appName) === appPath) {
cdpath = appName
} else {
cdpath = appPath
}

console.log(`${chalk.green('Success!')} Created ${appName} at ${appPath}`)
console.log('Inside that directory, you can run several commands:')
console.log()
console.log(chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}dev`))
console.log(' Starts the development server.')
console.log()
console.log(chalk.cyan(` ${displayedCommand} ${useYarn ? '' : 'run '}build`))
console.log(' Builds the app for production.')
console.log()
console.log(chalk.cyan(` ${displayedCommand} start`))
console.log(' Runs the built app in production mode.')
console.log()
console.log('We suggest that you begin by typing:')
console.log()
console.log(chalk.cyan(' cd'), cdpath)
console.log(
` ${chalk.cyan(`${displayedCommand} ${useYarn ? '' : 'run '}dev`)}`
)
console.log()
}
22 changes: 22 additions & 0 deletions packages/create-next-app/helpers/examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import got from 'got'
import promisePipe from 'promisepipe'
import tar from 'tar'

export async function hasExample(name: string): Promise<boolean> {
const res = await got(
`https://api.github.com/repos/zeit/next.js/contents/examples/${encodeURIComponent(
name
)}/package.json`
).catch(e => e)
return res.statusCode === 200
}

export async function downloadAndExtractExample(
root: string,
name: string
): Promise<void> {
return await promisePipe(
got.stream('https://codeload.github.com/zeit/next.js/tar.gz/canary'),
tar.extract({ cwd: root, strip: 3 }, [`next.js-canary/examples/${name}`])
)
}
Loading