1 | import path from 'path';
|
2 | import fs from 'fs';
|
3 | import chalk from 'chalk';
|
4 |
|
5 | import Impeller from './impeller';
|
6 |
|
7 | export default class ReplaceString {
|
8 | constructor(_dir, _translations) {
|
9 | this.dir = _dir;
|
10 | this.translationDir = path.resolve(_dir, 'translation', 'default.json');
|
11 | this.translations = _translations;
|
12 | }
|
13 |
|
14 | replace(_files) {
|
15 | return Promise.all(_files.map((_file) => {
|
16 | return new Promise((_resolve) => {
|
17 | let content = fs.readFileSync(_file, 'utf8');
|
18 | let replaceCount = 0;
|
19 | let translated = '';
|
20 | let impeller = new Impeller();
|
21 |
|
22 | if (!content) {
|
23 | console.log(chalk.red(_file + ': Could not read file'));
|
24 | return _resolve();
|
25 | }
|
26 |
|
27 | try {
|
28 | replaceCount = impeller
|
29 | .processFile(_file, content, this.translations);
|
30 |
|
31 | if (!replaceCount) {
|
32 | console.log(chalk.magenta(_file + ': Nothing to translate'));
|
33 | return _resolve();
|
34 | }
|
35 |
|
36 | translated = impeller.toString();
|
37 | if (typeof translated === 'undefined') {
|
38 | return _resolve;
|
39 | }
|
40 | } catch (e) {
|
41 | console.log(chalk.red(_file + ': Could not translate'));
|
42 | console.log(e.message);
|
43 | return _resolve();
|
44 | }
|
45 |
|
46 | try {
|
47 | fs.writeFileSync(_file, translated, 'utf-8');
|
48 | console.log(chalk.green(_file + ': Translated :)'));
|
49 | _resolve();
|
50 | } catch (e) {
|
51 | console.log(chalk.red(_file + ': Could not write file'));
|
52 | console.log(e.message);
|
53 | _resolve();
|
54 | }
|
55 | });
|
56 | }));
|
57 | }
|
58 | }
|