add product image and variant value

This commit is contained in:
2026-02-15 10:22:07 +03:30
parent 2b3abfc599
commit 98cf3d0a92
8 changed files with 129 additions and 91946 deletions
+103
View File
@@ -0,0 +1,103 @@
#!/usr/bin/env node
/**
* Import icons from dump/named_icons.csv into the icons table.
* Uses DB_* env vars (from .env or environment). Icons are assigned to ICON_GROUP_ID.
*
* Run from project root: node dump/import-named-icons.js
*/
const fs = require('fs');
const path = require('path');
const { Client } = require('pg');
const { ulid } = require('ulid');
// Load .env from project root
const envPath = path.join(__dirname, '..', '.env');
if (fs.existsSync(envPath)) {
const content = fs.readFileSync(envPath, 'utf8');
content.split('\n').forEach((line) => {
const trimmed = line.replace(/^\s+|\s+$/, '').replace(/#.*$/, '').trim();
if (!trimmed || !/^[A-Za-z_][A-Za-z0-9_]*=/.test(trimmed)) return;
const eq = trimmed.indexOf('=');
const key = trimmed.slice(0, eq);
const value = trimmed.slice(eq + 1).replace(/^["']|["']$/g, '');
if (!process.env[key]) process.env[key] = value;
});
}
const DB_HOST = process.env.DB_HOST || 'localhost';
const DB_PORT = process.env.DB_PORT || '5432';
const DB_NAME = process.env.DB_NAME || 'dmenu';
const DB_USER = process.env.DB_USER || 'postgres';
const DB_PASS = process.env.DB_PASS || '';
const ICON_GROUP_ID = process.env.ICON_GROUP_ID || '01KD7DG4VCTWZRSA54M3PEJ6NW';
const csvPath = path.join(__dirname, 'named_icons.csv');
function parseCsvLine(line) {
const parts = line.split(',');
if (parts.length < 3) return null;
const index = parts[0].trim();
const url = parts[parts.length - 1].trim();
const name = parts.slice(1, -1).join(',').trim();
return { index, name, url };
}
async function main() {
if (!fs.existsSync(csvPath)) {
console.error('Error: named_icons.csv not found at', csvPath);
process.exit(1);
}
const csvContent = fs.readFileSync(csvPath, 'utf8');
const lines = csvContent.split('\n').filter((l) => l.trim());
const header = lines[0];
const dataLines = lines.slice(1);
const rows = [];
for (const line of dataLines) {
const row = parseCsvLine(line);
if (row && row.url) rows.push(row);
}
if (rows.length === 0) {
console.error('Error: No valid rows found in named_icons.csv');
process.exit(1);
}
const client = new Client({
host: DB_HOST,
port: parseInt(DB_PORT, 10),
database: DB_NAME,
user: DB_USER,
password: DB_PASS,
});
try {
await client.connect();
} catch (err) {
console.error('Database connection failed:', err.message);
process.exit(1);
}
const now = new Date().toISOString();
let inserted = 0;
for (const row of rows) {
const id = ulid();
await client.query(
`INSERT INTO icons (id, created_at, updated_at, deleted_at, url, group_id)
VALUES ($1, $2::timestamptz, $3::timestamptz, NULL, $4, $5)`,
[id, now, now, row.url, ICON_GROUP_ID]
);
inserted++;
}
await client.end();
console.log(`Done. Inserted ${inserted} icons.`);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});