Add extract cli script
This commit is contained in:
		
							
								
								
									
										46
									
								
								bin/extract.js
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										46
									
								
								bin/extract.js
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| #! /usr/bin/env node | ||||
|  | ||||
| var cli = require('cli'); | ||||
| var fs = require('fs'); | ||||
| var path = require('path'); | ||||
|  | ||||
| var Extractor = require('../dist/extractor').Extractor; | ||||
| var JsonSerializer = require('../dist/serializers/json.serializer').JsonSerializer; | ||||
| var PotSerializer = require('../dist/serializers/pot.serializer').PotSerializer; | ||||
|  | ||||
| var options = cli.parse({ | ||||
| 	dir: ['d', 'Directory path you would like to extract strings from', 'dir', process.env.PWD], | ||||
| 	output: ['o', 'Directory path you would like to save extracted strings', 'dir', process.env.PWD], | ||||
| 	format: ['f', 'Output format', ['json', 'pot'], 'json'] | ||||
| }); | ||||
|  | ||||
| [options.dir, options.output].forEach(dir => { | ||||
| 	if (!fs.existsSync(dir)) { | ||||
| 		cli.fatal('The directory path you supplied was not found: ' + dir); | ||||
| 	} | ||||
| }); | ||||
|  | ||||
| switch (options.format) { | ||||
| 	case 'pot': | ||||
| 		var serializer = new PotSerializer(); | ||||
| 		break; | ||||
| 	case 'json': | ||||
| 		var serializer = new JsonSerializer(); | ||||
| 		break; | ||||
| } | ||||
|  | ||||
| var filename = 'template.' + options.format; | ||||
| var destination = path.join(options.output, filename); | ||||
|  | ||||
| try { | ||||
| 	var extractor = new Extractor(serializer); | ||||
| 	var messages = extractor.extract(options.dir); | ||||
| 	if (messages.length > 0) { | ||||
| 		extractor.save(destination); | ||||
| 		cli.ok(`Extracted ${messages.length} strings: '${destination}'`); | ||||
| 	} else { | ||||
| 		cli.info(`Found no strings extractable strings in the provided path: '${options.dir}'`); | ||||
| 	} | ||||
| } catch (e) { | ||||
| 	cli.fatal(e.toString()); | ||||
| } | ||||
							
								
								
									
										20
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								package.json
									
									
									
									
									
								
							| @@ -4,9 +4,17 @@ | ||||
|   "description": "Extract strings", | ||||
|   "main": "dist/index.js", | ||||
|   "typings": "dist/index.d.ts", | ||||
|   "files": [ | ||||
|     "bin/", | ||||
|     "dist/" | ||||
|   ], | ||||
|   "bin": { | ||||
|     "ng2-translate-extract": "bin/extract.js" | ||||
|   }, | ||||
|   "scripts": { | ||||
|     "build": "tsc", | ||||
|     "watch": "tsc -w", | ||||
|     "build": "npm run clean && tsc", | ||||
|     "watch": "npm run clean && tsc --watch", | ||||
|     "clean": "rm -rf ./dist", | ||||
|     "postinstall": "npm run build" | ||||
|   }, | ||||
|   "repository": { | ||||
| @@ -21,17 +29,19 @@ | ||||
|   }, | ||||
|   "homepage": "https://github.com/biesbjerg/ng2-translate-extract", | ||||
|   "engines": { | ||||
|     "node": ">=4.1.1" | ||||
|     "node": ">=6.9.0" | ||||
|   }, | ||||
|   "config": {}, | ||||
|   "devDependencies": { | ||||
|     "typescript": "~2.0.10" | ||||
|   }, | ||||
|   "config": {}, | ||||
|   "dependencies": { | ||||
|     "@types/glob": "^5.0.30", | ||||
|     "@types/lodash": "^4.14.41", | ||||
|     "cli": "^1.0.1", | ||||
|     "fs": "0.0.1-security", | ||||
|     "glob": "^7.1.1", | ||||
|     "lodash": "^4.17.2" | ||||
|     "lodash": "^4.17.2", | ||||
|     "path": "^0.12.7" | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user