2016-10-16 23:55:30 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
import { resolve, join, basename } from 'path'
|
|
|
|
import parseArgs from 'minimist'
|
|
|
|
import { exists, writeFile, mkdir } from 'mz/fs'
|
2016-11-03 10:07:15 +00:00
|
|
|
import mkdirp from 'mkdirp-then'
|
2016-10-16 23:55:30 +00:00
|
|
|
|
|
|
|
const argv = parseArgs(process.argv.slice(2), {
|
|
|
|
alias: {
|
|
|
|
h: 'help'
|
|
|
|
},
|
|
|
|
boolean: ['h']
|
|
|
|
})
|
|
|
|
|
2016-12-18 20:28:34 +00:00
|
|
|
if (argv.help) {
|
|
|
|
console.log(`
|
|
|
|
Description
|
|
|
|
Scaffolds a simple project structure to get started quickly
|
|
|
|
|
|
|
|
Usage
|
|
|
|
$ next init <dir>
|
|
|
|
|
|
|
|
If no directory is provided the current directory will be used.
|
|
|
|
|
|
|
|
Options
|
2017-01-15 01:31:09 +00:00
|
|
|
--help, -h Displays this message
|
2016-12-18 20:28:34 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
2016-10-16 23:55:30 +00:00
|
|
|
const dir = resolve(argv._[0] || '.')
|
|
|
|
|
2016-10-30 06:03:05 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-11-03 10:07:15 +00:00
|
|
|
exists(dir)
|
2016-10-16 23:55:30 +00:00
|
|
|
.then(async present => {
|
|
|
|
if (!present) {
|
2016-11-03 10:07:15 +00:00
|
|
|
await mkdirp(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await exists(join(dir, 'package.json'))) {
|
2016-10-28 11:05:11 +00:00
|
|
|
await writeFile(join(dir, 'package.json'), basePackage.replace(/my-app/g, basename(dir)))
|
2016-10-16 23:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-10-25 15:03:24 +00:00
|
|
|
process.exit(1)
|
2016-10-16 23:55:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const basePackage = `{
|
|
|
|
"name": "my-app",
|
2016-10-28 11:05:30 +00:00
|
|
|
"description": "",
|
2016-10-16 23:55:30 +00:00
|
|
|
"dependencies": {
|
2017-03-28 19:20:18 +00:00
|
|
|
"next": "latest",
|
|
|
|
"react": "^15.4.2",
|
|
|
|
"react-dom": "^15.4.2"
|
2016-10-16 23:55:30 +00:00
|
|
|
},
|
|
|
|
"scripts": {
|
|
|
|
"dev": "next",
|
|
|
|
"build": "next build",
|
|
|
|
"start": "next start"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2016-10-31 18:39:55 +00:00
|
|
|
const basePage = `
|
2016-12-11 20:01:39 +00:00
|
|
|
import Head from 'next/head'
|
|
|
|
|
|
|
|
export default () => (
|
|
|
|
<div>
|
|
|
|
<Head>
|
2016-12-23 11:08:49 +00:00
|
|
|
<meta name='viewport' content='width=device-width, initial-scale=1'/>
|
2016-12-11 20:01:39 +00:00
|
|
|
</Head>
|
|
|
|
|
2016-12-23 11:08:49 +00:00
|
|
|
<img width='112' src='https://cloud.githubusercontent.com/assets/13041/19686250/971bf7f8-9ac0-11e6-975c-188defd82df1.png' alt='next.js' />
|
2017-03-28 19:20:18 +00:00
|
|
|
|
|
|
|
<style jsx global>{\`
|
|
|
|
html, body {
|
|
|
|
height: 100%
|
|
|
|
}
|
|
|
|
|
|
|
|
body {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
\`}</style>
|
2016-12-11 20:01:39 +00:00
|
|
|
</div>
|
|
|
|
)
|
2016-10-16 23:55:30 +00:00
|
|
|
`
|