-
Notifications
You must be signed in to change notification settings - Fork 0
/
outFile.js
105 lines (97 loc) · 2.51 KB
/
outFile.js
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
102
103
104
105
const fs = require('fs')
module.exports = { outFile, codeConfig }
// 配置文档
function codeConfig () {
return {
config: `
phoneMsgKey: {
saveTime: 7200
},
`,
common: { // 公用模块配置
redis: getCommonRedis() // redis 公用模块配置
}
}
}
async function outFile (destBaseDir = '.', srcBaseDir = `${__dirname}/node_modules/skybase-tools/`) {
const obj = {
router: { // 生成 router目录
skyapi: { // 生成 router/skyapi 目录
'id.js': './router/skyapi/id.js', // 生成 router/skyapi/id.js 从 ./router/skyapi/id.js 拷贝 相对路径
'phoneMsg.js': './router/skyapi/phoneMsg.js'
}
},
model: {
api: {
skyapi: {
'id.js': './model/api/skyapi/id.js',
'phoneMsg.js': './model/api/skyapi/phoneMsg.js'
}
}
},
tools: {
'phoneCode.js': './tools/phoneCode.js'
}
}
try {
const isExist = fs.existsSync(srcBaseDir) // 判断目录是否存在
if (!isExist) {
console.error(srcBaseDir, '目录不存在!')
return
}
await outPutFile(destBaseDir, '', obj, srcBaseDir)
} catch (e) {
console.error(e.stack)
}
}
function getCommonRedis () {
return {
// config/config.default.js 中添加
config: `
redis: {
host: 'localhost',
port: 6379,
auth: '',
db: 1
},
`,
// index.js 中添加依赖文件
index: {
require: `
const createIoRedis = require('skybase/sky-module/create_ioredis')
`,
// index.js 系统启动前 beforeMound 中添加相应代码
beforeMount: `
// 连接redis
const redis = createIoRedis(config.redis)
await redis.waitForConnected()
global.redis = redis
// 连接mq
// global.rtsMQ = await createRbmq(config.stackRabbitMQ)
`
}
}
}
async function outPutFile (dir, key, obj, srcBaseDir) {
if (typeof obj === 'string') {
// console.log(`创建目录 ${dir}`)
await fs.mkdirSync(dir, { recursive: true })
// console.log(`原:${srcBaseDir}${obj} 目的:${dir}/${key}`)
if (!await checkFileExist(`${srcBaseDir}${obj}`)) {
console.error(`原文件不存在\t${srcBaseDir}${obj}`)
return
}
await fs.copyFileSync(`${srcBaseDir}${obj}`, `${dir}/${key}`)
return
}
for (const k in obj) {
outPutFile(key === '' ? dir : `${dir}/${key}`, k, obj[k], srcBaseDir)
}
}
async function checkFileExist (filePath) {
try {
await fs.accessSync(filePath, fs.constants.R_OK)
return true
} catch (e) { }
return false
}