Skip to content

Commit 54ce821

Browse files
committed
Обновление 1.0.0
+ chalk + lang + Выбор языка + Выбор скриптов для обработки + Перевод на английский
1 parent 7a8abdd commit 54ce821

File tree

13 files changed

+495
-83
lines changed

13 files changed

+495
-83
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
scripts/
3+
!scripts/example
4+
config.json

Script.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

index.js

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,47 @@ const OUT_FILE = 'output/TEXTMAP';
1111
const fs = require('fs');
1212
const path = require('path');
1313

14-
const Script = require('./Script');
14+
let chalk = null;
15+
let inquirer = null;
16+
try {
17+
chalk = require('chalk');
18+
inquirer = require('inquirer');
19+
} catch (e) {
20+
return console.error(chalk.red('You forgot to run the command: npm install'));
21+
}
22+
23+
let Config = null;
24+
try {
25+
Config = require('./config.json');
26+
} catch (e) {
27+
return require('./lib/firstrun')();
28+
}
29+
30+
let Lang = null;
31+
try {
32+
Lang = require(`./lang/${Config.lang}.json`);
33+
} catch (e) {
34+
return console.error(chalk.red(`Error reading file: ./lang/${Config.lang}.json.\nCheck the lang field in the file ./config.json`));
35+
}
36+
37+
const Script = require('./lib/Script');
1538

16-
const { udmf2json, json2udmf, jsonCompress, jsonDecompress } = require('./udmf2json');
39+
const { udmf2json, json2udmf, jsonCompress, jsonDecompress } = require('./lib/udmf2json');
1740
const packagejson = require('./package.json');
1841

