11import path from 'path' ;
22import fs from 'fs' ;
33
4+ // generalize so we can use this script in other es6 repos
5+ // so you can call:
6+ // combineTranslationKeys <inputPath> <inputPath> <inputPath> ... <outputPath>
7+
48const pathToCombinedTranslationKeys = path . join (
59 __dirname ,
610 './translationKeys/combined-translation-keys.txt'
711) ;
812
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 ;
1624
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 ;
2132
2233combineTranslationKeys ( ) ;
2334
2435function combineTranslationKeys ( ) {
2536 const dict = { } ;
2637 let maxLen = 0 ;
2738
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 ) ;
3043
3144 lines . forEach ( line => {
3245 const splitString = line . split ( / \/ \/ / ) ;
@@ -35,9 +48,9 @@ function combineTranslationKeys() {
3548 maxLen = Math . max ( maxLen , stringToTranslate . length ) ;
3649
3750 if ( ! dict [ stringToTranslate ] ) {
38- dict [ stringToTranslate ] = ' // ' + file . repository + ': ' + source ;
51+ dict [ stringToTranslate ] = ' // ' + repository + ': ' + source ;
3952 } else {
40- dict [ stringToTranslate ] += ` && ${ file . repository } : ${ source } ` ;
53+ dict [ stringToTranslate ] += ` && ${ repository } : ${ source } ` ;
4154 }
4255 } ) ;
4356 } ) ;
@@ -47,10 +60,16 @@ function combineTranslationKeys() {
4760 . map ( k => k + spaces ( maxLen - k . length ) + dict [ k ] )
4861 . join ( '\n' ) ;
4962
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 ) ;
5473}
5574
5675function spaces ( len ) {
0 commit comments