- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformToModule.js
More file actions
Latest commit
91 lines (91 loc) · 3.06 KB
/
Copy pathtransformToModule.js
File metadata and controls
91 lines (91 loc) · 3.06 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
constfs=require('fs');
constpath=require('path')
constgIndexJs=(rootFolderName)=>{
constrootPath=path.resolve(rootFolderName)
constdirAry=fs.readdirSync(rootPath);
constindexJsFilePath=path.resolve(rootFolderName+'/index.js')
constexportsAry=dirAry.map(fileName=>{
if(fileName.indexOf('.js')>-1){
constfileNameWithExt=fileName.slice(0,-3)
returnfileNameWithExt
}else{
return''
}
}).filter(n=>n!=='')
letindexJsStr=``
exportsAry.map((moduleName)=>{
conststr=`import ${moduleName} from './${moduleName}'\n`
indexJsStr+=str
})
constexportsStr=`export {${exportsAry.join(',')}}`
indexJsStr=indexJsStr+exportsStr
console.log(indexJsStr);
fs.writeFileSync(indexJsFilePath,indexJsStr,{
encoding: 'utf-8'
})
}
constextractGLSL=(rootFolderName)=>{
constrootPath=path.resolve(rootFolderName)
constdirAry=fs.readdirSync(rootPath);
dirAry.map((fileName)=>{
constrelativePath=path.join(rootFolderName,fileName)
constabsolutePath=path.resolve(relativePath)
conststat=fs.statSync(absolutePath)
if(stat.isDirectory()){
//
returnextractGLSL(absolutePath)
}else{
if(fileName.indexOf('.js')>-1){
constcontent=fs.readFileSync(absolutePath,{
encoding: 'utf8'
})
constfileNameWithExt=fileName.slice(0,-3)
letnewContent=content
if(content.indexOf('VSHADER_SOURCE')>-1&&content.indexOf('import VSHADER_SOURCE')===-1){
conststr=`import VSHADER_SOURCE from './${fileNameWithExt}.vert.glsl'`
newContent=str+'\n'+newContent
}
if(content.indexOf('FSHADER_SOURCE')>-1&&content.indexOf('import FSHADER_SOURCE')===-1){
conststr=`import FSHADER_SOURCE from './${fileNameWithExt}.frag.glsl'`
newContent=str+'\n'+newContent
}
if(content.indexOf('export default')===-1){
newContent+='\nexport default main'
}
fs.writeFileSync(absolutePath,newContent,{
encoding: 'utf8'
})
}
}
})
}
constfixImportFrag=(rootFolderName)=>{
constrootPath=path.resolve(rootFolderName)
constdirAry=fs.readdirSync(rootPath);
dirAry.map((fileName)=>{
constrelativePath=path.join(rootFolderName,fileName)
constabsolutePath=path.resolve(relativePath)
conststat=fs.statSync(absolutePath)
if(stat.isDirectory()){
//
returnextractGLSL(absolutePath)
}else{
if(fileName.indexOf('.js')>-1){
constcontent=fs.readFileSync(absolutePath,{
encoding: 'utf8'
})
constreg=/(import\sFSHADER_SOURCE[\s\w'./]*?\.)(vert)(\.glsl)/g
constnewContent=content.replace(reg,(total,$0,$1,$2)=>{
return$0+'frag'+$2
})
// console.log(1);
fs.writeFileSync(absolutePath,newContent,{
encoding: 'utf8'
})
}
}
})
}
// extractGLSL('./ch10')
// gIndexJs('./ch10')
fixImportFrag('./ch05')