Compare commits
	
		
			168 Commits
		
	
	
		
			1.0.0
			...
			chore-upda
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					567a0586dc | ||
| 
						 | 
					17dfbbed84 | ||
| 
						 | 
					acdffe0121 | ||
| 
						 | 
					116133ba32 | ||
| 
						 | 
					50b2ca6f4a | ||
| 
						 | 
					b46a914756 | ||
| 
						 | 
					bc3e5fbe2f | ||
| 
						 | 
					ea990d6f9d | ||
| 
						 | 
					c60705d5fa | ||
| 
						 | 
					85cd1e4a46 | ||
| 
						 | 
					8afbb2f3a9 | ||
| 
						 | 
					329c24d962 | ||
| 
						 | 
					a30a6f9215 | ||
| 
						 | 
					2adec54c00 | ||
| 
						 | 
					619b3c56ea | ||
| 
						 | 
					5e0da552b0 | ||
| 
						 | 
					5f2eb2a7a0 | ||
| 
						 | 
					90b59793a7 | ||
| 
						 | 
					deb6b2373b | ||
| 
						 | 
					17dec7deb8 | ||
| 
						 | 
					71f4f42b33 | ||
| 
						 | 
					73f39d625c | ||
| 
						 | 
					3bf2aaca4e | ||
| 
						 | 
					05d1917f9d | ||
| 
						 | 
					e50d52003b | ||
| 
						 | 
					cb8731ee0f | ||
| 
						 | 
					9908681243 | ||
| 
						 | 
					a83123fb12 | ||
| 
						 | 
					6b740867d6 | ||
| 
						 | 
					d0e9a8cd85 | ||
| 
						 | 
					f157025f0a | ||
| 
						 | 
					ecf629118a | ||
| 
						 | 
					ce399ee717 | ||
| 
						 | 
					e8bc023ea6 | ||
| 
						 | 
					33d8c26a28 | ||
| 
						 | 
					b07d929484 | ||
| 
						 | 
					a0f2b69f36 | ||
| 
						 | 
					13f46a524f | ||
| 
						 | 
					6ed962fa6e | ||
| 
						 | 
					8aa2774eca | ||
| 
						 | 
					37ca29648a | ||
| 
						 | 
					97d844c3d2 | ||
| 
						 | 
					e7795c5349 | ||
| 
						 | 
					9da4939f5d | ||
| 
						 | 
					8fa7b60d2d | ||
| 
						 | 
					d3d6a72d5f | ||
| 
						 | 
					bfd069b755 | ||
| 
						 | 
					72b4fb0545 | ||
| 
						 | 
					131713d9db | ||
| 
						 | 
					a17ad9c373 | ||
| 
						 | 
					56a5ab31bf | ||
| 
						 | 
					d579114dd2 | ||
| 
						 | 
					cc45df9b44 | ||
| 
						 | 
					b813ec0063 | ||
| 
						 | 
					7b94c9b9a5 | ||
| 
						 | 
					a45039ef17 | ||
| 
						 | 
					5d0c92871e | ||
| 
						 | 
					9887f9d6ab | ||
| 
						 | 
					608c4e8e22 | ||
| 
						 | 
					0345778aa1 | ||
| 
						 | 
					77769983d5 | ||
| 
						 | 
					306622b9a0 | ||
| 
						 | 
					7d0d52429f | ||
| 
						 | 
					2fce357306 | ||
| 
						 | 
					3827789346 | ||
| 
						 | 
					8c8fe8d131 | ||
| 
						 | 
					7bf0c138b8 | ||
| 
						 | 
					16bf5f59e0 | ||
| 
						 | 
					096fc79a9b | ||
| 
						 | 
					a72dbf0494 | ||
| 
						 | 
					8fd157802b | ||
| 
						 | 
					1db4794ee9 | ||
| 
						 | 
					1eb1d0092d | ||
| 
						 | 
					4d3a3529b8 | ||
| 
						 | 
					97e8937709 | ||
| 
						 | 
					eb7f3f603e | ||
| 
						 | 
					ab2b78eaec | ||
| 
						 | 
					75ee2bdfda | ||
| 
						 | 
					4fe3c43624 | ||
| 
						 | 
					41bd679fcd | ||
| 
						 | 
					24ebd8f428 | ||
| 
						 | 
					e1bb5bfd02 | ||
| 
						 | 
					1323c2e6a1 | ||
| 
						 | 
					0f465014df | ||
| 
						 | 
					5d5b07ba2c | ||
| 
						 | 
					7eefd6c8d3 | ||
| 
						 | 
					50fd3ae9e2 | ||
| 
						 | 
					a5b8f6e6c6 | ||
| 
						 | 
					3cbc20e0a0 | ||
| 
						 | 
					7ce01b97e4 | ||
| 
						 | 
					393e1ed03f | ||
| 
						 | 
					8b014abf49 | ||
| 
						 | 
					71cc6e6883 | ||
| 
						 | 
					ceb4be7e3d | ||
| 
						 | 
					e41fc88d97 | ||
| 
						 | 
					ac551b1824 | ||
| 
						 | 
					73877a5a35 | ||
| 
						 | 
					6e161c83f8 | ||
| 
						 | 
					7d1bcd2a80 | ||
| 
						 | 
					bc2bfac7d7 | ||
| 
						 | 
					c7563d4998 | ||
| 
						 | 
					f9b3c63c4c | ||
| 
						 | 
					9e5dad362c | ||
| 
						 | 
					e133e0ce30 | ||
| 
						 | 
					7ee0b7da71 | ||
| 
						 | 
					c38ca59d43 | ||
| 
						 | 
					98b84447c7 | ||
| 
						 | 
					2507d0cdd2 | ||
| 
						 | 
					69047857b2 | ||
| 
						 | 
					04b6684024 | ||
| 
						 | 
					9a8abb3248 | ||
| 
						 | 
					c8ba1312b5 | ||
| 
						 | 
					e0178b5a97 | ||
| 
						 | 
					3e43fde1cc | ||
| 
						 | 
					8493015e15 | ||
| 
						 | 
					deb38eb7c3 | ||
| 
						 | 
					36928e253d | ||
| 
						 | 
					230b13e245 | ||
| 
						 | 
					4fd7efa2dc | ||
| 
						 | 
					02fc705bc0 | ||
| 
						 | 
					cb53f6f3b1 | ||
| 
						 | 
					842b0a7d97 | ||
| 
						 | 
					f32128b2ec | ||
| 
						 | 
					53eb4d1202 | ||
| 
						 | 
					102286a209 | ||
| 
						 | 
					ab29c9ab67 | ||
| 
						 | 
					590f58fff3 | ||
| 
						 | 
					d07d81681e | ||
| 
						 | 
					141eaca7b1 | ||
| 
						 | 
					7d5d38e6a1 | ||
| 
						 | 
					64ebb5e6e8 | ||
| 
						 | 
					40051f4144 | ||
| 
						 | 
					14eb09f947 | ||
| 
						 | 
					8d1e2c5a2f | ||
| 
						 | 
					4892ea5146 | ||
| 
						 | 
					ee28fe2a64 | ||
| 
						 | 
					7c06b66974 | ||
| 
						 | 
					b2ae17697d | ||
| 
						 | 
					5259da8fe3 | ||
| 
						 | 
					2d73f056ff | ||
| 
						 | 
					fde5245731 | ||
| 
						 | 
					4ee7258a31 | ||
| 
						 | 
					a6c7af0630 | ||
| 
						 | 
					0949bf765b | ||
| 
						 | 
					d416c6b9fd | ||
| 
						 | 
					4e351405fb | ||
| 
						 | 
					39a335638b | ||
| 
						 | 
					3b9561916b | ||
| 
						 | 
					5cef383f3b | ||
| 
						 | 
					677d2a35ca | ||
| 
						 | 
					262a89206d | ||
| 
						 | 
					bcb4a9c069 | ||
| 
						 | 
					5ad1fe6a18 | ||
| 
						 | 
					bc5ce7e80d | ||
| 
						 | 
					030ab145d6 | ||
| 
						 | 
					daaebede6f | ||
| 
						 | 
					42a6568d47 | ||
| 
						 | 
					9a2108e9a9 | ||
| 
						 | 
					ff1c91010e | ||
| 
						 | 
					7654397bb3 | ||
| 
						 | 
					7b10b246e6 | ||
| 
						 | 
					2f68bbc660 | ||
| 
						 | 
					371a14d73e | ||
| 
						 | 
					4537c1224a | ||
| 
						 | 
					f61cdc4064 | ||
| 
						 | 
					72739f2e9a | ||
| 
						 | 
					3facc0c287 | ||
| 
						 | 
					c5d68cfcaa | 
							
								
								
									
										2
									
								
								.github/FUNDING.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.github/FUNDING.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
				
			|||||||
 | 
					github: biesbjerg
 | 
				
			||||||
 | 
					custom: https://donate.biesbjerg.com
 | 
				
			||||||
							
								
								
									
										6
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -8,10 +8,12 @@ npm-debug.log*
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
# Compiled files
 | 
					# Compiled files
 | 
				
			||||||
dist
 | 
					dist
 | 
				
			||||||
 | 
					src/**/*.js
 | 
				
			||||||
 | 
					tests/**/*.js
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Extracted strings
 | 
					# Extracted strings
 | 
				
			||||||
template.json
 | 
					strings.json
 | 
				
			||||||
template.pot
 | 
					strings.pot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Dependency directory
 | 
					# Dependency directory
 | 
				
			||||||
node_modules
 | 
					node_modules
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					MIT License
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Copyright (c) 2017 Kim Biesbjerg
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
				
			||||||
 | 
					of this software and associated documentation files (the "Software"), to deal
 | 
				
			||||||
 | 
					in the Software without restriction, including without limitation the rights
 | 
				
			||||||
 | 
					to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
				
			||||||
 | 
					copies of the Software, and to permit persons to whom the Software is
 | 
				
			||||||
 | 
					furnished to do so, subject to the following conditions:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The above copyright notice and this permission notice shall be included in all
 | 
				
			||||||
 | 
					copies or substantial portions of the Software.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
				
			||||||
 | 
					IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
				
			||||||
 | 
					FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
				
			||||||
 | 
					AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
				
			||||||
 | 
					LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
				
			||||||
 | 
					OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
				
			||||||
 | 
					SOFTWARE.
 | 
				
			||||||
							
								
								
									
										110
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								README.md
									
									
									
									
									
								
							@@ -1,40 +1,108 @@
 | 
				
			|||||||
