Skip to content

Commit 28b9c12

Browse files
committedJul 6, 2019
fix(alita): wacth change file bug
1 parent aaa932a commit 28b9c12

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed
 

‎src/filewatch/changeFile.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import addFile from './addFile';
2-
import unlinkFile from './unlinkFile';
2+
import getFiles from "./getFiles";
3+
import fse from "fs-extra";
34

45
export default async function changeFile(path) {
5-
await unlinkFile(path);
6-
await addFile(path);
6+
let oldFiles = [];
7+
if(path.endsWith('.js')) {
8+
oldFiles = await getFiles(path);
9+
}
10+
let newFiles = await addFile(path);
11+
12+
//删除冗余文件
13+
getDeleteFiles(oldFiles, newFiles).map(async (file) => {
14+
fse.remove(file).catch((err) => console.log(err));
15+
});
16+
}
17+
18+
function getDeleteFiles(oldFiles, newFiles) {
19+
return oldFiles.filter((v) => {
20+
return newFiles.indexOf(v) > -1 ? false : true;
21+
});
722
}

‎src/filewatch/getFiles.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import fse from "fs-extra";
2+
const path = require('path');
3+
4+
export default async function getFiles(targetPath, suffix) {
5+
const targetdir = path.dirname(targetPath);
6+
const noSuffix = targetPath.substring(0, targetPath.lastIndexOf(suffix));
7+
const suffixs = ['.json', '.wxss', '.js', '.comp.js', '.wxml'];
8+
const allFiles = suffixs.map((item) => {
9+
return noSuffix + item;
10+
});
11+
allFiles.push(noSuffix + 'Template.wxml');
12+
await fse.readdir(targetdir).then((files) => {
13+
files.forEach((fileName) => {
14+
if (fileName.indexOf(noSuffix.substring(noSuffix.lastIndexOf('/') + 1) + 'ICNP') === 0) {
15+
allFiles.push(path.resolve(targetdir, fileName));
16+
}
17+
18+
});
19+
});
20+
21+
return allFiles.map((file) => {
22+
return fse.existsSync(file);
23+
});
24+
}

‎src/filewatch/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default (ignored) => {
2626
{
2727
persistent: watchMode,
2828
ignored,
29-
interval: 1000
29+
interval: 200
3030
})
3131

3232
watcher

‎src/filewatch/unlinkFile.js

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fse from 'fs-extra';
22
import addFile from './addFile';
3-
import { isStaticRes } from "../util/util"
3+
import { isStaticRes } from "../util/util";
4+
import getFiles from './getFiles';
5+
46
const path = require('path');
57

68
/**
@@ -172,22 +174,3 @@ async function removeFiles(files) {
172174
});
173175
});
174176
}
175-
176-
async function getFiles(targetPath, suffix) {
177-
const targetdir = path.dirname(targetPath);
178-
const noSuffix = targetPath.substring(0, targetPath.lastIndexOf(suffix));
179-
const suffixs = ['.json', '.wxss', '.js', '.comp.js', '.wxml'];
180-
const allFiles = suffixs.map((item) => {
181-
return noSuffix + item;
182-
});
183-
allFiles.push(noSuffix + 'Template.wxml');
184-
await fse.readdir(targetdir).then((files) => {
185-
files.forEach((fileName) => {
186-
if (fileName.indexOf(noSuffix.substring(noSuffix.lastIndexOf('/') + 1) + 'ICNP') === 0) {
187-
allFiles.push(path.resolve(targetdir, fileName));
188-
}
189-
190-
});
191-
});
192-
return allFiles;
193-
}

0 commit comments

Comments
 (0)
Please sign in to comment.