-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-mail.js
executable file
·62 lines (55 loc) · 1.77 KB
/
make-mail.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
#!/usr/bin/env node
const program = require('commander');
const merge = require('lodash.merge');
const inquirer = require('inquirer');
const { version } = require('./package.json');
const fs = require('fs');
const { promisify } = require('util');
const trim = require('lodash.trim');
const pickBy = require('lodash.pickby');
const transform = require('lodash.transform');
const buildMail = require('./build-mail.js');
const configWalker = require('./config-walker.js');
const writeFile = promisify(fs.writeFile);
const { output, args: [file] } = program
.version(version)
.description('Make tool to build mail')
.option('-o, --output <file>', 'Output file')
.option('-l, --locale <locale>', 'Locale')
.arguments('<file>')
.parse(process.argv);
const locale = program.locale || (process.env.LANG || '').substr(0, 2) || 'en';
const environnements = transform(
pickBy(process.env || {}, (v, k) => k.startsWith('__')),
(result, value, key) => { result[trim(key, '_').toLowerCase()] = value; },
{},
);
if (!file) {
throw new Error('file is missing');
}
(async () => {
const config = await configWalker(file, process.cwd());
merge(config.vars, environnements);
const prompted = await inquirer.prompt((config
.questions || [])
.map((question) => {
if (typeof question === 'string') {
return {
type: 'input',
name: question,
message: question,
};
}
return question;
})
.filter(({ name }) => !config.vars[name]));
const html = await buildMail(config, prompted, { locale });
const keys = Object.keys(html);
if (output) {
await Promise.all(keys.map((async (k) => {
return writeFile(`${output}.${k}`, html[k]);
})));
} else {
keys.forEach(k => process.stdin.write(html[k]));
}
})();