Add cli option to customize compiler indentation. Closes #22
This commit is contained in:
		
							
								
								
									
										34
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								README.md
									
									
									
									
									
								
							| @@ -17,7 +17,7 @@ Add an `extract` script to your project's `package.json`: | |||||||
| ``` | ``` | ||||||
| You can now run `npm run extract` to extract strings. | You can now run `npm run extract` to extract strings. | ||||||
|  |  | ||||||
| ## Commandline examples | ## Extract examples | ||||||
|  |  | ||||||
| **Extract from dir and save to file** | **Extract from dir and save to file** | ||||||
|  |  | ||||||
| @@ -39,6 +39,17 @@ You can now run `npm run extract` to extract strings. | |||||||
|  |  | ||||||
| `ngx-translate-extract -i ./src -o ./src/i18n/*.json` | `ngx-translate-extract -i ./src -o ./src/i18n/*.json` | ||||||
|  |  | ||||||
|  | **or (update only)** | ||||||
|  |  | ||||||
|  | ## Custom indentation | ||||||
|  | By default, tabs are used for indentation when writing extracted strings to json formats: | ||||||
|  |  | ||||||
|  | `ngx-translate-extract -i ./src -o ./src/i18n/en.json --format-indentation $'\t'` | ||||||
|  |  | ||||||
|  | If you want to use spaces instead, you can do the following: | ||||||
|  |  | ||||||
|  | `ngx-translate-extract -i ./src -o ./src/i18n/en.json --format-indentation '  '` | ||||||
|  |  | ||||||
|  |  | ||||||
| Modify the scripts arguments as required. | Modify the scripts arguments as required. | ||||||
|  |  | ||||||
| @@ -50,18 +61,21 @@ ngx-translate-extract [options] | |||||||
| Options: | Options: | ||||||
|   --version, -v               Show version number                      [boolean] |   --version, -v               Show version number                      [boolean] | ||||||
|   --help, -h                  Show help                                [boolean] |   --help, -h                  Show help                                [boolean] | ||||||
|   --input, -i     Paths you would like to extract strings from. You can use path |   --input, -i                 Paths you would like to extract strings from. You | ||||||
|                   expansion, glob patterns and multiple paths |                               can use path expansion, glob patterns and multiple | ||||||
|                      [array] [default: "/Users/kim/ionic/ngx-translate-extract"] |                               paths | ||||||
|  |                      [array] [default: current working path] | ||||||
|   --patterns, -p              Extract strings from the following file patterns |   --patterns, -p              Extract strings from the following file patterns | ||||||
|                                     [array] [default: ["/**/*.html","/**/*.ts"]] |                                     [array] [default: ["/**/*.html","/**/*.ts"]] | ||||||
|   --output, -o    Paths where you would like to save extracted strings. You can |   --output, -o                Paths where you would like to save extracted | ||||||
|                   use path expansion, glob patterns and multiple paths |                               strings. You can use path expansion, glob patterns | ||||||
|                                                               [array] [required] |                               and multiple paths              [array] [required] | ||||||
|   --format, -f                Output format |   --format, -f                Output format | ||||||
|           [string] [choices: "json", "namespaced-json", "pot"] [default: "json"] |           [string] [choices: "json", "namespaced-json", "pot"] [default: "json"] | ||||||
|   --replace, -r   Replace the contents of output file if it exists (Merges by |   --format-indentation, --fi  Output format indentation [string] [default: "\t"] | ||||||
|                   default)                            [boolean] [default: false] |   --replace, -r               Replace the contents of output file if it exists | ||||||
|  |                               (Merges by default)     [boolean] [default: false] | ||||||
|   --sort, -s                  Sort strings in alphabetical order when saving |   --sort, -s                  Sort strings in alphabetical order when saving | ||||||
|                                                       [boolean] [default: false] |                                                       [boolean] [default: false] | ||||||
|   --clean, -c     Remove obsolete strings when merging[boolean] [default: false] |   --clean, -c                 Remove obsolete strings when merging | ||||||
|  |                                                       [boolean] [default: false] | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "@biesbjerg/ngx-translate-extract", |   "name": "@biesbjerg/ngx-translate-extract", | ||||||
|   "version": "2.0.1", |   "version": "2.1.0", | ||||||
|   "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", | ||||||
| @@ -50,17 +50,17 @@ | |||||||
|     "@types/chai": "3.4.35", |     "@types/chai": "3.4.35", | ||||||
|     "@types/glob": "5.0.30", |     "@types/glob": "5.0.30", | ||||||
|     "@types/mocha": "2.2.40", |     "@types/mocha": "2.2.40", | ||||||
|     "@types/cheerio": "0.22.0", |     "@types/cheerio": "0.22.1", | ||||||
|     "@types/chalk": "0.4.31", |     "@types/chalk": "0.4.31", | ||||||
|     "@types/flat": "0.0.28", |     "@types/flat": "0.0.28", | ||||||
|     "@types/yargs": "6.6.0", |     "@types/yargs": "6.6.0", | ||||||
|     "@types/mkdirp": "0.3.29", |     "@types/mkdirp": "0.3.29", | ||||||
|     "chai": "3.5.0", |     "chai": "3.5.0", | ||||||
|     "mocha": "3.2.0", |     "mocha": "3.2.0", | ||||||
|     "ts-node": "2.1.0", |     "ts-node": "3.0.2", | ||||||
|     "tslint": "4.5.1", |     "tslint": "4.5.1", | ||||||
|     "tslint-eslint-rules": "3.5.1", |     "tslint-eslint-rules": "3.5.1", | ||||||
|     "typescript": "2.2.1" |     "typescript": "2.2.2" | ||||||
|   }, |   }, | ||||||
|   "dependencies": { |   "dependencies": { | ||||||
|     "chalk": "1.1.3", |     "chalk": "1.1.3", | ||||||
|   | |||||||
| @@ -1,7 +1,10 @@ | |||||||
| import { ExtractTask } from './tasks/extract.task'; | import { ExtractTask } from './tasks/extract.task'; | ||||||
|  | import { ParserInterface } from '../parsers/parser.interface'; | ||||||
| import { PipeParser } from '../parsers/pipe.parser'; | import { PipeParser } from '../parsers/pipe.parser'; | ||||||
| import { DirectiveParser } from '../parsers/directive.parser'; | import { DirectiveParser } from '../parsers/directive.parser'; | ||||||
| import { ServiceParser } from '../parsers/service.parser'; | import { ServiceParser } from '../parsers/service.parser'; | ||||||
|  | import { CompilerInterface } from '../compilers/compiler.interface'; | ||||||
|  | import { CompilerFactory } from '../compilers/compiler.factory'; | ||||||
|  |  | ||||||
| import * as fs from 'fs'; | import * as fs from 'fs'; | ||||||
| import * as yargs from 'yargs'; | import * as yargs from 'yargs'; | ||||||
| @@ -48,6 +51,12 @@ export const cli = yargs | |||||||
| 		type: 'string', | 		type: 'string', | ||||||
| 		choices: ['json', 'namespaced-json', 'pot'] | 		choices: ['json', 'namespaced-json', 'pot'] | ||||||
| 	}) | 	}) | ||||||
|  | 	.option('format-indentation', { | ||||||
|  | 		alias: 'fi', | ||||||
|  | 		describe: 'Output format indentation', | ||||||
|  | 		default: '\t', | ||||||
|  | 		type: 'string' | ||||||
|  | 	}) | ||||||
| 	.option('replace', { | 	.option('replace', { | ||||||
| 		alias: 'r', | 		alias: 'r', | ||||||
| 		describe: 'Replace the contents of output file if it exists (Merges by default)', | 		describe: 'Replace the contents of output file if it exists (Merges by default)', | ||||||
| @@ -69,19 +78,22 @@ export const cli = yargs | |||||||
| 	.exitProcess(true) | 	.exitProcess(true) | ||||||
| 	.parse(process.argv); | 	.parse(process.argv); | ||||||
|  |  | ||||||
| const extractTask = new ExtractTask(cli.input, cli.output, { | const parsers: ParserInterface[] = [ | ||||||
|  | 	new ServiceParser(), | ||||||
|  | 	new PipeParser(), | ||||||
|  | 	new DirectiveParser() | ||||||
|  | ]; | ||||||
|  |  | ||||||
|  | const compiler: CompilerInterface = CompilerFactory.create(cli.format, { | ||||||
|  | 	indentation: cli.formatIndentation | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | new ExtractTask(cli.input, cli.output, { | ||||||
| 	replace: cli.replace, | 	replace: cli.replace, | ||||||
| 	sort: cli.sort, | 	sort: cli.sort, | ||||||
| 	clean: cli.clean, | 	clean: cli.clean, | ||||||
| 	patterns: cli.patterns | 	patterns: cli.patterns | ||||||
| }); | }) | ||||||
|  | .setParsers(parsers) | ||||||
| extractTask | .setCompiler(compiler) | ||||||
| 	.setParsers([ |  | ||||||
| 		new ServiceParser(), |  | ||||||
| 		new PipeParser(), |  | ||||||
| 		new DirectiveParser() |  | ||||||
| 	]) |  | ||||||
| 	.setCompiler(cli.format) |  | ||||||
| .execute(); | .execute(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,7 +2,6 @@ import { TranslationCollection } from '../../utils/translation.collection'; | |||||||
| import { TaskInterface } from './task.interface'; | import { TaskInterface } from './task.interface'; | ||||||
| import { ParserInterface } from '../../parsers/parser.interface'; | import { ParserInterface } from '../../parsers/parser.interface'; | ||||||
| import { CompilerInterface } from '../../compilers/compiler.interface'; | import { CompilerInterface } from '../../compilers/compiler.interface'; | ||||||
| import { CompilerFactory } from '../../compilers/compiler.factory'; |  | ||||||
|  |  | ||||||
| import * as chalk from 'chalk'; | import * as chalk from 'chalk'; | ||||||
| import * as glob from 'glob'; | import * as glob from 'glob'; | ||||||
| @@ -56,13 +55,8 @@ export class ExtractTask implements TaskInterface { | |||||||
| 		return this; | 		return this; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public setCompiler(compiler: CompilerInterface | string): this { | 	public setCompiler(compiler: CompilerInterface): this { | ||||||
| 		if (typeof compiler === 'string') { |  | ||||||
| 			this._compiler = CompilerFactory.create(compiler) |  | ||||||
| 		} else { |  | ||||||
| 		this._compiler = compiler; | 		this._compiler = compiler; | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		return this; | 		return this; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -70,10 +64,9 @@ export class ExtractTask implements TaskInterface { | |||||||
| 	 * Extract strings from input dirs using configured parsers | 	 * Extract strings from input dirs using configured parsers | ||||||
| 	 */ | 	 */ | ||||||
| 	protected _extract(): TranslationCollection { | 	protected _extract(): TranslationCollection { | ||||||
| 		let collection: TranslationCollection = new TranslationCollection(); |  | ||||||
|  |  | ||||||
| 		this._out(chalk.bold('Extracting strings...')); | 		this._out(chalk.bold('Extracting strings...')); | ||||||
|  |  | ||||||
|  | 		let collection: TranslationCollection = new TranslationCollection(); | ||||||
| 		this._input.forEach(dir => { | 		this._input.forEach(dir => { | ||||||
| 			this._readDir(dir, this._options.patterns).forEach(path => { | 			this._readDir(dir, this._options.patterns).forEach(path => { | ||||||
| 				this._out(chalk.gray('- %s'), path); | 				this._out(chalk.gray('- %s'), path); | ||||||
| @@ -83,6 +76,7 @@ export class ExtractTask implements TaskInterface { | |||||||
| 				}); | 				}); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		return collection; | 		return collection; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,11 +5,11 @@ import { PoCompiler } from '../compilers/po.compiler'; | |||||||
|  |  | ||||||
| export class CompilerFactory { | export class CompilerFactory { | ||||||
|  |  | ||||||
| 	public static create(format: string): CompilerInterface { | 	public static create(format: string, options?: {}): CompilerInterface { | ||||||
| 		switch (format) { | 		switch (format) { | ||||||
| 			case 'pot': return new PoCompiler(); | 			case 'pot': return new PoCompiler(options); | ||||||
| 			case 'json': return new JsonCompiler(); | 			case 'json': return new JsonCompiler(options); | ||||||
| 			case 'namespaced-json': return new NamespacedJsonCompiler(); | 			case 'namespaced-json': return new NamespacedJsonCompiler(options); | ||||||
| 			default: throw new Error(`Unknown format: ${format}`); | 			default: throw new Error(`Unknown format: ${format}`); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -3,10 +3,18 @@ import { TranslationCollection } from '../utils/translation.collection'; | |||||||
|  |  | ||||||
| export class JsonCompiler implements CompilerInterface { | export class JsonCompiler implements CompilerInterface { | ||||||
|  |  | ||||||
|  | 	public indentation: string = '\t'; | ||||||
|  |  | ||||||
| 	public extension = 'json'; | 	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 { | ||||||
| 		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 { | ||||||
|   | |||||||
| @@ -5,13 +5,21 @@ import * as flat from 'flat'; | |||||||
|  |  | ||||||
| export class NamespacedJsonCompiler implements CompilerInterface { | export class NamespacedJsonCompiler implements CompilerInterface { | ||||||
|  |  | ||||||
|  | 	public indentation: string = '\t'; | ||||||
|  |  | ||||||
| 	public extension = 'json'; | 	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: {} = flat.unflatten(collection.values, { | ||||||
| 			object: true | 			object: true | ||||||
| 		}); | 		}); | ||||||
| 		return JSON.stringify(values, null, '\t'); | 		return JSON.stringify(values, null, this.indentation); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public parse(contents: string): TranslationCollection { | 	public parse(contents: string): TranslationCollection { | ||||||
|   | |||||||
| @@ -12,6 +12,8 @@ export class PoCompiler implements CompilerInterface { | |||||||
| 	 */ | 	 */ | ||||||
| 	public domain = ''; | 	public domain = ''; | ||||||
|  |  | ||||||
|  | 	public constructor(options?: any) { } | ||||||
|  |  | ||||||
| 	public compile(collection: TranslationCollection): string { | 	public compile(collection: TranslationCollection): string { | ||||||
| 		const data = { | 		const data = { | ||||||
| 			charset: 'utf-8', | 			charset: 'utf-8', | ||||||
|   | |||||||
| @@ -45,4 +45,16 @@ describe('NamespacedJsonCompiler', () => { | |||||||
| 		expect(result).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}'); | 		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}'); | ||||||
|  | 	}); | ||||||
|  | 
 | ||||||
| }); | }); | ||||||
		Reference in New Issue
	
	Block a user