Skip to content

Commit bf7fe92

Browse files
committed
Rewrite as ES module
1 parent 68fde2e commit bf7fe92

File tree

12 files changed

+58
-48
lines changed

12 files changed

+58
-48
lines changed

bin/germknoedel

Lines changed: 0 additions & 3 deletions
This file was deleted.

bin/germknoedel.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
import '../lib/main.js';

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
module.exports = {
2-
calculate: require('./lib/calculate.js'),
3-
validate: require('./lib/validate')
1+
import calculate from './lib/calculate.js';
2+
import validate from './lib/validate.js';
3+
4+
export {
5+
calculate,
6+
validate
47
};

lib/authorities.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const fs = require('fs');
2-
const http = require('http');
3-
const zlib = require('zlib');
1+
import fs from 'fs';
2+
import http from 'http';
3+
import zlib from 'zlib';
44

5-
const { success, fail } = require('./feedback');
5+
import { success, fail } from './feedback.js';
6+
import { __dirname } from './util.js';
67

78
const url = 'http://www.pruefziffernberechnung.de/Begleitdokumente/BKZ.sh.gz';
89
const authorities = JSON.parse(fs.readFileSync(__dirname + '/../authorities.json'));
@@ -95,4 +96,4 @@ const update = () => {
9596
});
9697
};
9798

98-
module.exports = { authorities, query, update };
99+
export { authorities, query, update };

lib/calculate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const format = date => {
2727
].join('');
2828
};
2929

30-
module.exports = (serial = '0'.repeat(9), gender = 'x', dateOfBirth = new Date(0), dateOfExpiry = new Date()) => {
30+
export default (serial = '0'.repeat(9), gender = 'x', dateOfBirth = new Date(0), dateOfExpiry = new Date()) => {
3131
if (typeof serial === 'object') {
3232
const params = serial;
3333
var { serial = '0'.repeat(9), gender = 'x', dateOfBirth = new Date(0), dateOfExpiry = new Date() } = params;

lib/feedback.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
const chalk = require('chalk');
1+
import chalk from 'chalk';
22

3-
module.exports = {
4-
success: (...args) => {
5-
args.unshift('✅');
6-
console.log.apply(console, args.map(arg => chalk.green.bold(arg)));
7-
},
3+
export const success = (...args) => {
4+
args.unshift('✅');
5+
console.log.apply(console, args.map(arg => chalk.green.bold(arg)));
6+
};
87

9-
warn: (...args) => {
10-
args.unshift('⚠️');
11-
console.warn.apply(console, args.map(arg => chalk.yellow.bold(arg)));
12-
},
8+
export const warn = (...args) => {
9+
args.unshift('⚠️');
10+
console.warn.apply(console, args.map(arg => chalk.yellow.bold(arg)));
11+
};
1312

14-
fail: (...args) => {
15-
args.unshift('‼️');
16-
console.error.apply(console, args.map(arg => chalk.red.bold(arg)));
17-
}
13+
export const fail = (...args) => {
14+
args.unshift('‼️');
15+
console.error.apply(console, args.map(arg => chalk.red.bold(arg)));
1816
};

lib/help.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
const chalk = require('chalk');
2-
const commandLineUsage = require('command-line-usage');
1+
import chalk from 'chalk';
2+
import commandLineUsage from 'command-line-usage';
33

4-
module.exports = args => {
4+
export default args => {
55
const usage = commandLineUsage([
66
{
77
header: 'Name'.toUpperCase(),
88
content: 'germknoedel – generate passport codes.'
99
},
1010
{
1111
header: 'Synopsis'.toUpperCase(),
12-
content: `germknoedel <date-of-birth> <date-of-expiry>
13-
germknodel (alternative spelling)`
12+
content: 'germknoedel <date-of-birth> <date-of-expiry>'
1413
},
1514
{
1615
header: 'Examples'.toUpperCase(),

lib/main.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
const chalk = require('chalk');
2-
const commandLineArgs = require('command-line-args');
1+
import chalk from 'chalk';
2+
import commandLineArgs from 'command-line-args';
3+
import fs from 'fs';
34

4-
const calculate = require('./calculate');
5-
const { fail } = require('./feedback');
6-
const help = require('./help');
7-
const package = require('../package.json');
8-
const { query, update } = require('./authorities');
9-
const validate = require('./validate');
5+
import calculate from './calculate.js';
6+
import { fail } from './feedback.js';
7+
import help from './help.js';
8+
import { query, update } from './authorities.js';
9+
import validate from './validate.js';
10+
11+
import { __dirname } from './util.js';
12+
13+
const module = JSON.parse(fs.readFileSync(__dirname + '/../package.json'));
1014

1115
const mainArgs = [
1216
{ name: 'authority', alias: 'a', type: String, description: 'The ID of the issuing authority' },
@@ -33,7 +37,7 @@ args.dateOfExpiry = argv[1];
3337
if (args.help) {
3438
help(mainArgs);
3539
} else if (args.version) {
36-
console.log(package.version)
40+
console.log(module.version);
3741
} else if (args.update) {
3842
update();
3943
} else if (typeof args.query !== 'undefined') {

lib/util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { fileURLToPath } from 'url';
2+
import { dirname } from 'path';
3+
4+
export const __filename = fileURLToPath(import.meta.url);
5+
export const __dirname = dirname(__filename);

lib/validate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { authorities } = require('./authorities');
2-
const { warn } = require('./feedback');
1+
import { authorities } from './authorities.js';
2+
import { warn } from './feedback.js';
33

44
const CHARACTERS = '0123456789abcdefghijklmnpqrstuvwxyz';
55
const GENDERS = { female: 'f', male: 'm', unspecified: 'x' };
@@ -15,7 +15,7 @@ const randomDate = () => {
1515
return new Date(randomInt(new Date().getFullYear()), randomInt(12) - 1, randomInt(31));
1616
};
1717

18-
module.exports = args => {
18+
export default args => {
1919
const validated = Object.assign({}, args);
2020

2121
if (validated.dateOfBirth) {

0 commit comments

Comments
 (0)