Skip to content

Commit 4367a01

Browse files
committed
style: fixes
1 parent b3e5439 commit 4367a01

File tree

4 files changed

+166
-149
lines changed

4 files changed

+166
-149
lines changed

src/index.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { LanguageInterface } from './interfaces/language'
44
import { LanguageJsonFileInterface } from './interfaces/language-json-file'
55
import { ReplacementsInterface } from './interfaces/replacements'
66
import { choose } from './pluralization'
7-
import { avoidExceptionOnPromise, avoidException } from './utils/avoid-exceptions';
7+
import { avoidExceptionOnPromise, avoidException } from './utils/avoid-exceptions'
88

99
const isServer = typeof window === 'undefined'
1010

@@ -139,35 +139,42 @@ function setLanguage({ lang, messages }: LanguageInterface): string {
139139
* It resolves the language file or data, from direct data, require or Promise.
140140
*/
141141
async function resolveLang(callable: Function, lang: string): Promise<LanguageJsonFileInterface> {
142-
const hasPhpTranslations = typeof process !== 'undefined' && process.env ? process.env.LARAVEL_VUE_I18N_HAS_PHP : false;
142+
const hasPhpTranslations =
143+
typeof process !== 'undefined' && process.env ? process.env.LARAVEL_VUE_I18N_HAS_PHP : false
143144

144-
let data = avoidException(callable, lang);
145+
let data = avoidException(callable, lang)
145146

146147
if (data instanceof Promise) {
147148
if (hasPhpTranslations) {
148-
const phpLang = await avoidExceptionOnPromise(callable(`php_${lang}`));
149-
const jsonLang = await avoidExceptionOnPromise(data);
150-
151-
return new Promise((resolve) => resolve({
152-
default: {
153-
...phpLang,
154-
...jsonLang,
155-
}
156-
}));
149+
const phpLang = await avoidExceptionOnPromise(callable(`php_${lang}`))
150+
const jsonLang = await avoidExceptionOnPromise(data)
151+
152+
return new Promise((resolve) =>
153+
resolve({
154+
default: {
155+
...phpLang,
156+
...jsonLang
157+
}
158+
})
159+
)
157160
}
158161

159-
return new Promise(async resolve => resolve({
160-
default: await avoidExceptionOnPromise(data),
161-
}));
162+
return new Promise(async (resolve) =>
163+
resolve({
164+
default: await avoidExceptionOnPromise(data)
165+
})
166+
)
162167
}
163168

164169
if (hasPhpTranslations) {
165-
return new Promise((resolve) => resolve({
166-
default: {
167-
...data,
168-
...(avoidException(callable, `php_${lang}`)),
169-
}
170-
}));
170+
return new Promise((resolve) =>
171+
resolve({
172+
default: {
173+
...data,
174+
...avoidException(callable, `php_${lang}`)
175+
}
176+
})
177+
)
171178
}
172179

173180
return new Promise((resolve) => resolve({ default: data }))

src/loader.ts

Lines changed: 86 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,119 @@
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'
44

55
export const hasPhpTranslations = (folderPath: string): boolean => {
6-
folderPath = folderPath.replace(/[\\/]$/, '') + path.sep;
6+
folderPath = folderPath.replace(/[\\/]$/, '') + path.sep
77

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()
1112

12-
for (const folder of folders) {
13-
const lang = {};
13+
for (const folder of folders) {
14+
const lang = {}
1415

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))
1717

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
4920
}
21+
}
5022

51-
return data.map(({ folder, translations }) => {
52-
const name = `php_${folder}.json`;
53-
const path = folderPath + name;
23+
return false
24+
}
5425

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+
})
5860
}
5961

6062
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
6364

64-
return convertToDotsSyntax(parseItem(arr.expr));
65+
return convertToDotsSyntax(parseItem(arr.expr))
6566
}
6667

6768
const parseItem = (expr) => {
68-
if (expr.kind === 'string') {
69-
return expr.value;
70-
}
69+
if (expr.kind === 'string') {
70+
return expr.value
71+
}
7172

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))
7475

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), {})
8078
}
8179

82-
if (expr.key) {
83-
return { [expr.key.value]: parseItem(expr.value) }
84-
}
80+
return items
81+
}
8582

86-
return parseItem(expr.value)
83+
if (expr.key) {
84+
return { [expr.key.value]: parseItem(expr.value) }
85+
}
86+
87+
return parseItem(expr.value)
8788
}
8889

