Skip to content

Commit

Permalink
fix: 修复toFile 不存在时不自动创建的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
HOMEDO\zhangjiayun committed Jul 11, 2022
1 parent 02be0f7 commit a3b3256
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
51 changes: 27 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { normalizePath } from 'vite';
let isServer = false;
let loadFiles: string[] = [];
let tmpRemoves: string[] = [];
const toFileContents:Map<string,string> = new Map();
const toFileContents: Map<string, string> = new Map();
//path转驼峰变量名并剔除最后的index/Index
export const getName = function (fileName: string, nameTemplate = '{{name}}'): string {
const index = fileName.lastIndexOf('.');
Expand Down Expand Up @@ -57,8 +57,8 @@ const getCode = function (dir: string,
codeTemplates: codeTemplate[] = []): codeTemplate[] {
const filePath = getFileImportPath(dir, fileName);
let relativePath = normalizePath(path.relative(path.dirname(path.resolve(toFile)), filePath));
if(!relativePath.startsWith('../')){
relativePath = './'+relativePath;
if (!relativePath.startsWith('../')) {
relativePath = './' + relativePath;
}
if (typeof name == 'function') {
fileName = name(getName(fileName));
Expand Down Expand Up @@ -102,32 +102,25 @@ const loadPath = async function (dir: string,
}


export function readFileSync(...args:Parameters<typeof fs.readFileSync>):ReturnType< typeof fs.readFileSync> | undefined{
try{
export function readFileSync(...args: Parameters<typeof fs.readFileSync>): ReturnType<typeof fs.readFileSync> | undefined {
try {
return fs.readFileSync(...args);
}catch{
} catch {
return undefined;
}
}

export default function loadPathsPlugin(dirOptions: dirOptions) {
return {
name: 'load-path-ts',
async buildStart() {
let proArr: Promise<unknown>[] = [];
dirOptions.forEach(item => {
proArr.push(loadPath(item.dir, item.toFile, item.pattern, item.options || {}, item.name, item.template, item.codeTemplates));
})
await Promise.allSettled(proArr);
},
configureServer() {//服务器启动时被调用
dirOptions.forEach(item => {
isServer = true;
fs.watch(item.dir, { recursive: true },
function (eventType: fs.WatchEventType, fileName: string) {
fileName = normalizePath(fileName);
if (eventType === 'rename') {
let str = <string>readFileSync(item.toFile, 'utf8') || '' ;
let str = <string>readFileSync(item.toFile, 'utf8') || '';
let filePath = path.resolve(item.dir, fileName);
if (fs.existsSync(filePath)) {//存在
let stat = fs.lstatSync(filePath);
Expand All @@ -150,7 +143,7 @@ export default function loadPathsPlugin(dirOptions: dirOptions) {
loadFiles.push(prefix + fileName);
})
if (changeFiles.length) {
toFileContents.set(item.toFile,str);
toFileContents.set(item.toFile, str);
fs.writeFileSync(item.toFile, str);
console.log(item.toFile + ' add code');
}
Expand All @@ -165,7 +158,7 @@ export default function loadPathsPlugin(dirOptions: dirOptions) {
loadFiles.slice(loadFiles.indexOf(fileName), 1);
});
if (changeFiles.length) {
toFileContents.set(item.toFile,str);
toFileContents.set(item.toFile, str);
fs.writeFileSync(item.toFile, str);
if (changeFiles[0] !== fileName) {
tmpRemoves = changeFiles.map(name => name.slice(fileName.length + 1));
Expand All @@ -176,15 +169,25 @@ export default function loadPathsPlugin(dirOptions: dirOptions) {
}
}
}
});
fs.watch(item.toFile,{},function(eventType: fs.WatchEventType, fileName: string){
const content = toFileContents.get(item.toFile);
if(content !== undefined && content !== readFileSync(item.toFile, 'utf8')){
fs.writeFileSync(item.toFile,content);
}
})
});
})

},
async buildStart() {
let proArr: Promise<unknown>[] = [];
dirOptions.forEach(item => {
proArr.push(loadPath(item.dir, item.toFile, item.pattern, item.options || {}, item.name, item.template, item.codeTemplates));
})
await Promise.allSettled(proArr);
if (isServer) {
dirOptions.forEach(item => {
fs.watch(item.toFile, {}, function (eventType: fs.WatchEventType, fileName: string) {
const content = toFileContents.get(item.toFile);
if (content !== undefined && content !== readFileSync(item.toFile, 'utf8')) {
fs.writeFileSync(item.toFile, content);
}
});
});
}
}
}
}
4 changes: 2 additions & 2 deletions test/types/components.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import a from "../components/a.vue"
import b from "../components/b.vue"
import adavs from "../components/adavs/Index.vue"
import a from "../components/a.vue"
//import code
declare module "@vue/runtime-core" {
interface GlobalComponents {
a:typeof a
b:typeof b
adavs:typeof adavs
a:typeof a
//key code
}
}
Expand Down

0 comments on commit a3b3256

Please sign in to comment.