#!/usr/bin/env node import { resolve, join, basename } from 'path' import parseArgs from 'minimist' import { exists, writeFile, mkdir } from 'mz/fs' import mkdirp from 'mkdirp-then' const argv = parseArgs(process.argv.slice(2), { alias: { h: 'help' }, boolean: ['h'] }) if (argv.help) { console.log(` Description Scaffolds a simple project structure to get started quickly Usage $ next init If no directory is provided the current directory will be used. Options --help, -p Displays this message `) process.exit(0) } const dir = resolve(argv._[0] || '.') if (basename(dir) === 'pages') { console.warn('Your root directory is named "pages". This looks suspicious. You probably want to go one directory up.') process.exit(1) } exists(dir) .then(async present => { if (!present) { await mkdirp(dir) } if (!await exists(join(dir, 'package.json'))) { await writeFile(join(dir, 'package.json'), basePackage.replace(/my-app/g, basename(dir))) } if (!await exists(join(dir, 'static'))) { await mkdir(join(dir, 'static')) } if (!await exists(join(dir, 'pages'))) { await mkdir(join(dir, 'pages')) await writeFile(join(dir, 'pages', 'index.js'), basePage) } }) .catch((err) => { console.error(err) process.exit(1) }) const basePackage = `{ "name": "my-app", "description": "", "dependencies": { "next": "latest" }, "scripts": { "dev": "next", "build": "next build", "start": "next start" } }` const basePage = ` import React from 'react' import Head from 'next/head' export default () => (
next.js
) const bodyStyles = \` html, body { height: 100% } body { display: flex; align-items: center; justify-content: center; } \` `