46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const mysql = require('mysql2/promise');
|
|
require('dotenv').config({ path: path.join(__dirname, '../.env') });
|
|
|
|
const SmartPackageExcelImportService = require('../src/services/smartPackageExcelImportService');
|
|
|
|
const args = process.argv.slice(2);
|
|
const apply = args.includes('--apply');
|
|
const fileArg = args.find((arg) => !arg.startsWith('--'));
|
|
|
|
if (!fileArg) {
|
|
console.error('Brug: node scripts/import-smart-packages-excel.js <fil.xlsx> [--apply]');
|
|
process.exit(1);
|
|
}
|
|
|
|
const filePath = path.resolve(process.cwd(), fileArg);
|
|
if (!fs.existsSync(filePath)) {
|
|
console.error(`Filen findes ikke: ${filePath}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const pool = mysql.createPool({
|
|
host: process.env.DB_HOST || '127.0.0.1',
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME || 'tilbudgivern',
|
|
connectionLimit: 2
|
|
});
|
|
|
|
const service = new SmartPackageExcelImportService({ pool });
|
|
|
|
(async () => {
|
|
try {
|
|
const result = apply ? await service.import(filePath) : service.preview(filePath);
|
|
console.log(JSON.stringify({ mode: apply ? 'apply' : 'preview', ...result }, null, 2));
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
})().catch((error) => {
|
|
console.error(error.message);
|
|
process.exit(1);
|
|
});
|