|
1 |
| -import fs from 'fs'; |
2 |
| -import path from 'path'; |
3 |
| -import { Engine } from 'php-parser'; |
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { Engine } from 'php-parser' |
4 | 4 |
|
5 | 5 | export const hasPhpTranslations = (folderPath: string): boolean => {
|
6 |
| - folderPath = folderPath.replace(/[\\/]$/, '') + path.sep; |
| 6 | + folderPath = folderPath.replace(/[\\/]$/, '') + path.sep |
7 | 7 |
|
8 |
| - const folders = fs.readdirSync(folderPath) |
9 |
| - .filter(file => fs.statSync(folderPath + path.sep + file).isDirectory()) |
10 |
| - .sort(); |
| 8 | + const folders = fs |
| 9 | + .readdirSync(folderPath) |
| 10 | + .filter((file) => fs.statSync(folderPath + path.sep + file).isDirectory()) |
| 11 | + .sort() |
11 | 12 |
|
12 |
| - for (const folder of folders) { |
13 |
| - const lang = {}; |
| 13 | + for (const folder of folders) { |
| 14 | + const lang = {} |
14 | 15 |
|
15 |
| - const files = fs.readdirSync(folderPath + path.sep + folder) |
16 |
| - .filter(file => /\.php$/.test(file)); |
| 16 | + const files = fs.readdirSync(folderPath + path.sep + folder).filter((file) => /\.php$/.test(file)) |
17 | 17 |
|
18 |
| - if (files.length > 0) { |
19 |
| - return true; |
20 |
| - } |
21 |
| - } |
22 |
| - |
23 |
| - return false; |
24 |
| -} |
25 |
| - |
26 |
| -export const parseAll = (folderPath: string): { name: string, path: string }[] => { |
27 |
| - folderPath = folderPath.replace(/[\\/]$/, '') + path.sep; |
28 |
| - |
29 |
| - const folders = fs.readdirSync(folderPath) |
30 |
| - .filter(file => fs.statSync(folderPath + path.sep + file).isDirectory()) |
31 |
| - .sort(); |
32 |
| - |
33 |
| - const data = []; |
34 |
| - for (const folder of folders) { |
35 |
| - const lang = {}; |
36 |
| - |
37 |
| - fs |
38 |
| - .readdirSync(folderPath + path.sep + folder) |
39 |
| - .filter(file => ! fs.statSync(folderPath + path.sep + folder + path.sep + file).isDirectory()) |
40 |
| - .sort() |
41 |
| - .forEach((file) => { |
42 |
| - lang[file.replace(/\.\w+$/, '')] = parse(fs.readFileSync(folderPath + path.sep + folder + path.sep + file).toString()); |
43 |
| - }); |
44 |
| - |
45 |
| - data.push({ |
46 |
| - folder, |
47 |
| - translations: convertToDotsSyntax(lang), |
48 |
| - }); |
| 18 | + if (files.length > 0) { |
| 19 | + return true |
49 | 20 | }
|
| 21 | + } |
50 | 22 |
|
51 |
| - return data.map(({ folder, translations }) => { |
52 |
| - const name = `php_${folder}.json`; |
53 |
| - const path = folderPath + name; |
| 23 | + return false |
| 24 | +} |
54 | 25 |
|
55 |
| - fs.writeFileSync(path, JSON.stringify(translations)); |
56 |
| - return { name, path } |
57 |
| - }); |
| 26 | +export const parseAll = (folderPath: string): { name: string; path: string }[] => { |
| 27 | + folderPath = folderPath.replace(/[\\/]$/, '') + path.sep |
| 28 | + |
| 29 | + const folders = fs |
| 30 | + .readdirSync(folderPath) |
| 31 | + .filter((file) => fs.statSync(folderPath + path.sep + file).isDirectory()) |
| 32 | + .sort() |
| 33 | + |
| 34 | + const data = [] |
| 35 | + for (const folder of folders) { |
| 36 | + const lang = {} |
| 37 | + |
| 38 | + fs.readdirSync(folderPath + path.sep + folder) |
| 39 | + .filter((file) => !fs.statSync(folderPath + path.sep + folder + path.sep + file).isDirectory()) |
| 40 | + .sort() |
| 41 | + .forEach((file) => { |
| 42 | + lang[file.replace(/\.\w+$/, '')] = parse( |
| 43 | + fs.readFileSync(folderPath + path.sep + folder + path.sep + file).toString() |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + data.push({ |
| 48 | + folder, |
| 49 | + translations: convertToDotsSyntax(lang) |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + return data.map(({ folder, translations }) => { |
| 54 | + const name = `php_${folder}.json` |
| 55 | + const path = folderPath + name |
| 56 | + |
| 57 | + fs.writeFileSync(path, JSON.stringify(translations)) |
| 58 | + return { name, path } |
| 59 | + }) |
58 | 60 | }
|
59 | 61 |
|
60 | 62 | export const parse = (content: string) => {
|
61 |
| - const arr = (new Engine({})).parseCode(content, 'lang').children |
62 |
| - .filter(child => child.kind === 'return')[0] as any; |
| 63 | + const arr = new Engine({}).parseCode(content, 'lang').children.filter((child) => child.kind === 'return')[0] as any |
63 | 64 |
|
64 |
| - return convertToDotsSyntax(parseItem(arr.expr)); |
| 65 | + return convertToDotsSyntax(parseItem(arr.expr)) |
65 | 66 | }
|
66 | 67 |
|
67 | 68 | const parseItem = (expr) => {
|
68 |
| - if (expr.kind === 'string') { |
69 |
| - return expr.value; |
70 |
| - } |
| 69 | + if (expr.kind === 'string') { |
| 70 | + return expr.value |
| 71 | + } |
71 | 72 |
|
72 |
| - if (expr.kind === 'array') { |
73 |
| - let items = expr.items.map((item) => parseItem(item)); |
| 73 | + if (expr.kind === 'array') { |
| 74 | + let items = expr.items.map((item) => parseItem(item)) |
74 | 75 |
|
75 |
| - if (expr.items.every((item) => item.key !== null)) { |
76 |
| - items = items.reduce((acc, val) => Object.assign({}, acc, val), {}) |
77 |
| - } |
78 |
| - |
79 |
| - return items; |
| 76 | + if (expr.items.every((item) => item.key !== null)) { |
| 77 | + items = items.reduce((acc, val) => Object.assign({}, acc, val), {}) |
80 | 78 | }
|
81 | 79 |
|
82 |
| - if (expr.key) { |
83 |
| - return { [expr.key.value]: parseItem(expr.value) } |
84 |
| - } |
| 80 | + return items |
| 81 | + } |
85 | 82 |
|
86 |
| - return parseItem(expr.value) |
| 83 | + if (expr.key) { |
| 84 | + return { [expr.key.value]: parseItem(expr.value) } |
| 85 | + } |
| 86 | + |
| 87 | + return parseItem(expr.value) |
87 | 88 | }
|
88 | 89 |
|
89 | 90 | const convertToDotsSyntax = (list) => {
|
90 |
| - const flatten = (items, context = '') => { |
91 |
| - const data = {}; |
| 91 | + const flatten = (items, context = '') => { |
| 92 | + const data = {} |
92 | 93 |
|
93 |
| - Object.entries(items).forEach(([key, value]) => { |
94 |
| - if (typeof value === 'string') { |
95 |
| - data[context + key] = value; |
96 |
| - return; |
97 |
| - } |
| 94 | + Object.entries(items).forEach(([key, value]) => { |
| 95 | + if (typeof value === 'string') { |
| 96 | + data[context + key] = value |
| 97 | + return |
| 98 | + } |
98 | 99 |
|
99 |
| - Object.entries(flatten(value, context + key + '.')).forEach(([itemKey, itemValue]) => { |
100 |
| - data[itemKey] = itemValue; |
101 |
| - }); |
102 |
| - }); |
| 100 | + Object.entries(flatten(value, context + key + '.')).forEach(([itemKey, itemValue]) => { |
| 101 | + data[itemKey] = itemValue |
| 102 | + }) |
| 103 | + }) |
103 | 104 |
|
104 |
| - return data; |
105 |
| - } |
| 105 | + return data |
| 106 | + } |
106 | 107 |
|
107 |
| - return flatten(list); |
| 108 | + return flatten(list) |
108 | 109 | }
|
109 | 110 |
|
110 | 111 | export const reset = (folderPath) => {
|
111 |
| - const dir = fs.readdirSync(folderPath); |
| 112 | + const dir = fs.readdirSync(folderPath) |
112 | 113 |
|
113 |
| - dir.filter(file => file.match(/^php_/)).forEach(file => { |
114 |
| - fs.unlinkSync(folderPath + file); |
115 |
| - }); |
| 114 | + dir |
| 115 | + .filter((file) => file.match(/^php_/)) |
| 116 | + .forEach((file) => { |
| 117 | + fs.unlinkSync(folderPath + file) |
| 118 | + }) |
116 | 119 | }
|
0 commit comments