2020-05-28 10:23:10 +02:00
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
2020-05-26 18:39:18 +02:00
|
|
|
import retry from 'async-retry'
|
2019-07-19 21:55:30 +02:00
|
|
|
import chalk from 'chalk'
|
|
|
|
import cpy from 'cpy'
|
|
|
|
import fs from 'fs'
|
|
|
|
import os from 'os'
|
|
|
|
import path from 'path'
|
2020-02-27 16:32:33 +01:00
|
|
|
import {
|
|
|
|
downloadAndExtractExample,
|
|
|
|
downloadAndExtractRepo,
|
2020-05-26 18:39:18 +02:00
|
|
|
getRepoInfo,
|
|
|
|
hasExample,
|
|
|
|
hasRepo,
|
2020-02-27 16:32:33 +01:00
|
|
|
RepoInfo,
|
|
|
|
} from './helpers/examples'
|
2020-07-10 03:29:03 +02:00
|
|
|
import { makeDir } from './helpers/make-dir'
|
2019-11-11 06:42:51 +01:00
|
|
|
import { tryGitInit } from './helpers/git'
|
2019-07-19 21:55:30 +02:00
|
|
|
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'
|
2020-08-19 19:09:34 +02:00
|
|
|
import { isWriteable } from './helpers/is-writeable'
|
2019-07-19 21:55:30 +02:00
|
|
|
|
2020-05-26 18:39:18 +02:00
|
|
|
export class DownloadError extends Error {}
|
|
|
|
|
2019-07-19 21:55:30 +02:00
|
|
|
export async function createApp({
|
|
|
|
appPath,
|
|
|
|
useNpm,
|
|
|
|
example,
|
2020-02-27 16:32:33 +01:00
|
|
|
examplePath,
|
2019-07-19 21:55:30 +02:00
|
|
|
}: {
|
|
|
|
appPath: string
|
|
|
|
useNpm: boolean
|
|
|
|
example?: string
|
2020-02-27 16:32:33 +01:00
|
|
|
examplePath?: string
|
2020-05-10 23:51:47 +02:00
|
|
|
}): Promise<void> {
|
2020-02-27 16:32:33 +01:00
|
|
|
let repoInfo: RepoInfo | undefined
|
|
|
|
|
2019-07-19 21:55:30 +02:00
|
|
|
if (example) {
|
2020-02-27 16:32:33 +01:00
|
|
|
let repoUrl: URL | undefined
|
|
|
|
|
|
|
|
try {
|
|
|
|
repoUrl = new URL(example)
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'ERR_INVALID_URL') {
|
|
|
|
console.error(error)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repoUrl) {
|
|
|
|
if (repoUrl.origin !== 'https://github.com') {
|
|
|
|
console.error(
|
|
|
|
`Invalid URL: ${chalk.red(
|
|
|
|
`"${example}"`
|
|
|
|
)}. Only GitHub repositories are supported. Please use a GitHub URL and try again.`
|
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2020-04-06 15:57:19 +02:00
|
|
|
repoInfo = await getRepoInfo(repoUrl, examplePath)
|
2020-02-27 16:32:33 +01:00
|
|
|
|
|
|
|
if (!repoInfo) {
|
|
|
|
console.error(
|
|
|
|
`Found invalid GitHub URL: ${chalk.red(
|
|
|
|
`"${example}"`
|
|
|
|
)}. Please fix the URL and try again.`
|
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const found = await hasRepo(repoInfo)
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
console.error(
|
|
|
|
`Could not locate the repository for ${chalk.red(
|
|
|
|
`"${example}"`
|
|
|
|
)}. Please check that the repository exists and try again.`
|
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2020-05-26 18:39:18 +02:00
|
|
|
} else if (example !== '__internal-testing-retry') {
|
2020-02-27 16:32:33 +01:00
|
|
|
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)
|
|
|
|
}
|
2019-07-19 21:55:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const root = path.resolve(appPath)
|
2020-08-19 19:09:34 +02:00
|
|
|
|
|
|
|
if (!(await isWriteable(path.dirname(root)))) {
|
|
|
|
console.error(
|
|
|
|
'The application path is not writable, please check folder permissions and try again.'
|
|
|
|
)
|
|
|
|
console.error(
|
|
|
|
'It is likely you do not have write permissions for this folder.'
|
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2019-07-19 21:55:30 +02:00
|
|
|
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) {
|
2020-05-26 18:39:18 +02:00
|
|
|
try {
|
|
|
|
if (repoInfo) {
|
|
|
|
const repoInfo2 = repoInfo
|
|
|
|
console.log(
|
|
|
|
`Downloading files from repo ${chalk.cyan(
|
|
|
|
example
|
|
|
|
)}. This might take a moment.`
|
|
|
|
)
|
|
|
|
console.log()
|
|
|
|
await retry(() => downloadAndExtractRepo(root, repoInfo2), {
|
|
|
|
retries: 3,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
`Downloading files for example ${chalk.cyan(
|
|
|
|
example
|
|
|
|
)}. This might take a moment.`
|
|
|
|
)
|
|
|
|
console.log()
|
|
|
|
await retry(() => downloadAndExtractExample(root, example), {
|
|
|
|
retries: 3,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (reason) {
|
|
|
|
throw new DownloadError(reason)
|
2020-02-27 16:32:33 +01:00
|
|
|
}
|
2019-11-11 06:42:51 +01:00
|
|
|
// Copy our default `.gitignore` if the application did not provide one
|
|
|
|
const ignorePath = path.join(root, '.gitignore')
|
|
|
|
if (!fs.existsSync(ignorePath)) {
|
|
|
|
fs.copyFileSync(
|
|
|
|
path.join(__dirname, 'templates', 'default', 'gitignore'),
|
|
|
|
ignorePath
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-19 21:55:30 +02:00
|
|
|
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'),
|
2020-05-18 21:24:37 +02:00
|
|
|
rename: (name) => {
|
2019-07-19 21:55:30 +02:00
|
|
|
switch (name) {
|
|
|
|
case 'gitignore': {
|
|
|
|
return '.'.concat(name)
|
|
|
|
}
|
2020-02-16 16:00:12 +01:00
|
|
|
// README.md is ignored by webpack-asset-relocator-loader used by ncc:
|
|
|
|
// https://github.com/zeit/webpack-asset-relocator-loader/blob/e9308683d47ff507253e37c9bcbb99474603192b/src/asset-relocator.js#L227
|
|
|
|
case 'README-template.md': {
|
|
|
|
return 'README.md'
|
|
|
|
}
|
2019-07-19 21:55:30 +02:00
|
|
|
default: {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-11 06:42:51 +01:00
|
|
|
if (tryGitInit(root)) {
|
|
|
|
console.log('Initialized a git repository.')
|
|
|
|
console.log()
|
|
|
|
}
|
|
|
|
|
2019-07-19 21:55:30 +02:00
|
|
|
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()
|
|
|
|
}
|