forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext-init
executable file
·101 lines (82 loc) · 2 KB
/
next-init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/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 <dir>
If no directory is provided the current directory will be used.
Options
--help, -h 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 Head from 'next/head'
export default () => (
<div>
<Head>
<meta name='viewport' content='width=device-width, initial-scale=1'/>
<style>{bodyStyles}</style>
</Head>
<img width='112' src='https://cloud.githubusercontent.com/assets/13041/19686250/971bf7f8-9ac0-11e6-975c-188defd82df1.png' alt='next.js' />
</div>
)
const bodyStyles = \`
html, body {
height: 100%
}
body {
display: flex;
align-items: center;
justify-content: center;
}
\`
`