@e-invoice-eu/cli
Version:
Generate e-invoices (E-Rechnung in German) conforming to EN16931 (Factur-X/ZUGFeRD, UBL, CII, XRechnung aka X-Rechnung) from LibreOffice Calc/Excel data or JSON.
183 lines • 6.07 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validate = void 0;
const runtime_1 = require("@esgettext/runtime");
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs/promises"));
const optspec_1 = require("../optspec");
const package_1 = require("../package");
const gtx = runtime_1.Textdomain.getInstance('e-invoice-eu-cli');
const options = {
url: {
group: gtx._('Server Location'),
alias: ['u'],
type: 'string',
demandOption: false,
describe: gtx._('base URL of the server'),
default: 'http://localhost:8080',
},
verbose: {
group: gtx._('Mode of Operation'),
alias: ['v'],
type: 'boolean',
conflicts: 'quiet',
demandOption: false,
describe: gtx._('output all reports'),
},
quiet: {
group: gtx._('Mode of Operation'),
alias: ['q'],
type: 'boolean',
conflicts: 'verbose',
demandOption: false,
describe: gtx._('suppress output'),
},
};
class Validate {
synopsis() {
return '<invoice> [invoices...]';
}
description() {
return gtx._('Validate invoices.');
}
aliases() {
return [];
}
build(argv) {
return argv.options(options);
}
async validate(url, filename, configOptions) {
const mimeType = filename.match(/\.pdf$/i)
? 'application/pdf'
: 'application/xml';
const body = await fs.readFile(filename);
const form = new FormData();
const blob = new Blob([body], { type: mimeType });
form.append('invoice', blob, filename);
let response;
try {
response = await fetch(url, {
method: 'POST',
body: form,
});
}
catch (error) {
console.error(`${filename}:`, chalk_1.default.bold.red('✗ ' +
gtx._x('invalid. Is the validation server running at {url}? Error: {error}.', {
filename,
url,
error,
})));
return false;
}
if (response.status === 200) {
if (!configOptions.quiet) {
console.log(`${filename}:`, chalk_1.default.green('✓ ' + gtx._('valid')));
}
if (configOptions.verbose) {
console.log(await response.text());
}
}
else {
if (!configOptions.quiet) {
console.error(`${filename}:`, chalk_1.default.bold.red('✗ ' + gtx._('invalid')));
console.error(await response.text());
}
return false;
}
return true;
}
async doRun(configOptions) {
const url = new URL(configOptions.url);
url.pathname = '/validate';
const invoices = [configOptions.invoice, ...configOptions.invoices];
let success = 0;
let errors = 0;
for (const invoice of invoices) {
try {
if (await this.validate(url, invoice, configOptions)) {
++success;
}
else {
++errors;
}
}
catch (e) {
if (!configOptions.quiet) {
console.error(e);
}
++errors;
}
}
if (!configOptions.quiet) {
console.log(gtx._nx('One invoice is valid', '{num} invoices are valid', success, {
num: success.toString(),
}));
if (errors) {
console.log(gtx._nx('One invoice is invalid', '{num} invoices are invalid', errors, {
num: success.toString(),
}));
}
}
if (errors) {
throw new Error(gtx._x('Validation failed!'));
}
}
async run(argv) {
const configOptions = argv;
if (!(0, optspec_1.coerceOptions)(argv, options)) {
return 1;
}
try {
await this.doRun(configOptions);
return 0;
}
catch (e) {
if (configOptions.verbose) {
console.error(gtx._x('{programName}: {error}', {
programName: package_1.Package.getName(),
error: e,
}));
}
return 1;
}
}
}
exports.Validate = Validate;
//# sourceMappingURL=validate.js.map