1
1
import path from 'path' ;
2
2
import fs from 'fs' ;
3
3
4
+ // generalize so we can use this script in other es6 repos
5
+ // so you can call:
6
+ // combineTranslationKeys <inputPath> <inputPath> <inputPath> ... <outputPath>
7
+
4
8
const pathToCombinedTranslationKeys = path . join (
5
9
__dirname ,
6
10
'./translationKeys/combined-translation-keys.txt'
7
11
) ;
8
12
9
- const plotlyJS = {
10
- repository : 'plotly.js' ,
11
- path : path . join (
12
- __dirname ,
13
- '../node_modules/plotly.js/dist/translation-keys.txt'
14
- ) ,
15
- } ;
13
+ const plotlyJS = path . join (
14
+ __dirname ,
15
+ '../node_modules/plotly.js/dist/translation-keys.txt'
16
+ ) ;
17
+
18
+ const editor = path . join ( __dirname , './translationKeys/translation-keys.txt' ) ;
19
+
20
+ const argvLen = process . argv . length ;
21
+ const minHasPaths = 4 ;
22
+
23
+ const hasPaths = argvLen >= minHasPaths ;
16
24
17
- const editor = {
18
- repository : 'react-plotly.js-editor' ,
19
- path : path . join ( __dirname , './translationKeys/translation-keys.txt' ) ,
20
- } ;
25
+ const inputPaths = hasPaths
26
+ ? process . argv . slice ( 2 , argvLen - 1 )
27
+ : [ plotlyJS , editor ] ;
28
+
29
+ const outputPath = hasPaths
30
+ ? process . argv [ argvLen - 1 ]
31
+ : pathToCombinedTranslationKeys ;
21
32
22
33
combineTranslationKeys ( ) ;
23
34
24
35
function combineTranslationKeys ( ) {
25
36
const dict = { } ;
26
37
let maxLen = 0 ;
27
38
28
- [ plotlyJS , editor ] . forEach ( file => {
29
- const lines = fs . readFileSync ( file . path , 'utf-8' ) . split ( / \r ? \n / ) ;
39
+ inputPaths . forEach ( inputPath => {
40
+ const lines = fs . readFileSync ( inputPath , 'utf-8' ) . split ( / \r ? \n / ) ;
41
+
42
+ const repository = getRepository ( inputPath ) ;
30
43
31
44
lines . forEach ( line => {
32
45
const splitString = line . split ( / \/ \/ / ) ;
@@ -35,9 +48,9 @@ function combineTranslationKeys() {
35
48
maxLen = Math . max ( maxLen , stringToTranslate . length ) ;
36
49
37
50
if ( ! dict [ stringToTranslate ] ) {
38
- dict [ stringToTranslate ] = ' // ' + file . repository + ': ' + source ;
51
+ dict [ stringToTranslate ] = ' // ' + repository + ': ' + source ;
39
52
} else {
40
- dict [ stringToTranslate ] += ` && ${ file . repository } : ${ source } ` ;
53
+ dict [ stringToTranslate ] += ` && ${ repository } : ${ source } ` ;
41
54
}
42
55
} ) ;
43
56
} ) ;
@@ -47,10 +60,16 @@ function combineTranslationKeys() {
47
60
. map ( k => k + spaces ( maxLen - k . length ) + dict [ k ] )
48
61
. join ( '\n' ) ;
49
62
50
- fs . writeFile ( pathToCombinedTranslationKeys , strings ) ;
51
- console . log (
52
- `combined translation keys were written to: ${ pathToCombinedTranslationKeys } `
53
- ) ;
63
+ fs . writeFile ( outputPath , strings ) ;
64
+ console . log ( `combined translation keys were written to: ${ outputPath } ` ) ;
65
+ }
66
+
67
+ function getRepository ( inputPath ) {
68
+ var dir = path . dirname ( inputPath ) ;
69
+ if ( fs . existsSync ( path . join ( dir , 'package.json' ) ) ) {
70
+ return path . basename ( dir ) ;
71
+ }
72
+ return getRepository ( dir ) ;
54
73
}
55
74
56
75
function spaces ( len ) {
0 commit comments