-
Notifications
You must be signed in to change notification settings - Fork 1
/
rename.js
64 lines (52 loc) · 1.64 KB
/
rename.js
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
const fs = require('fs');
const path = require('path');
const doRename = (parent, _v, log) => {
const cwd = path.join(__dirname, parent, _v);
const cwdContents = fs.readdirSync(cwd);
let key;
cwdContents.every((f, k) => {
if(f.endsWith('moc')){
key = k;
return false;
}
return true;
});
if(!key){
log(`Skipping ${_v}...`);
return false;
}
try{
if(cwdContents[key + 1] === undefined){
log(`Skipping ${_v}...`);
return false;
}
const fileMap = {};
const modelPath = path.join(cwd, cwdContents[key + 1]);
const model = JSON.parse(fs.readFileSync(modelPath), 'utf8');
fileMap[path.join(_v + '.model.json')] = cwdContents[key + 1];
fileMap['character.dat'] = cwdContents[key];
fs.renameSync(modelPath, path.join(cwd, _v + '.model.json'));
fs.renameSync(path.join(cwd, cwdContents[key]), path.join(cwd, 'character.dat'));
let dir = cwdContents.slice();
dir[key] = undefined;
dir[key + 1] = undefined;
dir = dir.filter(v => v !== undefined);
const files = [];
if(model.textures) files.push(...model.textures);
if(model.motions) Object.keys(model.motions).map((v) => model.motions[v]).forEach((v) => {
files.push(...v.map((v) => v.file));
});
if(model.expressions) files.push(...model.expressions.map((v) => v.file));
files.sort().forEach((v, k) => {
fileMap[v] = dir[k];
fs.renameSync(path.join(cwd, dir[k]), path.join(cwd, v));
});
fs.writeFileSync(path.join(cwd, 'stix-rename-data.json'), JSON.stringify(fileMap));
return true;
}catch(e){
log(`Skipping ${_v}...`);
log(e);
return false;
}
};
module.exports = doRename;