1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/bin/next-init
Tim Oxley 5595232f58 next init: Exit non-zero when refusing to run in 'pages' dir. (#156)
Should only exit `0` if the command was successful. The command refusing to execute should be considered a failure.

Also, this condition can be detected synchronously, no need to wait on the async existence check.
2016-10-30 15:03:05 +09:00

57 lines
1.2 KiB
JavaScript
Executable file

#!/usr/bin/env node
import { resolve, join, basename } from 'path'
import parseArgs from 'minimist'
import { exists, writeFile, mkdir } from 'mz/fs'
const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help'
},
boolean: ['h']
})
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(join(dir, 'package.json'))
.then(async present => {
if (!present) {
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'
export default () => <p>Hello, world</p>
`