/
index.js
153 lines (141 loc) · 5.09 KB
/
index.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
#! /usr/bin/env node
const fs = require('fs');
const path = require('path');
var log = function (message, important) {
if (process.argv.includes('--silent')) {
return;
}
if (process.argv.includes('--verbose') || process.argv.includes('-v') || important) {
console.log(message);
}
}
if (process.argv.includes('--help') || process.argv.includes('-h')) {
console.log('Usage: npx laravel-language-extractor [options]');
console.log('Options:');
console.log(' --default-file <file> Specify the default file to write to. Defaults to lang/en.json');
console.log(' --path <path> Specify the path to scan. Defaults to the current directory.');
console.log(' --silent Don\'t output anything.');
console.log(' --verbose Output everything.');
console.log(' --help Show this help message.');
console.log(' --theme <theme> Specify the theme to scan. Defaults to the default theme.');
return;
}
var dir;
if (process.argv.includes('--path')) {
var dir = process.argv[process.argv.indexOf('--path') + 1];
if (!dir) {
log('You must specify a path to scan.', true);
return;
}
} else {
var dir = path.resolve();
}
log('Starting...')
const readDir = (dir) => {
const files = fs.readdirSync(dir);
let fileList = [];
files.forEach((file) => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = fileList.concat(readDir(path.join(dir, file)));
}
else {
fileList.push(path.join(dir, file));
}
});
return fileList;
};
// Support for https://github.com/qirolab/laravel-themer
var themeDir;
if (process.argv.includes('--theme')) {
themeDir = path.join(dir, '/themes/' + process.argv[process.argv.indexOf('--theme') + 1] + '/views');
} else {
themeDir = path.join(dir, '/resources/views');
}
const themeFiles = readDir(themeDir);
var lang;
try {
lang = require(dir + '/lang/en.json');
} catch (e) {
lang = {};
}
log('Looping through theme files...')
themeFiles.forEach((file) => {
const fileContent = fs.readFileSync(path.join(file), 'utf8');
const matches = fileContent.match(/{{\s*__\(\s*(?:'|")(.*?)(?:'|")\s*\)\s*}}/g);
if (matches) {
matches.forEach((match) => {
const key = match.replace(/{{\s*__\(\s*(?:'|")(.*?)(?:'|")\s*\)\s*}}/g, '$1');
if (!lang[key]) {
lang[key] = key;
}
});
}
});
if (!process.argv.includes('--no-controllers')) {
var controllerDir = path.join(dir, '/app/Http/Controllers');
if (!controllerDir) return;
const controllerFiles = readDir(controllerDir);
log('Looping through controller files...')
controllerFiles.forEach((file) => {
const fileContent = fs.readFileSync(path.join(file), 'utf8');
// Match all the Lang::get() calls
const matches = fileContent.match(/Lang::get\('(.*?)'\)/g);
if (matches) {
matches.forEach((match) => {
const key = match.replace(/Lang::get\('(.*?)'\)/, '$1');
if (!lang[key]) {
lang[key] = key;
}
});
}
});
}
lang = Object.keys(lang).sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' })).reduce((r, k) => (r[k] = lang[k], r), {});
if (process.argv.includes('--default-file')) {
const defaultFile = process.argv[process.argv.indexOf('--default-file') + 1];
if (defaultFile) {
fs.writeFileSync(path.join(dir, defaultFile), JSON.stringify(lang, null, 4));
log('Writing ' + defaultFile + ' file...')
}
} else {
fs.writeFileSync(path.join(dir, '/lang/en.json'), JSON.stringify(lang, null, 4));
log('Writing lang/en.json file...')
}
// Loop through all the files in the lang directory and if the file is outdated, update it
const langDir = path.join(dir, '/lang');
const langFiles = readDir(langDir);
var filesUpdated = 0;
var stringsAdded = 0;
var existingLang;
langFiles.forEach((file) => {
// If the file isn't a .json file, skip it
if (!file.endsWith('.json')) {
return;
}
// Read the file
try {
existingLang = require(file);
} catch (e) {
if (e.message.includes('Unexpected end of JSON input')) {
existingLang = {};
} else {
log('Error reading ' + file + ', skipping...', true)
return;
}
}
// Loop through all the keys in the en.json file
Object.keys(lang).forEach((key) => {
// If the key doesn't exist in the existing language file, add it
if (!existingLang[key]) {
existingLang[key] = key;
stringsAdded++;
}
});
// Sort the keys
existingLang = Object.keys(existingLang).sort((a, b) => a.localeCompare(b, undefined, { sensitivity: 'base' })).reduce((r, k) => (r[k] = existingLang[k], r), {});
// Write the file
fs.writeFileSync(file, JSON.stringify(existingLang, null, 4));
log(`Updating ${file}...`);
filesUpdated++;
});
log('Done, updated ' + filesUpdated + ' files with ' + stringsAdded + ' new strings.', true);