|
| 1 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 2 | +import retry from 'async-retry'; |
| 3 | +import chalk from 'chalk'; |
| 4 | +import cpy from 'cpy'; |
| 5 | +import fs from 'fs'; |
| 6 | +import os from 'os'; |
| 7 | +import path from 'path'; |
| 8 | +import { makeDir } from './helpers/make-dir'; |
| 9 | +import { tryGitInit } from './helpers/git'; |
| 10 | +import { install } from './helpers/install'; |
| 11 | +import { isFolderEmpty } from './helpers/is-folder-empty'; |
| 12 | +import { getOnline } from './helpers/is-online'; |
| 13 | +import { isWriteable } from './helpers/is-writeable'; |
| 14 | +import type { PackageManager } from './helpers/get-pkg-manager'; |
| 15 | + |
| 16 | +export class DownloadError extends Error {} |
| 17 | + |
| 18 | +export async function createApp({ |
| 19 | + appPath, |
| 20 | + packageManager, |
| 21 | + typescript, |
| 22 | +}: { |
| 23 | + appPath: string; |
| 24 | + packageManager: PackageManager; |
| 25 | + typescript?: boolean; |
| 26 | +}): Promise<void> { |
| 27 | + const template = typescript ? 'typescript' : 'default'; |
| 28 | + |
| 29 | + const root = path.resolve(appPath); |
| 30 | + |
| 31 | + if (!(await isWriteable(path.dirname(root)))) { |
| 32 | + console.error( |
| 33 | + 'The application path is not writable, please check folder permissions and try again.' |
| 34 | + ); |
| 35 | + console.error( |
| 36 | + 'It is likely you do not have write permissions for this folder.' |
| 37 | + ); |
| 38 | + process.exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + const appName = path.basename(root); |
| 42 | + |
| 43 | + await makeDir(root); |
| 44 | + if (!isFolderEmpty(root, appName)) { |
| 45 | + process.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + const useYarn = packageManager === 'yarn'; |
| 49 | + const isOnline = !useYarn || (await getOnline()); |
| 50 | + const originalDirectory = process.cwd(); |
| 51 | + |
| 52 | + console.log(`Creating a new Next.js app in ${chalk.green(root)}.`); |
| 53 | + console.log(); |
| 54 | + |
| 55 | + process.chdir(root); |
| 56 | + |
| 57 | + /** |
| 58 | + * Otherwise, if an example repository is not provided for cloning, proceed |
| 59 | + * by installing from a template. |
| 60 | + */ |
| 61 | + console.log(chalk.bold(`Using ${packageManager}.`)); |
| 62 | + /** |
| 63 | + * Create a package.json for the new project. |
| 64 | + */ |
| 65 | + const packageJson = { |
| 66 | + name: appName, |
| 67 | + version: '0.1.0', |
| 68 | + private: true, |
| 69 | + scripts: { |
| 70 | + dev: 'next dev', |
| 71 | + build: 'next build', |
| 72 | + start: 'next start', |
| 73 | + lint: 'next lint', |
| 74 | + }, |
| 75 | + }; |
| 76 | + /** |
| 77 | + * Write it to disk. |
| 78 | + */ |
| 79 | + fs.writeFileSync( |
| 80 | + path.join(root, 'package.json'), |
| 81 | + JSON.stringify(packageJson, null, 2) + os.EOL |
| 82 | + ); |
| 83 | + /** |
| 84 | + * These flags will be passed to `install()`. |
| 85 | + */ |
| 86 | + const installFlags = { packageManager, isOnline }; |
| 87 | + /** |
| 88 | + * Default dependencies. |
| 89 | + */ |
| 90 | + const dependencies = ['react', 'react-dom', 'next', 'wagmi', 'ethers']; |
| 91 | + /** |
| 92 | + * Default devDependencies. |
| 93 | + */ |
| 94 | + const devDependencies = [ |
| 95 | + 'eslint', |
| 96 | + 'eslint-config-next', |
| 97 | + 'autoprefixer', |
| 98 | + 'postcss', |
| 99 | + 'tailwindcss', |
| 100 | + ]; |
| 101 | + /** |
| 102 | + * TypeScript projects will have type definitions and other devDependencies. |
| 103 | + */ |
| 104 | + if (typescript) { |
| 105 | + devDependencies.push( |
| 106 | + 'typescript', |
| 107 | + '@types/react', |
| 108 | + '@types/node', |
| 109 | + '@types/react-dom' |
| 110 | + ); |
| 111 | + } |
| 112 | + /** |
| 113 | + * Install package.json dependencies if they exist. |
| 114 | + */ |
| 115 | + if (dependencies.length) { |
| 116 | + console.log(); |
| 117 | + console.log('Installing dependencies:'); |
| 118 | + for (const dependency of dependencies) { |
| 119 | + console.log(`- ${chalk.cyan(dependency)}`); |
| 120 | + } |
| 121 | + console.log(); |
| 122 | + |
| 123 | + await install(root, dependencies, installFlags); |
| 124 | + } |
| 125 | + /** |
| 126 | + * Install package.json devDependencies if they exist. |
| 127 | + */ |
| 128 | + if (devDependencies.length) { |
| 129 | + console.log(); |
| 130 | + console.log('Installing devDependencies:'); |
| 131 | + for (const devDependency of devDependencies) { |
| 132 | + console.log(`- ${chalk.cyan(devDependency)}`); |
| 133 | + } |
| 134 | + console.log(); |
| 135 | + |
| 136 | + const devInstallFlags = { devDependencies: true, ...installFlags }; |
| 137 | + await install(root, devDependencies, devInstallFlags); |
| 138 | + } |
| 139 | + console.log(); |
| 140 | + /** |
| 141 | + * Copy the template files to the target directory. |
| 142 | + */ |
| 143 | + await cpy('**', root, { |
| 144 | + parents: true, |
| 145 | + cwd: path.join(__dirname, 'templates', template), |
| 146 | + rename: (name) => { |
| 147 | + switch (name) { |
| 148 | + case 'gitignore': |
| 149 | + case 'eslintrc.json': { |
| 150 | + return '.'.concat(name); |
| 151 | + } |
| 152 | + // README.md is ignored by webpack-asset-relocator-loader used by ncc: |
| 153 | + // https://github.com/vercel/webpack-asset-relocator-loader/blob/e9308683d47ff507253e37c9bcbb99474603192b/src/asset-relocator.js#L227 |
| 154 | + case 'README-template.md': { |
| 155 | + return 'README.md'; |
| 156 | + } |
| 157 | + default: { |
| 158 | + return name; |
| 159 | + } |
| 160 | + } |
| 161 | + }, |
| 162 | + }); |
| 163 | + |
| 164 | + if (tryGitInit(root)) { |
| 165 | + console.log('Initialized a git repository.'); |
| 166 | + console.log(); |
| 167 | + } |
| 168 | + |
| 169 | + let cdpath: string; |
| 170 | + if (path.join(originalDirectory, appName) === appPath) { |
| 171 | + cdpath = appName; |
| 172 | + } else { |
| 173 | + cdpath = appPath; |
| 174 | + } |
| 175 | + |
| 176 | + console.log(`${chalk.green('Success!')} Created ${appName} at ${appPath}`); |
| 177 | + console.log('Inside that directory, you can run several commands:'); |
| 178 | + console.log(); |
| 179 | + console.log(chalk.cyan(` ${packageManager} ${useYarn ? '' : 'run '}dev`)); |
| 180 | + console.log(' Starts the development server.'); |
| 181 | + console.log(); |
| 182 | + console.log(chalk.cyan(` ${packageManager} ${useYarn ? '' : 'run '}build`)); |
| 183 | + console.log(' Builds the app for production.'); |
| 184 | + console.log(); |
| 185 | + console.log(chalk.cyan(` ${packageManager} start`)); |
| 186 | + console.log(' Runs the built app in production mode.'); |
| 187 | + console.log(); |
| 188 | + console.log('We suggest that you begin by typing:'); |
| 189 | + console.log(); |
| 190 | + console.log(chalk.cyan(' cd'), cdpath); |
| 191 | + console.log( |
| 192 | + ` ${chalk.cyan(`${packageManager} ${useYarn ? '' : 'run '}dev`)}` |
| 193 | + ); |
| 194 | + console.log(); |
| 195 | +} |
0 commit comments