-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.js
290 lines (290 loc) · 11.1 KB
/
config.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @File : config.ts
* @Author : dtysky ([email protected])
* @Date : 2018-7-27 11:08:05
* @Description:
*/
const path = require("path");
const child_process_1 = require("child_process");
const inquirer = require("inquirer");
const through = require("through2");
const copy = require("recursive-copy");
const fs = require("fs");
exports.DEV = false;
exports.templatesRoot = path.resolve(__dirname, './templates/templates');
exports.containersRoot = path.resolve(__dirname, './templates/containers');
exports.projectRoot = path.resolve(__dirname, './templates/project');
exports.assetsRoot = path.resolve(__dirname, './templates/assets');
exports.unityRoot = path.resolve(__dirname, './templates/unity');
exports.currentRoot = process.cwd();
exports.TEMPLATES = [
{ name: 'Lite, a simple rendering project', value: 'lite' },
{ name: 'Tiny, a tiny project with one simple script', value: 'tiny' },
{ name: 'Simple, a simple project with starndard Sein.js gameplay framework', value: 'simple' }
];
exports.ENGINES = [
'none',
'react',
'wx-mini-game',
'wx-mini-program',
'my-tiny-game',
'my-tiny-program'
];
exports.SEIN_DEPENDENCIES = {
seinjs: '^1.5.0',
'cannon-dtysky': '^0.6.4',
'seinjs-camera-controls': '^0.8.2',
'seinjs-debug-tools': '^0.8.0',
'seinjs-dom-hud': '^0.8.0',
'seinjs-gpu-particle-system': '^0.8.3',
'seinjs-audio': '^0.9.0',
'seinjs-post-processing-system': '^0.8.0'
};
function showError(msg) {
console.error('\x1b[31m%s', `Error: ${msg}`);
process.exit(0);
}
exports.showError = showError;
function showInfo(msg) {
console.info('\x1b[32m%s\x1b[0m', msg);
}
exports.showInfo = showInfo;
function toSnakeCase(str) {
const upperChars = str.match(/([A-Z])/g);
if (!upperChars) {
return str;
}
for (var i = 0, n = upperChars.length; i < n; i += 1) {
str = str.replace(new RegExp(upperChars[i]), '-' + upperChars[i].toLowerCase());
}
if (str.slice(0, 1) === '-') {
str = str.slice(1);
}
return str;
}
exports.toSnakeCase = toSnakeCase;
function getTemplate(argv) {
return __awaiter(this, void 0, void 0, function* () {
let { template } = argv;
if (!template) {
template = (yield inquirer.prompt([
{
name: 'template',
type: 'list',
message: 'Type of template.',
default: exports.TEMPLATES[0],
choices: exports.TEMPLATES
}
])).template;
}
return path.resolve(exports.templatesRoot, template);
});
}
exports.getTemplate = getTemplate;
function getRoot(argv) {
return __awaiter(this, void 0, void 0, function* () {
let { root } = argv;
if (!root) {
root = (yield inquirer.prompt([
{
name: 'root',
type: 'input',
message: 'Project root folder.',
default: '.'
}
])).root;
}
return path.resolve(exports.currentRoot, root);
});
}
exports.getRoot = getRoot;
function getEngine(argv) {
return __awaiter(this, void 0, void 0, function* () {
let { engine } = argv;
if (!engine) {
engine = (yield inquirer.prompt([
{
name: 'engine',
type: 'list',
message: 'Container engine',
default: 'none',
choices: exports.ENGINES
}
])).engine;
}
return path.resolve(exports.containersRoot, engine);
});
}
exports.getEngine = getEngine;
function getAuthor() {
return __awaiter(this, void 0, void 0, function* () {
function gitConfig(field, callback) {
var command = 'git config --get user.' + field;
child_process_1.exec(command, function (err, stdout, stderr) {
if (err) {
callback(err);
return;
}
callback(null, stdout.toString().replace(/(\r\n|\n|\r)/gm, ""));
});
}
function getAll(callback) {
gitConfig("name", function (err, name) {
if (err) {
callback(err);
return;
}
gitConfig("email", function (err, email) {
if (err) {
callback(err);
return;
}
callback(null, { name, email });
});
});
}
return new Promise(resolve => {
getAll((err, result) => {
if (err) {
resolve({ name: '', email: '' });
return;
}
resolve(result);
});
});
});
}
exports.getAuthor = getAuthor;
;
function getProjectConfig(root, container, template) {
return __awaiter(this, void 0, void 0, function* () {
const author = yield getAuthor();
const meta = {
NAME: toSnakeCase(root.split(/[/\\]/g).pop()),
'AUTHOR.NAME': author.name,
'AUTHOR.EMAIL': author.email,
DATE: new Date().toDateString(),
DEV_DEPENDENCIES: {},
DEPENDENCIES: Object.assign({}, exports.SEIN_DEPENDENCIES),
SCRIPTS: {},
WEBPACK_FILE_PREFIX: ``,
WEBPACK_MAIN_ENTRY_PREFIX: '',
WEBPACK_MAIN_ENTRY: 'src/index.ts',
WEBPACK_OUTPUT_PATH: 'dist',
WEBPACK_OUTPUT_NAME: '[name].[hash].js',
WEBPACK_OUTPUT_CHUNKNAME: '[name].[hash].js',
WEBPACK_PUBLIC_PATH: '/',
WEBPACK_ASSETS_PATH: '',
WEBPACK_OUTPUT_POSTFIX: '',
WEBPACK_LOADER_POSTFIX: '',
WEBPACK_PLUGIN_POSTFIX: '',
};
const config = { meta, overWriteDir: '', sourceDir: 'src', assetsDir: 'assets' };
config.meta.WEBPACK_ASSETS_PATH = path.join(config.sourceDir, config.assetsDir);
container = container.split('/').pop();
if (!container) {
return config;
}
const cDir = path.resolve(exports.containersRoot, container);
const cProjectDir = path.resolve(cDir, 'project');
const cConfigPath = path.resolve(cProjectDir, 'config.js');
if (!fs.existsSync(cProjectDir)) {
return config;
}
config.overWriteDir = cProjectDir;
if (!fs.existsSync(cConfigPath)) {
return config;
}
const c = (yield Promise.resolve().then(() => require(cConfigPath))) || {};
c.meta.DEPENDENCIES = Object.assign(c.meta.DEPENDENCIES, config.meta.DEPENDENCIES);
Object.assign(config.meta, c.meta);
config.sourceDir = c.sourceDir || config.sourceDir;
config.assetsDir = c.assetsDir || config.assetsDir;
return config;
});
}
exports.getProjectConfig = getProjectConfig;
function generate(templatePath, root, meta, filter = () => false) {
return __awaiter(this, void 0, void 0, function* () {
const { DEPENDENCIES, DEV_DEPENDENCIES, SCRIPTS } = meta, strMeta = __rest(meta, ["DEPENDENCIES", "DEV_DEPENDENCIES", "SCRIPTS"]);
const options = {
overwrite: true,
dot: true,
filter: (src) => !(/\.git\//.test(src) || filter(src)),
transform: (src, dest, stats) => {
return through((chunk, enc, done) => {
let output;
if (['.jpg', '.png', '.bmp', '.tileset', '.bin', '.unitypackage', '.glb', '.dll', '.pdb'].indexOf(path.extname(src)) > -1) {
output = chunk;
}
else {
output = chunk.toString();
if (path.basename(src) === 'package.json') {
output = JSON.parse(output);
if (output.dependencies) {
Object.assign(output.dependencies, DEPENDENCIES);
}
if (output.devDependencies) {
Object.assign(output.devDependencies, DEV_DEPENDENCIES);
}
if (output.scripts) {
Object.assign(output.scripts, SCRIPTS);
}
output = JSON.stringify(output, null, 2);
}
Object.keys(strMeta).forEach(key => {
output = output.replace(new RegExp(`{{${key}}}`, 'g'), strMeta[key].trim());
});
}
done(null, output);
});
}
};
try {
yield copy(templatePath, root, options)
.on(copy.events.COPY_FILE_START, copyOperation => {
showInfo(`Generate ${copyOperation.dest}...`);
});
}
catch (error) {
showError(`Generate failed: ${error}\nPlease clean current directory and retry !`);
}
});
}
exports.generate = generate;
function generateUnity(root, assetsFolder, meta) {
return __awaiter(this, void 0, void 0, function* () {
const unityFolder = path.resolve(root, 'unity');
if (!fs.existsSync(unityFolder)) {
fs.mkdirSync(unityFolder);
}
yield generate(exports.unityRoot, unityFolder, meta);
// const configPath = path.resolve(unityFolder, 'Assets/SeinJSUnityToolkit/src/config.json');
// const config = JSON.parse(fs.readFileSync(configPath, {encoding: 'utf8'}));
// config.exportPath = path.relative(path.resolve(unityFolder, 'Assets'), path.resolve(assetsFolder, 'gltfs'));
// fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
});
}
exports.generateUnity = generateUnity;