Allow --output to be either a directory or a full path including filename
This commit is contained in:
parent
d786aa5f64
commit
031890c2a4
@ -12,22 +12,30 @@ import * as path from 'path';
|
|||||||
import * as cli from 'cli';
|
import * as cli from 'cli';
|
||||||
|
|
||||||
const options = cli.parse({
|
const options = cli.parse({
|
||||||
dir: ['d', 'Directory path you would like to extract strings from', 'dir', process.env.PWD],
|
dir: ['d', 'Path you would like to extract strings from', 'dir', process.env.PWD],
|
||||||
output: ['o', 'Directory path you would like to save extracted strings to', 'dir', process.env.PWD],
|
output: ['o', 'Path you would like to save extracted strings to', 'dir', process.env.PWD],
|
||||||
format: ['f', 'Output format', ['json', 'pot'], 'json'],
|
format: ['f', 'Output format', ['json', 'pot'], 'json'],
|
||||||
replace: ['r', 'Replace the contents of output file if it exists (Merges by default)', 'boolean', false],
|
replace: ['r', 'Replace the contents of output file if it exists (Merges by default)', 'boolean', false],
|
||||||
clean: ['c', 'Remove obsolete strings when merging', 'boolean', false]
|
clean: ['c', 'Remove obsolete strings when merging', 'boolean', false]
|
||||||
});
|
});
|
||||||
|
|
||||||
[options.dir, options.output].forEach(dir => {
|
const normalizedDir: string = path.resolve(options.dir);
|
||||||
if (!fs.existsSync(dir)) {
|
const normalizedOutput: string = path.resolve(options.output);
|
||||||
cli.fatal(`The directory path you supplied was not found: '${dir}'`);
|
|
||||||
|
let outputDir: string = normalizedOutput;
|
||||||
|
let outputFilename: string = `template.${options.format}`;
|
||||||
|
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}'`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const filename: string = 'template.' + options.format;
|
|
||||||
const dest: string = path.join(options.output, filename);
|
|
||||||
|
|
||||||
const parsers: ParserInterface[] = [
|
const parsers: ParserInterface[] = [
|
||||||
new PipeParser(),
|
new PipeParser(),
|
||||||
new DirectiveParser(),
|
new DirectiveParser(),
|
||||||
@ -41,9 +49,9 @@ const patterns: string[] = [
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const extractor: Extractor = new Extractor(parsers, patterns);
|
const extractor: Extractor = new Extractor(parsers, patterns);
|
||||||
cli.info(`Extracting strings from '${options.dir}'`);
|
cli.info(`Extracting strings from '${normalizedDir}'`);
|
||||||
|
|
||||||
const extracted: TranslationCollection = extractor.process(options.dir);
|
const extracted: TranslationCollection = extractor.process(normalizedDir);
|
||||||
cli.ok(`* Extracted ${extracted.count()} strings`);
|
cli.ok(`* Extracted ${extracted.count()} strings`);
|
||||||
|
|
||||||
let collection: TranslationCollection = extracted;
|
let collection: TranslationCollection = extracted;
|
||||||
@ -53,8 +61,8 @@ try {
|
|||||||
compiler = new PoCompiler();
|
compiler = new PoCompiler();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!options.replace && fs.existsSync(dest)) {
|
if (!options.replace && fs.existsSync(outputPath)) {
|
||||||
const existing: TranslationCollection = compiler.parse(fs.readFileSync(dest, 'utf-8'));
|
const existing: TranslationCollection = compiler.parse(fs.readFileSync(outputPath, 'utf-8'));
|
||||||
if (existing.count() > 0) {
|
if (existing.count() > 0) {
|
||||||
collection = extracted.union(existing);
|
collection = extracted.union(existing);
|
||||||
cli.ok(`* Merged with ${existing.count()} existing strings`);
|
cli.ok(`* Merged with ${existing.count()} existing strings`);
|
||||||
@ -70,8 +78,8 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFileSync(dest, compiler.compile(collection));
|
fs.writeFileSync(outputPath, compiler.compile(collection));
|
||||||
cli.ok(`* Saved to '${dest}'`);
|
cli.ok(`* Saved to '${outputPath}'`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
cli.fatal(e.toString());
|
cli.fatal(e.toString());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user