20 lines
525 B
JavaScript
20 lines
525 B
JavaScript
import fs from 'node:fs';
|
|
import { v4 as id } from 'uuid';
|
|
|
|
console.log(id()); // 97412ca6-c7a8-4868-a49b-3cf4c2258dd8
|
|
|
|
try {
|
|
const data = JSON.parse(fs.readFileSync('../data/products.json', 'utf-8'));
|
|
|
|
const products = data.map((product, index) => {
|
|
return { _id: product._id || id(), position: index + 1, ...product };
|
|
});
|
|
|
|
console.log(products);
|
|
|
|
fs.writeFileSync('../data/products.json', JSON.stringify(products, null, 2), 'utf-8');
|
|
console.log('File modified');
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|