Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.

Commit 55c2d33

Browse files
ElNinjaNerdTmartinlindhe
authored andcommitted
Add file name and lang files command line options (#70)
* Add command line option for output file name * Add command line option for lang files * Make tests pass
1 parent 2b26c09 commit 55c2d33

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/Commands/GenerateInclude.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class GenerateInclude extends Command
1111
*
1212
* @var string
1313
*/
14-
protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor}';
14+
protected $signature = 'vue-i18n:generate {--umd} {--multi} {--with-vendor} {--file-name=} {--lang-files=}';
1515

1616
/**
1717
* The console command description.
@@ -33,6 +33,8 @@ public function handle()
3333
$umd = $this->option('umd');
3434
$multipleFiles = $this->option('multi');
3535
$withVendor = $this->option('with-vendor');
36+
$fileName = $this->option('file-name');
37+
$langFiles = $this->option('lang-files');
3638

3739
if ($multipleFiles) {
3840
$files = (new Generator($config))
@@ -45,14 +47,32 @@ public function handle()
4547
return;
4648
}
4749

50+
if ($langFiles) {
51+
$langFiles = explode(',', $langFiles);
52+
}
53+
4854
$data = (new Generator($config))
49-
->generateFromPath($root, $umd, $withVendor);
55+
->generateFromPath($root, $umd, $withVendor, $langFiles);
56+
5057

51-
$jsFile = base_path() . config('vue-i18n-generator.jsFile');
58+
$jsFile = $this->getFileName($fileName);
5259
file_put_contents($jsFile, $data);
5360

5461
if ($config['showOutputMessages']) {
5562
$this->info("Written to : ".$jsFile);
5663
}
5764
}
65+
66+
/**
67+
* @param string $fileNameOption
68+
* @return string
69+
*/
70+
private function getFileName($fileNameOption)
71+
{
72+
if (isset($fileNameOption)) {
73+
return base_path() . $fileNameOption;
74+
}
75+
76+
return base_path() . config('vue-i18n-generator.jsFile');
77+
}
5878
}

src/Generator.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Generator
1010

1111
private $availableLocales = [];
1212
private $filesToCreate = [];
13+
private $langFiles;
1314

1415
const VUEX_I18N = 'vuex-i18n';
1516
const VUE_I18N = 'vue-i18n';
@@ -34,12 +35,14 @@ public function __construct($config = [])
3435
* @return string
3536
* @throws Exception
3637
*/
37-
public function generateFromPath($path, $umd = null, $withVendor = false)
38+
public function generateFromPath($path, $umd = null, $withVendor = false, $langFiles = [])
3839
{
3940
if (!is_dir($path)) {
4041
throw new Exception('Directory not found: ' . $path);
4142
}
4243

44+
$this->langFiles = $langFiles;
45+
4346
$locales = [];
4447
$files = [];
4548
$dir = new DirectoryIterator($path);
@@ -208,9 +211,7 @@ private function allocateLocaleArray($path)
208211
continue;
209212
}
210213

211-
if ((isset($this->config['langFiles']) && !empty($this->config['langFiles']) && !in_array($noExt, $this->config['langFiles']))
212-
|| (isset($this->config['excludes']) && in_array($noExt, $this->config['excludes']))
213-
) {
214+
if ($this->shouldIgnoreLangFile($noExt)) {
214215
continue;
215216
}
216217

@@ -233,6 +234,21 @@ private function allocateLocaleArray($path)
233234
return $data;
234235
}
235236

237+
/**
238+
* @param string $noExt
239+
* @return boolean
240+
*/
241+
private function shouldIgnoreLangFile($noExt)
242+
{
243+
// langFiles passed by option have priority
244+
if (isset($this->langFiles) && !empty($this->langFiles)) {
245+
return !in_array($noExt, $this->langFiles);
246+
}
247+
248+
return (isset($this->config['langFiles']) && !empty($this->config['langFiles']) && !in_array($noExt, $this->config['langFiles']))
249+
|| (isset($this->config['excludes']) && in_array($noExt, $this->config['excludes']));
250+
}
251+
236252
/**
237253
* @param array $arr
238254
* @return array

0 commit comments

Comments
 (0)