This commit is contained in:
Philippe Torrel
2026-08-03 16:00:02 +02:00
parent 7d8daeb14c
commit 17aa46a4bc
22 changed files with 6855 additions and 81 deletions

View File

@@ -0,0 +1,32 @@
import fs from 'node:fs';
import { v4 as id } from 'uuid';
const PRODUCTS_PATH = '../data/products.json';
/** Ein vollständiges Produkt, so wie es in products.json steht. */
interface Product {
_id: string;
position: number;
name: string;
price: number;
}
/** Produkt aus der Datei - _id und position können noch fehlen. */
type ProductInput = Omit<Product, '_id' | 'position'> & Partial<Pick<Product, '_id' | 'position'>>;
console.log(id()); // 97412ca6-c7a8-4868-a49b-3cf4c2258dd8
try {
const data = JSON.parse(fs.readFileSync(PRODUCTS_PATH, 'utf-8')) as ProductInput[];
const products: Product[] = data.map((product, index) => {
return { _id: product._id || id(), position: index + 1, ...product };
});
console.log(products);
fs.writeFileSync(PRODUCTS_PATH, JSON.stringify(products, null, 2), 'utf-8');
console.log('File modified');
} catch (error) {
console.log(error);
}

View File

@@ -9,8 +9,43 @@
"version": "1.0.0",
"dependencies": {
"uuid": "^14.0.1"
},
"devDependencies": {
"@types/node": "^24.10.1",
"typescript": "^5.9.3"
}
},
"node_modules/@types/node": {
"version": "24.13.3",
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.13.3.tgz",
"integrity": "sha512-Dh8vAsV36ig5wa9OX4pXvMc9D3Veibfw2wix0CUwYODLD8nkj9UsLjASr49nPg+2eKzxhBV+v7L8pXvT4e639Q==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~7.18.0"
}
},
"node_modules/typescript": {
"version": "5.9.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/undici-types": {
"version": "7.18.2",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.18.2.tgz",
"integrity": "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==",
"dev": true,
"license": "MIT"
},
"node_modules/uuid": {
"version": "14.0.1",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-14.0.1.tgz",

View File

@@ -2,13 +2,19 @@
"name": "utils",
"version": "1.0.0",
"description": "",
"main": "json-modifier.js",
"main": "json-modifier.ts",
"scripts": {
"start": "node json-modifier.ts",
"typecheck": "tsc --noEmit",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"type": "module",
"dependencies": {
"uuid": "^14.0.1"
},
"devDependencies": {
"@types/node": "^24.10.1",
"typescript": "^5.9.3"
}
}

View File

@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["ES2022"],
/* Node >= 22.18 führt .ts-Dateien direkt aus (type stripping),
tsc wird deshalb nur zum Typprüfen genutzt. */
"noEmit": true,
"allowImportingTsExtensions": true,
"erasableSyntaxOnly": true,
"verbatimModuleSyntax": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true
},
"include": ["**/*.ts"],
"exclude": ["node_modules"]
}