8990
const convertToDotsSyntax = (list) => {
90-
const flatten = (items, context = '') => {
91-
const data = {};
91+
const flatten = (items, context = '') => {
92+
const data = {}
9293

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+
}
9899

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+
})
103104

104-
return data;
105-
}
105+
return data
106+
}
106107

107-
return flatten(list);
108+
return flatten(list)
108109
}
109110

110111
export const reset = (folderPath) => {
111-
const dir = fs.readdirSync(folderPath);
112+
const dir = fs.readdirSync(folderPath)
112113

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+
})
116119
}

src/mix.ts

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
1-
import path from 'path';
2-
import fs from 'fs';
1+
import path from 'path'
2+
import fs from 'fs'
33

4-
import mix from 'laravel-mix';
5-
import { Component } from 'laravel-mix/src/components/Component';
6-
import { EnvironmentPlugin, Configuration } from 'webpack';
4+
import mix from 'laravel-mix'
5+
import { Component } from 'laravel-mix/src/components/Component'
6+
import { EnvironmentPlugin, Configuration } from 'webpack'
77

8-
import { parseAll, hasPhpTranslations } from './loader';
8+
import { parseAll, hasPhpTranslations } from './loader'
99

1010
class BeforeBuildPlugin {
11-
callback: Function;
11+
callback: Function
1212

13-
constructor(callback: Function) {
14-
this.callback = callback;
15-
}
13+
constructor(callback: Function) {
14+
this.callback = callback
15+
}
1616

17-
apply(compiler): void {
18-
compiler.hooks.compile.tap('BeforeBuildPlugin', this.callback);
19-
}
17+
apply(compiler): void {
18+
compiler.hooks.compile.tap('BeforeBuildPlugin', this.callback)
19+
}
2020
}
2121

22-
mix.extend('i18n', class extends Component {
23-
langPath: string;
24-
context: any;
22+
mix.extend(
23+
'i18n',
24+
class extends Component {
25+
langPath: string
26+
context: any
2527

2628
register(langPath: string = 'lang'): void {
27-
this.langPath = this.context.paths.rootPath + path.sep + langPath;
29+
this.langPath = this.context.paths.rootPath + path.sep + langPath
2830
}
2931

3032
webpackConfig(config: Configuration): void {
31-
let files = [];
32-
33-
config.watchOptions = {
34-
ignored: /php_\w+\.json/,
35-
};
36-
37-
if (hasPhpTranslations(this.langPath)) {
38-
config.plugins.push(new EnvironmentPlugin({
39-
'LARAVEL_VUE_I18N_HAS_PHP': true,
40-
}));
41-
}
42-
43-
config.plugins.push(new BeforeBuildPlugin(() => {
44-
files = parseAll(this.langPath);
45-
}))
46-
47-
this.context.listen('build', () => {
48-
files.forEach(file => {
49-
if (fs.existsSync(file.path)) {
50-
fs.unlinkSync(file.path);
51-
}
52-
});
53-
});
33+
let files = []
34+
35+
config.watchOptions = {
36+
ignored: /php_\w+\.json/
37+
}
38+
39+
if (hasPhpTranslations(this.langPath)) {
40+
config.plugins.push(
41+
new EnvironmentPlugin({
42+
LARAVEL_VUE_I18N_HAS_PHP: true
43+
})
44+
)
45+
}
46+
47+
config.plugins.push(
48+
new BeforeBuildPlugin(() => {
49+
files = parseAll(this.langPath)
50+
})
51+
)
52+
53+
this.context.listen('build', () => {
54+
files.forEach((file) => {
55+
if (fs.existsSync(file.path)) {
56+
fs.unlinkSync(file.path)
57+
}
58+
})
59+
})
5460
}
55-
});
61+
}
62+
)

src/utils/avoid-exceptions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
export const avoidException = (callback: Function, param, defaults = {}) => {
22
try {
3-
return callback(param);
3+
return callback(param)
44
} catch {
5-
return defaults;
5+
return defaults
66
}
77
}
88

99
export const avoidExceptionOnPromise = async (promise, defaults = {}) => {
1010
try {
11-
return (await promise).default || defaults;
11+
return (await promise).default || defaults
1212
} catch (e) {
13-
return defaults;
13+
return defaults
1414
}
1515
}

0 commit comments

Comments
 (0)