-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
80 lines (75 loc) · 2.35 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env node
const shelljs = require('shelljs')
const { getFiles, extractTextContent, generateKeyValue, generateFile, generateTemplates } = require('./files')
require('yargs')
.command('input [folder]', 'Extract text from folder', (yargs) => {
yargs
.positional('folder', {
describe: 'port to bind on',
type: 'string'
})
}, (argv) => {
if (argv.verbose) console.info(`List :${argv.folder}`)
processInputCommand({
input: argv.folder,
separator: argv.separator,
output: argv.output,
templates: argv.templates,
v: argv.verbose,
language: argv.lang
})
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging'
})
.option('output', {
alias: 'o',
type: 'string',
default: './',
description: 'Output directory.'
})
.option('templates', {
alias: 't',
type: 'string',
default: 'en,es',
description: 'Generate template for other languages.'
})
.option('lang', {
alias: 'l',
type: 'string',
default: 'en',
description: 'Language of the texts to be extracted.'
})
.option('separator', {
alias: 's',
type: 'string',
default: '_',
description: 'Separator to generate the key names.'
})
.argv
/**
* @param {{
input: string,
separator?: string,
output: string,
templates: string,
language: string,
v: boolean,
}} args
*/
function processInputCommand(args) {
const REGEX_HTML_FINDER = /(.*\.html$)/
const list = shelljs.ls('-R', args.input)
const htmls = getFiles(list, REGEX_HTML_FINDER).map(filePath => args.input.concat('/').concat(filePath))
const extractedText = extractTextContent(htmls)
const generatedContent = generateKeyValue(extractedText, args.separator)
const basePath = args.output + `${args.language.concat('.json')}`;
if (args.v) {
console.log('Path:', args.output)
console.log('Texts extracted:', Object.keys(generatedContent).length)
}
generateFile(args.output, basePath, JSON.stringify(generatedContent, null, 2))
generateTemplates(args.output, args.templates, generatedContent)
}