-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformToModule.js
More file actions
91 lines (91 loc) · 3.06 KB
/
transformToModule.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
const fs = require('fs');
const path = require('path')
const gIndexJs = (rootFolderName) => {
const rootPath = path.resolve(rootFolderName)
const dirAry = fs.readdirSync(rootPath);
const indexJsFilePath = path.resolve(rootFolderName + '/index.js')
const exportsAry = dirAry.map(fileName => {
if (fileName.indexOf('.js') > -1) {
const fileNameWithExt = fileName.slice(0, -3)
return fileNameWithExt
} else {
return ''
}
}).filter(n => n !== '')
let indexJsStr = ``
exportsAry.map((moduleName) => {
const str = `import ${moduleName} from './${moduleName}'\n`
indexJsStr += str
})
const exportsStr = `export {${exportsAry.join(',')}}`
indexJsStr = indexJsStr + exportsStr
console.log(indexJsStr);
fs.writeFileSync(indexJsFilePath, indexJsStr, {
encoding: 'utf-8'
})
}
const extractGLSL = (rootFolderName) => {
const rootPath = path.resolve(rootFolderName)
const dirAry = fs.readdirSync(rootPath);
dirAry.map((fileName) => {
const relativePath = path.join(rootFolderName, fileName)
const absolutePath = path.resolve(relativePath)
const stat = fs.statSync(absolutePath)
if (stat.isDirectory()) {
//
return extractGLSL(absolutePath)
} else {
if (fileName.indexOf('.js') > -1) {
const content = fs.readFileSync(absolutePath, {
encoding: 'utf8'
})
const fileNameWithExt = fileName.slice(0, -3)
let newContent = content
if (content.indexOf('VSHADER_SOURCE') > -1 && content.indexOf('import VSHADER_SOURCE') === -1) {
const str = `import VSHADER_SOURCE from './${fileNameWithExt}.vert.glsl'`
newContent = str + '\n' + newContent
}
if (content.indexOf('FSHADER_SOURCE') > -1 && content.indexOf('import FSHADER_SOURCE') === -1) {
const str = `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'
})
}
}
})
}
const fixImportFrag = (rootFolderName) => {
const rootPath = path.resolve(rootFolderName)
const dirAry = fs.readdirSync(rootPath);
dirAry.map((fileName) => {
const relativePath = path.join(rootFolderName, fileName)
const absolutePath = path.resolve(relativePath)
const stat = fs.statSync(absolutePath)
if (stat.isDirectory()) {
//
return extractGLSL(absolutePath)
} else {
if (fileName.indexOf('.js') > -1) {
const content = fs.readFileSync(absolutePath, {
encoding: 'utf8'
})
const reg = /(import\sFSHADER_SOURCE[\s\w'./]*?\.)(vert)(\.glsl)/g
const newContent = 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')