-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode-maker.js
executable file
·136 lines (111 loc) · 3.93 KB
/
mode-maker.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
var fs = require('fs')
var prompt = require('prompt');
var Git = require("nodegit");
var rimraf = require('rimraf');
var config, files;
const initialPrompt = [
{
name: 'name',
description: 'Name for mode, this must not have spaces. It will be used to name the mode folder\nDo not preface with robopaint-mode',
required: true
}
];
var newModeInfo = {};
var modeFolderName = '';
prompt.start();
prompt.addProperties(newModeInfo, initialPrompt, function (err) {
modeFolderName = `robopaint-mode-${newModeInfo.name}`;
console.log(`cloning repo to ${modeFolderName}`);
Git.Clone("https://github.com/docprofsky/robopaint-mode-template", modeFolderName).then(function(repository) {
console.log("Cloned it!");
config = require(`./${modeFolderName}/replace-values.js`);
files = Object.keys(config.replace);
openFiles(0, function () {
prepareModeFolder(config.files, modeFolderName, newModeInfo.name, 0, function () {
console.log("Done processing.");
});
});
});
})
function openFiles(i, callback) {
if (i === files.length) {
if(callback) callback();
return;
}
// The file we are working with
const file = files[i];
var fileData = fs.readFileSync(`${modeFolderName}/${file}`).toString();
doFileConfig(config.replace[file], fileData, 0, function (changedFileData) {
fs.writeFileSync(`${modeFolderName}/${file}`, changedFileData);
openFiles(i + 1, callback);
});
}
function doFileConfig(fileConfig, fileData, i, callback) {
const fileFields = Object.keys(fileConfig);
if (i === fileFields.length) {
if(callback) callback(fileData);
return;
}
// The name of the text being replaed
const field = fileFields[i];
// The query to match the text to replace
const query = fileConfig[field];
// Get info from user if not already asked for
if (newModeInfo.hasOwnProperty(field)) {
changeFileData(fileData, query, newModeInfo[field], function (fileData) {
doFileConfig(fileConfig, fileData, i + 1, callback);
});
} else {
prompt.get(field, function (err, result) {
newModeInfo[field] = result[field];
changeFileData(fileData, query, newModeInfo[field], function (fileData) {
doFileConfig(fileConfig, fileData, i + 1, callback);
});
});
}
}
function prepareModeFolder(modeFiles, modeDir, modeName, i, callback) {
const renameLength = modeFiles.rename.length;
if(i === renameLength + modeFiles.delete.length) {
if(callback) callback();
return;
}
// We are renaming the files
if(i < renameLength) {
const oldPath = `${modeDir}/${modeFiles.rename[i]}`;
const newPath = `${modeDir}/${modeFiles.rename[i].replace('template', modeName)}`;
fs.rename(oldPath, newPath, function (err) {
if (err) throw err;
prepareModeFolder(modeFiles, modeDir, modeName, i + 1, callback);
});
} else {
const deletePath = `${modeDir}/${modeFiles.delete[i - renameLength]}`;
rimraf(deletePath, function (err) {
if (err) throw err;
prepareModeFolder(modeFiles, modeDir, modeName, i + 1, callback);
});
}
}
function changeFileData(fileData, query, replacement, callback) {
// We are using RegEx to match the field
if (query.constructor === RegExp) {
fileData = fileData.replace(query, replacement);
} else {
// Parse the file as JSON and change the JSON, and save it as a string
var changedObject = changeObjectValue(JSON.parse(fileData), query, replacement);
fileData = JSON.stringify(changedObject, null, 2);
}
if (callback) callback(fileData);
}
// Use: changeObjectValue(testData, "info.name", "changedname thing");
function changeObjectValue(object, property, value) {
var propertyPath = property.split('.');
if (propertyPath.length > 1) {
object[propertyPath[0]] = changeObjectValue(object[propertyPath[0]], propertyPath.slice(1).join(''), value);
return object;
} else {
object[propertyPath[0]] = value;
return object;
}
}