From fbc92c33359a095ab18b37138409cd160e51365a Mon Sep 17 00:00:00 2001 From: Kim Biesbjerg Date: Sun, 4 Dec 2016 22:54:35 +0100 Subject: [PATCH] Add extract cli script --- bin/extract.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 20 +++++++++++++++----- 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100755 bin/extract.js diff --git a/bin/extract.js b/bin/extract.js new file mode 100755 index 0000000..894550b --- /dev/null +++ b/bin/extract.js @@ -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()); +} diff --git a/package.json b/package.json index ba504d5..e6fb456 100644 --- a/package.json +++ b/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" } }