Compare commits
5 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
5a9040c735 | |
|
|
321723d2cd | |
|
|
d4fe48f4c9 | |
|
|
48de9e590b | |
|
|
6b8a852105 |
23
index.js
23
index.js
|
|
@ -19,7 +19,9 @@ import {
|
|||
import { writeMainJs, writeAppJs, writeModelJs } from './lib/demo-js.js'
|
||||
|
||||
const CURRENT_DIR = process.cwd()
|
||||
const root = dirname(import.meta.url.slice(process.platform === 'win32' ? 10 : 7))
|
||||
const root = dirname(
|
||||
import.meta.url.slice(process.platform === 'win32' ? 10 : 7)
|
||||
)
|
||||
const { version } = JSON.parse(fs.cat(join(root, './package.json')))
|
||||
|
||||
const DEFAULT_NAME = 'gm5-app'
|
||||
|
|
@ -41,7 +43,7 @@ function sleep(num = 1) {
|
|||
|
||||
function printHelp() {
|
||||
console.log('Usage: create-five {command} [arguments]')
|
||||
console.log(' ', 'create-five init', '一个快速创建Five.js项目的小工具')
|
||||
console.log(' ', 'create-five init', '一个快速创建`@gm5`项目的小工具')
|
||||
console.log(' ', 'create-five -h[--help]', '打印帮助信息')
|
||||
console.log()
|
||||
process.exit()
|
||||
|
|
@ -72,7 +74,8 @@ function printHelp() {
|
|||
{
|
||||
name: 'shouldOverwrite',
|
||||
type: _ => (isEmpty(targetDir) ? null : 'toggle'),
|
||||
message: _ => `目录 ${cyan(targetDir)} 非空, 是否${red('删除')}目录下所有的文件?`,
|
||||
message: _ =>
|
||||
`目录 ${cyan(targetDir)} 非空, 是否${red('删除')}目录下所有的文件?`,
|
||||
initial: false,
|
||||
active: '是',
|
||||
inactive: '否'
|
||||
|
|
@ -82,7 +85,7 @@ function printHelp() {
|
|||
type: shouldOverwrite => {
|
||||
if (shouldOverwrite === false) {
|
||||
console.log(red('✖') + ' 操作取消~~')
|
||||
process.exit(1)
|
||||
process.exit()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
|
@ -91,6 +94,11 @@ function printHelp() {
|
|||
|
||||
console.log()
|
||||
|
||||
if (res.projectName === undefined) {
|
||||
console.log('已取消操作~~')
|
||||
process.exit()
|
||||
}
|
||||
|
||||
if (res.projectName === '.') {
|
||||
res.projectName = DEFAULT_NAME
|
||||
}
|
||||
|
|
@ -109,6 +117,7 @@ function printHelp() {
|
|||
}
|
||||
|
||||
console.log(cyan('\n初始化项目...'))
|
||||
console.log('[c---------]', '10%')
|
||||
|
||||
fs.mkdir(join(targetDir, 'apps'))
|
||||
fs.mkdir(join(targetDir, 'models'))
|
||||
|
|
@ -117,7 +126,7 @@ function printHelp() {
|
|||
fs.mkdir(join(targetDir, 'public'))
|
||||
fs.mkdir(join(targetDir, 'views'))
|
||||
|
||||
console.log('[c---------]', '10%')
|
||||
console.log('[ooc-------]', '30%')
|
||||
|
||||
writePackageJson(join(targetDir, 'package.json'), res.projectName)
|
||||
writeGitIgnore(join(targetDir, '.gitignore'))
|
||||
|
|
@ -132,8 +141,6 @@ function printHelp() {
|
|||
cluster: false
|
||||
})
|
||||
|
||||
console.log('[ooc-------]', '30%')
|
||||
|
||||
console.log('[oooooc----]', '60%')
|
||||
|
||||
writeMainJs(join(targetDir, 'app.js'))
|
||||
|
|
@ -142,9 +149,11 @@ function printHelp() {
|
|||
|
||||
console.log('[oooooooooo]', '100%')
|
||||
console.log(cyan('初始化完成, 可依次执行以下命令启动项目: '))
|
||||
console.log(blue('cd ' + res.projectName))
|
||||
console.log(blue('npm i'))
|
||||
console.log(blue('pm2 start app.dev.yaml'), gray('# 开发模式'))
|
||||
console.log(blue('pm2 start app.yaml'), gray('# 生产模式'))
|
||||
console.log('访问地址: ', cyan('http://127.0.0.1:3000'))
|
||||
|
||||
break
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ export function writePackageJson(file, name) {
|
|||
type: 'module',
|
||||
main: 'app.js',
|
||||
dependencies: {
|
||||
'@gm5/core': '^1.1.4'
|
||||
'@gm5/core': '^2.0.0'
|
||||
}
|
||||
},
|
||||
null,
|
||||
|
|
@ -27,9 +27,6 @@ export function writePackageJson(file, name) {
|
|||
export function writeGitIgnore(file) {
|
||||
fs.echo(
|
||||
`
|
||||
*.sublime-project
|
||||
*.sublime-workspace
|
||||
|
||||
node_modules
|
||||
package-lock.json
|
||||
data/logs
|
||||
|
|
@ -56,7 +53,7 @@ jsxBracketSameLine: true
|
|||
jsxSingleQuote: true
|
||||
semi: false
|
||||
singleQuote: true
|
||||
printWidth: 100
|
||||
printWidth: 80
|
||||
useTabs: false
|
||||
tabWidth: 2
|
||||
trailingComma: none
|
||||
|
|
@ -67,10 +64,13 @@ arrowParens: avoid
|
|||
)
|
||||
}
|
||||
|
||||
export function writePm2File(file, { env = 'production', name = 'gm5-app', cluster = true }) {
|
||||
export function writePm2File(
|
||||
file,
|
||||
{ env = 'production', name = 'gm5-app', cluster = true }
|
||||
) {
|
||||
fs.echo(
|
||||
`
|
||||
script: node --experimental-json-modules ./app.js
|
||||
script: node ./app.js
|
||||
cwd: ./
|
||||
watch: true
|
||||
name: ${name}
|
||||
|
|
|
|||
|
|
@ -9,22 +9,23 @@ import fs from 'iofs'
|
|||
export function writeMainJs(file) {
|
||||
fs.echo(
|
||||
`
|
||||
import Five from '@gm5/core'
|
||||
import { createApp, mount } from '@gm5/core'
|
||||
|
||||
const app = createApp()
|
||||
|
||||
// // 也可以在创建应用时, 就传递一些配置进去, 等价于下面的 app.set({...})
|
||||
// const app = createApp({...})
|
||||
|
||||
const app = new Five()
|
||||
|
||||
// app.set({
|
||||
// // 可开启session支持
|
||||
// session: { enabled: true, type: 'redis' }
|
||||
//
|
||||
// // 可开启模板引擎的支持
|
||||
// views: { enabled: true, dir: './views' }
|
||||
// // 可开启跨域支持
|
||||
// cors: { enabled: true }
|
||||
// })
|
||||
|
||||
|
||||
|
||||
// 预加载应用, 这一步是必须的, 且需要在listen方法前调用
|
||||
app.preload('./apps/')
|
||||
// 预加载应用, 这一步是必须的, 且需要在run()方法前调用
|
||||
app.use(mount('./apps/'))
|
||||
|
||||
// 中间件示例
|
||||
// 这里的回调, 如果不用箭头函数的话, this指向app
|
||||
|
|
@ -39,14 +40,17 @@ app.preload('./apps/')
|
|||
|
||||
// 安装拓展包, 可以应用中通过 this.context.$$mysql 调用到,
|
||||
// 在中间件, 或后面的其他拓展包中, 可以直接 this.$$mysql 调用
|
||||
// app.install({
|
||||
// app.use({
|
||||
// name: 'mysql',
|
||||
// install: function() {
|
||||
// return new Mysqli(conf)
|
||||
// }
|
||||
// })
|
||||
|
||||
app.listen(3000)
|
||||
app.listen(3000, () => {
|
||||
// 这里可以做一些启动后的操作
|
||||
// 初始化websocket服务
|
||||
})
|
||||
|
||||
`,
|
||||
file
|
||||
|
|
@ -83,9 +87,9 @@ import Controller from '@gm5/controller'
|
|||
|
||||
// import Model from '../models/index.js'
|
||||
|
||||
// 所有的应用, 都要继承Controller
|
||||
// 以获取跟框架交互的能力
|
||||
export default class Index extends Controller {
|
||||
// 也可以不继承 Controller。 如用到session/jwt/views模块, 可继承Controller, 以获得更简单的调用方式(也可以不继承)。
|
||||
//
|
||||
export default class extends Controller {
|
||||
|
||||
// 这个main方法是可选的, 如果有定义, 会自动先调用
|
||||
// 可以在这里做任何需要预处理的事, 支持async/await
|
||||
|
|
@ -95,8 +99,8 @@ export default class Index extends Controller {
|
|||
// this.model = new Model(this.context.$$mysql)
|
||||
}
|
||||
|
||||
async indexAction(){
|
||||
this.response.end('It works!')
|
||||
async indexAction() {
|
||||
this.response.body = 'It works!'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
"name": "create-five",
|
||||
"description": "一个快速创建Five.js项目的小工具",
|
||||
"type": "module",
|
||||
"version": "1.0.1",
|
||||
"version": "1.2.0",
|
||||
"author": "yutent <yutent.io@gmail.com>",
|
||||
"bin": {
|
||||
"create-five": "index.js"
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/bytedo/gmf.cli.git"
|
||||
"url": "https://git.wkit.fun/gm5/cli.git"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue