Skip to content

Commit

Permalink
fix(alita): wacth change file bug
Browse files Browse the repository at this point in the history
  • Loading branch information
YvetteLau committed Jul 6, 2019
1 parent aaa932a commit 28b9c12
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
21 changes: 18 additions & 3 deletions src/filewatch/changeFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import addFile from './addFile';
import unlinkFile from './unlinkFile';
import getFiles from "./getFiles";
import fse from "fs-extra";

export default async function changeFile(path) {
await unlinkFile(path);
await addFile(path);
let oldFiles = [];
if(path.endsWith('.js')) {
oldFiles = await getFiles(path);
}
let newFiles = await addFile(path);

//删除冗余文件
getDeleteFiles(oldFiles, newFiles).map(async (file) => {
fse.remove(file).catch((err) => console.log(err));
});
}

function getDeleteFiles(oldFiles, newFiles) {
return oldFiles.filter((v) => {
return newFiles.indexOf(v) > -1 ? false : true;
});
}
24 changes: 24 additions & 0 deletions src/filewatch/getFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fse from "fs-extra";
const path = require('path');

export default async function getFiles(targetPath, suffix) {
const targetdir = path.dirname(targetPath);
const noSuffix = targetPath.substring(0, targetPath.lastIndexOf(suffix));
const suffixs = ['.json', '.wxss', '.js', '.comp.js', '.wxml'];
const allFiles = suffixs.map((item) => {
return noSuffix + item;
});
allFiles.push(noSuffix + 'Template.wxml');
await fse.readdir(targetdir).then((files) => {
files.forEach((fileName) => {
if (fileName.indexOf(noSuffix.substring(noSuffix.lastIndexOf('/') + 1) + 'ICNP') === 0) {
allFiles.push(path.resolve(targetdir, fileName));
}

});
});

return allFiles.map((file) => {
return fse.existsSync(file);
});
}
2 changes: 1 addition & 1 deletion src/filewatch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default (ignored) => {
{
persistent: watchMode,
ignored,
interval: 1000
interval: 200
})

watcher
Expand Down
23 changes: 3 additions & 20 deletions src/filewatch/unlinkFile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fse from 'fs-extra';
import addFile from './addFile';
import { isStaticRes } from "../util/util"
import { isStaticRes } from "../util/util";
import getFiles from './getFiles';

const path = require('path');

/**
Expand Down Expand Up @@ -172,22 +174,3 @@ async function removeFiles(files) {
});
});
}

async function getFiles(targetPath, suffix) {
const targetdir = path.dirname(targetPath);
const noSuffix = targetPath.substring(0, targetPath.lastIndexOf(suffix));
const suffixs = ['.json', '.wxss', '.js', '.comp.js', '.wxml'];
const allFiles = suffixs.map((item) => {
return noSuffix + item;
});
allFiles.push(noSuffix + 'Template.wxml');
await fse.readdir(targetdir).then((files) => {
files.forEach((fileName) => {
if (fileName.indexOf(noSuffix.substring(noSuffix.lastIndexOf('/') + 1) + 'ICNP') === 0) {
allFiles.push(path.resolve(targetdir, fileName));
}

});
});
return allFiles;
}

0 comments on commit 28b9c12

Please sign in to comment.