1942
console.info(`
20-
#====== UDMF Map Processor ======#
21-
v.${packagejson.version} by PROPHESSOR
22-
#====== ------------------ ======#
43+
${chalk.cyan(`#====== ${chalk.yellow('UDMF Map Processor')} ======#`)}
44+
${chalk.yellow(`v.${packagejson.version} by PROPHESSOR`)}
45+
${chalk.cyan(`#====== ${chalk.yellow('------------------')} ======#`)}
2346
2447
`);
2548

2649
const udmfarray = udmf2json(IN_FILE, OUT_FILE);
2750
const udmfobject = jsonDecompress(udmfarray);
2851

29-
console.log('Подключение скриптов...');
52+
console.log(chalk.cyan(Lang.log.connecting_scripts));
3053

54+
// Read the script folder
3155
const scripts = [];
3256
{
3357
const scriptpath = path.join(__dirname, 'scripts');
@@ -36,24 +60,38 @@ const scripts = [];
3660
folderdata = fs.readdirSync(scriptpath);
3761
} catch (e) {
3862
console.error(e);
39-
return console.error(`\n\nНевозможно прочитать содержимое папки по пути ${scriptpath}!\n`);
63+
return console.error(`\n\n${Lang.error.readdir} ${scriptpath}!\n`);
4064
}
4165

4266
for (const scriptfolder of folderdata) {
4367
scripts.push(new Script(path.join(scriptpath, scriptfolder)));
4468
}
4569
}
4670

47-
console.log('Обработка карты...');
71+
// Scriptlist
4872
{
49-
let i = 0;
50-
for (const script of scripts) {
51-
console.log(`> [${Math.floor(i++ / scripts.length * 100)}%]: Обработка скриптом ${script.name}`);
52-
Object.assign(udmfarray, script.run(udmfarray, udmfobject));
53-
}
54-
}
73+
const choices = scripts.map(e => ({ name: `${e.name} v.${e.version} by ${e.author}`, value: e.name }));
74+
inquirer.prompt([
75+
{
76+
type: 'checkbox',
77+
name: 'scripts',
78+
message: Lang.question.select_scripts,
79+
choices
80+
},
81+
]).then(ss => {
82+
const selectedScripts = scripts.filter((script) => ss.scripts.includes(script.name));
83+
console.log(chalk.cyan(Lang.log.processing_map));
84+
{
85+
let i = 0;
86+
for (const script of selectedScripts) {
87+
console.log(`${chalk.yellow('>')} [${Math.floor(i++ / selectedScripts.length * 100)}%]: ${chalk.cyan(`${Lang.log.script_process} ${script.name}`)}`);
88+
Object.assign(udmfarray, script.run(udmfarray, udmfobject));
89+
}
90+
}
5591

56-
json2udmf(jsonCompress(jsonDecompress(udmfarray)), OUT_FILE);
92+
json2udmf(jsonCompress(jsonDecompress(udmfarray)), OUT_FILE);
5793

58-
console.info('> [100%]: Обработка карты завершена!');
59-
console.info(`Путь к файлу: ${OUT_FILE}`);
94+
console.info(`${chalk.yellow('>')} [100%]: ${chalk.green(Lang.log.success)}!`);
95+
console.info(chalk.yellow(`${Lang.log.path_to_file}: ${OUT_FILE}`));
96+
});
97+
}

lang/en.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"log": {
3+
"connecting_scripts": "Connecting scripts...",
4+
"processing_map": "Processing a map...",
5+
"script_process": "Script processing",
6+
"success": "Map processing completed",
7+
"path_to_file": "The path to the file"
8+
},
9+
"error": {
10+
"readdir": "Can not read the contents of the folder along the path",
11+
"readfile": "Can not read file by path",
12+
"infile": "Error in file",
13+
"writefile": "Error writing to file"
14+
},
15+
"question": {
16+
"select_scripts": "Select scripts to process"
17+
},
18+
"Script": {
19+
"error": {
20+
"must_have_path": "The script must have a path",
21+
"type_error": "The field type ${key} must be ${value}"
22+
},
23+
"log": {
24+
"success": "Script \"${name}\" v.${version} by \"${author}\" has been successfully activated!"
25+
}
26+
},
27+
"Config": {
28+
"log": {
29+
"success": "Setup completed successfully!",
30+
"restart": "Please restart the program"
31+
}
32+
}
33+
}

lang/langs.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"name": "English",
4+
"value": "en"
5+
},
6+
{
7+
"name": "Russian (Русский)",
8+
"value": "ru"
9+
}
10+
]

lang/ru.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"log": {
3+
"connecting_scripts": "Подключение скриптов...",
4+
"processing_map": "Обработка карты...",
5+
"script_process": "Обработка скриптом",
6+
"success": "Обработка карты завершена",
7+
"path_to_file": "Путь к файлу"
8+
},
9+
"error": {
10+
"readdir": "Невозможно прочитать содержимое папки по пути",
11+
"readfile": "Невозможно прочитать файл по пути",
12+
"infile": "Ошибка в файле",
13+
"writefile": "Ошибка записи в файл"
14+
},
15+
"question": {
16+
"select_scripts": "Выберите нужные скрипты для обработки"
17+
},
18+
"Script": {
19+
"error": {
20+
"must_have_path": "Скрипт должен иметь путь",
21+
"type_error": "Тип поля ${key} должен быть ${value}"
22+
},
23+
"log": {
24+
"success": "Скрипт \"${name}\" v.${version} от \"${author}\" успешно активирован!"
25+
}
26+
},
27+
"Config": {
28+
"log": {
29+
"success": "Настройка успешно завершена!",
30+
"restart": "Пожалуйста, перезапустите программу"
31+
}
32+
}
33+
}

lib/Script.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright (c) 2018 PROPHESSOR
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
8+
const path = require('path');
9+
const chalk = require('chalk');
10+
11+
const Config = require('../config.json');
12+
const Lang = require(`../lang/${Config.lang}.json`);
13+
14+
module.exports = class Script {
15+
/**
16+
* @constructor
17+
* @param {string} folder - Путь к папке скрипта
18+
*/
19+
constructor(folder) {
20+
if(!folder) throw new Error(chalk.red(`[Script.js->constructor]: ${Lang.Script.error.must_have_path}`));
21+
22+
let umpscript = null;
23+
const umpscriptpath = path.join(folder, 'umpscript.json');
24+
25+
try {
26+
umpscript = require(umpscriptpath);
27+
} catch (e) {
28+
console.error(e);
29+
return console.error(chalk.red(`\n\n${Lang.error.readfile} ${umpscriptpath}!\n`));
30+
}
31+
32+
{
33+
const validator = {
34+
'name': 'string',
35+
'author': 'string',
36+
'version': 'string',
37+
'description': 'string',
38+
'url': 'string',
39+
'main': 'string',
40+
'dependencies': 'object'
41+
};
42+
43+
for(const key in umpscript) {
44+
if(validator[key] && typeof umpscript[key] !== validator[key]) {
45+
console.error(chalk.red(`\n${Lang.error.infile} ${umpscriptpath}! ${Lang.Script.error.type_error.replace(/\${key}/g, key).replace(/\${value}/g, validator[key])}\n`));
46+
return process.exit(1);
47+
}
48+
}
49+
}
50+
51+
Object.assign(this, umpscript);
52+
53+
const mainpath = path.join(folder, umpscript.main);
54+
try {
55+
this.run = require(mainpath);
56+
} catch(e) {
57+
console.error(e);
58+
return console.error(chalk.red(`\n\n${Lang.error.readfile} ${umpscriptpath}!\n`));
59+
}
60+
61+
console.info(chalk.green(Lang.Script.log.success.replace(/\${name}/g, this.name).replace(/\${version}/g, this.version).replace(/\${author}/g, this.author)));
62+
}
63+
};
64+
65+
if(!module.parent) new module.exports(path.join(__dirname, './scripts/example/'));

lib/firstrun.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) 2018 PROPHESSOR
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
8+
'use strict';
9+
10+
const fs = require('fs');
11+
const path = require('path');
12+
const inquirer = require('inquirer');
13+
const chalk = require('chalk');
14+
15+
const Config = {
16+
'lang': 'en'
17+
};
18+
19+
module.exports = () => {
20+
inquirer.prompt([
21+
{
22+
type: 'list',
23+
name: 'lang',
24+
message: 'Select language:',
25+
default: 'en',
26+
choices: require('../lang/langs.json')
27+
},
28+
]).then(res => {
29+
const { lang } = res;
30+
let Lang = null;
31+
try {
32+
Lang = require(`../lang/${lang}.json`);
33+
} catch(e) {
34+
console.error(e);
35+
return console.error(chalk.red(`\n\nError reading file: ./lang/${Config.lang}.json.\nCheck the lang field in the file ./config.json`));
36+
}
37+
38+
Config.lang = lang;
39+
try {
40+
fs.writeFileSync(path.join(__dirname, '..', 'config.json'), JSON.stringify(Config, null, 4), 'utf8');
41+
} catch(e) {
42+
console.error(chalk.red(`${Lang.error.writefile} ../config.json`));
43+
}
44+
console.info(chalk.green(Lang.Config.log.success));
45+
console.info(chalk.yellow(Lang.Config.log.restart));
46+
process.exit(0);
47+
});
48+
};
File renamed without changes.

0 commit comments

Comments
 (0)