If you like this project please show your support with a GitHub star. Much appreciated!
 | 
					If this tool saves you time, please consider making a donation towards the continued maintainence and development: https://donate.biesbjerg.com
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[](https://donate.biesbjerg.com)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# ngx-translate-extract
 | 
					# ngx-translate-extract
 | 
				
			||||||
Extract translatable (ngx-translate) strings and save as a JSON or Gettext pot file.
 | 
					Extract translatable (ngx-translate) strings and save as a JSON or Gettext pot file.
 | 
				
			||||||
Merges with existing strings if the output file already exists.
 | 
					Merges with existing strings if the output file already exists.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Usage
 | 
					## Install
 | 
				
			||||||
Install the package in your project:
 | 
					Install the package in your project:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
`npm install @biesbjerg/ngx-translate-extract --save-dev`
 | 
					`npm install @biesbjerg/ngx-translate-extract --save-dev`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Add an `extract` script to your project's `package.json`:
 | 
					Add a script to your project's `package.json`:
 | 
				
			||||||
```
 | 
					```json
 | 
				
			||||||
 | 
					...
 | 
				
			||||||
"scripts": {
 | 
					"scripts": {
 | 
				
			||||||
  "extract": "ngx-translate-extract --dir ./src --output ./ --format=json --clean"
 | 
					  "i18n:init": "ngx-translate-extract --input ./src --output ./src/assets/i18n/template.json --key-as-default-value --replace --format json",
 | 
				
			||||||
 | 
					  "i18n:extract": "ngx-translate-extract --input ./src --output ./src/assets/i18n/{en,da,de,fi,nb,nl,sv}.json --clean --format json"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					...
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
You can now run `npm run extract` to extract strings from your project's `src` dir. The extracted strings are saved in `JSON`-format in your project's root.
 | 
					You can now run `npm run extract-i18n` and it will extract strings from your project.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Modify the scripts arguments as required.
 | 
					## Usage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Commandline arguments
 | 
					**Extract from dir and save to file**
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					`ngx-translate-extract --input ./src --output ./src/assets/i18n/strings.json`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					**Extract from multiple dirs**
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					`ngx-translate-extract --input ./src-a ./src-b --output ./src/assets/i18n/strings.json`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					**Extract and save to multiple files using path expansion**
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					`ngx-translate-extract --input ./src --output ./src/i18n/{da,en}.json`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### JSON indentation
 | 
				
			||||||
 | 
					Tabs are used by default for indentation when saving extracted strings in json formats:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If you want to use spaces instead, you can do the following:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					`ngx-translate-extract --input ./src --output ./src/i18n/en.json --format-indentation '  '`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Marker function
 | 
				
			||||||
 | 
					If you want to extract strings that are not passed directly to `TranslateService`'s `get()`/`instant()`/`stream()` methods, you can wrap them in a marker function to let `ngx-translate-extract` know you want to extract them.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Install marker function:
 | 
				
			||||||
 | 
					`npm install @biesbjerg/ngx-translate-extract-marker`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```ts
 | 
				
			||||||
 | 
					import { marker } from '@biesbjerg/ngx-translate-extract-marker';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					marker('Extract me');
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					You can alias the marker function if needed:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```ts
 | 
				
			||||||
 | 
					import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_('Extract me');
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_Note: `ngx-translate-extract` will automatically detect the import name_
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Commandline arguments
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
Usage:
 | 
					Usage:
 | 
				
			||||||
  ngx-translate-extract [OPTIONS] [ARGS]
 | 
					ngx-translate-extract [options]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Output
 | 
				
			||||||
 | 
					  --format, -f                Format    [string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
 | 
				
			||||||
 | 
					  --format-indentation, --fi  Format indentation (JSON/Namedspaced JSON)                [string] [default: "	"]
 | 
				
			||||||
 | 
					  --sort, -s                  Sort strings in alphabetical order                                     [boolean]
 | 
				
			||||||
 | 
					  --clean, -c                 Remove obsolete strings after merge                                    [boolean]
 | 
				
			||||||
 | 
					  --replace, -r               Replace the contents of output file if it exists (Merges by default)   [boolean]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Extracted key value (defaults to empty string)
 | 
				
			||||||
 | 
					  --key-as-default-value, -k     Use key as default value                                            [boolean]
 | 
				
			||||||
 | 
					  --null-as-default-value, -n    Use null as default value                                           [boolean]
 | 
				
			||||||
 | 
					  --string-as-default-value, -d  Use string as default value                                          [string]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Options:
 | 
					Options:
 | 
				
			||||||
  -d, --dir [DIR]        Directory path you would like to extract strings from  (Default is current directory)
 | 
					  --version, -v  Show version number                                                                 [boolean]
 | 
				
			||||||
  -o, --output [DIR]     Directory path you would like to save extracted
 | 
					  --help, -h     Show help                                                                           [boolean]
 | 
				
			||||||
                         strings  (Default is current directory/template.json)
 | 
					  --input, -i    Paths you would like to extract strings from. You can use path expansion, glob patterns and
 | 
				
			||||||
  -f, --format [VALUE]   Output format. VALUE must be either
 | 
					                 multiple paths        [array] [required] [default: ["/Users/kim/apps/ngx-translate-extract"]]
 | 
				
			||||||
                         [json|namespaced-json|pot]  (Default is json)
 | 
					  --output, -o   Paths where you would like to save extracted strings. You can use path expansion, glob
 | 
				
			||||||
  -r, --replace BOOLEAN  Replace the contents of output file if it exists
 | 
					                 patterns and multiple paths                                                [array] [required]
 | 
				
			||||||
                         (Merges by default)
 | 
					
 | 
				
			||||||
  -s, --sort BOOLEAN     Sort translations in the output file in alphabetical
 | 
					Examples:
 | 
				
			||||||
                         order
 | 
					  ngx-translate-extract -i ./src-a/ -i ./src-b/ -o strings.json           Extract (ts, html) from multiple paths
 | 
				
			||||||
  -c, --clean BOOLEAN    Remove obsolete strings when merging
 | 
					  ngx-translate-extract -i './{src-a,src-b}/' -o strings.json             Extract (ts, html) from multiple paths using brace
 | 
				
			||||||
  -e, --experimental BOOLEAN Use experimental AST Service Parser
 | 
					                                                           expansion
 | 
				
			||||||
  -h, --help             Display help and usage details
 | 
					  ngx-translate-extract -i ./src/ -o ./i18n/da.json -o ./i18n/en.json     Extract (ts, html) and save to da.json and en.json
 | 
				
			||||||
 | 
					  ngx-translate-extract -i ./src/ -o './i18n/{en,da}.json'                Extract (ts, html) and save to da.json and en.json
 | 
				
			||||||
 | 
					                                                           using brace expansion
 | 
				
			||||||
 | 
					  ngx-translate-extract -i './src/**/*.{ts,tsx,html}' -o strings.json     Extract from ts, tsx and html
 | 
				
			||||||
 | 
					  ngx-translate-extract -i './src/**/!(*.spec).{ts,html}' -o              Extract from ts, html, excluding files with ".spec"
 | 
				
			||||||
 | 
					  strings.json
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Note for GetText users
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Please pay attention of which version of `gettext-parser` you actually use in your project. For instance, `gettext-parser:1.2.2` does not support HTML tags in translation keys.
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										3
									
								
								bin/cli.js
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								bin/cli.js
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					#! /usr/bin/env node
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require('../dist/cli/cli');
 | 
				
			||||||
@@ -1,3 +0,0 @@
 | 
				
			|||||||
#! /usr/bin/env node
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
require('../dist/cli/extract');
 | 
					 | 
				
			||||||
							
								
								
									
										
											BIN
										
									
								
								images/donate-badge.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								images/donate-badge.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 16 KiB  | 
							
								
								
									
										2281
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										2281
									
								
								package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										82
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										82
									
								
								package.json
									
									
									
									
									
								
							@@ -1,6 +1,6 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "@biesbjerg/ngx-translate-extract",
 | 
					  "name": "@biesbjerg/ngx-translate-extract",
 | 
				
			||||||
  "version": "1.0.0",
 | 
					  "version": "7.0.3",
 | 
				
			||||||
  "description": "Extract strings from projects using ngx-translate",
 | 
					  "description": "Extract strings from projects using ngx-translate",
 | 
				
			||||||
  "main": "dist/index.js",
 | 
					  "main": "dist/index.js",
 | 
				
			||||||
  "typings": "dist/index.d.ts",
 | 
					  "typings": "dist/index.d.ts",
 | 
				
			||||||
@@ -9,25 +9,39 @@
 | 
				
			|||||||
    "dist/"
 | 
					    "dist/"
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
  "bin": {
 | 
					  "bin": {
 | 
				
			||||||
    "ngx-translate-extract": "bin/extract.js"
 | 
					    "ngx-translate-extract": "bin/cli.js"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "scripts": {
 | 
					  "scripts": {
 | 
				
			||||||
    "build": "npm run clean && tsc",
 | 
					    "build": "npm run clean && tsc",
 | 
				
			||||||
    "watch": "npm run clean && tsc --watch",
 | 
					    "watch": "npm run clean && tsc --watch",
 | 
				
			||||||
    "clean": "rm -rf ./dist",
 | 
					    "clean": "rimraf ./dist",
 | 
				
			||||||
    "lint": "tslint --force './src/**/*.ts'",
 | 
					    "lint": "tslint --force './src/**/*.ts'",
 | 
				
			||||||
    "test": "mocha -r ts-node/register tests/**/*.spec.ts"
 | 
					    "test": "mocha -r ts-node/register tests/**/*.spec.ts"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					  "husky": {
 | 
				
			||||||
 | 
					    "hooks": {
 | 
				
			||||||
 | 
					      "pre-commit": "lint-staged && npm test"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "prettier": {
 | 
				
			||||||
 | 
					    "trailingComma": "none",
 | 
				
			||||||
 | 
					    "printWidth": 145,
 | 
				
			||||||
 | 
					    "useTabs": true,
 | 
				
			||||||
 | 
					    "singleQuote": true
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "lint-staged": {
 | 
				
			||||||
 | 
					    "{src,tests}/**/*.{ts}": [
 | 
				
			||||||
 | 
					      "tslint --project tsconfig.json -c tslint.commit.json --fix",
 | 
				
			||||||
 | 
					      "prettier --write"
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
  "repository": {
 | 
					  "repository": {
 | 
				
			||||||
    "type": "git",
 | 
					    "type": "git",
 | 
				
			||||||
    "url": "https://github.com/biesbjerg/ngx-translate-extract.git"
 | 
					    "url": "https://github.com/biesbjerg/ngx-translate-extract.git"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "keywords": [
 | 
					  "keywords": [
 | 
				
			||||||
    "angular",
 | 
					    "angular",
 | 
				
			||||||
    "angular2",
 | 
					 | 
				
			||||||
    "ionic",
 | 
					    "ionic",
 | 
				
			||||||
    "ionic2",
 | 
					 | 
				
			||||||
    "ng2-translate",
 | 
					 | 
				
			||||||
    "ngx-translate",
 | 
					    "ngx-translate",
 | 
				
			||||||
    "extract",
 | 
					    "extract",
 | 
				
			||||||
    "extractor",
 | 
					    "extractor",
 | 
				
			||||||
@@ -43,28 +57,48 @@
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
  "homepage": "https://github.com/biesbjerg/ngx-translate-extract",
 | 
					  "homepage": "https://github.com/biesbjerg/ngx-translate-extract",
 | 
				
			||||||
  "engines": {
 | 
					  "engines": {
 | 
				
			||||||
    "node": ">=4.3.2"
 | 
					    "node": ">=11.15.0"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "config": {},
 | 
					  "config": {},
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
    "@types/chai": "3.4.35",
 | 
					    "@angular/compiler": "^11.2.9",
 | 
				
			||||||
    "@types/cheerio": "0.17.31",
 | 
					    "@types/braces": "^3.0.0",
 | 
				
			||||||
    "@types/glob": "5.0.30",
 | 
					    "@types/chai": "^4.2.16",
 | 
				
			||||||
    "@types/mocha": "2.2.39",
 | 
					    "@types/flat": "^5.0.1",
 | 
				
			||||||
    "chai": "3.5.0",
 | 
					    "@types/gettext-parser": "4.0.0",
 | 
				
			||||||
    "mocha": "3.2.0",
 | 
					    "@types/glob": "^7.1.3",
 | 
				
			||||||
    "ts-node": "2.1.0",
 | 
					    "@types/mkdirp": "^1.0.1",
 | 
				
			||||||
    "tslint": "4.5.1",
 | 
					    "@types/mocha": "^8.2.2",
 | 
				
			||||||
    "tslint-eslint-rules": "3.4.0",
 | 
					    "@types/node": "^14.14.37",
 | 
				
			||||||
    "typescript": "2.2.1"
 | 
					    "@types/yargs": "^16.0.1",
 | 
				
			||||||
 | 
					    "braces": "^3.0.2",
 | 
				
			||||||
 | 
					    "chai": "^4.3.4",
 | 
				
			||||||
 | 
					    "husky": "^6.0.0",
 | 
				
			||||||
 | 
					    "lint-staged": "^10.5.4",
 | 
				
			||||||
 | 
					    "mocha": "^8.3.2",
 | 
				
			||||||
 | 
					    "prettier": "^2.2.1",
 | 
				
			||||||
 | 
					    "rimraf": "^3.0.2",
 | 
				
			||||||
 | 
					    "ts-node": "^9.1.1",
 | 
				
			||||||
 | 
					    "tslint": "^6.1.3",
 | 
				
			||||||
 | 
					    "tslint-config-prettier": "^1.18.0",
 | 
				
			||||||
 | 
					    "tslint-eslint-rules": "^5.4.0",
 | 
				
			||||||
 | 
					    "tslint-etc": "^1.13.9",
 | 
				
			||||||
 | 
					    "typescript": "^4.2.4"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "peerDependencies": {
 | 
				
			||||||
 | 
					    "@angular/compiler": ">=8.0.0",
 | 
				
			||||||
 | 
					    "typescript": ">=3.0.0"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "dependencies": {
 | 
					  "dependencies": {
 | 
				
			||||||
    "cheerio": "0.22.0",
 | 
					    "@phenomnomnominal/tsquery": "^4.1.1",
 | 
				
			||||||
    "cli": "1.0.1",
 | 
					    "boxen": "^5.0.1",
 | 
				
			||||||
    "fs": "0.0.1-security",
 | 
					    "colorette": "^1.2.2",
 | 
				
			||||||
    "gettext-parser": "1.2.2",
 | 
					    "flat": "^5.0.2",
 | 
				
			||||||
    "glob": "7.1.1",
 | 
					    "gettext-parser": "^4.0.4",
 | 
				
			||||||
    "path": "0.12.7",
 | 
					    "glob": "^7.1.6",
 | 
				
			||||||
    "flat": "2.0.1"
 | 
					    "mkdirp": "^1.0.4",
 | 
				
			||||||
 | 
					    "path": "^0.12.7",
 | 
				
			||||||
 | 
					    "terminal-link": "^2.1.1",
 | 
				
			||||||
 | 
					    "yargs": "^16.2.0"
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										161
									
								
								src/cli/cli.ts
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										161
									
								
								src/cli/cli.ts
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,161 @@
 | 
				
			|||||||
 | 
					import * as yargs from 'yargs';
 | 
				
			||||||
 | 
					import { red, green } from 'colorette';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { ExtractTask } from './tasks/extract.task';
 | 
				
			||||||
 | 
					import { ParserInterface } from '../parsers/parser.interface';
 | 
				
			||||||
 | 
					import { PipeParser } from '../parsers/pipe.parser';
 | 
				
			||||||
 | 
					import { DirectiveParser } from '../parsers/directive.parser';
 | 
				
			||||||
 | 
					import { ServiceParser } from '../parsers/service.parser';
 | 
				
			||||||
 | 
					import { MarkerParser } from '../parsers/marker.parser';
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from '../post-processors/post-processor.interface';
 | 
				
			||||||
 | 
					import { SortByKeyPostProcessor } from '../post-processors/sort-by-key.post-processor';
 | 
				
			||||||
 | 
					import { KeyAsDefaultValuePostProcessor } from '../post-processors/key-as-default-value.post-processor';
 | 
				
			||||||
 | 
					import { NullAsDefaultValuePostProcessor } from '../post-processors/null-as-default-value.post-processor';
 | 
				
			||||||
 | 
					import { StringAsDefaultValuePostProcessor } from '../post-processors/string-as-default-value.post-processor';
 | 
				
			||||||
 | 
					import { PurgeObsoleteKeysPostProcessor } from '../post-processors/purge-obsolete-keys.post-processor';
 | 
				
			||||||
 | 
					import { CompilerInterface } from '../compilers/compiler.interface';
 | 
				
			||||||
 | 
					import { CompilerFactory } from '../compilers/compiler.factory';
 | 
				
			||||||
 | 
					import { normalizePaths } from '../utils/fs-helpers';
 | 
				
			||||||
 | 
					import { donateMessage } from '../utils/donate';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// First parsing pass to be able to access pattern argument for use input/output arguments
 | 
				
			||||||
 | 
					const y = yargs.option('patterns', {
 | 
				
			||||||
 | 
						alias: 'p',
 | 
				
			||||||
 | 
						describe: 'Default patterns',
 | 
				
			||||||
 | 
						type: 'array',
 | 
				
			||||||
 | 
						default: ['/**/*.html', '/**/*.ts'],
 | 
				
			||||||
 | 
						hidden: true
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const parsed = y.parse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const cli = y
 | 
				
			||||||
 | 
						.usage('Extract strings from files for translation.\nUsage: $0 [options]')
 | 
				
			||||||
 | 
						.version(require(__dirname + '/../../package.json').version)
 | 
				
			||||||
 | 
						.alias('version', 'v')
 | 
				
			||||||
 | 
						.help('help')
 | 
				
			||||||
 | 
						.alias('help', 'h')
 | 
				
			||||||
 | 
						.option('input', {
 | 
				
			||||||
 | 
							alias: 'i',
 | 
				
			||||||
 | 
							describe: 'Paths you would like to extract strings from. You can use path expansion, glob patterns and multiple paths',
 | 
				
			||||||
 | 
							default: [process.env.PWD],
 | 
				
			||||||
 | 
							type: 'array',
 | 
				
			||||||
 | 
							normalize: true,
 | 
				
			||||||
 | 
							required: true
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.coerce('input', (input: string[]) => {
 | 
				
			||||||
 | 
							const paths = normalizePaths(input, parsed.patterns);
 | 
				
			||||||
 | 
							return paths;
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('output', {
 | 
				
			||||||
 | 
							alias: 'o',
 | 
				
			||||||
 | 
							describe: 'Paths where you would like to save extracted strings. You can use path expansion, glob patterns and multiple paths',
 | 
				
			||||||
 | 
							type: 'array',
 | 
				
			||||||
 | 
							normalize: true,
 | 
				
			||||||
 | 
							required: true
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.coerce('output', (output: string[]) => {
 | 
				
			||||||
 | 
							const paths = normalizePaths(output, parsed.patterns);
 | 
				
			||||||
 | 
							return paths;
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('format', {
 | 
				
			||||||
 | 
							alias: 'f',
 | 
				
			||||||
 | 
							describe: 'Format',
 | 
				
			||||||
 | 
							default: 'json',
 | 
				
			||||||
 | 
							type: 'string',
 | 
				
			||||||
 | 
							choices: ['json', 'namespaced-json', 'pot']
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('format-indentation', {
 | 
				
			||||||
 | 
							alias: 'fi',
 | 
				
			||||||
 | 
							describe: 'Format indentation (JSON/Namedspaced JSON)',
 | 
				
			||||||
 | 
							default: '\t',
 | 
				
			||||||
 | 
							type: 'string'
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('replace', {
 | 
				
			||||||
 | 
							alias: 'r',
 | 
				
			||||||
 | 
							describe: 'Replace the contents of output file if it exists (Merges by default)',
 | 
				
			||||||
 | 
							type: 'boolean'
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('sort', {
 | 
				
			||||||
 | 
							alias: 's',
 | 
				
			||||||
 | 
							describe: 'Sort strings in alphabetical order',
 | 
				
			||||||
 | 
							type: 'boolean'
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('clean', {
 | 
				
			||||||
 | 
							alias: 'c',
 | 
				
			||||||
 | 
							describe: 'Remove obsolete strings after merge',
 | 
				
			||||||
 | 
							type: 'boolean'
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('key-as-default-value', {
 | 
				
			||||||
 | 
							alias: 'k',
 | 
				
			||||||
 | 
							describe: 'Use key as default value',
 | 
				
			||||||
 | 
							type: 'boolean',
 | 
				
			||||||
 | 
							conflicts: ['null-as-default-value', 'string-as-default-value']
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('null-as-default-value', {
 | 
				
			||||||
 | 
							alias: 'n',
 | 
				
			||||||
 | 
							describe: 'Use null as default value',
 | 
				
			||||||
 | 
							type: 'boolean',
 | 
				
			||||||
 | 
							conflicts: ['key-as-default-value', 'string-as-default-value']
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.option('string-as-default-value', {
 | 
				
			||||||
 | 
							alias: 'd',
 | 
				
			||||||
 | 
							describe: 'Use string as default value',
 | 
				
			||||||
 | 
							type: 'string',
 | 
				
			||||||
 | 
							conflicts: ['null-as-default-value', 'key-as-default-value']
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						.group(['format', 'format-indentation', 'sort', 'clean', 'replace'], 'Output')
 | 
				
			||||||
 | 
						.group(['key-as-default-value', 'null-as-default-value', 'string-as-default-value'], 'Extracted key value (defaults to empty string)')
 | 
				
			||||||
 | 
						.conflicts('key-as-default-value', 'null-as-default-value')
 | 
				
			||||||
 | 
						.example(`$0 -i ./src-a/ -i ./src-b/ -o strings.json`, 'Extract (ts, html) from multiple paths')
 | 
				
			||||||
 | 
						.example(`$0 -i './{src-a,src-b}/' -o strings.json`, 'Extract (ts, html) from multiple paths using brace expansion')
 | 
				
			||||||
 | 
						.example(`$0 -i ./src/ -o ./i18n/da.json -o ./i18n/en.json`, 'Extract (ts, html) and save to da.json and en.json')
 | 
				
			||||||
 | 
						.example(`$0 -i ./src/ -o './i18n/{en,da}.json'`, 'Extract (ts, html) and save to da.json and en.json using brace expansion')
 | 
				
			||||||
 | 
						.example(`$0 -i './src/**/*.{ts,tsx,html}' -o strings.json`, 'Extract from ts, tsx and html')
 | 
				
			||||||
 | 
						.example(`$0 -i './src/**/!(*.spec).{ts,html}' -o strings.json`, 'Extract from ts, html, excluding files with ".spec" in filename')
 | 
				
			||||||
 | 
						.wrap(110)
 | 
				
			||||||
 | 
						.exitProcess(true)
 | 
				
			||||||
 | 
						.parse(process.argv);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const extractTask = new ExtractTask(cli.input, cli.output, {
 | 
				
			||||||
 | 
						replace: cli.replace
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Parsers
 | 
				
			||||||
 | 
					const parsers: ParserInterface[] = [new PipeParser(), new DirectiveParser(), new ServiceParser(), new MarkerParser()];
 | 
				
			||||||
 | 
					extractTask.setParsers(parsers);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Post processors
 | 
				
			||||||
 | 
					const postProcessors: PostProcessorInterface[] = [];
 | 
				
			||||||
 | 
					if (cli.clean) {
 | 
				
			||||||
 | 
						postProcessors.push(new PurgeObsoleteKeysPostProcessor());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					if (cli.keyAsDefaultValue) {
 | 
				
			||||||
 | 
						postProcessors.push(new KeyAsDefaultValuePostProcessor());
 | 
				
			||||||
 | 
					} else if (cli.nullAsDefaultValue) {
 | 
				
			||||||
 | 
						postProcessors.push(new NullAsDefaultValuePostProcessor());
 | 
				
			||||||
 | 
					} else if (cli.stringAsDefaultValue) {
 | 
				
			||||||
 | 
						postProcessors.push(new StringAsDefaultValuePostProcessor({ defaultValue: cli.stringAsDefaultValue as string }));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (cli.sort) {
 | 
				
			||||||
 | 
						postProcessors.push(new SortByKeyPostProcessor());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					extractTask.setPostProcessors(postProcessors);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Compiler
 | 
				
			||||||
 | 
					const compiler: CompilerInterface = CompilerFactory.create(cli.format, {
 | 
				
			||||||
 | 
						indentation: cli.formatIndentation
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					extractTask.setCompiler(compiler);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Run task
 | 
				
			||||||
 | 
					try {
 | 
				
			||||||
 | 
						extractTask.execute();
 | 
				
			||||||
 | 
						console.log(green('\nDone.\n'));
 | 
				
			||||||
 | 
						console.log(donateMessage);
 | 
				
			||||||
 | 
						process.exit(0);
 | 
				
			||||||
 | 
					} catch (e) {
 | 
				
			||||||
 | 
						console.log(red(`\nAn error occurred: ${e}\n`));
 | 
				
			||||||
 | 
						process.exit(1);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,105 +0,0 @@
 | 
				
			|||||||
import { Extractor } from '../utils/extractor';
 | 
					 | 
				
			||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					 | 
				
			||||||
import { ParserInterface } from '../parsers/parser.interface';
 | 
					 | 
				
			||||||
import { PipeParser } from '../parsers/pipe.parser';
 | 
					 | 
				
			||||||
import { DirectiveParser } from '../parsers/directive.parser';
 | 
					 | 
				
			||||||
import { ServiceParser } from '../parsers/service.parser';
 | 
					 | 
				
			||||||
import { AstServiceParser } from '../parsers/ast-service.parser';
 | 
					 | 
				
			||||||
import { CompilerInterface } from '../compilers/compiler.interface';
 | 
					 | 
				
			||||||
import { JsonCompiler } from '../compilers/json.compiler';
 | 
					 | 
				
			||||||
import { NamespacedJsonCompiler } from '../compilers/namespaced-json.compiler';
 | 
					 | 
				
			||||||
import { PoCompiler } from '../compilers/po.compiler';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import * as fs from 'fs';
 | 
					 | 
				
			||||||
import * as path from 'path';
 | 
					 | 
				
			||||||
import * as cli from 'cli';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const options = cli.parse({
 | 
					 | 
				
			||||||
	dir: ['d', 'Path you would like to extract strings from', 'dir', process.env.PWD],
 | 
					 | 
				
			||||||
	output: ['o', 'Path you would like to save extracted strings to', 'dir', process.env.PWD],
 | 
					 | 
				
			||||||
	format: ['f', 'Output format', ['json', 'namespaced-json', 'pot'], 'json'],
 | 
					 | 
				
			||||||
	replace: ['r', 'Replace the contents of output file if it exists (Merges by default)', 'boolean', false],
 | 
					 | 
				
			||||||
	sort: ['s', 'Sort translations in the output file in alphabetical order', 'boolean', false],
 | 
					 | 
				
			||||||
	clean: ['c', 'Remove obsolete strings when merging', 'boolean', false],
 | 
					 | 
				
			||||||
	experimental: ['e', 'Use experimental AST Service Parser', 'boolean', false]
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const patterns: string[] = [
 | 
					 | 
				
			||||||
	'/**/*.html',
 | 
					 | 
				
			||||||
	'/**/*.ts'
 | 
					 | 
				
			||||||
];
 | 
					 | 
				
			||||||
const parsers: ParserInterface[] = [
 | 
					 | 
				
			||||||
	new PipeParser(),
 | 
					 | 
				
			||||||
	new DirectiveParser(),
 | 
					 | 
				
			||||||
	options.experimental ? new AstServiceParser() : new ServiceParser()
 | 
					 | 
				
			||||||
];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
let compiler: CompilerInterface;
 | 
					 | 
				
			||||||
let ext: string;
 | 
					 | 
				
			||||||
switch (options.format) {
 | 
					 | 
				
			||||||
	case 'pot':
 | 
					 | 
				
			||||||
		compiler = new PoCompiler();
 | 
					 | 
				
			||||||
		ext = 'pot';
 | 
					 | 
				
			||||||
		break;
 | 
					 | 
				
			||||||
	case 'json':
 | 
					 | 
				
			||||||
		compiler = new JsonCompiler();
 | 
					 | 
				
			||||||
		ext = 'json';
 | 
					 | 
				
			||||||
		break;
 | 
					 | 
				
			||||||
	case 'namespaced-json':
 | 
					 | 
				
			||||||
		compiler = new NamespacedJsonCompiler();
 | 
					 | 
				
			||||||
		ext = 'json';
 | 
					 | 
				
			||||||
		break;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const normalizedDir: string = path.resolve(options.dir);
 | 
					 | 
				
			||||||
const normalizedOutput: string = path.resolve(options.output);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
let outputDir: string = normalizedOutput;
 | 
					 | 
				
			||||||
let outputFilename: string = `template.${ext}`;
 | 
					 | 
				
			||||||
if (!fs.existsSync(normalizedOutput) || !fs.statSync(normalizedOutput).isDirectory()) {
 | 
					 | 
				
			||||||
	outputDir = path.dirname(normalizedOutput);
 | 
					 | 
				
			||||||
	outputFilename = path.basename(normalizedOutput);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
const outputPath: string = path.join(outputDir, outputFilename);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
[normalizedDir, outputDir].forEach(dir => {
 | 
					 | 
				
			||||||
	if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
 | 
					 | 
				
			||||||
		cli.fatal(`The path you supplied was not found: '${dir}'`);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
try {
 | 
					 | 
				
			||||||
	const extractor: Extractor = new Extractor(parsers, patterns);
 | 
					 | 
				
			||||||
	cli.info(`Extracting strings from '${normalizedDir}'`);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	const extracted: TranslationCollection = extractor.process(normalizedDir);
 | 
					 | 
				
			||||||
	cli.ok(`* Extracted ${extracted.count()} strings`);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	let collection: TranslationCollection = extracted;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (!options.replace && fs.existsSync(outputPath)) {
 | 
					 | 
				
			||||||
		const existing: TranslationCollection = compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
 | 
					 | 
				
			||||||
		if (existing.count() > 0) {
 | 
					 | 
				
			||||||
			collection = extracted.union(existing);
 | 
					 | 
				
			||||||
			cli.ok(`* Merged with ${existing.count()} existing strings`);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (options.clean) {
 | 
					 | 
				
			||||||
			const collectionCount = collection.count();
 | 
					 | 
				
			||||||
			collection = collection.intersect(extracted);
 | 
					 | 
				
			||||||
			const removeCount = collectionCount - collection.count();
 | 
					 | 
				
			||||||
			if (removeCount > 0) {
 | 
					 | 
				
			||||||
				cli.ok(`* Removed ${removeCount} obsolete strings`);
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (options.sort) {
 | 
					 | 
				
			||||||
		collection = collection.sort();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	fs.writeFileSync(outputPath, compiler.compile(collection));
 | 
					 | 
				
			||||||
	cli.ok(`* Saved to '${outputPath}'`);
 | 
					 | 
				
			||||||
} catch (e) {
 | 
					 | 
				
			||||||
	cli.fatal(e.toString());
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										181
									
								
								src/cli/tasks/extract.task.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										181
									
								
								src/cli/tasks/extract.task.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,181 @@
 | 
				
			|||||||
 | 
					import { TranslationCollection } from '../../utils/translation.collection';
 | 
				
			||||||
 | 
					import { TaskInterface } from './task.interface';
 | 
				
			||||||
 | 
					import { ParserInterface } from '../../parsers/parser.interface';
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from '../../post-processors/post-processor.interface';
 | 
				
			||||||
 | 
					import { CompilerInterface } from '../../compilers/compiler.interface';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { cyan, green, bold, dim, red } from 'colorette';
 | 
				
			||||||
 | 
					import * as glob from 'glob';
 | 
				
			||||||
 | 
					import * as fs from 'fs';
 | 
				
			||||||
 | 
					import * as path from 'path';
 | 
				
			||||||
 | 
					import * as mkdirp from 'mkdirp';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface ExtractTaskOptionsInterface {
 | 
				
			||||||
 | 
						replace?: boolean;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class ExtractTask implements TaskInterface {
 | 
				
			||||||
 | 
						protected options: ExtractTaskOptionsInterface = {
 | 
				
			||||||
 | 
							replace: false
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected parsers: ParserInterface[] = [];
 | 
				
			||||||
 | 
						protected postProcessors: PostProcessorInterface[] = [];
 | 
				
			||||||
 | 
						protected compiler: CompilerInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public constructor(protected inputs: string[], protected outputs: string[], options?: ExtractTaskOptionsInterface) {
 | 
				
			||||||
 | 
							this.inputs = inputs.map((input) => path.resolve(input));
 | 
				
			||||||
 | 
							this.outputs = outputs.map((output) => path.resolve(output));
 | 
				
			||||||
 | 
							this.options = { ...this.options, ...options };
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public execute(): void {
 | 
				
			||||||
 | 
							if (!this.compiler) {
 | 
				
			||||||
 | 
								throw new Error('No compiler configured');
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							this.printEnabledParsers();
 | 
				
			||||||
 | 
							this.printEnabledPostProcessors();
 | 
				
			||||||
 | 
							this.printEnabledCompiler();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							this.out(bold('Extracting:'));
 | 
				
			||||||
 | 
							const extracted = this.extract();
 | 
				
			||||||
 | 
							this.out(green(`\nFound %d strings.\n`), extracted.count());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							this.out(bold('Saving:'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							this.outputs.forEach((output) => {
 | 
				
			||||||
 | 
								let dir: string = output;
 | 
				
			||||||
 | 
								let filename: string = `strings.${this.compiler.extension}`;
 | 
				
			||||||
 | 
								if (!fs.existsSync(output) || !fs.statSync(output).isDirectory()) {
 | 
				
			||||||
 | 
									dir = path.dirname(output);
 | 
				
			||||||
 | 
									filename = path.basename(output);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								const outputPath: string = path.join(dir, filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								let existing: TranslationCollection = new TranslationCollection();
 | 
				
			||||||
 | 
								if (!this.options.replace && fs.existsSync(outputPath)) {
 | 
				
			||||||
 | 
									try {
 | 
				
			||||||
 | 
										existing = this.compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
 | 
				
			||||||
 | 
									} catch (e) {
 | 
				
			||||||
 | 
										this.out(`%s %s`, dim(`- ${outputPath}`), red(`[ERROR]`));
 | 
				
			||||||
 | 
										throw e;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// merge extracted strings with existing
 | 
				
			||||||
 | 
								const draft = extracted.union(existing);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// Run collection through post processors
 | 
				
			||||||
 | 
								const final = this.process(draft, extracted, existing);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// Save
 | 
				
			||||||
 | 
								try {
 | 
				
			||||||
 | 
									let event = 'CREATED';
 | 
				
			||||||
 | 
									if (fs.existsSync(outputPath)) {
 | 
				
			||||||
 | 
										this.options.replace ? (event = 'REPLACED') : (event = 'MERGED');
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									this.save(outputPath, final);
 | 
				
			||||||
 | 
									this.out(`%s %s`, dim(`- ${outputPath}`), green(`[${event}]`));
 | 
				
			||||||
 | 
								} catch (e) {
 | 
				
			||||||
 | 
									this.out(`%s %s`, dim(`- ${outputPath}`), red(`[ERROR]`));
 | 
				
			||||||
 | 
									throw e;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public setParsers(parsers: ParserInterface[]): this {
 | 
				
			||||||
 | 
							this.parsers = parsers;
 | 
				
			||||||
 | 
							return this;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public setPostProcessors(postProcessors: PostProcessorInterface[]): this {
 | 
				
			||||||
 | 
							this.postProcessors = postProcessors;
 | 
				
			||||||
 | 
							return this;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public setCompiler(compiler: CompilerInterface): this {
 | 
				
			||||||
 | 
							this.compiler = compiler;
 | 
				
			||||||
 | 
							return this;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Extract strings from specified input dirs using configured parsers
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected extract(): TranslationCollection {
 | 
				
			||||||
 | 
							let collection: TranslationCollection = new TranslationCollection();
 | 
				
			||||||
 | 
							this.inputs.forEach((pattern) => {
 | 
				
			||||||
 | 
								this.getFiles(pattern).forEach((filePath) => {
 | 
				
			||||||
 | 
									this.out(dim('- %s'), filePath);
 | 
				
			||||||
 | 
									const contents: string = fs.readFileSync(filePath, 'utf-8');
 | 
				
			||||||
 | 
									this.parsers.forEach((parser) => {
 | 
				
			||||||
 | 
										const extracted = parser.extract(contents, filePath);
 | 
				
			||||||
 | 
										if (extracted instanceof TranslationCollection) {
 | 
				
			||||||
 | 
											collection = collection.union(extracted);
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									});
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							return collection;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Run strings through configured post processors
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
 | 
				
			||||||
 | 
							this.postProcessors.forEach((postProcessor) => {
 | 
				
			||||||
 | 
								draft = postProcessor.process(draft, extracted, existing);
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							return draft;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Compile and save translations
 | 
				
			||||||
 | 
						 * @param collection
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected save(output: string, collection: TranslationCollection): void {
 | 
				
			||||||
 | 
							const dir = path.dirname(output);
 | 
				
			||||||
 | 
							if (!fs.existsSync(dir)) {
 | 
				
			||||||
 | 
								mkdirp.sync(dir);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							fs.writeFileSync(output, this.compiler.compile(collection));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Get all files matching pattern
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected getFiles(pattern: string): string[] {
 | 
				
			||||||
 | 
							return glob.sync(pattern).filter((filePath) => fs.statSync(filePath).isFile());
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected out(...args: any[]): void {
 | 
				
			||||||
 | 
							console.log.apply(this, arguments);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected printEnabledParsers(): void {
 | 
				
			||||||
 | 
							this.out(cyan('Enabled parsers:'));
 | 
				
			||||||
 | 
							if (this.parsers.length) {
 | 
				
			||||||
 | 
								this.out(cyan(dim(this.parsers.map((parser) => `- ${parser.constructor.name}`).join('\n'))));
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								this.out(cyan(dim('(none)')));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							this.out();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected printEnabledPostProcessors(): void {
 | 
				
			||||||
 | 
							this.out(cyan('Enabled post processors:'));
 | 
				
			||||||
 | 
							if (this.postProcessors.length) {
 | 
				
			||||||
 | 
								this.out(cyan(dim(this.postProcessors.map((postProcessor) => `- ${postProcessor.constructor.name}`).join('\n'))));
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								this.out(cyan(dim('(none)')));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							this.out();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected printEnabledCompiler(): void {
 | 
				
			||||||
 | 
							this.out(cyan('Compiler:'));
 | 
				
			||||||
 | 
							this.out(cyan(dim(`- ${this.compiler.constructor.name}`)));
 | 
				
			||||||
 | 
							this.out();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										3
									
								
								src/cli/tasks/task.interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/cli/tasks/task.interface.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					export interface TaskInterface {
 | 
				
			||||||
 | 
						execute(): void;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										19
									
								
								src/compilers/compiler.factory.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/compilers/compiler.factory.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					import { CompilerInterface } from '../compilers/compiler.interface';
 | 
				
			||||||
 | 
					import { JsonCompiler } from '../compilers/json.compiler';
 | 
				
			||||||
 | 
					import { NamespacedJsonCompiler } from '../compilers/namespaced-json.compiler';
 | 
				
			||||||
 | 
					import { PoCompiler } from '../compilers/po.compiler';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class CompilerFactory {
 | 
				
			||||||
 | 
						public static create(format: string, options?: {}): CompilerInterface {
 | 
				
			||||||
 | 
							switch (format) {
 | 
				
			||||||
 | 
								case 'pot':
 | 
				
			||||||
 | 
									return new PoCompiler(options);
 | 
				
			||||||
 | 
								case 'json':
 | 
				
			||||||
 | 
									return new JsonCompiler(options);
 | 
				
			||||||
 | 
								case 'namespaced-json':
 | 
				
			||||||
 | 
									return new NamespacedJsonCompiler(options);
 | 
				
			||||||
 | 
								default:
 | 
				
			||||||
 | 
									throw new Error(`Unknown format: ${format}`);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,9 +1,9 @@
 | 
				
			|||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface CompilerInterface {
 | 
					export interface CompilerInterface {
 | 
				
			||||||
 | 
						extension: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	compile(collection: TranslationCollection): string;
 | 
						compile(collection: TranslationCollection): string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	parse(contents: string): TranslationCollection;
 | 
						parse(contents: string): TranslationCollection;
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,33 @@
 | 
				
			|||||||
import { CompilerInterface } from './compiler.interface';
 | 
					import { CompilerInterface } from './compiler.interface';
 | 
				
			||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { stripBOM } from '../utils/utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { flatten } from 'flat';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class JsonCompiler implements CompilerInterface {
 | 
					export class JsonCompiler implements CompilerInterface {
 | 
				
			||||||
 | 
						public indentation: string = '\t';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public extension: string = 'json';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public constructor(options?: any) {
 | 
				
			||||||
 | 
							if (options && typeof options.indentation !== 'undefined') {
 | 
				
			||||||
 | 
								this.indentation = options.indentation;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public compile(collection: TranslationCollection): string {
 | 
						public compile(collection: TranslationCollection): string {
 | 
				
			||||||
		return JSON.stringify(collection.values, null, '\t');
 | 
							return JSON.stringify(collection.values, null, this.indentation);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public parse(contents: string): TranslationCollection {
 | 
						public parse(contents: string): TranslationCollection {
 | 
				
			||||||
		return new TranslationCollection(JSON.parse(contents));
 | 
							let values: any = JSON.parse(stripBOM(contents));
 | 
				
			||||||
 | 
							if (this.isNamespacedJsonFormat(values)) {
 | 
				
			||||||
 | 
								values = flatten(values);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return new TranslationCollection(values);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected isNamespacedJsonFormat(values: any): boolean {
 | 
				
			||||||
 | 
							return Object.keys(values).some((key) => typeof values[key] === 'object');
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,18 +1,29 @@
 | 
				
			|||||||
import { CompilerInterface } from './compiler.interface';
 | 
					import { CompilerInterface } from './compiler.interface';
 | 
				
			||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { stripBOM } from '../utils/utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import * as flat from 'flat';
 | 
					import { flatten, unflatten } from 'flat';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class NamespacedJsonCompiler implements CompilerInterface {
 | 
					export class NamespacedJsonCompiler implements CompilerInterface {
 | 
				
			||||||
 | 
						public indentation: string = '\t';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public extension = 'json';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public constructor(options?: any) {
 | 
				
			||||||
 | 
							if (options && typeof options.indentation !== 'undefined') {
 | 
				
			||||||
 | 
								this.indentation = options.indentation;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public compile(collection: TranslationCollection): string {
 | 
						public compile(collection: TranslationCollection): string {
 | 
				
			||||||
		const values = flat.unflatten(collection.values);
 | 
							const values: {} = unflatten(collection.values, {
 | 
				
			||||||
		return JSON.stringify(values, null, '\t');
 | 
								object: true
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							return JSON.stringify(values, null, this.indentation);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public parse(contents: string): TranslationCollection {
 | 
						public parse(contents: string): TranslationCollection {
 | 
				
			||||||
		const values = flat.flatten(JSON.parse(contents));
 | 
							const values: {} = flatten(JSON.parse(stripBOM(contents)));
 | 
				
			||||||
		return new TranslationCollection(values);
 | 
							return new TranslationCollection(values);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,17 @@
 | 
				
			|||||||
import { CompilerInterface } from './compiler.interface';
 | 
					import { CompilerInterface } from './compiler.interface';
 | 
				
			||||||
import { TranslationCollection, TranslationType } from '../utils/translation.collection';
 | 
					import { TranslationCollection, TranslationType } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import * as gettext from 'gettext-parser';
 | 
					import { po } from 'gettext-parser';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class PoCompiler implements CompilerInterface {
 | 
					export class PoCompiler implements CompilerInterface {
 | 
				
			||||||
 | 
						public extension: string = 'po';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Translation domain
 | 
						 * Translation domain
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public domain = '';
 | 
						public domain: string = '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public constructor(options?: any) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public compile(collection: TranslationCollection): string {
 | 
						public compile(collection: TranslationCollection): string {
 | 
				
			||||||
		const data = {
 | 
							const data = {
 | 
				
			||||||
@@ -20,34 +23,38 @@ export class PoCompiler implements CompilerInterface {
 | 
				
			|||||||
			},
 | 
								},
 | 
				
			||||||
			translations: {
 | 
								translations: {
 | 
				
			||||||
				[this.domain]: Object.keys(collection.values).reduce((translations, key) => {
 | 
									[this.domain]: Object.keys(collection.values).reduce((translations, key) => {
 | 
				
			||||||
					translations[key] = {
 | 
										return {
 | 
				
			||||||
 | 
											...translations,
 | 
				
			||||||
 | 
											[key]: {
 | 
				
			||||||
							msgid: key,
 | 
												msgid: key,
 | 
				
			||||||
							msgstr: collection.get(key)
 | 
												msgstr: collection.get(key)
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
					};
 | 
										};
 | 
				
			||||||
					return translations;
 | 
									}, {} as any)
 | 
				
			||||||
				}, <any> {})
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		};
 | 
							};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return gettext.po.compile(data, 'utf-8');
 | 
							return po.compile(data).toString('utf8');
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public parse(contents: string): TranslationCollection {
 | 
						public parse(contents: string): TranslationCollection {
 | 
				
			||||||
		const collection = new TranslationCollection();
 | 
							const collection = new TranslationCollection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		const po = gettext.po.parse(contents, 'utf-8');
 | 
							const parsedPo = po.parse(contents, 'utf8');
 | 
				
			||||||
		if (!po.translations.hasOwnProperty(this.domain)) {
 | 
					
 | 
				
			||||||
 | 
							if (!parsedPo.translations.hasOwnProperty(this.domain)) {
 | 
				
			||||||
			return collection;
 | 
								return collection;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		const values = Object.keys(po.translations[this.domain])
 | 
							const values = Object.keys(parsedPo.translations[this.domain])
 | 
				
			||||||
			.filter(key => key.length > 0)
 | 
								.filter((key) => key.length > 0)
 | 
				
			||||||
			.reduce((values, key) => {
 | 
								.reduce((result, key) => {
 | 
				
			||||||
				values[key] = po.translations[this.domain][key].msgstr.pop();
 | 
									return {
 | 
				
			||||||
				return values;
 | 
										...result,
 | 
				
			||||||
			}, <TranslationType> {});
 | 
										[key]: parsedPo.translations[this.domain][key].msgstr.pop()
 | 
				
			||||||
 | 
									};
 | 
				
			||||||
 | 
								}, {} as TranslationType);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return new TranslationCollection(values);
 | 
							return new TranslationCollection(values);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										3
									
								
								src/declarations.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								src/declarations.d.ts
									
									
									
									
										vendored
									
									
								
							@@ -1,3 +0,0 @@
 | 
				
			|||||||
declare module 'cli';
 | 
					 | 
				
			||||||
declare module 'flat';
 | 
					 | 
				
			||||||
declare module 'gettext-parser';
 | 
					 | 
				
			||||||
							
								
								
									
										15
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/index.ts
									
									
									
									
									
								
							@@ -1,12 +1,23 @@
 | 
				
			|||||||
export * from './utils/extractor';
 | 
					 | 
				
			||||||
export * from './utils/translation.collection';
 | 
					export * from './utils/translation.collection';
 | 
				
			||||||
 | 
					export * from './utils/utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export * from './cli/cli';
 | 
				
			||||||
 | 
					export * from './cli/tasks/task.interface';
 | 
				
			||||||
 | 
					export * from './cli/tasks/extract.task';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export * from './parsers/parser.interface';
 | 
					export * from './parsers/parser.interface';
 | 
				
			||||||
export * from './parsers/abstract-template.parser';
 | 
					 | 
				
			||||||
export * from './parsers/directive.parser';
 | 
					export * from './parsers/directive.parser';
 | 
				
			||||||
export * from './parsers/pipe.parser';
 | 
					export * from './parsers/pipe.parser';
 | 
				
			||||||
export * from './parsers/service.parser';
 | 
					export * from './parsers/service.parser';
 | 
				
			||||||
 | 
					export * from './parsers/marker.parser';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export * from './compilers/compiler.interface';
 | 
					export * from './compilers/compiler.interface';
 | 
				
			||||||
 | 
					export * from './compilers/compiler.factory';
 | 
				
			||||||
export * from './compilers/json.compiler';
 | 
					export * from './compilers/json.compiler';
 | 
				
			||||||
 | 
					export * from './compilers/namespaced-json.compiler';
 | 
				
			||||||
export * from './compilers/po.compiler';
 | 
					export * from './compilers/po.compiler';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export * from './post-processors/post-processor.interface';
 | 
				
			||||||
 | 
					export * from './post-processors/key-as-default-value.post-processor';
 | 
				
			||||||
 | 
					export * from './post-processors/purge-obsolete-keys.post-processor';
 | 
				
			||||||
 | 
					export * from './post-processors/sort-by-key.post-processor';
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,24 +0,0 @@
 | 
				
			|||||||
export abstract class AbstractTemplateParser {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Checks if file is of type javascript or typescript and
 | 
					 | 
				
			||||||
	 * makes the assumption that it is an Angular Component
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _isAngularComponent(path: string): boolean {
 | 
					 | 
				
			||||||
		return (/\.ts|js$/i).test(path);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Extracts inline template from components
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _extractInlineTemplate(contents: string): string {
 | 
					 | 
				
			||||||
		const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/;
 | 
					 | 
				
			||||||
		const match = regExp.exec(contents);
 | 
					 | 
				
			||||||
		if (match !== null) {
 | 
					 | 
				
			||||||
			return match[2];
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return '';
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,159 +0,0 @@
 | 
				
			|||||||
import { ParserInterface } from './parser.interface';
 | 
					 | 
				
			||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					 | 
				
			||||||
import { syntaxKindToName } from '../utils/ast-utils';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import * as ts from 'typescript';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export class AstServiceParser implements ParserInterface {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	protected _sourceFile: ts.SourceFile;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	protected _instancePropertyName: any;
 | 
					 | 
				
			||||||
	protected _serviceClassName: string = 'TranslateService';
 | 
					 | 
				
			||||||
	protected _serviceMethodNames: string[] = ['get', 'instant'];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public extract(contents: string, path?: string): TranslationCollection {
 | 
					 | 
				
			||||||
		let collection: TranslationCollection = new TranslationCollection();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		this._sourceFile = this._createSourceFile(path, contents);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		this._instancePropertyName = this._getInstancePropertyName();
 | 
					 | 
				
			||||||
		if (!this._instancePropertyName) {
 | 
					 | 
				
			||||||
			return collection;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		const callNodes = this._findCallNodes();
 | 
					 | 
				
			||||||
		callNodes.forEach(callNode => {
 | 
					 | 
				
			||||||
			const keys: string[] = this._getCallArgStrings(callNode);
 | 
					 | 
				
			||||||
			if (keys && keys.length) {
 | 
					 | 
				
			||||||
				collection = collection.addKeys(keys);
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return collection;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	protected _createSourceFile(path: string, contents: string): ts.SourceFile {
 | 
					 | 
				
			||||||
		return ts.createSourceFile(path, contents, null, /*setParentNodes */ false);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Detect what the TranslateService instance property
 | 
					 | 
				
			||||||
	 * is called by inspecting constructor params
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _getInstancePropertyName(): string {
 | 
					 | 
				
			||||||
		const constructorNode = this._findConstructorNode();
 | 
					 | 
				
			||||||
		if (!constructorNode) {
 | 
					 | 
				
			||||||
			return null;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		const result = constructorNode.parameters.find(parameter => {
 | 
					 | 
				
			||||||
			// Skip if visibility modifier is not present (we want it set as an instance property)
 | 
					 | 
				
			||||||
			if (!parameter.modifiers) {
 | 
					 | 
				
			||||||
				return false;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			// Make sure className is of the correct type
 | 
					 | 
				
			||||||
			const className: string = ((parameter.type as ts.TypeReferenceNode).typeName as ts.Identifier).text;
 | 
					 | 
				
			||||||
			if (className !== this._serviceClassName) {
 | 
					 | 
				
			||||||
				return false;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return true;
 | 
					 | 
				
			||||||
		});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (result) {
 | 
					 | 
				
			||||||
			return (result.name as ts.Identifier).text;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Find first constructor
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _findConstructorNode(): ts.ConstructorDeclaration {
 | 
					 | 
				
			||||||
		const constructors = this._findNodes(this._sourceFile, ts.SyntaxKind.Constructor, true) as ts.ConstructorDeclaration[];
 | 
					 | 
				
			||||||
		if (constructors.length) {
 | 
					 | 
				
			||||||
			return constructors[0];
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Find all calls to TranslateService methods
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _findCallNodes(node?: ts.Node): ts.CallExpression[] {
 | 
					 | 
				
			||||||
		if (!node) {
 | 
					 | 
				
			||||||
			node = this._sourceFile;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		let callNodes = this._findNodes(node, ts.SyntaxKind.CallExpression) as ts.CallExpression[];
 | 
					 | 
				
			||||||
		callNodes = callNodes
 | 
					 | 
				
			||||||
			// Only call expressions with arguments
 | 
					 | 
				
			||||||
			.filter(callNode => callNode.arguments.length > 0)
 | 
					 | 
				
			||||||
			// More filters
 | 
					 | 
				
			||||||
			.filter(callNode => {
 | 
					 | 
				
			||||||
				const propAccess = callNode.getChildAt(0).getChildAt(0) as ts.PropertyAccessExpression;
 | 
					 | 
				
			||||||
				if (!propAccess || propAccess.kind !== ts.SyntaxKind.PropertyAccessExpression) {
 | 
					 | 
				
			||||||
					return false;
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				if (!propAccess.getFirstToken() || propAccess.getFirstToken().kind !== ts.SyntaxKind.ThisKeyword) {
 | 
					 | 
				
			||||||
					return false;
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				if (propAccess.name.text !== this._instancePropertyName) {
 | 
					 | 
				
			||||||
					return false;
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				const methodAccess = callNode.getChildAt(0) as ts.PropertyAccessExpression;
 | 
					 | 
				
			||||||
				if (!methodAccess || methodAccess.kind !== ts.SyntaxKind.PropertyAccessExpression) {
 | 
					 | 
				
			||||||
					return false;
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				if (!methodAccess.name || this._serviceMethodNames.indexOf(methodAccess.name.text) === -1) {
 | 
					 | 
				
			||||||
					return false;
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				return true;
 | 
					 | 
				
			||||||
			});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return callNodes;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Get strings from function call's first argument
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _getCallArgStrings(callNode: ts.CallExpression): string[] {
 | 
					 | 
				
			||||||
		if (!callNode.arguments.length) {
 | 
					 | 
				
			||||||
			return;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		const firstArg = callNode.arguments[0];
 | 
					 | 
				
			||||||
		switch (firstArg.kind) {
 | 
					 | 
				
			||||||
			case ts.SyntaxKind.StringLiteral:
 | 
					 | 
				
			||||||
			case ts.SyntaxKind.FirstTemplateToken:
 | 
					 | 
				
			||||||
				return [(firstArg as ts.StringLiteral).text];
 | 
					 | 
				
			||||||
			case ts.SyntaxKind.ArrayLiteralExpression:
 | 
					 | 
				
			||||||
				return (firstArg as ts.ArrayLiteralExpression).elements
 | 
					 | 
				
			||||||
					.map((element: ts.StringLiteral) => element.text);
 | 
					 | 
				
			||||||
			case ts.SyntaxKind.Identifier:
 | 
					 | 
				
			||||||
				console.log('WARNING: We cannot extract variable values passed to TranslateService (yet)');
 | 
					 | 
				
			||||||
				break;
 | 
					 | 
				
			||||||
			default:
 | 
					 | 
				
			||||||
				console.log(`SKIP: Unknown argument type: '${syntaxKindToName(firstArg.kind)}'`, firstArg);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Find all child nodes of a kind
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _findNodes(node: ts.Node, kind: ts.SyntaxKind, onlyOne: boolean = false): ts.Node[] {
 | 
					 | 
				
			||||||
		if (node.kind === kind && onlyOne) {
 | 
					 | 
				
			||||||
			return [node];
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		const childrenNodes: ts.Node[] = node.getChildren(this._sourceFile);
 | 
					 | 
				
			||||||
		const initialValue: ts.Node[] = node.kind === kind ? [node] : [];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return childrenNodes.reduce((result: ts.Node[], childNode: ts.Node) => {
 | 
					 | 
				
			||||||
			return result.concat(this._findNodes(childNode, kind));
 | 
					 | 
				
			||||||
		}, initialValue);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,55 +1,180 @@
 | 
				
			|||||||
 | 
					import {
 | 
				
			||||||
 | 
						parseTemplate,
 | 
				
			||||||
 | 
						TmplAstNode as Node,
 | 
				
			||||||
 | 
						TmplAstElement as Element,
 | 
				
			||||||
 | 
						TmplAstText as Text,
 | 
				
			||||||
 | 
						TmplAstTemplate as Template,
 | 
				
			||||||
 | 
						TmplAstTextAttribute as TextAttribute,
 | 
				
			||||||
 | 
						TmplAstBoundAttribute as BoundAttribute,
 | 
				
			||||||
 | 
						AST,
 | 
				
			||||||
 | 
						ASTWithSource,
 | 
				
			||||||
 | 
						LiteralPrimitive,
 | 
				
			||||||
 | 
						Conditional,
 | 
				
			||||||
 | 
						Binary,
 | 
				
			||||||
 | 
						BindingPipe,
 | 
				
			||||||
 | 
						Interpolation,
 | 
				
			||||||
 | 
						LiteralArray,
 | 
				
			||||||
 | 
						LiteralMap
 | 
				
			||||||
 | 
					} from '@angular/compiler';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { ParserInterface } from './parser.interface';
 | 
					import { ParserInterface } from './parser.interface';
 | 
				
			||||||
import { AbstractTemplateParser } from './abstract-template.parser';
 | 
					 | 
				
			||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import * as $ from 'cheerio';
 | 
					const TRANSLATE_ATTR_NAME = 'translate';
 | 
				
			||||||
 | 
					type ElementLike = Element | Template;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class DirectiveParser extends AbstractTemplateParser implements ParserInterface {
 | 
					export class DirectiveParser implements ParserInterface {
 | 
				
			||||||
 | 
						public extract(source: string, filePath: string): TranslationCollection | null {
 | 
				
			||||||
	public extract(contents: string, path?: string): TranslationCollection {
 | 
					 | 
				
			||||||
		if (path && this._isAngularComponent(path)) {
 | 
					 | 
				
			||||||
			contents = this._extractInlineTemplate(contents);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return this._parseTemplate(contents);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	protected _parseTemplate(template: string): TranslationCollection {
 | 
					 | 
				
			||||||
		let collection: TranslationCollection = new TranslationCollection();
 | 
							let collection: TranslationCollection = new TranslationCollection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		template = this._normalizeTemplateAttributes(template);
 | 
							if (filePath && isPathAngularComponent(filePath)) {
 | 
				
			||||||
 | 
								source = extractComponentInlineTemplate(source);
 | 
				
			||||||
		const selector = '[translate], [ng2-translate]';
 | 
					 | 
				
			||||||
		$(template)
 | 
					 | 
				
			||||||
			.find(selector)
 | 
					 | 
				
			||||||
			.addBack(selector)
 | 
					 | 
				
			||||||
			.each((i: number, element: CheerioElement) => {
 | 
					 | 
				
			||||||
				const $element = $(element);
 | 
					 | 
				
			||||||
				const attr = $element.attr('translate') || $element.attr('ng2-translate');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				if (attr) {
 | 
					 | 
				
			||||||
					collection = collection.add(attr);
 | 
					 | 
				
			||||||
				} else {
 | 
					 | 
				
			||||||
					$element
 | 
					 | 
				
			||||||
						.contents()
 | 
					 | 
				
			||||||
						.toArray()
 | 
					 | 
				
			||||||
						.filter(node => node.type === 'text')
 | 
					 | 
				
			||||||
						.map(node => node.nodeValue.trim())
 | 
					 | 
				
			||||||
						.filter(text => text.length > 0)
 | 
					 | 
				
			||||||
						.forEach(text => collection = collection.add(text));
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
			});
 | 
							const nodes: Node[] = this.parseTemplate(source, filePath);
 | 
				
			||||||
 | 
							const elements: ElementLike[] = this.getElementsWithTranslateAttribute(nodes);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							elements.forEach((element) => {
 | 
				
			||||||
 | 
								const attribute = this.getAttribute(element, TRANSLATE_ATTR_NAME);
 | 
				
			||||||
 | 
								if (attribute?.value) {
 | 
				
			||||||
 | 
									collection = collection.add(attribute.value);
 | 
				
			||||||
 | 
									return;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								const boundAttribute = this.getBoundAttribute(element, TRANSLATE_ATTR_NAME);
 | 
				
			||||||
 | 
								if (boundAttribute?.value) {
 | 
				
			||||||
 | 
									this.getLiteralPrimitives(boundAttribute.value).forEach((literalPrimitive) => {
 | 
				
			||||||
 | 
										collection = collection.add(literalPrimitive.value);
 | 
				
			||||||
 | 
									});
 | 
				
			||||||
 | 
									return;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								const textNodes = this.getTextNodes(element);
 | 
				
			||||||
 | 
								textNodes.forEach((textNode) => {
 | 
				
			||||||
 | 
									collection = collection.add(textNode.value.trim());
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
		return collection;
 | 
							return collection;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Angular's `[attr]="'val'"` syntax is not valid HTML,
 | 
						 * Find all ElementLike nodes with a translate attribute
 | 
				
			||||||
	 * so it can't be parsed by standard HTML parsers.
 | 
						 * @param nodes
 | 
				
			||||||
	 * This method replaces `[attr]="'val'""` with `attr="val"`
 | 
					 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	protected _normalizeTemplateAttributes(template: string): string {
 | 
						protected getElementsWithTranslateAttribute(nodes: Node[]): ElementLike[] {
 | 
				
			||||||
		return template.replace(/\[([^\]]+)\]="'([^']*)'"/g, '$1="$2"');
 | 
							let elements: ElementLike[] = [];
 | 
				
			||||||
 | 
							nodes.filter(this.isElementLike).forEach((element) => {
 | 
				
			||||||
 | 
								if (this.hasAttribute(element, TRANSLATE_ATTR_NAME)) {
 | 
				
			||||||
 | 
									elements = [...elements, element];
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if (this.hasBoundAttribute(element, TRANSLATE_ATTR_NAME)) {
 | 
				
			||||||
 | 
									elements = [...elements, element];
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								const childElements = this.getElementsWithTranslateAttribute(element.children);
 | 
				
			||||||
 | 
								if (childElements.length) {
 | 
				
			||||||
 | 
									elements = [...elements, ...childElements];
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							return elements;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Get direct child nodes of type Text
 | 
				
			||||||
 | 
						 * @param element
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected getTextNodes(element: ElementLike): Text[] {
 | 
				
			||||||
 | 
							return element.children.filter(this.isText);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Check if attribute is present on element
 | 
				
			||||||
 | 
						 * @param element
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected hasAttribute(element: ElementLike, name: string): boolean {
 | 
				
			||||||
 | 
							return this.getAttribute(element, name) !== undefined;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Get attribute value if present on element
 | 
				
			||||||
 | 
						 * @param element
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected getAttribute(element: ElementLike, name: string): TextAttribute {
 | 
				
			||||||
 | 
							return element.attributes.find((attribute) => attribute.name === name);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Check if bound attribute is present on element
 | 
				
			||||||
 | 
						 * @param element
 | 
				
			||||||
 | 
						 * @param name
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected hasBoundAttribute(element: ElementLike, name: string): boolean {
 | 
				
			||||||
 | 
							return this.getBoundAttribute(element, name) !== undefined;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Get bound attribute if present on element
 | 
				
			||||||
 | 
						 * @param element
 | 
				
			||||||
 | 
						 * @param name
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected getBoundAttribute(element: ElementLike, name: string): BoundAttribute {
 | 
				
			||||||
 | 
							return element.inputs.find((input) => input.name === name);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Get literal primitives from expression
 | 
				
			||||||
 | 
						 * @param exp
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected getLiteralPrimitives(exp: AST): LiteralPrimitive[] {
 | 
				
			||||||
 | 
							if (exp instanceof LiteralPrimitive) {
 | 
				
			||||||
 | 
								return [exp];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							let visit: AST[] = [];
 | 
				
			||||||
 | 
							if (exp instanceof Interpolation) {
 | 
				
			||||||
 | 
								visit = exp.expressions;
 | 
				
			||||||
 | 
							} else if (exp instanceof LiteralArray) {
 | 
				
			||||||
 | 
								visit = exp.expressions;
 | 
				
			||||||
 | 
							} else if (exp instanceof LiteralMap) {
 | 
				
			||||||
 | 
								visit = exp.values;
 | 
				
			||||||
 | 
							} else if (exp instanceof BindingPipe) {
 | 
				
			||||||
 | 
								visit = [exp.exp];
 | 
				
			||||||
 | 
							} else if (exp instanceof Conditional) {
 | 
				
			||||||
 | 
								visit = [exp.trueExp, exp.falseExp];
 | 
				
			||||||
 | 
							} else if (exp instanceof Binary) {
 | 
				
			||||||
 | 
								visit = [exp.left, exp.right];
 | 
				
			||||||
 | 
							} else if (exp instanceof ASTWithSource) {
 | 
				
			||||||
 | 
								visit = [exp.ast];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							let results: LiteralPrimitive[] = [];
 | 
				
			||||||
 | 
							visit.forEach((child) => {
 | 
				
			||||||
 | 
								results = [...results, ...this.getLiteralPrimitives(child)];
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							return results;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Check if node type is ElementLike
 | 
				
			||||||
 | 
						 * @param node
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected isElementLike(node: Node): node is ElementLike {
 | 
				
			||||||
 | 
							return node instanceof Element || node instanceof Template;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Check if node type is Text
 | 
				
			||||||
 | 
						 * @param node
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected isText(node: Node): node is Text {
 | 
				
			||||||
 | 
							return node instanceof Text;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Parse a template into nodes
 | 
				
			||||||
 | 
						 * @param template
 | 
				
			||||||
 | 
						 * @param path
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						protected parseTemplate(template: string, path: string): Node[] {
 | 
				
			||||||
 | 
							return parseTemplate(template, path).nodes;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										32
									
								
								src/parsers/marker.parser.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/parsers/marker.parser.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					import { tsquery } from '@phenomnomnominal/tsquery';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { ParserInterface } from './parser.interface';
 | 
				
			||||||
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { getNamedImportAlias, findFunctionCallExpressions, getStringsFromExpression } from '../utils/ast-helpers';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const MARKER_MODULE_NAME = '@biesbjerg/ngx-translate-extract-marker';
 | 
				
			||||||
 | 
					const MARKER_IMPORT_NAME = 'marker';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class MarkerParser implements ParserInterface {
 | 
				
			||||||
 | 
						public extract(source: string, filePath: string): TranslationCollection | null {
 | 
				
			||||||
 | 
							const sourceFile = tsquery.ast(source, filePath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							const markerImportName = getNamedImportAlias(sourceFile, MARKER_MODULE_NAME, MARKER_IMPORT_NAME);
 | 
				
			||||||
 | 
							if (!markerImportName) {
 | 
				
			||||||
 | 
								return null;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							let collection: TranslationCollection = new TranslationCollection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							const callExpressions = findFunctionCallExpressions(sourceFile, markerImportName);
 | 
				
			||||||
 | 
							callExpressions.forEach((callExpression) => {
 | 
				
			||||||
 | 
								const [firstArg] = callExpression.arguments;
 | 
				
			||||||
 | 
								if (!firstArg) {
 | 
				
			||||||
 | 
									return;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								const strings = getStringsFromExpression(firstArg);
 | 
				
			||||||
 | 
								collection = collection.addKeys(strings);
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							return collection;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,7 +1,5 @@
 | 
				
			|||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface ParserInterface {
 | 
					export interface ParserInterface {
 | 
				
			||||||
 | 
						extract(source: string, filePath: string): TranslationCollection | null;
 | 
				
			||||||
	extract(contents: string, path?: string): TranslationCollection;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,27 +1,163 @@
 | 
				
			|||||||
 | 
					import {
 | 
				
			||||||
 | 
						AST,
 | 
				
			||||||
 | 
						TmplAstNode,
 | 
				
			||||||
 | 
						parseTemplate,
 | 
				
			||||||
 | 
						BindingPipe,
 | 
				
			||||||
 | 
						LiteralPrimitive,
 | 
				
			||||||
 | 
						Conditional,
 | 
				
			||||||
 | 
						TmplAstTextAttribute,
 | 
				
			||||||
 | 
						Binary,
 | 
				
			||||||
 | 
						LiteralMap,
 | 
				
			||||||
 | 
						LiteralArray,
 | 
				
			||||||
 | 
						Interpolation,
 | 
				
			||||||
 | 
						MethodCall
 | 
				
			||||||
 | 
					} from '@angular/compiler';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { ParserInterface } from './parser.interface';
 | 
					import { ParserInterface } from './parser.interface';
 | 
				
			||||||
import { AbstractTemplateParser } from './abstract-template.parser';
 | 
					 | 
				
			||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { isPathAngularComponent, extractComponentInlineTemplate } from '../utils/utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class PipeParser extends AbstractTemplateParser implements ParserInterface {
 | 
					const TRANSLATE_PIPE_NAME = 'translate';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public extract(contents: string, path?: string): TranslationCollection {
 | 
					export class PipeParser implements ParserInterface {
 | 
				
			||||||
		if (path && this._isAngularComponent(path)) {
 | 
						public extract(source: string, filePath: string): TranslationCollection | null {
 | 
				
			||||||
			contents = this._extractInlineTemplate(contents);
 | 
							if (filePath && isPathAngularComponent(filePath)) {
 | 
				
			||||||
 | 
								source = extractComponentInlineTemplate(source);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return this._parseTemplate(contents);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	protected _parseTemplate(template: string): TranslationCollection {
 | 
					 | 
				
			||||||
		let collection: TranslationCollection = new TranslationCollection();
 | 
							let collection: TranslationCollection = new TranslationCollection();
 | 
				
			||||||
 | 
							const nodes: TmplAstNode[] = this.parseTemplate(source, filePath);
 | 
				
			||||||
		const regExp: RegExp = /(['"`])([^>\1\r\n]*?)\1\s*\|\s*translate/g;
 | 
							const pipes: BindingPipe[] = nodes.map((node) => this.findPipesInNode(node)).flat();
 | 
				
			||||||
		let matches: RegExpExecArray;
 | 
							pipes.forEach((pipe) => {
 | 
				
			||||||
		while (matches = regExp.exec(template)) {
 | 
								this.parseTranslationKeysFromPipe(pipe).forEach((key: string) => {
 | 
				
			||||||
			collection = collection.add(matches[2]);
 | 
									collection = collection.add(key);
 | 
				
			||||||
		}
 | 
								});
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
		return collection;
 | 
							return collection;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected findPipesInNode(node: any): BindingPipe[] {
 | 
				
			||||||
 | 
							let ret: BindingPipe[] = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (node?.children) {
 | 
				
			||||||
 | 
								ret = node.children.reduce(
 | 
				
			||||||
 | 
									(result: BindingPipe[], childNode: TmplAstNode) => {
 | 
				
			||||||
 | 
										const children = this.findPipesInNode(childNode);
 | 
				
			||||||
 | 
										return result.concat(children);
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									[ret]
 | 
				
			||||||
 | 
								);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (node?.value?.ast) {
 | 
				
			||||||
 | 
								ret.push(...this.getTranslatablesFromAst(node.value.ast));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (node?.attributes) {
 | 
				
			||||||
 | 
								const translateableAttributes = node.attributes.filter((attr: TmplAstTextAttribute) => {
 | 
				
			||||||
 | 
									return attr.name === TRANSLATE_PIPE_NAME;
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
								ret = [...ret, ...translateableAttributes];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (node?.inputs) {
 | 
				
			||||||
 | 
								node.inputs.forEach((input: any) => {
 | 
				
			||||||
 | 
									// <element [attrib]="'identifier' | translate">
 | 
				
			||||||
 | 
									if (input?.value?.ast) {
 | 
				
			||||||
 | 
										ret.push(...this.getTranslatablesFromAst(input.value.ast));
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return ret;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected parseTranslationKeysFromPipe(pipeContent: BindingPipe | LiteralPrimitive | Conditional): string[] {
 | 
				
			||||||
 | 
							const ret: string[] = [];
 | 
				
			||||||
 | 
							if (pipeContent instanceof LiteralPrimitive) {
 | 
				
			||||||
 | 
								ret.push(pipeContent.value);
 | 
				
			||||||
 | 
							} else if (pipeContent instanceof Conditional) {
 | 
				
			||||||
 | 
								const trueExp: LiteralPrimitive | Conditional = pipeContent.trueExp as any;
 | 
				
			||||||
 | 
								ret.push(...this.parseTranslationKeysFromPipe(trueExp));
 | 
				
			||||||
 | 
								const falseExp: LiteralPrimitive | Conditional = pipeContent.falseExp as any;
 | 
				
			||||||
 | 
								ret.push(...this.parseTranslationKeysFromPipe(falseExp));
 | 
				
			||||||
 | 
							} else if (pipeContent instanceof BindingPipe) {
 | 
				
			||||||
 | 
								ret.push(...this.parseTranslationKeysFromPipe(pipeContent.exp as any));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return ret;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected getTranslatablesFromAst(ast: AST): BindingPipe[] {
 | 
				
			||||||
 | 
							// the entire expression is the translate pipe, e.g.:
 | 
				
			||||||
 | 
							// - 'foo' | translate
 | 
				
			||||||
 | 
							// - (condition ? 'foo' : 'bar') | translate
 | 
				
			||||||
 | 
							if (this.expressionIsOrHasBindingPipe(ast)) {
 | 
				
			||||||
 | 
								return [ast];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// angular double curly bracket interpolation, e.g.:
 | 
				
			||||||
 | 
							// - {{ expressions }}
 | 
				
			||||||
 | 
							if (ast instanceof Interpolation) {
 | 
				
			||||||
 | 
								return this.getTranslatablesFromAsts(ast.expressions);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// ternary operator, e.g.:
 | 
				
			||||||
 | 
							// - condition ? null : ('foo' | translate)
 | 
				
			||||||
 | 
							// - condition ? ('foo' | translate) : null
 | 
				
			||||||
 | 
							if (ast instanceof Conditional) {
 | 
				
			||||||
 | 
								return this.getTranslatablesFromAsts([ast.trueExp, ast.falseExp]);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// string concatenation, e.g.:
 | 
				
			||||||
 | 
							// - 'foo' + 'bar' + ('baz' | translate)
 | 
				
			||||||
 | 
							if (ast instanceof Binary) {
 | 
				
			||||||
 | 
								return this.getTranslatablesFromAsts([ast.left, ast.right]);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// a pipe on the outer expression, but not the translate pipe - ignore the pipe, visit the expression, e.g.:
 | 
				
			||||||
 | 
							// - { foo: 'Hello' | translate } | json
 | 
				
			||||||
 | 
							if (ast instanceof BindingPipe) {
 | 
				
			||||||
 | 
								return this.getTranslatablesFromAst(ast.exp);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// object - ignore the keys, visit all values, e.g.:
 | 
				
			||||||
 | 
							// - { key1: 'value1' | translate, key2: 'value2' | translate }
 | 
				
			||||||
 | 
							if (ast instanceof LiteralMap) {
 | 
				
			||||||
 | 
								return this.getTranslatablesFromAsts(ast.values);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// array - visit all its values, e.g.:
 | 
				
			||||||
 | 
							// - [ 'value1' | translate, 'value2' | translate ]
 | 
				
			||||||
 | 
							if (ast instanceof LiteralArray) {
 | 
				
			||||||
 | 
								return this.getTranslatablesFromAsts(ast.expressions);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (ast instanceof MethodCall) {
 | 
				
			||||||
 | 
								return this.getTranslatablesFromAsts(ast.args);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return [];
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected getTranslatablesFromAsts(asts: AST[]): BindingPipe[] {
 | 
				
			||||||
 | 
							return this.flatten(asts.map((ast) => this.getTranslatablesFromAst(ast)));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected flatten<T extends AST>(array: T[][]): T[] {
 | 
				
			||||||
 | 
							return [].concat(...array);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected expressionIsOrHasBindingPipe(exp: any): exp is BindingPipe {
 | 
				
			||||||
 | 
							if (exp.name && exp.name === TRANSLATE_PIPE_NAME) {
 | 
				
			||||||
 | 
								return true;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if (exp.exp && exp.exp instanceof BindingPipe) {
 | 
				
			||||||
 | 
								return this.expressionIsOrHasBindingPipe(exp.exp);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected parseTemplate(template: string, path: string): TmplAstNode[] {
 | 
				
			||||||
 | 
							return parseTemplate(template, path).nodes;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,59 +1,64 @@
 | 
				
			|||||||
 | 
					import { ClassDeclaration, CallExpression } from 'typescript';
 | 
				
			||||||
 | 
					import { tsquery } from '@phenomnomnominal/tsquery';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { ParserInterface } from './parser.interface';
 | 
					import { ParserInterface } from './parser.interface';
 | 
				
			||||||
import { TranslationCollection } from '../utils/translation.collection';
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import {
 | 
				
			||||||
 | 
						findClassDeclarations,
 | 
				
			||||||
 | 
						findClassPropertyByType,
 | 
				
			||||||
 | 
						findPropertyCallExpressions,
 | 
				
			||||||
 | 
						findMethodCallExpressions,
 | 
				
			||||||
 | 
						getStringsFromExpression,
 | 
				
			||||||
 | 
						findMethodParameterByType,
 | 
				
			||||||
 | 
						findConstructorDeclaration
 | 
				
			||||||
 | 
					} from '../utils/ast-helpers';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const TRANSLATE_SERVICE_TYPE_REFERENCE = 'TranslateService';
 | 
				
			||||||
 | 
					const TRANSLATE_SERVICE_METHOD_NAMES = ['get', 'instant', 'stream'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class ServiceParser implements ParserInterface {
 | 
					export class ServiceParser implements ParserInterface {
 | 
				
			||||||
 | 
						public extract(source: string, filePath: string): TranslationCollection | null {
 | 
				
			||||||
 | 
							const sourceFile = tsquery.ast(source, filePath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							const classDeclarations = findClassDeclarations(sourceFile);
 | 
				
			||||||
 | 
							if (!classDeclarations) {
 | 
				
			||||||
 | 
								return null;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public extract(contents: string, path?: string): TranslationCollection {
 | 
					 | 
				
			||||||
		let collection: TranslationCollection = new TranslationCollection();
 | 
							let collection: TranslationCollection = new TranslationCollection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		const translateServiceVar = this._extractTranslateServiceVar(contents);
 | 
							classDeclarations.forEach((classDeclaration) => {
 | 
				
			||||||
		if (!translateServiceVar) {
 | 
								const callExpressions = [
 | 
				
			||||||
 | 
									...this.findConstructorParamCallExpressions(classDeclaration),
 | 
				
			||||||
 | 
									...this.findPropertyCallExpressions(classDeclaration)
 | 
				
			||||||
 | 
								];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								callExpressions.forEach((callExpression) => {
 | 
				
			||||||
 | 
									const [firstArg] = callExpression.arguments;
 | 
				
			||||||
 | 
									if (!firstArg) {
 | 
				
			||||||
 | 
										return;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									const strings = getStringsFromExpression(firstArg);
 | 
				
			||||||
 | 
									collection = collection.addKeys(strings);
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
		return collection;
 | 
							return collection;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		const methodRegExp: RegExp = /(?:get|instant)\s*\(\s*(\[?\s*(['"`])([^\1\r\n]*)\2\s*\]?)/;
 | 
						protected findConstructorParamCallExpressions(classDeclaration: ClassDeclaration): CallExpression[] {
 | 
				
			||||||
		const regExp: RegExp = new RegExp(`\\.${translateServiceVar}\\.${methodRegExp.source}`, 'g');
 | 
							const constructorDeclaration = findConstructorDeclaration(classDeclaration);
 | 
				
			||||||
 | 
							if (!constructorDeclaration) {
 | 
				
			||||||
		let matches: RegExpExecArray;
 | 
					 | 
				
			||||||
		while (matches = regExp.exec(contents)) {
 | 
					 | 
				
			||||||
			if (this._stringContainsArray(matches[1])) {
 | 
					 | 
				
			||||||
				collection = collection.addKeys(this._stringToArray(matches[1]));
 | 
					 | 
				
			||||||
			} else {
 | 
					 | 
				
			||||||
				collection = collection.add(matches[3]);
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return collection;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Extracts name of TranslateService variable for use in patterns
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _extractTranslateServiceVar(contents: string): string {
 | 
					 | 
				
			||||||
		const matches = contents.match(/([a-z0-9_]+)\s*:\s*TranslateService/i);
 | 
					 | 
				
			||||||
		if (matches === null) {
 | 
					 | 
				
			||||||
			return '';
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return matches[1];
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Checks if string contains an array
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _stringContainsArray(input: string): boolean {
 | 
					 | 
				
			||||||
		return input.startsWith('[') && input.endsWith(']');
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Converts string to array
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _stringToArray(input: string): string[] {
 | 
					 | 
				
			||||||
		if (this._stringContainsArray(input)) {
 | 
					 | 
				
			||||||
			return eval(input);
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return [];
 | 
								return [];
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							const paramName = findMethodParameterByType(constructorDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
 | 
				
			||||||
 | 
							return findMethodCallExpressions(constructorDeclaration, paramName, TRANSLATE_SERVICE_METHOD_NAMES);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						protected findPropertyCallExpressions(classDeclaration: ClassDeclaration): CallExpression[] {
 | 
				
			||||||
 | 
							const propName: string = findClassPropertyByType(classDeclaration, TRANSLATE_SERVICE_TYPE_REFERENCE);
 | 
				
			||||||
 | 
							if (!propName) {
 | 
				
			||||||
 | 
								return [];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return findPropertyCallExpressions(classDeclaration, propName, TRANSLATE_SERVICE_METHOD_NAMES);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								src/post-processors/key-as-default-value.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/post-processors/key-as-default-value.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from './post-processor.interface';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class KeyAsDefaultValuePostProcessor implements PostProcessorInterface {
 | 
				
			||||||
 | 
						public name: string = 'KeyAsDefaultValue';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
 | 
				
			||||||
 | 
							return draft.map((key, val) => (val === '' ? key : val));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										10
									
								
								src/post-processors/null-as-default-value.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/post-processors/null-as-default-value.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from './post-processor.interface';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class NullAsDefaultValuePostProcessor implements PostProcessorInterface {
 | 
				
			||||||
 | 
						public name: string = 'NullAsDefaultValue';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
 | 
				
			||||||
 | 
							return draft.map((key, val) => (existing.get(key) === undefined ? null : val));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										7
									
								
								src/post-processors/post-processor.interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/post-processors/post-processor.interface.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface PostProcessorInterface {
 | 
				
			||||||
 | 
						name: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										10
									
								
								src/post-processors/purge-obsolete-keys.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/post-processors/purge-obsolete-keys.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from './post-processor.interface';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class PurgeObsoleteKeysPostProcessor implements PostProcessorInterface {
 | 
				
			||||||
 | 
						public name: string = 'PurgeObsoleteKeys';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
 | 
				
			||||||
 | 
							return draft.intersect(extracted);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										10
									
								
								src/post-processors/sort-by-key.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/post-processors/sort-by-key.post-processor.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from './post-processor.interface';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class SortByKeyPostProcessor implements PostProcessorInterface {
 | 
				
			||||||
 | 
						public name: string = 'SortByKey';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
 | 
				
			||||||
 | 
							return draft.sort();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					import { TranslationCollection } from '../utils/translation.collection';
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from './post-processor.interface';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					interface Options {
 | 
				
			||||||
 | 
						defaultValue: string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class StringAsDefaultValuePostProcessor implements PostProcessorInterface {
 | 
				
			||||||
 | 
						public name: string = 'StringAsDefaultValue';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public constructor(protected options: Options) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
 | 
				
			||||||
 | 
							return draft.map((key, val) => (existing.get(key) === undefined ? this.options.defaultValue : val));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										157
									
								
								src/utils/ast-helpers.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										157
									
								
								src/utils/ast-helpers.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,157 @@
 | 
				
			|||||||
 | 
					import { tsquery } from '@phenomnomnominal/tsquery';
 | 
				
			||||||
 | 
					import {
 | 
				
			||||||
 | 
						SyntaxKind,
 | 
				
			||||||
 | 
						Node,
 | 
				
			||||||
 | 
						NamedImports,
 | 
				
			||||||
 | 
						Identifier,
 | 
				
			||||||
 | 
						ClassDeclaration,
 | 
				
			||||||
 | 
						ConstructorDeclaration,
 | 
				
			||||||
 | 
						isStringLiteralLike,
 | 
				
			||||||
 | 
						isArrayLiteralExpression,
 | 
				
			||||||
 | 
						CallExpression,
 | 
				
			||||||
 | 
						Expression,
 | 
				
			||||||
 | 
						isBinaryExpression,
 | 
				
			||||||
 | 
						isConditionalExpression,
 | 
				
			||||||
 | 
						PropertyAccessExpression
 | 
				
			||||||
 | 
					} from 'typescript';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function getNamedImports(node: Node, moduleName: string): NamedImports[] {
 | 
				
			||||||
 | 
						const query = `ImportDeclaration[moduleSpecifier.text="${moduleName}"] NamedImports`;
 | 
				
			||||||
 | 
						return tsquery<NamedImports>(node, query);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function getNamedImportAlias(node: Node, moduleName: string, importName: string): string | null {
 | 
				
			||||||
 | 
						const [namedImportNode] = getNamedImports(node, moduleName);
 | 
				
			||||||
 | 
						if (!namedImportNode) {
 | 
				
			||||||
 | 
							return null;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const query = `ImportSpecifier:has(Identifier[name="${importName}"]) > Identifier`;
 | 
				
			||||||
 | 
						const identifiers = tsquery<Identifier>(namedImportNode, query);
 | 
				
			||||||
 | 
						if (identifiers.length === 1) {
 | 
				
			||||||
 | 
							return identifiers[0].text;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (identifiers.length > 1) {
 | 
				
			||||||
 | 
							return identifiers[identifiers.length - 1].text;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return null;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findClassDeclarations(node: Node): ClassDeclaration[] {
 | 
				
			||||||
 | 
						const query = 'ClassDeclaration';
 | 
				
			||||||
 | 
						return tsquery<ClassDeclaration>(node, query);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findClassPropertyByType(node: ClassDeclaration, type: string): string | null {
 | 
				
			||||||
 | 
						return findClassPropertyConstructorParameterByType(node, type) || findClassPropertyDeclarationByType(node, type);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findConstructorDeclaration(node: ClassDeclaration): ConstructorDeclaration {
 | 
				
			||||||
 | 
						const query = `Constructor`;
 | 
				
			||||||
 | 
						const [result] = tsquery<ConstructorDeclaration>(node, query);
 | 
				
			||||||
 | 
						return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findMethodParameterByType(node: Node, type: string): string | null {
 | 
				
			||||||
 | 
						const query = `Parameter:has(TypeReference > Identifier[name="${type}"]) > Identifier`;
 | 
				
			||||||
 | 
						const [result] = tsquery<Identifier>(node, query);
 | 
				
			||||||
 | 
						if (result) {
 | 
				
			||||||
 | 
							return result.text;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return null;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findMethodCallExpressions(node: Node, propName: string, fnName: string | string[]): CallExpression[] {
 | 
				
			||||||
 | 
						if (Array.isArray(fnName)) {
 | 
				
			||||||
 | 
							fnName = fnName.join('|');
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						const query = `CallExpression > PropertyAccessExpression:has(Identifier[name=/^(${fnName})$/]):has(PropertyAccessExpression:has(Identifier[name="${propName}"]):not(:has(ThisKeyword)))`;
 | 
				
			||||||
 | 
						const nodes = tsquery<PropertyAccessExpression>(node, query).map((n) => n.parent as CallExpression);
 | 
				
			||||||
 | 
						return nodes;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findClassPropertyConstructorParameterByType(node: ClassDeclaration, type: string): string | null {
 | 
				
			||||||
 | 
						const query = `Constructor Parameter:has(TypeReference > Identifier[name="${type}"]):has(PublicKeyword,ProtectedKeyword,PrivateKeyword) > Identifier`;
 | 
				
			||||||
 | 
						const [result] = tsquery<Identifier>(node, query);
 | 
				
			||||||
 | 
						if (result) {
 | 
				
			||||||
 | 
							return result.text;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return null;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findClassPropertyDeclarationByType(node: ClassDeclaration, type: string): string | null {
 | 
				
			||||||
 | 
						const query = `PropertyDeclaration:has(TypeReference > Identifier[name="${type}"]) > Identifier`;
 | 
				
			||||||
 | 
						const [result] = tsquery<Identifier>(node, query);
 | 
				
			||||||
 | 
						if (result) {
 | 
				
			||||||
 | 
							return result.text;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return null;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findFunctionCallExpressions(node: Node, fnName: string | string[]): CallExpression[] {
 | 
				
			||||||
 | 
						if (Array.isArray(fnName)) {
 | 
				
			||||||
 | 
							fnName = fnName.join('|');
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						const query = `CallExpression:has(Identifier[name="${fnName}"]):not(:has(PropertyAccessExpression))`;
 | 
				
			||||||
 | 
						const nodes = tsquery<CallExpression>(node, query);
 | 
				
			||||||
 | 
						return nodes;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function findPropertyCallExpressions(node: Node, prop: string, fnName: string | string[]): CallExpression[] {
 | 
				
			||||||
 | 
						if (Array.isArray(fnName)) {
 | 
				
			||||||
 | 
							fnName = fnName.join('|');
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						const query = `CallExpression > PropertyAccessExpression:has(Identifier[name=/^(${fnName})$/]):has(PropertyAccessExpression:has(Identifier[name="${prop}"]):has(ThisKeyword))`;
 | 
				
			||||||
 | 
						const nodes = tsquery<PropertyAccessExpression>(node, query).map((n) => n.parent as CallExpression);
 | 
				
			||||||
 | 
						return nodes;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function getStringsFromExpression(expression: Expression): string[] {
 | 
				
			||||||
 | 
						if (isStringLiteralLike(expression)) {
 | 
				
			||||||
 | 
							return [expression.text];
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (isArrayLiteralExpression(expression)) {
 | 
				
			||||||
 | 
							return expression.elements.reduce((result: string[], element: Expression) => {
 | 
				
			||||||
 | 
								const strings = getStringsFromExpression(element);
 | 
				
			||||||
 | 
								return [...result, ...strings];
 | 
				
			||||||
 | 
							}, []);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (isBinaryExpression(expression)) {
 | 
				
			||||||
 | 
							const [left] = getStringsFromExpression(expression.left);
 | 
				
			||||||
 | 
							const [right] = getStringsFromExpression(expression.right);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (expression.operatorToken.kind === SyntaxKind.PlusToken) {
 | 
				
			||||||
 | 
								if (typeof left === 'string' && typeof right === 'string') {
 | 
				
			||||||
 | 
									return [left + right];
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (expression.operatorToken.kind === SyntaxKind.BarBarToken) {
 | 
				
			||||||
 | 
								const result = [];
 | 
				
			||||||
 | 
								if (typeof left === 'string') {
 | 
				
			||||||
 | 
									result.push(left);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if (typeof right === 'string') {
 | 
				
			||||||
 | 
									result.push(right);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								return result;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (isConditionalExpression(expression)) {
 | 
				
			||||||
 | 
							const [whenTrue] = getStringsFromExpression(expression.whenTrue);
 | 
				
			||||||
 | 
							const [whenFalse] = getStringsFromExpression(expression.whenFalse);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							const result = [];
 | 
				
			||||||
 | 
							if (typeof whenTrue === 'string') {
 | 
				
			||||||
 | 
								result.push(whenTrue);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if (typeof whenFalse === 'string') {
 | 
				
			||||||
 | 
								result.push(whenFalse);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return result;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return [];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,19 +0,0 @@
 | 
				
			|||||||
import * as ts from 'typescript';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export function printAllChildren(sourceFile: ts.SourceFile, node: ts.Node, depth = 0) {
 | 
					 | 
				
			||||||
	console.log(
 | 
					 | 
				
			||||||
		new Array(depth + 1).join('----'),
 | 
					 | 
				
			||||||
		`[${node.kind}]`,
 | 
					 | 
				
			||||||
		syntaxKindToName(node.kind),
 | 
					 | 
				
			||||||
		`[pos: ${node.pos}-${node.end}]`,
 | 
					 | 
				
			||||||
		':\t\t\t',
 | 
					 | 
				
			||||||
		node.getFullText(sourceFile).trim()
 | 
					 | 
				
			||||||
	);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	depth++;
 | 
					 | 
				
			||||||
	node.getChildren(sourceFile).forEach(childNode => printAllChildren(sourceFile, childNode, depth));
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export function syntaxKindToName(kind: ts.SyntaxKind) {
 | 
					 | 
				
			||||||
	return ts.SyntaxKind[kind];
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										17
									
								
								src/utils/donate.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/utils/donate.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					import { yellow } from 'colorette';
 | 
				
			||||||
 | 
					import * as boxen from 'boxen';
 | 
				
			||||||
 | 
					import * as terminalLink from 'terminal-link';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const url = 'https://donate.biesbjerg.com';
 | 
				
			||||||
 | 
					const link = terminalLink(url, url);
 | 
				
			||||||
 | 
					const message = `
 | 
				
			||||||
 | 
					If this tool saves you or your company time, please consider making a
 | 
				
			||||||
 | 
					donation to support my work and the continued maintainence and development:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					${yellow(link)}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const donateMessage = boxen(message.trim(), {
 | 
				
			||||||
 | 
						padding: 1,
 | 
				
			||||||
 | 
						margin: 0,
 | 
				
			||||||
 | 
						dimBorder: true
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@@ -1,38 +0,0 @@
 | 
				
			|||||||
import { ParserInterface } from '../parsers/parser.interface';
 | 
					 | 
				
			||||||
import { TranslationCollection } from './translation.collection';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import * as glob from 'glob';
 | 
					 | 
				
			||||||
import * as fs from 'fs';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export class Extractor {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public constructor(public parsers: ParserInterface[], public patterns: string[]) { }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Extract strings from dir
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public process(dir: string): TranslationCollection {
 | 
					 | 
				
			||||||
		let collection: TranslationCollection = new TranslationCollection();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		this._readDir(dir, this.patterns).forEach(path => {
 | 
					 | 
				
			||||||
			const contents: string = fs.readFileSync(path, 'utf-8');
 | 
					 | 
				
			||||||
			this.parsers.forEach((parser: ParserInterface) => {
 | 
					 | 
				
			||||||
				collection = collection.union(parser.extract(contents, path));
 | 
					 | 
				
			||||||
			});
 | 
					 | 
				
			||||||
		});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return collection;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Get all files in dir matching patterns
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	protected _readDir(dir: string, patterns: string[]): string[] {
 | 
					 | 
				
			||||||
		return patterns.reduce((results, pattern) => {
 | 
					 | 
				
			||||||
			return glob.sync(dir + pattern)
 | 
					 | 
				
			||||||
				.filter(path => fs.statSync(path).isFile())
 | 
					 | 
				
			||||||
				.concat(results);
 | 
					 | 
				
			||||||
		}, []);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										36
									
								
								src/utils/fs-helpers.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								src/utils/fs-helpers.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					import * as os from 'os';
 | 
				
			||||||
 | 
					import * as fs from 'fs';
 | 
				
			||||||
 | 
					import * as braces from 'braces';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					declare module 'braces' {
 | 
				
			||||||
 | 
						interface Options {
 | 
				
			||||||
 | 
							keepEscaping?: boolean; // Workaround for option not present in @types/braces 3.0.0
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function normalizeHomeDir(path: string): string {
 | 
				
			||||||
 | 
						if (path.substring(0, 1) === '~') {
 | 
				
			||||||
 | 
							return `${os.homedir()}/${path.substring(1)}`;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return path;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function expandPattern(pattern: string): string[] {
 | 
				
			||||||
 | 
						return braces(pattern, { expand: true, keepEscaping: true });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function normalizePaths(patterns: string[], defaultPatterns: string[] = []): string[] {
 | 
				
			||||||
 | 
						return patterns
 | 
				
			||||||
 | 
							.map((pattern) =>
 | 
				
			||||||
 | 
								expandPattern(pattern)
 | 
				
			||||||
 | 
									.map((path) => {
 | 
				
			||||||
 | 
										path = normalizeHomeDir(path);
 | 
				
			||||||
 | 
										if (fs.existsSync(path) && fs.statSync(path).isDirectory()) {
 | 
				
			||||||
 | 
											return defaultPatterns.map((defaultPattern) => path + defaultPattern);
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										return path;
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
									.flat()
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
							.flat();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,9 +1,8 @@
 | 
				
			|||||||
export interface TranslationType {
 | 
					export interface TranslationType {
 | 
				
			||||||
	[key: string]: string
 | 
						[key: string]: string;
 | 
				
			||||||
};
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class TranslationCollection {
 | 
					export class TranslationCollection {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	public values: TranslationType = {};
 | 
						public values: TranslationType = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public constructor(values: TranslationType = {}) {
 | 
						public constructor(values: TranslationType = {}) {
 | 
				
			||||||
@@ -11,29 +10,28 @@ export class TranslationCollection {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public add(key: string, val: string = ''): TranslationCollection {
 | 
						public add(key: string, val: string = ''): TranslationCollection {
 | 
				
			||||||
		return new TranslationCollection(Object.assign({}, this.values, { [key]: val }));
 | 
							return new TranslationCollection({ ...this.values, [key]: val });
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public addKeys(keys: string[]): TranslationCollection {
 | 
						public addKeys(keys: string[]): TranslationCollection {
 | 
				
			||||||
		const values = keys.reduce((results, key) => {
 | 
							const values = keys.reduce((results, key) => {
 | 
				
			||||||
			results[key] = '';
 | 
								return { ...results, [key]: '' };
 | 
				
			||||||
			return results;
 | 
							}, {} as TranslationType);
 | 
				
			||||||
		}, <TranslationType> {});
 | 
							return new TranslationCollection({ ...this.values, ...values });
 | 
				
			||||||
		return new TranslationCollection(Object.assign({}, this.values, values));
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public remove(key: string): TranslationCollection {
 | 
						public remove(key: string): TranslationCollection {
 | 
				
			||||||
		return this.filter(k => key !== k);
 | 
							return this.filter((k) => key !== k);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public forEach(callback: (key?: string, val?: string) => void): TranslationCollection {
 | 
						public forEach(callback: (key?: string, val?: string) => void): TranslationCollection {
 | 
				
			||||||
		Object.keys(this.values).forEach(key => callback.call(this, key, this.values[key]));
 | 
							Object.keys(this.values).forEach((key) => callback.call(this, key, this.values[key]));
 | 
				
			||||||
		return this;
 | 
							return this;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public filter(callback: (key?: string, val?: string) => boolean): TranslationCollection {
 | 
						public filter(callback: (key?: string, val?: string) => boolean): TranslationCollection {
 | 
				
			||||||
		let values: TranslationType = {};
 | 
							const values: TranslationType = {};
 | 
				
			||||||
		this.forEach((key: string, val: string) => {
 | 
							this.forEach((key, val) => {
 | 
				
			||||||
			if (callback.call(this, key, val)) {
 | 
								if (callback.call(this, key, val)) {
 | 
				
			||||||
				values[key] = val;
 | 
									values[key] = val;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -41,14 +39,21 @@ export class TranslationCollection {
 | 
				
			|||||||
		return new TranslationCollection(values);
 | 
							return new TranslationCollection(values);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public map(callback: (key?: string, val?: string) => string): TranslationCollection {
 | 
				
			||||||
 | 
							const values: TranslationType = {};
 | 
				
			||||||
 | 
							this.forEach((key, val) => {
 | 
				
			||||||
 | 
								values[key] = callback.call(this, key, val);
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							return new TranslationCollection(values);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public union(collection: TranslationCollection): TranslationCollection {
 | 
						public union(collection: TranslationCollection): TranslationCollection {
 | 
				
			||||||
		return new TranslationCollection(Object.assign({}, this.values, collection.values));
 | 
							return new TranslationCollection({ ...this.values, ...collection.values });
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public intersect(collection: TranslationCollection): TranslationCollection {
 | 
						public intersect(collection: TranslationCollection): TranslationCollection {
 | 
				
			||||||
		let values: TranslationType = {};
 | 
							const values: TranslationType = {};
 | 
				
			||||||
		this.filter(key => collection.has(key))
 | 
							this.filter((key) => collection.has(key)).forEach((key, val) => {
 | 
				
			||||||
			.forEach((key: string, val: string) => {
 | 
					 | 
				
			||||||
			values[key] = val;
 | 
								values[key] = val;
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -76,8 +81,10 @@ export class TranslationCollection {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public sort(compareFn?: (a: string, b: string) => number): TranslationCollection {
 | 
						public sort(compareFn?: (a: string, b: string) => number): TranslationCollection {
 | 
				
			||||||
		let values: TranslationType = {};
 | 
							const values: TranslationType = {};
 | 
				
			||||||
		this.keys().sort(compareFn).forEach((key) => {
 | 
							this.keys()
 | 
				
			||||||
 | 
								.sort(compareFn)
 | 
				
			||||||
 | 
								.forEach((key) => {
 | 
				
			||||||
				values[key] = this.get(key);
 | 
									values[key] = this.get(key);
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										22
									
								
								src/utils/utils.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/utils/utils.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Assumes file is an Angular component if type is javascript/typescript
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export function isPathAngularComponent(path: string): boolean {
 | 
				
			||||||
 | 
						return /\.ts|js$/i.test(path);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Extract inline template from a component
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export function extractComponentInlineTemplate(contents: string): string {
 | 
				
			||||||
 | 
						const regExp: RegExp = /template\s*:\s*(["'`])([^\1]*?)\1/;
 | 
				
			||||||
 | 
						const match = regExp.exec(contents);
 | 
				
			||||||
 | 
						if (match !== null) {
 | 
				
			||||||
 | 
							return match[2];
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return '';
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function stripBOM(contents: string): string {
 | 
				
			||||||
 | 
						return contents.trim();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,38 +0,0 @@
 | 
				
			|||||||
import { expect } from 'chai';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
					 | 
				
			||||||
import { NamespacedJsonCompiler } from '../../src/compilers/namespaced-json.compiler';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
describe('NamespacedJsonCompiler', () => {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	let compiler: NamespacedJsonCompiler;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	beforeEach(() => {
 | 
					 | 
				
			||||||
		compiler = new NamespacedJsonCompiler();
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should flatten keys on parse', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				"NAMESPACE": {
 | 
					 | 
				
			||||||
					"KEY": {
 | 
					 | 
				
			||||||
						"FIRST_KEY": "",
 | 
					 | 
				
			||||||
						"SECOND_KEY": "VALUE"
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const collection: TranslationCollection = compiler.parse(contents);
 | 
					 | 
				
			||||||
		expect(collection.values).to.deep.equal({'NAMESPACE.KEY.FIRST_KEY': '', 'NAMESPACE.KEY.SECOND_KEY': 'VALUE' });
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should unflatten keys on compile', () => {
 | 
					 | 
				
			||||||
		const collection = new TranslationCollection({
 | 
					 | 
				
			||||||
			'NAMESPACE.KEY.FIRST_KEY': '',
 | 
					 | 
				
			||||||
			'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
 | 
					 | 
				
			||||||
		});
 | 
					 | 
				
			||||||
		const result: string = compiler.compile(collection);
 | 
					 | 
				
			||||||
		expect(result).to.equal('{\n\t"NAMESPACE": {\n\t\t"KEY": {\n\t\t\t"FIRST_KEY": "",\n\t\t\t"SECOND_KEY": "VALUE"\n\t\t}\n\t}\n}');
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
							
								
								
									
										70
									
								
								tests/compilers/namespaced-json.compiler.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								tests/compilers/namespaced-json.compiler.spec.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,70 @@
 | 
				
			|||||||
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
				
			||||||
 | 
					import { NamespacedJsonCompiler } from '../../src/compilers/namespaced-json.compiler';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('NamespacedJsonCompiler', () => {
 | 
				
			||||||
 | 
						let compiler: NamespacedJsonCompiler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						beforeEach(() => {
 | 
				
			||||||
 | 
							compiler = new NamespacedJsonCompiler();
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should flatten keys on parse', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									"NAMESPACE": {
 | 
				
			||||||
 | 
										"KEY": {
 | 
				
			||||||
 | 
											"FIRST_KEY": "",
 | 
				
			||||||
 | 
											"SECOND_KEY": "VALUE"
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const collection: TranslationCollection = compiler.parse(contents);
 | 
				
			||||||
 | 
							expect(collection.values).to.deep.equal({
 | 
				
			||||||
 | 
								'NAMESPACE.KEY.FIRST_KEY': '',
 | 
				
			||||||
 | 
								'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should unflatten keys on compile', () => {
 | 
				
			||||||
 | 
							const collection = new TranslationCollection({
 | 
				
			||||||
 | 
								'NAMESPACE.KEY.FIRST_KEY': '',
 | 
				
			||||||
 | 
								'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const result: string = compiler.compile(collection);
 | 
				
			||||||
 | 
							expect(result).to.equal('{\n\t"NAMESPACE": {\n\t\t"KEY": {\n\t\t\t"FIRST_KEY": "",\n\t\t\t"SECOND_KEY": "VALUE"\n\t\t}\n\t}\n}');
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should preserve numeric values on compile', () => {
 | 
				
			||||||
 | 
							const collection = new TranslationCollection({
 | 
				
			||||||
 | 
								'option.0': '',
 | 
				
			||||||
 | 
								'option.1': '',
 | 
				
			||||||
 | 
								'option.2': ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const result: string = compiler.compile(collection);
 | 
				
			||||||
 | 
							expect(result).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}');
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should use custom indentation chars', () => {
 | 
				
			||||||
 | 
							const collection = new TranslationCollection({
 | 
				
			||||||
 | 
								'NAMESPACE.KEY.FIRST_KEY': '',
 | 
				
			||||||
 | 
								'NAMESPACE.KEY.SECOND_KEY': 'VALUE'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const customCompiler = new NamespacedJsonCompiler({
 | 
				
			||||||
 | 
								indentation: '  '
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const result: string = customCompiler.compile(collection);
 | 
				
			||||||
 | 
							expect(result).to.equal('{\n  "NAMESPACE": {\n    "KEY": {\n      "FIRST_KEY": "",\n      "SECOND_KEY": "VALUE"\n    }\n  }\n}');
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should not reorder keys when compiled', () => {
 | 
				
			||||||
 | 
							const collection = new TranslationCollection({
 | 
				
			||||||
 | 
								BROWSE: '',
 | 
				
			||||||
 | 
								LOGIN: ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const result: string = compiler.compile(collection);
 | 
				
			||||||
 | 
							expect(result).to.equal('{\n\t"BROWSE": "",\n\t"LOGIN": ""\n}');
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
							
								
								
									
										24
									
								
								tests/compilers/po.compiler.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								tests/compilers/po.compiler.spec.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
				
			||||||
 | 
					import { PoCompiler } from '../../src/compilers/po.compiler';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('PoCompiler', () => {
 | 
				
			||||||
 | 
						let compiler: PoCompiler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						beforeEach(() => {
 | 
				
			||||||
 | 
							compiler = new PoCompiler();
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should still include html ', () => {
 | 
				
			||||||
 | 
							const collection = new TranslationCollection({
 | 
				
			||||||
 | 
								'A <strong>test</strong>': 'Un <strong>test</strong>',
 | 
				
			||||||
 | 
								'With a lot of <em>html</em> included': 'Avec beaucoup d\'<em>html</em> inclus'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const result: Buffer = Buffer.from(compiler.compile(collection));
 | 
				
			||||||
 | 
							expect(result.toString('utf8')).to.equal('msgid ""\nmsgstr ""\n"mime-version: 1.0\\n"\n"Content-Type: text/plain; charset=utf-8\\n"\n"Content-Transfer-Encoding: 8bit\\n"\n\nmsgid "A <strong>test</strong>"\nmsgstr "Un <strong>test</strong>"\n\nmsgid "With a lot of <em>html</em> included"\nmsgstr "Avec beaucoup d\'<em>html</em> inclus"');
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -1,154 +0,0 @@
 | 
				
			|||||||
import { expect } from 'chai';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import { AstServiceParser } from '../../src/parsers/ast-service.parser';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class TestAstServiceParser extends AstServiceParser {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/*public getInstancePropertyName(): string {
 | 
					 | 
				
			||||||
		return this._getInstancePropertyName();
 | 
					 | 
				
			||||||
	}*/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
describe('AstServiceParser', () => {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	const componentFilename: string = 'test.component.ts';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	let parser: TestAstServiceParser;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	beforeEach(() => {
 | 
					 | 
				
			||||||
		parser = new TestAstServiceParser();
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/*it('should extract variable used for TranslateService', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(
 | 
					 | 
				
			||||||
					_serviceA: ServiceA,
 | 
					 | 
				
			||||||
					public _serviceB: ServiceB,
 | 
					 | 
				
			||||||
					protected _translateService: TranslateService
 | 
					 | 
				
			||||||
			) { }
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const name = parser.getInstancePropertyName();
 | 
					 | 
				
			||||||
		expect(name).to.equal('_translateService');
 | 
					 | 
				
			||||||
	});*/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract strings in TranslateService\'s get() method', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(protected _translateService: TranslateService) { }
 | 
					 | 
				
			||||||
				public test() {
 | 
					 | 
				
			||||||
					this._translateService.get('Hello World');
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract strings in TranslateService\'s instant() method', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(protected _translateService: TranslateService) { }
 | 
					 | 
				
			||||||
				public test() {
 | 
					 | 
				
			||||||
					this._translateService.instant('Hello World');
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract array of strings in TranslateService\'s get() method', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(protected _translateService: TranslateService) { }
 | 
					 | 
				
			||||||
				public test() {
 | 
					 | 
				
			||||||
					this._translateService.get(['Hello', 'World']);
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello', 'World']);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract array of strings in TranslateService\'s instant() method', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(protected _translateService: TranslateService) { }
 | 
					 | 
				
			||||||
				public test() {
 | 
					 | 
				
			||||||
					this._translateService.instant(['Hello', 'World']);
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const key = parser.extract(contents, componentFilename).keys();
 | 
					 | 
				
			||||||
		expect(key).to.deep.equal(['Hello', 'World']);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should not extract strings in get()/instant() methods of other services', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(
 | 
					 | 
				
			||||||
					protected _translateService: TranslateService,
 | 
					 | 
				
			||||||
					protected _otherService: OtherService
 | 
					 | 
				
			||||||
				) { }
 | 
					 | 
				
			||||||
				public test() {
 | 
					 | 
				
			||||||
					this._otherService.get('Hello World');
 | 
					 | 
				
			||||||
					this._otherService.instant('Hi there');
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal([]);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract strings with liberal spacing', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(
 | 
					 | 
				
			||||||
					protected _translateService: TranslateService,
 | 
					 | 
				
			||||||
					protected _otherService: OtherService
 | 
					 | 
				
			||||||
				) { }
 | 
					 | 
				
			||||||
				public test() {
 | 
					 | 
				
			||||||
					this._translateService.instant('Hello');
 | 
					 | 
				
			||||||
					this._translateService.get ( 'World' );
 | 
					 | 
				
			||||||
					this._translateService.instant ( ['How'] );
 | 
					 | 
				
			||||||
					this._translateService.get([ 'Are' ]);
 | 
					 | 
				
			||||||
					this._translateService.get([ 'You' , 'Today' ]);
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello', 'World', 'How', 'Are', 'You', 'Today']);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should not extract string when not accessing property', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(protected trans: TranslateService) { }
 | 
					 | 
				
			||||||
				public test() {
 | 
					 | 
				
			||||||
					trans.get("You are expected at {{time}}", {time: moment.format('H:mm')}).subscribe();
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal([]);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract string with params on same line', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			@Component({ })
 | 
					 | 
				
			||||||
			export class AppComponent {
 | 
					 | 
				
			||||||
				public constructor(protected _translateService: TranslateService) { }
 | 
					 | 
				
			||||||
				public test() {
 | 
					 | 
				
			||||||
					this._translateService.get('You are expected at {{time}}', {time: moment.format('H:mm')});
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal(['You are expected at {{time}}']);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
@@ -2,78 +2,99 @@ import { expect } from 'chai';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import { DirectiveParser } from '../../src/parsers/directive.parser';
 | 
					import { DirectiveParser } from '../../src/parsers/directive.parser';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestDirectiveParser extends DirectiveParser {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public normalizeTemplateAttributes(template: string): string {
 | 
					 | 
				
			||||||
		return this._normalizeTemplateAttributes(template);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
describe('DirectiveParser', () => {
 | 
					describe('DirectiveParser', () => {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	const templateFilename: string = 'test.template.html';
 | 
						const templateFilename: string = 'test.template.html';
 | 
				
			||||||
	const componentFilename: string = 'test.component.ts';
 | 
						const componentFilename: string = 'test.component.ts';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let parser: TestDirectiveParser;
 | 
						let parser: DirectiveParser;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	beforeEach(() => {
 | 
						beforeEach(() => {
 | 
				
			||||||
		parser = new TestDirectiveParser();
 | 
							parser = new DirectiveParser();
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract contents when no translate attribute value is provided', () => {
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys when using literal map in bound attribute', () => {
 | 
				
			||||||
 | 
							const contents = `<div [translate]="{ key1: 'value1' | translate, key2: 'value2' | translate }"></div>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['value1', 'value2']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys when using literal arrays in bound attribute', () => {
 | 
				
			||||||
 | 
							const contents = `<div [translate]="[ 'value1' | translate, 'value2' | translate ]"></div>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['value1', 'value2']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys when using binding pipe in bound attribute', () => {
 | 
				
			||||||
 | 
							const contents = `<div [translate]="'KEY1' | withPipe"></div>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['KEY1']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys when using binary expression in bound attribute', () => {
 | 
				
			||||||
 | 
							const contents = `<div [translate]="keyVar || 'KEY1'"></div>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['KEY1']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys when using literal primitive in bound attribute', () => {
 | 
				
			||||||
 | 
							const contents = `<div [translate]="'KEY1'"></div>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['KEY1']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys when using conditional in bound attribute', () => {
 | 
				
			||||||
 | 
							const contents = `<div [translate]="condition ? 'KEY1' : 'KEY2'"></div>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['KEY1', 'KEY2']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys when using nested conditionals in bound attribute', () => {
 | 
				
			||||||
 | 
							const contents = `<div [translate]="isSunny ? (isWarm ? 'Sunny and warm' : 'Sunny but cold') : 'Not sunny'"></div>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Sunny and warm', 'Sunny but cold', 'Not sunny']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys when using interpolation', () => {
 | 
				
			||||||
 | 
							const contents = `<div translate="{{ 'KEY1' + key2 + 'KEY3' }}"></div>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['KEY1', 'KEY3']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys keeping proper whitespace', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								<div translate>
 | 
				
			||||||
 | 
									Wubba
 | 
				
			||||||
 | 
									Lubba
 | 
				
			||||||
 | 
									Dub Dub
 | 
				
			||||||
 | 
								</div>
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Wubba Lubba Dub Dub']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should use element contents as key when no translate attribute value is present', () => {
 | 
				
			||||||
		const contents = '<div translate>Hello World</div>';
 | 
							const contents = '<div translate>Hello World</div>';
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract translate attribute if provided', () => {
 | 
						it('should use translate attribute value as key when present', () => {
 | 
				
			||||||
		const contents = '<div translate="KEY">Hello World<div>';
 | 
							const contents = '<div translate="MY_KEY">Hello World<div>';
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['KEY']);
 | 
							expect(keys).to.deep.equal(['MY_KEY']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract bound translate attribute as key if provided', () => {
 | 
						it('should extract keys from child elements when translate attribute is present', () => {
 | 
				
			||||||
		const contents = `<div [translate]="'KEY'">Hello World<div>`;
 | 
							const contents = `<div translate>Hello <strong translate>World</strong></div>`;
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['KEY']);
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract direct text nodes when no translate attribute value is provided', () => {
 | 
						it('should not extract keys from child elements when translate attribute is not present', () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `<div translate>Hello <strong>World</strong></div>`;
 | 
				
			||||||
			<div translate>
 | 
					 | 
				
			||||||
				<span>✓</span>
 | 
					 | 
				
			||||||
				Hello <strong>World</strong>
 | 
					 | 
				
			||||||
				Hi <em>there</em>
 | 
					 | 
				
			||||||
			</div>
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello', 'Hi']);
 | 
							expect(keys).to.deep.equal(['Hello']);
 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract direct text nodes of tags with a translate attribute', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			<div translate>
 | 
					 | 
				
			||||||
				<span>✓</span>
 | 
					 | 
				
			||||||
				Hello World
 | 
					 | 
				
			||||||
				<div translate>Hi there</div>
 | 
					 | 
				
			||||||
			</div>
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello World', 'Hi there']);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract translate attribute if provided or direct text nodes if not', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
					 | 
				
			||||||
			<div translate="KEY">
 | 
					 | 
				
			||||||
				<span>✓</span>
 | 
					 | 
				
			||||||
				Hello World
 | 
					 | 
				
			||||||
				<p translate>Hi there</p>
 | 
					 | 
				
			||||||
				<p [translate]="'OTHER_KEY'">Lorem Ipsum</p>
 | 
					 | 
				
			||||||
			</div>
 | 
					 | 
				
			||||||
		`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal(['KEY', 'Hi there', 'OTHER_KEY']);
 | 
					 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract and parse inline template', () => {
 | 
						it('should extract and parse inline template', () => {
 | 
				
			||||||
@@ -88,34 +109,65 @@ describe('DirectiveParser', () => {
 | 
				
			|||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract contents when no ng2-translate attribute value is provided', () => {
 | 
						it('should extract contents when no translate attribute value is provided', () => {
 | 
				
			||||||
		const contents = '<div ng2-translate>Hello World</div>';
 | 
							const contents = '<div translate>Hello World</div>';
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract ng2-translate attribute if provided', () => {
 | 
						it('should extract translate attribute value if provided', () => {
 | 
				
			||||||
		const contents = '<div ng2-translate="KEY">Hello World<div>';
 | 
							const contents = '<div translate="KEY">Hello World<div>';
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
					 | 
				
			||||||
		expect(keys).to.deep.equal(['KEY']);
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	it('should extract bound ng2-translate attribute as key if provided', () => {
 | 
					 | 
				
			||||||
		const contents = `<div [ng2-translate]="'KEY'">Hello World<div>`;
 | 
					 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['KEY']);
 | 
							expect(keys).to.deep.equal(['KEY']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should not extract translate pipe in html tag', () => {
 | 
						it('should not extract translate pipe in html tag', () => {
 | 
				
			||||||
		const contents = `<p>{{ 'Audiobooks for personal development' | translate }}</p>`;
 | 
							const contents = `<p>{{ 'Audiobooks for personal development' | translate }}</p>`;
 | 
				
			||||||
		const collection = parser.extract(contents, templateFilename);
 | 
							const collection = parser.extract(contents, templateFilename);
 | 
				
			||||||
		expect(collection.values).to.deep.equal({});
 | 
							expect(collection.values).to.deep.equal({});
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should normalize bound attributes', () => {
 | 
						it('should extract contents from custom elements', () => {
 | 
				
			||||||
		const contents = `<p [translate]="'KEY'">Hello World</p>`;
 | 
							const contents = `<custom-table><tbody><tr><td translate>Hello World</td></tr></tbody></custom-table>`;
 | 
				
			||||||
		const template = parser.normalizeTemplateAttributes(contents);
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(template).to.equal('<p translate="KEY">Hello World</p>');
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract from template without leading/trailing whitespace', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								<div *ngIf="!isLoading && studentsToGrid && studentsToGrid.length == 0" class="no-students" mt-rtl translate>There
 | 
				
			||||||
 | 
									are currently no students in this class. The good news is, adding students is really easy! Just use the options
 | 
				
			||||||
 | 
									at the top.
 | 
				
			||||||
 | 
								</div>
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal([
 | 
				
			||||||
 | 
								'There are currently no students in this class. The good news is, adding students is really easy! Just use the options at the top.'
 | 
				
			||||||
 | 
							]);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract keys from element without leading/trailing whitespace', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								<div translate>
 | 
				
			||||||
 | 
									this is an example
 | 
				
			||||||
 | 
									of a long label
 | 
				
			||||||
 | 
								</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								<div>
 | 
				
			||||||
 | 
									<p translate>
 | 
				
			||||||
 | 
										this is an example
 | 
				
			||||||
 | 
										of another a long label
 | 
				
			||||||
 | 
									</p>
 | 
				
			||||||
 | 
								</div>
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['this is an example of a long label', 'this is an example of another a long label']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should collapse excessive whitespace', () => {
 | 
				
			||||||
 | 
							const contents = '<p translate>this      is an example</p>';
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['this is an example']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										64
									
								
								tests/parsers/marker.parser.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								tests/parsers/marker.parser.spec.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
				
			|||||||
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { MarkerParser } from '../../src/parsers/marker.parser';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('MarkerParser', () => {
 | 
				
			||||||
 | 
						const componentFilename: string = 'test.component.ts';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						let parser: MarkerParser;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						beforeEach(() => {
 | 
				
			||||||
 | 
							parser = new MarkerParser();
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings using marker function', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								import { marker } from '@biesbjerg/ngx-translate-extract-marker';
 | 
				
			||||||
 | 
								marker('Hello world');
 | 
				
			||||||
 | 
								marker(['I', 'am', 'extracted']);
 | 
				
			||||||
 | 
								otherFunction('But I am not');
 | 
				
			||||||
 | 
								marker(message || 'binary expression');
 | 
				
			||||||
 | 
								marker(message ? message : 'conditional operator');
 | 
				
			||||||
 | 
								marker('FOO.bar');
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello world', 'I', 'am', 'extracted', 'binary expression', 'conditional operator', 'FOO.bar']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract split strings', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
 | 
				
			||||||
 | 
								_('Hello ' + 'world');
 | 
				
			||||||
 | 
								_('This is a ' + 'very ' + 'very ' + 'very ' + 'very ' + 'long line.');
 | 
				
			||||||
 | 
								_('Mix ' + \`of \` + 'different ' + \`types\`);
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello world', 'This is a very very very very long line.', 'Mix of different types']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract split strings while keeping html tags', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
 | 
				
			||||||
 | 
								_('Hello ' + 'world');
 | 
				
			||||||
 | 
								_('This <em>is</em> a ' + 'very ' + 'very ' + 'very ' + 'very ' + 'long line.');
 | 
				
			||||||
 | 
								_('Mix ' + \`of \` + 'different ' + \`types\`);
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello world', 'This <em>is</em> a very very very very long line.', 'Mix of different types']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract the strings', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
							import { marker } from '@biesbjerg/ngx-translate-extract-marker';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							export class AppModule {
 | 
				
			||||||
 | 
								constructor() {
 | 
				
			||||||
 | 
									marker('DYNAMIC_TRAD.val1');
 | 
				
			||||||
 | 
									marker('DYNAMIC_TRAD.val2');
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['DYNAMIC_TRAD.val1', 'DYNAMIC_TRAD.val2']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@@ -3,7 +3,6 @@ import { expect } from 'chai';
 | 
				
			|||||||
import { PipeParser } from '../../src/parsers/pipe.parser';
 | 
					import { PipeParser } from '../../src/parsers/pipe.parser';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe('PipeParser', () => {
 | 
					describe('PipeParser', () => {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	const templateFilename: string = 'test.template.html';
 | 
						const templateFilename: string = 'test.template.html';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let parser: PipeParser;
 | 
						let parser: PipeParser;
 | 
				
			||||||
@@ -18,42 +17,126 @@ describe('PipeParser', () => {
 | 
				
			|||||||
		expect(keys).to.deep.equal(['SomeKey_NotWorking']);
 | 
							expect(keys).to.deep.equal(['SomeKey_NotWorking']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract string using pipe, but between quotes only', () => {
 | 
				
			||||||
 | 
							const contents = `<input class="form-control" type="text" placeholder="{{'user.settings.form.phone.placeholder' | translate}}" [formControl]="settingsForm.controls['phone']">`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['user.settings.form.phone.placeholder']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract interpolated strings using translate pipe', () => {
 | 
						it('should extract interpolated strings using translate pipe', () => {
 | 
				
			||||||
		const contents = `Hello {{ 'World' | translate }}`;
 | 
							const contents = `Hello {{ 'World' | translate }}`;
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['World']);
 | 
							expect(keys).to.deep.equal(['World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract interpolated strings when translate pipe is used before other pipes', () => {
 | 
				
			||||||
 | 
							const contents = `Hello {{ 'World' | translate | upper }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract interpolated strings when translate pipe is used after other pipes', () => {
 | 
				
			||||||
 | 
							const contents = `Hello {{ 'World'  | upper | translate }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from ternary operators inside interpolations', () => {
 | 
				
			||||||
 | 
							const contents = `{{ (condition ? 'Hello' : 'World') | translate }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from ternary operators right expression', () => {
 | 
				
			||||||
 | 
							const contents = `{{ condition ? null : ('World' | translate) }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from ternary operators inside attribute bindings', () => {
 | 
				
			||||||
 | 
							const contents = `<span [attr]="condition ? null : ('World' | translate)"></span>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from ternary operators left expression', () => {
 | 
				
			||||||
 | 
							const contents = `{{ condition ? ('World' | translate) : null }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings inside string concatenation', () => {
 | 
				
			||||||
 | 
							const contents = `{{ 'a' + ('Hello' | translate) + 'b' + 'c' + ('World' | translate) + 'd' }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from object', () => {
 | 
				
			||||||
 | 
							const contents = `{{ { foo: 'Hello' | translate, bar: ['World' | translate], deep: { nested: { baz: 'Yes' | translate } } } | json }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello', 'World', 'Yes']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from ternary operators inside attribute bindings', () => {
 | 
				
			||||||
 | 
							const contents = `<span [attr]="(condition ? 'Hello' : 'World') | translate"></span>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from nested expressions', () => {
 | 
				
			||||||
 | 
							const contents = `<span [attr]="{ foo: ['a' + ((condition ? 'Hello' : 'World') | translate) + 'b'] }"></span>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from nested ternary operators ', () => {
 | 
				
			||||||
 | 
							const contents = `<h3>{{ (condition ? 'Hello' : anotherCondition ? 'Nested' : 'World' ) | translate }}</h3>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello', 'Nested', 'World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from ternary operators inside attribute interpolations', () => {
 | 
				
			||||||
 | 
							const contents = `<span attr="{{(condition ? 'Hello' : 'World') | translate}}"></span>`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract strings with escaped quotes', () => {
 | 
						it('should extract strings with escaped quotes', () => {
 | 
				
			||||||
		const contents = `Hello {{ 'World\'s largest potato' | translate }}`;
 | 
							const contents = `Hello {{ 'World\\'s largest potato' | translate }}`;
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal([`World's largest potato`]);
 | 
							expect(keys).to.deep.equal([`World's largest potato`]);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings with multiple escaped quotes', () => {
 | 
				
			||||||
 | 
							const contents = `{{ 'C\\'est ok. C\\'est ok' | translate }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal([`C'est ok. C'est ok`]);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract interpolated strings using translate pipe in attributes', () => {
 | 
						it('should extract interpolated strings using translate pipe in attributes', () => {
 | 
				
			||||||
		const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`;
 | 
							const contents = `<span attr="{{ 'Hello World' | translate }}"></span>`;
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract bound strings using translate pipe in attributes', () => {
 | 
						it('should extract bound strings using translate pipe in attributes', () => {
 | 
				
			||||||
		const contents = `<span [attr]="'Hello World' | translate"></span>`;
 | 
							const contents = `<span [attr]="'Hello World' | translate"></span>`;
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should not use a greedy regular expression', () => {
 | 
						it('should extract multiple entries from nodes', () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			<ion-header>
 | 
								<ion-header>
 | 
				
			||||||
				<ion-navbar color="brand">
 | 
									<ion-navbar color="brand">
 | 
				
			||||||
					<ion-title>{{ 'Info' | translate }}</ion-title>
 | 
										<ion-title>{{ 'Info' | translate }}</ion-title>
 | 
				
			||||||
				</ion-navbar>
 | 
									</ion-navbar>
 | 
				
			||||||
			</ion-header>
 | 
								</ion-header>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			<ion-content>
 | 
								<ion-content>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				<content-loading *ngIf="isLoading">
 | 
									<content-loading *ngIf="isLoading">
 | 
				
			||||||
					{{ 'Loading...' | translate }}
 | 
										{{ 'Loading...' | translate }}
 | 
				
			||||||
				</content-loading>
 | 
									</content-loading>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			</ion-content>
 | 
								</ion-content>
 | 
				
			||||||
@@ -63,7 +146,7 @@ describe('PipeParser', () => {
 | 
				
			|||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract strings on same line', () => {
 | 
						it('should extract strings on same line', () => {
 | 
				
			||||||
		const contents = `<span [attr]="'Hello' | translate"></span><span [attr]="'World' | translate"></span>`;
 | 
							const contents = `<span [attr]="'Hello' | translate"></span><span [attr]="'World' | translate"></span>`;
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['Hello', 'World']);
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
@@ -73,7 +156,7 @@ describe('PipeParser', () => {
 | 
				
			|||||||
			<ion-list inset>
 | 
								<ion-list inset>
 | 
				
			||||||
				<ion-item>
 | 
									<ion-item>
 | 
				
			||||||
					<ion-icon item-left name="person" color="dark"></ion-icon>
 | 
										<ion-icon item-left name="person" color="dark"></ion-icon>
 | 
				
			||||||
					<ion-input formControlName="name" type="text" [placeholder]="'Name' | translate"></ion-input>
 | 
										<ion-input formControlName="name" type="text" [placeholder]="'Name' | translate"></ion-input>
 | 
				
			||||||
				</ion-item>
 | 
									</ion-item>
 | 
				
			||||||
				<ion-item>
 | 
									<ion-item>
 | 
				
			||||||
					<p color="danger" danger *ngFor="let error of form.get('name').getError('remote')">
 | 
										<p color="danger" danger *ngFor="let error of form.get('name').getError('remote')">
 | 
				
			||||||
@@ -82,11 +165,40 @@ describe('PipeParser', () => {
 | 
				
			|||||||
				</ion-item>
 | 
									</ion-item>
 | 
				
			||||||
			</ion-list>
 | 
								</ion-list>
 | 
				
			||||||
			<div class="form-actions">
 | 
								<div class="form-actions">
 | 
				
			||||||
				<button ion-button (click)="onSubmit()" color="secondary" block>{{ 'Create account' | translate }}</button>
 | 
									<button ion-button (click)="onSubmit()" color="secondary" block>{{ 'Create account' | translate }}</button>
 | 
				
			||||||
			</div>
 | 
								</div>
 | 
				
			||||||
		`;
 | 
							`;
 | 
				
			||||||
		const keys = parser.extract(contents, templateFilename).keys();
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['Name', 'Create account']);
 | 
							expect(keys).to.deep.equal(['Name', 'Create account']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should not extract variables', () => {
 | 
				
			||||||
 | 
							const contents = '<p>{{ message | translate }}</p>';
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal([]);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should be able to extract without html', () => {
 | 
				
			||||||
 | 
							const contents = `{{ 'message' | translate }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['message']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should ignore calculated values', () => {
 | 
				
			||||||
 | 
							const contents = `{{ 'SOURCES.' + source.name + '.NAME_PLURAL' | translate }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal([]);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should not extract pipe argument', () => {
 | 
				
			||||||
 | 
							const contents = `{{ value | valueToTranslationKey: 'argument' | translate }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal([]);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from piped arguments inside a function calls on templates', () => {
 | 
				
			||||||
 | 
							const contents = `{{ callMe('Hello' | translate, 'World' | translate ) }}`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, templateFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal([`Hello`, `World`]);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,39 +2,56 @@ import { expect } from 'chai';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import { ServiceParser } from '../../src/parsers/service.parser';
 | 
					import { ServiceParser } from '../../src/parsers/service.parser';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestServiceParser extends ServiceParser {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public extractTranslateServiceVar(contents: string): string {
 | 
					 | 
				
			||||||
		return this._extractTranslateServiceVar(contents);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
describe('ServiceParser', () => {
 | 
					describe('ServiceParser', () => {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	const componentFilename: string = 'test.component.ts';
 | 
						const componentFilename: string = 'test.component.ts';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	let parser: TestServiceParser;
 | 
						let parser: ServiceParser;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	beforeEach(() => {
 | 
						beforeEach(() => {
 | 
				
			||||||
		parser = new TestServiceParser();
 | 
							parser = new ServiceParser();
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract variable used for TranslateService', () => {
 | 
						it('should extract strings when TranslateService is accessed directly via constructor parameter', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								@Component({ })
 | 
				
			||||||
 | 
								export class MyComponent {
 | 
				
			||||||
 | 
									public constructor(protected translateService: TranslateService) {
 | 
				
			||||||
 | 
										translateService.get('It works!');
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['It works!']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should support extracting binary expressions', () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			@Component({ })
 | 
								@Component({ })
 | 
				
			||||||
			export class AppComponent {
 | 
								export class AppComponent {
 | 
				
			||||||
				public constructor(
 | 
									public constructor(protected _translateService: TranslateService) { }
 | 
				
			||||||
					_serviceA: ServiceA,
 | 
									public test() {
 | 
				
			||||||
					public _serviceB: ServiceB,
 | 
										const message = 'The Message';
 | 
				
			||||||
					protected _translateService: TranslateService
 | 
										this._translateService.get(message || 'Fallback message');
 | 
				
			||||||
			) { }
 | 
									}
 | 
				
			||||||
		`;
 | 
							`;
 | 
				
			||||||
		const name = parser.extractTranslateServiceVar(contents);
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
		expect(name).to.equal('_translateService');
 | 
							expect(keys).to.deep.equal(['Fallback message']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract strings in TranslateService\'s get() method', () => {
 | 
						it('should support conditional operator', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								@Component({ })
 | 
				
			||||||
 | 
								export class AppComponent {
 | 
				
			||||||
 | 
									public constructor(protected _translateService: TranslateService) { }
 | 
				
			||||||
 | 
									public test() {
 | 
				
			||||||
 | 
										const message = 'The Message';
 | 
				
			||||||
 | 
										this._translateService.get(message ? message : 'Fallback message');
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Fallback message']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it("should extract strings in TranslateService's get() method", () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			@Component({ })
 | 
								@Component({ })
 | 
				
			||||||
			export class AppComponent {
 | 
								export class AppComponent {
 | 
				
			||||||
@@ -47,7 +64,7 @@ describe('ServiceParser', () => {
 | 
				
			|||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract strings in TranslateService\'s instant() method', () => {
 | 
						it("should extract strings in TranslateService's instant() method", () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			@Component({ })
 | 
								@Component({ })
 | 
				
			||||||
			export class AppComponent {
 | 
								export class AppComponent {
 | 
				
			||||||
@@ -60,7 +77,20 @@ describe('ServiceParser', () => {
 | 
				
			|||||||
		expect(keys).to.deep.equal(['Hello World']);
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract array of strings in TranslateService\'s get() method', () => {
 | 
						it("should extract strings in TranslateService's stream() method", () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								@Component({ })
 | 
				
			||||||
 | 
								export class AppComponent {
 | 
				
			||||||
 | 
									public constructor(protected _translateService: TranslateService) { }
 | 
				
			||||||
 | 
									public test() {
 | 
				
			||||||
 | 
										this._translateService.stream('Hello World');
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it("should extract array of strings in TranslateService's get() method", () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			@Component({ })
 | 
								@Component({ })
 | 
				
			||||||
			export class AppComponent {
 | 
								export class AppComponent {
 | 
				
			||||||
@@ -73,7 +103,7 @@ describe('ServiceParser', () => {
 | 
				
			|||||||
		expect(keys).to.deep.equal(['Hello', 'World']);
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract array of strings in TranslateService\'s instant() method', () => {
 | 
						it("should extract array of strings in TranslateService's instant() method", () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			@Component({ })
 | 
								@Component({ })
 | 
				
			||||||
			export class AppComponent {
 | 
								export class AppComponent {
 | 
				
			||||||
@@ -86,7 +116,33 @@ describe('ServiceParser', () => {
 | 
				
			|||||||
		expect(key).to.deep.equal(['Hello', 'World']);
 | 
							expect(key).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should not extract strings in get()/instant() methods of other services', () => {
 | 
						it("should extract array of strings in TranslateService's stream() method", () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								@Component({ })
 | 
				
			||||||
 | 
								export class AppComponent {
 | 
				
			||||||
 | 
									public constructor(protected _translateService: TranslateService) { }
 | 
				
			||||||
 | 
									public test() {
 | 
				
			||||||
 | 
										this._translateService.stream(['Hello', 'World']);
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const key = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(key).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract string arrays encapsulated in backticks', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								@Component({ })
 | 
				
			||||||
 | 
								export class AppComponent {
 | 
				
			||||||
 | 
									public constructor(protected _translateService: TranslateService) { }
 | 
				
			||||||
 | 
									public test() {
 | 
				
			||||||
 | 
										this._translateService.get([\`Hello\`, \`World\`]);
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello', 'World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should not extract strings in get()/instant()/stream() methods of other services', () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			@Component({ })
 | 
								@Component({ })
 | 
				
			||||||
			export class AppComponent {
 | 
								export class AppComponent {
 | 
				
			||||||
@@ -97,6 +153,7 @@ describe('ServiceParser', () => {
 | 
				
			|||||||
				public test() {
 | 
									public test() {
 | 
				
			||||||
					this._otherService.get('Hello World');
 | 
										this._otherService.get('Hello World');
 | 
				
			||||||
					this._otherService.instant('Hi there');
 | 
										this._otherService.instant('Hi there');
 | 
				
			||||||
 | 
										this._otherService.stream('Hi there');
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
		`;
 | 
							`;
 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
@@ -129,7 +186,7 @@ describe('ServiceParser', () => {
 | 
				
			|||||||
			export class AppComponent {
 | 
								export class AppComponent {
 | 
				
			||||||
				public constructor(protected trans: TranslateService) { }
 | 
									public constructor(protected trans: TranslateService) { }
 | 
				
			||||||
				public test() {
 | 
									public test() {
 | 
				
			||||||
					trans.get('Hello World');
 | 
										trans.get("You are expected at {{time}}", {time: moment.format('H:mm')}).subscribe();
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		`;
 | 
							`;
 | 
				
			||||||
@@ -137,8 +194,7 @@ describe('ServiceParser', () => {
 | 
				
			|||||||
		expect(keys).to.deep.equal([]);
 | 
							expect(keys).to.deep.equal([]);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// FAILS (Use AstServiceParser)
 | 
						it('should extract string with params on same line', () => {
 | 
				
			||||||
	/*it('should extract string with params on same line', () => {
 | 
					 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			@Component({ })
 | 
								@Component({ })
 | 
				
			||||||
			export class AppComponent {
 | 
								export class AppComponent {
 | 
				
			||||||
@@ -150,6 +206,103 @@ describe('ServiceParser', () => {
 | 
				
			|||||||
		`;
 | 
							`;
 | 
				
			||||||
		const keys = parser.extract(contents, componentFilename).keys();
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
		expect(keys).to.deep.equal(['You are expected at {{time}}']);
 | 
							expect(keys).to.deep.equal(['You are expected at {{time}}']);
 | 
				
			||||||
	});*/
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should not crash when constructor parameter has no type', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								@Component({ })
 | 
				
			||||||
 | 
								export class AppComponent {
 | 
				
			||||||
 | 
									public constructor(protected _translateService) { }
 | 
				
			||||||
 | 
									public test() {
 | 
				
			||||||
 | 
										this._translateService.instant('Hello World');
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal([]);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should not extract variables', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								@Component({ })
 | 
				
			||||||
 | 
								export class AppComponent {
 | 
				
			||||||
 | 
									public constructor(protected translateService: TranslateService) { }
 | 
				
			||||||
 | 
									public test() {
 | 
				
			||||||
 | 
										this.translateService.get(["yes", variable]).then(translations => {
 | 
				
			||||||
 | 
											console.log(translations[variable]);
 | 
				
			||||||
 | 
										});
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['yes']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings from all classes in the file', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								import { Injectable } from '@angular/core';
 | 
				
			||||||
 | 
								import { TranslateService } from '@ngx-translate/core';
 | 
				
			||||||
 | 
								export class Stuff {
 | 
				
			||||||
 | 
									thing: string;
 | 
				
			||||||
 | 
									translate: any;
 | 
				
			||||||
 | 
									constructor(thing: string) {
 | 
				
			||||||
 | 
										this.translate.get('Not me');
 | 
				
			||||||
 | 
										this.thing = thing;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								@Injectable()
 | 
				
			||||||
 | 
								export class MyComponent {
 | 
				
			||||||
 | 
									constructor(public translate: TranslateService) {
 | 
				
			||||||
 | 
										this.translate.instant("Extract me!");
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								export class OtherClass {
 | 
				
			||||||
 | 
									constructor(thing: string, _translate: TranslateService) {
 | 
				
			||||||
 | 
										this._translate.get("Do not extract me");
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								@Injectable()
 | 
				
			||||||
 | 
								export class AuthService {
 | 
				
			||||||
 | 
									constructor(public translate: TranslateService) {
 | 
				
			||||||
 | 
										this.translate.instant("Hello!");
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Extract me!', 'Hello!']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings when TranslateService is declared as a property', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								export class MyComponent {
 | 
				
			||||||
 | 
									protected translateService: TranslateService;
 | 
				
			||||||
 | 
									public constructor() {
 | 
				
			||||||
 | 
										this.translateService = new TranslateService();
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									public test() {
 | 
				
			||||||
 | 
										this.translateService.instant('Hello World');
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Hello World']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract strings passed to TranslateServices methods only', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								export class AppComponent implements OnInit {
 | 
				
			||||||
 | 
									constructor(protected config: Config, protected translateService: TranslateService) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									public ngOnInit(): void {
 | 
				
			||||||
 | 
										this.localizeBackButton();
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									protected localizeBackButton(): void {
 | 
				
			||||||
 | 
										this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
 | 
				
			||||||
 | 
											this.config.set('backButtonText', this.translateService.instant('Back'));
 | 
				
			||||||
 | 
										});
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const keys = parser.extract(contents, componentFilename).keys();
 | 
				
			||||||
 | 
							expect(keys).to.deep.equal(['Back']);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,39 +1,20 @@
 | 
				
			|||||||
import { expect } from 'chai';
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { AbstractTemplateParser } from '../../src/parsers/abstract-template.parser';
 | 
					import { isPathAngularComponent, extractComponentInlineTemplate } from '../../src/utils/utils';
 | 
				
			||||||
 | 
					 | 
				
			||||||
class TestTemplateParser extends AbstractTemplateParser {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public isAngularComponent(filePath: string): boolean {
 | 
					 | 
				
			||||||
		return this._isAngularComponent(filePath);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public extractInlineTemplate(contents: string): string {
 | 
					 | 
				
			||||||
		return this._extractInlineTemplate(contents);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
describe('AbstractTemplateParser', () => {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	let parser: TestTemplateParser;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	beforeEach(() => {
 | 
					 | 
				
			||||||
		parser = new TestTemplateParser();
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('Utils', () => {
 | 
				
			||||||
	it('should recognize js extension as angular component', () => {
 | 
						it('should recognize js extension as angular component', () => {
 | 
				
			||||||
		const result = parser.isAngularComponent('test.js');
 | 
							const result = isPathAngularComponent('test.js');
 | 
				
			||||||
		expect(result).to.equal(true);
 | 
							expect(result).to.equal(true);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should recognize ts extension as angular component', () => {
 | 
						it('should recognize ts extension as angular component', () => {
 | 
				
			||||||
		const result = parser.isAngularComponent('test.ts');
 | 
							const result = isPathAngularComponent('test.ts');
 | 
				
			||||||
		expect(result).to.equal(true);
 | 
							expect(result).to.equal(true);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should not recognize html extension as angular component', () => {
 | 
						it('should not recognize html extension as angular component', () => {
 | 
				
			||||||
		const result = parser.isAngularComponent('test.html');
 | 
							const result = isPathAngularComponent('test.html');
 | 
				
			||||||
		expect(result).to.equal(false);
 | 
							expect(result).to.equal(false);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -45,10 +26,22 @@ describe('AbstractTemplateParser', () => {
 | 
				
			|||||||
			})
 | 
								})
 | 
				
			||||||
			export class TestComponent { }
 | 
								export class TestComponent { }
 | 
				
			||||||
		`;
 | 
							`;
 | 
				
			||||||
		const template = parser.extractInlineTemplate(contents);
 | 
							const template = extractComponentInlineTemplate(contents);
 | 
				
			||||||
		expect(template).to.equal('<p translate>Hello World</p>');
 | 
							expect(template).to.equal('<p translate>Hello World</p>');
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should extract inline template without html', () => {
 | 
				
			||||||
 | 
							const contents = `
 | 
				
			||||||
 | 
								@Component({
 | 
				
			||||||
 | 
									selector: 'test',
 | 
				
			||||||
 | 
									template: '{{ "Hello World" | translate }}'
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								export class TestComponent { }
 | 
				
			||||||
 | 
							`;
 | 
				
			||||||
 | 
							const template = extractComponentInlineTemplate(contents);
 | 
				
			||||||
 | 
							expect(template).to.equal('{{ "Hello World" | translate }}');
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should extract inline template spanning multiple lines', () => {
 | 
						it('should extract inline template spanning multiple lines', () => {
 | 
				
			||||||
		const contents = `
 | 
							const contents = `
 | 
				
			||||||
			@Component({
 | 
								@Component({
 | 
				
			||||||
@@ -66,8 +59,7 @@ describe('AbstractTemplateParser', () => {
 | 
				
			|||||||
			})
 | 
								})
 | 
				
			||||||
			export class TestComponent { }
 | 
								export class TestComponent { }
 | 
				
			||||||
		`;
 | 
							`;
 | 
				
			||||||
		const template = parser.extractInlineTemplate(contents);
 | 
							const template = extractComponentInlineTemplate(contents);
 | 
				
			||||||
		expect(template).to.equal('\n\t\t\t\t\t<p>\n\t\t\t\t\t\tHello World\n\t\t\t\t\t</p>\n\t\t\t\t');
 | 
							expect(template).to.equal('\n\t\t\t\t\t<p>\n\t\t\t\t\t\tHello World\n\t\t\t\t\t</p>\n\t\t\t\t');
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface';
 | 
				
			||||||
 | 
					import { KeyAsDefaultValuePostProcessor } from '../../src/post-processors/key-as-default-value.post-processor';
 | 
				
			||||||
 | 
					import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('KeyAsDefaultValuePostProcessor', () => {
 | 
				
			||||||
 | 
						let processor: PostProcessorInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						beforeEach(() => {
 | 
				
			||||||
 | 
							processor = new KeyAsDefaultValuePostProcessor();
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should use key as default value', () => {
 | 
				
			||||||
 | 
							const collection = new TranslationCollection({
 | 
				
			||||||
 | 
								'I have no value': '',
 | 
				
			||||||
 | 
								'I am already translated': 'Jeg er allerede oversat',
 | 
				
			||||||
 | 
								'Use this key as value as well': ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection();
 | 
				
			||||||
 | 
							const existing = new TranslationCollection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							expect(processor.process(collection, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'I have no value': 'I have no value',
 | 
				
			||||||
 | 
								'I am already translated': 'Jeg er allerede oversat',
 | 
				
			||||||
 | 
								'Use this key as value as well': 'Use this key as value as well'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@@ -0,0 +1,40 @@
 | 
				
			|||||||
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface';
 | 
				
			||||||
 | 
					import { NullAsDefaultValuePostProcessor } from '../../src/post-processors/null-as-default-value.post-processor';
 | 
				
			||||||
 | 
					import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('NullAsDefaultValuePostProcessor', () => {
 | 
				
			||||||
 | 
						let processor: PostProcessorInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						beforeEach(() => {
 | 
				
			||||||
 | 
							processor = new NullAsDefaultValuePostProcessor();
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should use null as default value', () => {
 | 
				
			||||||
 | 
							const draft = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							const existing = new TranslationCollection();
 | 
				
			||||||
 | 
							expect(processor.process(draft, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'String A': null
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should keep existing value even if it is an empty string', () => {
 | 
				
			||||||
 | 
							const draft = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							const existing = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							expect(processor.process(draft, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'String A': ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should keep existing value', () => {
 | 
				
			||||||
 | 
							const draft = new TranslationCollection({ 'String A': 'Streng A' });
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection({ 'String A': 'Streng A' });
 | 
				
			||||||
 | 
							const existing = new TranslationCollection({ 'String A': 'Streng A' });
 | 
				
			||||||
 | 
							expect(processor.process(draft, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'String A': 'Streng A'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface';
 | 
				
			||||||
 | 
					import { PurgeObsoleteKeysPostProcessor } from '../../src/post-processors/purge-obsolete-keys.post-processor';
 | 
				
			||||||
 | 
					import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('PurgeObsoleteKeysPostProcessor', () => {
 | 
				
			||||||
 | 
						let postProcessor: PostProcessorInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						beforeEach(() => {
 | 
				
			||||||
 | 
							postProcessor = new PurgeObsoleteKeysPostProcessor();
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should purge obsolete keys', () => {
 | 
				
			||||||
 | 
							const draft = new TranslationCollection({
 | 
				
			||||||
 | 
								'I am completely new': '',
 | 
				
			||||||
 | 
								'I already exist': '',
 | 
				
			||||||
 | 
								'I already exist but was not present in extract': ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection({
 | 
				
			||||||
 | 
								'I am completely new': '',
 | 
				
			||||||
 | 
								'I already exist': ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const existing = new TranslationCollection({
 | 
				
			||||||
 | 
								'I already exist': '',
 | 
				
			||||||
 | 
								'I already exist but was not present in extract': ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							expect(postProcessor.process(draft, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'I am completely new': '',
 | 
				
			||||||
 | 
								'I already exist': ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
							
								
								
									
										31
									
								
								tests/post-processors/sort-by-key.post-processor.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								tests/post-processors/sort-by-key.post-processor.spec.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
				
			|||||||
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface';
 | 
				
			||||||
 | 
					import { SortByKeyPostProcessor } from '../../src/post-processors/sort-by-key.post-processor';
 | 
				
			||||||
 | 
					import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('SortByKeyPostProcessor', () => {
 | 
				
			||||||
 | 
						let processor: PostProcessorInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						beforeEach(() => {
 | 
				
			||||||
 | 
							processor = new SortByKeyPostProcessor();
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should sort keys alphanumerically', () => {
 | 
				
			||||||
 | 
							const collection = new TranslationCollection({
 | 
				
			||||||
 | 
								z: 'last value',
 | 
				
			||||||
 | 
								a: 'a value',
 | 
				
			||||||
 | 
								'9': 'a numeric key',
 | 
				
			||||||
 | 
								b: 'another value'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection();
 | 
				
			||||||
 | 
							const existing = new TranslationCollection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							expect(processor.process(collection, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'9': 'a numeric key',
 | 
				
			||||||
 | 
								a: 'a value',
 | 
				
			||||||
 | 
								b: 'another value',
 | 
				
			||||||
 | 
								z: 'last value'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@@ -0,0 +1,40 @@
 | 
				
			|||||||
 | 
					import { expect } from 'chai';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import { PostProcessorInterface } from '../../src/post-processors/post-processor.interface';
 | 
				
			||||||
 | 
					import { StringAsDefaultValuePostProcessor } from '../../src/post-processors/string-as-default-value.post-processor';
 | 
				
			||||||
 | 
					import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('StringAsDefaultValuePostProcessor', () => {
 | 
				
			||||||
 | 
						let processor: PostProcessorInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						beforeEach(() => {
 | 
				
			||||||
 | 
							processor = new StringAsDefaultValuePostProcessor({ defaultValue: 'default' });
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should use string as default value', () => {
 | 
				
			||||||
 | 
							const draft = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							const existing = new TranslationCollection();
 | 
				
			||||||
 | 
							expect(processor.process(draft, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'String A': 'default'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should keep existing value even if it is an empty string', () => {
 | 
				
			||||||
 | 
							const draft = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							const existing = new TranslationCollection({ 'String A': '' });
 | 
				
			||||||
 | 
							expect(processor.process(draft, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'String A': ''
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should keep existing value', () => {
 | 
				
			||||||
 | 
							const draft = new TranslationCollection({ 'String A': 'Streng A' });
 | 
				
			||||||
 | 
							const extracted = new TranslationCollection({ 'String A': 'Streng A' });
 | 
				
			||||||
 | 
							const existing = new TranslationCollection({ 'String A': 'Streng A' });
 | 
				
			||||||
 | 
							expect(processor.process(draft, extracted, existing).values).to.deep.equal({
 | 
				
			||||||
 | 
								'String A': 'Streng A'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@@ -3,7 +3,6 @@ import { expect } from 'chai';
 | 
				
			|||||||
import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
					import { TranslationCollection } from '../../src/utils/translation.collection';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe('StringCollection', () => {
 | 
					describe('StringCollection', () => {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	let collection: TranslationCollection;
 | 
						let collection: TranslationCollection;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	beforeEach(() => {
 | 
						beforeEach(() => {
 | 
				
			||||||
@@ -64,12 +63,15 @@ describe('StringCollection', () => {
 | 
				
			|||||||
	it('should merge with other collection', () => {
 | 
						it('should merge with other collection', () => {
 | 
				
			||||||
		collection = collection.add('oldKey', 'oldVal');
 | 
							collection = collection.add('oldKey', 'oldVal');
 | 
				
			||||||
		const newCollection = new TranslationCollection({ newKey: 'newVal' });
 | 
							const newCollection = new TranslationCollection({ newKey: 'newVal' });
 | 
				
			||||||
		expect(collection.union(newCollection).values).to.deep.equal({ oldKey: 'oldVal', newKey: 'newVal' });
 | 
							expect(collection.union(newCollection).values).to.deep.equal({
 | 
				
			||||||
 | 
								oldKey: 'oldVal',
 | 
				
			||||||
 | 
								newKey: 'newVal'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should intersect with passed collection', () => {
 | 
						it('should intersect with passed collection', () => {
 | 
				
			||||||
		collection = collection.addKeys(['red', 'green', 'blue']);
 | 
							collection = collection.addKeys(['red', 'green', 'blue']);
 | 
				
			||||||
		const newCollection = new TranslationCollection( { red: '', blue: '' });
 | 
							const newCollection = new TranslationCollection({ red: '', blue: '' });
 | 
				
			||||||
		expect(collection.intersect(newCollection).values).to.deep.equal({ red: '', blue: '' });
 | 
							expect(collection.intersect(newCollection).values).to.deep.equal({ red: '', blue: '' });
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -79,10 +81,19 @@ describe('StringCollection', () => {
 | 
				
			|||||||
		expect(collection.intersect(newCollection).values).to.deep.equal({ red: 'rød', blue: 'blå' });
 | 
							expect(collection.intersect(newCollection).values).to.deep.equal({ red: 'rød', blue: 'blå' });
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	it('should sort translations in alphabetical order', () => {
 | 
						it('should sort keys alphabetically', () => {
 | 
				
			||||||
		collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' });
 | 
							collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' });
 | 
				
			||||||
		collection = collection.sort();
 | 
							collection = collection.sort();
 | 
				
			||||||
		expect(collection.keys()).deep.equal(['blue', 'green', 'red']);
 | 
							expect(collection.keys()).deep.equal(['blue', 'green', 'red']);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						it('should map values', () => {
 | 
				
			||||||
 | 
							collection = new TranslationCollection({ red: 'rød', green: 'grøn', blue: 'blå' });
 | 
				
			||||||
 | 
							collection = collection.map((key, val) => 'mapped value');
 | 
				
			||||||
 | 
							expect(collection.values).to.deep.equal({
 | 
				
			||||||
 | 
								red: 'mapped value',
 | 
				
			||||||
 | 
								green: 'mapped value',
 | 
				
			||||||
 | 
								blue: 'mapped value'
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,23 +1,25 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    "compilerOptions": {
 | 
					    "compilerOptions": {
 | 
				
			||||||
        "allowSyntheticDefaultImports": true,
 | 
					        "allowSyntheticDefaultImports": true,
 | 
				
			||||||
        "noUnusedLocals": true,
 | 
					 | 
				
			||||||
        "noImplicitAny": true,
 | 
					        "noImplicitAny": true,
 | 
				
			||||||
 | 
					        "noImplicitReturns": true,
 | 
				
			||||||
 | 
					        "noImplicitThis": true,
 | 
				
			||||||
 | 
					        "noImplicitUseStrict": true,
 | 
				
			||||||
        "removeComments": true,
 | 
					        "removeComments": true,
 | 
				
			||||||
        "declaration": true,
 | 
					        "declaration": true,
 | 
				
			||||||
        "target": "es5",
 | 
					        "target": "es2019",
 | 
				
			||||||
        "lib": [
 | 
					        "lib": [
 | 
				
			||||||
            "dom",
 | 
					            "es2019"
 | 
				
			||||||
            "es2015"
 | 
					 | 
				
			||||||
        ],
 | 
					        ],
 | 
				
			||||||
        "module": "commonjs",
 | 
					        "module": "commonjs",
 | 
				
			||||||
        "outDir": "./dist/",
 | 
					        "outDir": "dist",
 | 
				
			||||||
        "sourceMap": true
 | 
					        "sourceMap": true,
 | 
				
			||||||
 | 
					        "typeRoots" : [
 | 
				
			||||||
 | 
					            "node_modules/@types"
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "include": [
 | 
					    "include": [
 | 
				
			||||||
        "src/**/*.ts"
 | 
					        "src/**/*.ts"
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
    "exclude": [
 | 
					    "exclude": []
 | 
				
			||||||
        "node_modules"
 | 
					 | 
				
			||||||
    ]
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								tslint.commit.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								tslint.commit.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					  "extends": ["./tslint.json", "tslint-etc"],
 | 
				
			||||||
 | 
					  "jsRules": {},
 | 
				
			||||||
 | 
					  "rules": {
 | 
				
			||||||
 | 
					    "ordered-imports": false,
 | 
				
			||||||
 | 
					    "no-unused-declaration": true
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										125
									
								
								tslint.json
									
									
									
									
									
								
							
							
						
						
									
										125
									
								
								tslint.json
									
									
									
									
									
								
							@@ -1,53 +1,118 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	"rulesDirectory": [
 | 
					  "defaultSeverity": "error",
 | 
				
			||||||
		"node_modules/tslint-eslint-rules/dist/rules"
 | 
					  "extends": [
 | 
				
			||||||
 | 
					    "tslint-config-prettier"
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
  "rules": {
 | 
					  "rules": {
 | 
				
			||||||
		"indent": [true, "tabs"],
 | 
					    "arrow-return-shorthand": true,
 | 
				
			||||||
		"semicolon": [true, "always", "ignore-interfaces"],
 | 
					    "callable-types": true,
 | 
				
			||||||
		"quotemark": [true, "single", "avoid-escape"],
 | 
					    "class-name": true,
 | 
				
			||||||
		"only-arrow-functions": false,
 | 
					    "comment-format": [
 | 
				
			||||||
		"no-duplicate-variable": true,
 | 
					      true,
 | 
				
			||||||
		"member-access": true,
 | 
					      "check-space"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "curly": true,
 | 
				
			||||||
 | 
					    "deprecation": {
 | 
				
			||||||
 | 
					      "severity": "warn"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    "eofline": true,
 | 
				
			||||||
 | 
					    "forin": true,
 | 
				
			||||||
 | 
					    "import-spacing": true,
 | 
				
			||||||
 | 
					    "indent": [
 | 
				
			||||||
 | 
					      true,
 | 
				
			||||||
 | 
					      "tabs"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "interface-over-type-literal": true,
 | 
				
			||||||
 | 
					    "label-position": true,
 | 
				
			||||||
 | 
					    "max-line-length": [
 | 
				
			||||||
 | 
					      true,
 | 
				
			||||||
 | 
					      220
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "member-access": false,
 | 
				
			||||||
    "member-ordering": [
 | 
					    "member-ordering": [
 | 
				
			||||||
      true,
 | 
					      true,
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        "order": [
 | 
					        "order": [
 | 
				
			||||||
					"public-static-field",
 | 
					          "static-field",
 | 
				
			||||||
					"public-static-method",
 | 
					          "instance-field",
 | 
				
			||||||
					"protected-static-field",
 | 
					          "static-method",
 | 
				
			||||||
					"protected-static-method",
 | 
					          "instance-method"
 | 
				
			||||||
					"private-static-field",
 | 
					 | 
				
			||||||
					"private-static-method",
 | 
					 | 
				
			||||||
					"public-instance-field",
 | 
					 | 
				
			||||||
					"protected-instance-field",
 | 
					 | 
				
			||||||
					"private-instance-field",
 | 
					 | 
				
			||||||
					"constructor",
 | 
					 | 
				
			||||||
					"public-instance-method",
 | 
					 | 
				
			||||||
					"protected-instance-method",
 | 
					 | 
				
			||||||
					"private-instance-method"
 | 
					 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
		"curly": true,
 | 
					    "no-arg": true,
 | 
				
			||||||
		"eofline": true,
 | 
					    "no-bitwise": true,
 | 
				
			||||||
 | 
					    "no-console": [
 | 
				
			||||||
 | 
					      true,
 | 
				
			||||||
 | 
					      "debug",
 | 
				
			||||||
 | 
					      "info",
 | 
				
			||||||
 | 
					      "time",
 | 
				
			||||||
 | 
					      "timeEnd",
 | 
				
			||||||
 | 
					      "trace"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "no-construct": true,
 | 
				
			||||||
 | 
					    "no-debugger": true,
 | 
				
			||||||
 | 
					    "no-duplicate-super": true,
 | 
				
			||||||
 | 
					    "no-empty": false,
 | 
				
			||||||
 | 
					    "no-empty-interface": true,
 | 
				
			||||||
 | 
					    "no-eval": true,
 | 
				
			||||||
 | 
					    "no-inferrable-types": [
 | 
				
			||||||
 | 
					      false,
 | 
				
			||||||
 | 
					      "ignore-params"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "no-misused-new": true,
 | 
				
			||||||
 | 
					    "no-non-null-assertion": true,
 | 
				
			||||||
 | 
					    "no-shadowed-variable": true,
 | 
				
			||||||
 | 
					    "no-string-literal": false,
 | 
				
			||||||
 | 
					    "no-string-throw": true,
 | 
				
			||||||
 | 
					    "no-switch-case-fall-through": true,
 | 
				
			||||||
    "no-trailing-whitespace": true,
 | 
					    "no-trailing-whitespace": true,
 | 
				
			||||||
		"trailing-comma": [
 | 
					    "no-unnecessary-initializer": true,
 | 
				
			||||||
 | 
					    "no-unused-expression": true,
 | 
				
			||||||
 | 
					    "no-var-keyword": true,
 | 
				
			||||||
 | 
					    "object-literal-sort-keys": false,
 | 
				
			||||||
 | 
					    "one-line": [
 | 
				
			||||||
 | 
					      true,
 | 
				
			||||||
 | 
					      "check-open-brace",
 | 
				
			||||||
 | 
					      "check-catch",
 | 
				
			||||||
 | 
					      "check-else",
 | 
				
			||||||
 | 
					      "check-whitespace"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "prefer-const": true,
 | 
				
			||||||
 | 
					    "radix": true,
 | 
				
			||||||
 | 
					    "semicolon": [
 | 
				
			||||||
 | 
					      true,
 | 
				
			||||||
 | 
					      "always"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "triple-equals": [
 | 
				
			||||||
 | 
					      true,
 | 
				
			||||||
 | 
					      "allow-null-check"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
 | 
					    "typedef-whitespace": [
 | 
				
			||||||
      true,
 | 
					      true,
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
				"multiline": "never",
 | 
					        "call-signature": "nospace",
 | 
				
			||||||
				"singleline": "never"
 | 
					        "index-signature": "nospace",
 | 
				
			||||||
 | 
					        "parameter": "nospace",
 | 
				
			||||||
 | 
					        "property-declaration": "nospace",
 | 
				
			||||||
 | 
					        "variable-declaration": "nospace"
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
 | 
					    "unified-signatures": true,
 | 
				
			||||||
 | 
					    "variable-name": [
 | 
				
			||||||
 | 
					      true,
 | 
				
			||||||
 | 
					      "ban-keywords",
 | 
				
			||||||
 | 
					      "allow-pascal-case",
 | 
				
			||||||
 | 
					      "check-format"
 | 
				
			||||||
 | 
					    ],
 | 
				
			||||||
    "whitespace": [
 | 
					    "whitespace": [
 | 
				
			||||||
      true,
 | 
					      true,
 | 
				
			||||||
      "check-branch",
 | 
					      "check-branch",
 | 
				
			||||||
      "check-decl",
 | 
					      "check-decl",
 | 
				
			||||||
      "check-operator",
 | 
					      "check-operator",
 | 
				
			||||||
			"check-module",
 | 
					 | 
				
			||||||
      "check-separator",
 | 
					      "check-separator",
 | 
				
			||||||
			"check-type",
 | 
					      "check-type"
 | 
				
			||||||
			"check-typecast"
 | 
					    ],
 | 
				
			||||||
		]
 | 
					    "quotemark": [true, "single"]
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user