forked from dcloudio/uni-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (57 loc) · 2.23 KB
/
index.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
const fs = require('fs-extra')
const path = require('path')
class CopyPlugin {
constructor(option) {
this.options = option
}
apply(compiler) {
const { from, to } = this.options
const fromFiles = fs.readdirSync(from)
const exists = fs.existsSync(to)
if (exists) {
fs.removeSync(to)
}
fromFiles.forEach(pathName => {
if(pathName === '.DS_Store') return
// let name = path.join(from, pathName, pathName + '.vue')
const subFileLists = fs.readdirSync(path.join(from, pathName))
subFileLists.forEach((item) => {
const basename = path.basename(item, '.vue')
fs.copySync(path.join(from, pathName, item), path.join(to, pathName, basename + '.nvue'))
})
})
compiler.hooks.watchRun.tapAsync('CopyPlugin', (watching, callback) => {
// changedFiles 格式为键值对,键为发生变化的文件路径。
// 获取发生变化的文件列表
const changedFiles = watching.watchFileSystem.watcher.mtimes;
Object.keys(changedFiles).forEach(filePath => {
/*
例子:
filePath = /Users/mehaotian/project/uni-cli/my-project/src/copy/index/index.vue
from = /Users/mehaotian/project/uni-cli/my-project/src/pages
to = /Users/mehaotian/project/uni-cli/my-project/src/copy
*/
// 获取被同步文件根目录 如; /Users/mehaotian/project/uni-cli/my-project/src
// const fromDirname = path.join(from,'..')
// 获取 from 相对路径 如 : /index/index.vue
const name = filePath.split(from).pop()
// 获取后缀 如 : .vue
const extname = path.extname(filePath)
// 获取文件不带后缀 index
const basename = path.basename(name, extname)
// 获取要copy 的路径
const toFilePath = path.join(to, name, '..', basename + '.nvue')
// console.log(path.join(filePath, '..'))
// console.log(from)
if (path.join(filePath, '..').indexOf(from) === -1) {
// console.log('不修改这个文件', filePath)
} else {
// console.log('同步文件到:', toFilePath)
fs.copySync(filePath, toFilePath)
}
})
callback();
});
}
}
module.exports = CopyPlugin;