Skip to content

Commit 59527b1

Browse files
committed
feat(alita): 添加 --comp参数, 添加转化完成的log
1 parent 5d82044 commit 59527b1

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

src/filewatch/index.js

+33-6
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,47 @@
77
*/
88

99
import addFile from './addFile'
10+
1011
const chokidar = require('chokidar')
12+
const events = require('events');
13+
// 创建 eventEmitter 对象
14+
const eventEmitter = new events.EventEmitter();
15+
16+
const DONE_EVENT = 'DONE_EVENT'
1117

1218
export default (ignored) => {
1319
const {INPUT_DIR, watchMode} = global.execArgs
14-
console.log('watchMode:', watchMode)
1520

21+
const fileSet = new Set([])
1622
const watcher = chokidar.watch(INPUT_DIR,
1723
{
18-
persistent: false,
24+
persistent: watchMode,
1925
ignored,
2026
})
2127

22-
watcher.on('add', async (path) => {
23-
const allFilepaths = await addFile(path)
24-
console.log(path, ' ok! 生成:', allFilepaths)
25-
})
28+
watcher
29+
.on('add', async (path) => {
30+
fileSet.add(path)
31+
const allFilepaths = await addFile(path)
32+
fileSet.delete(path)
33+
34+
if (fileSet.size === 0) {
35+
eventEmitter.emit(DONE_EVENT)
36+
}
37+
})
38+
.on('change', async path => {
39+
console.log(path, ' changed!')
40+
})
41+
.on('unlink', async path => {
42+
console.log(path, ' delete!')
43+
})
44+
.on('ready', () => {
45+
eventEmitter.on(DONE_EVENT, () => {
46+
if (watchMode) {
47+
console.log(`转化完成!监听文件修改...`.info)
48+
} else {
49+
console.log('转化完成!'.info)
50+
}
51+
})
52+
})
2653
}

src/index.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import fse from 'fs-extra'
11-
import {geneWXFileStruc, exitStruc} from './struc/index'
11+
import {geneWXFileStruc} from './struc/index'
1212
import {getDependenciesMap, getRNCompList, emptyDir} from './util/util'
1313
import packagz from '../package.json'
1414
import filewatch from './filewatch/index'
@@ -37,7 +37,8 @@ const options = getopts(process.argv, {
3737
w: 'watch',
3838
v: 'version',
3939
config: 'config',
40-
beta: 'beta'
40+
beta: 'beta',
41+
comp: 'component'
4142
},
4243
})
4344

@@ -89,9 +90,6 @@ emptyDir(OUT_DIR, new Set([
8990
]))
9091
console.log('输出目录清理完成'.info)
9192

92-
const watchMode = options.watch
93-
94-
9593
const CONFIGPATH = path.resolve(INPUT_DIR, options.config || 'alita.config.js')
9694
let configObj = DEFAULTCONFIG
9795

@@ -121,7 +119,8 @@ const {extCompPathMaps, extChildComp, allExtComp, extReactComp, jsxPropsMap} = g
121119
global.execArgs = {
122120
INPUT_DIR,
123121
OUT_DIR,
124-
watchMode,
122+
watchMode: !!options.watch,
123+
tranComp: !!options.component,
125124
configObj,
126125
extCompPathMaps,
127126
extChildComp,
@@ -249,10 +248,6 @@ async function main() {
249248
// 生成微信package.json文件
250249
geneWXPackgejson(configObj)
251250

252-
process.on('beforeExit', (code) => {
253-
exitStruc()
254-
})
255-
256251
const ignored = /node_modules|\.git|\.expo|android|ios|\.idea|__tests__|.ios\.js|.android\.js|\.web\.js|\.sh|\.iml|\.vs_code|alita\.config\.js|babel\.config\.js|metro\.config\.js|\.gitignore|app\.json|package\.json|package-lock\.json|\.eslintrc\.js|\.eslintrc\.json|\.eslintrc|yarn\.lock|\.test\.js|.watchmanconfig/
257252
filewatch(ignored)
258253
}

src/struc/index.js

+13-25
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
8+
99
import fse from 'fs-extra'
1010
import {getFileInfo, parseCode} from '../util/uast'
1111
import {isStaticRes, base64Encode} from '../util/util'
@@ -28,6 +28,9 @@ let entryFilePath = null
2828
* @returns {Promise<void>}
2929
*/
3030
export default async function (srcpath, targetpath) {
31+
//如果tranComp为true,表明只是单纯的转化组件,而不是整个项目
32+
const tranComp = global.execArgs.tranComp
33+
3134
const fsStat = await fse.stat(srcpath)
3235
if (fsStat.isDirectory()) {
3336
return []
@@ -41,10 +44,10 @@ export default async function (srcpath, targetpath) {
4144
if (isRNEntry) return []
4245

4346
if (isEntry && isRF) { // 入口文件 保证入口文件一定最先处理
44-
const entryResult = handleEntry(ast, targetpath)
47+
const entryResult = handleEntry(ast, targetpath)
4548
entryFilePath = entryResult.filepath
4649
allCompSet = entryResult.allCompSet
47-
for(let i = 0; i<RFFileList.length; i++ ) {
50+
for (let i = 0; i < RFFileList.length; i++) {
4851
const {ast, targetpath, srcpath, isFuncComp, isStatelessComp, done} = RFFileList[i]
4952
try {
5053
const allFilepaths = handleRF(ast, targetpath, isFuncComp, entryFilePath, isPageComp(targetpath, allCompSet), isStatelessComp)
@@ -55,13 +58,18 @@ export default async function (srcpath, targetpath) {
5558
}
5659
return [targetpath]
5760
} else if (isRF) {
58-
if (entryFilePath) {
61+
if (tranComp) {
62+
try {
63+
return handleRF(ast, targetpath, isFuncComp, entryFilePath, false, isStatelessComp)
64+
} catch (e) {
65+
console.log(colors.error(`tran ${srcpath} error ! reason: `), e)
66+
}
67+
} else if (entryFilePath) {
5968
try {
6069
return handleRF(ast, targetpath, isFuncComp, entryFilePath, isPageComp(targetpath, allCompSet), isStatelessComp)
6170
} catch (e) {
6271
console.log(colors.error(`tran ${srcpath} error ! reason: `), e)
6372
}
64-
6573
} else {
6674
// 保证入口文件一定最先处理, 如果入口文件还未被处理,则把react文件先入队列
6775
return new Promise((resolve) => {
@@ -92,26 +100,6 @@ export async function geneWXFileStruc(targetpath) {
92100
await fse.copy(mptempDir, targetpath)
93101
}
94102

95-
/**
96-
* 程序退出之前的操作
97-
*/
98-
export async function exitStruc() {
99-
// 没有入口文件
100-
if (!entryFilePath) {
101-
for(let i = 0; i< RFFileList.length; i++ ) {
102-
const {ast, targetpath, srcpath, isFuncComp, isStatelessComp} = RFFileList[i]
103-
try {
104-
handleRF(ast, targetpath, isFuncComp, entryFilePath, false, isStatelessComp)
105-
} catch (e) {
106-
console.log(colors.error(`tran ${srcpath} error ! reason: `), e)
107-
}
108-
}
109-
// 以免再次触发 exitStruc
110-
entryFilePath = "DONE"
111-
}
112-
}
113-
114-
115103
function isPageComp(targetpath, allCompSet) {
116104
const originPath = targetpath
117105
.replace(global.execArgs.OUT_DIR + path.sep, '')

0 commit comments

Comments
 (0)