diff --git a/create_mapping.ts b/create_mapping.ts new file mode 100644 index 0000000..e60d396 --- /dev/null +++ b/create_mapping.ts @@ -0,0 +1,182 @@ +/** + * create_mapping_config.ts + * Script pour générer une nouvelle configuration qui étend MappingConfigType + * + * Usage: + * npx ts-node create_mapping_config.ts + */ + +import * as fs from 'fs'; +import * as path from 'path'; +import * as readline from 'readline'; + +// Créer une interface pour les entrées utilisateur +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +// Fonction pour demander le nom de la configuration +async function askConfigName(): Promise { + return new Promise((resolve) => { + rl.question('Entrez le nom de la nouvelle configuration (ex: MappingMonProjet): ', (answer) => { + resolve(answer); + }); + }); +} + +// Fonction pour demander une description courte +async function askConfigDescription(): Promise { + return new Promise((resolve) => { + rl.question('Entrez une courte description pour cette configuration: ', (answer) => { + resolve(answer); + }); + }); +} + +// Fonction pour demander l'auteur +async function askConfigAuthor(): Promise { + return new Promise((resolve) => { + rl.question('Entrez votre nom et email (ex: John Doe ): ', (answer) => { + resolve(answer); + }); + }); +} + +// Fonction pour générer le contenu du fichier de configuration +function generateConfigContent(configName: string, description: string, author: string): string { + return `/** + * ${description} + */ +import MappingConfigType from "../mapping-config.type"; + +const ${configName}: MappingConfigType = { + config_name: "${configName}", + config_author: "${author}", + default_properties_of_point: { + // Ajoutez ici les propriétés par défaut pour vos points + }, + source: { + geojson_path: '', + url: '' + }, + filters: { + // exclude_point_if_tag_not_empty: ['id_osm'], // exclure les points ayant déjà un id_osm pour éviter les doublons + // offset: 1 + }, + add_not_mapped_tags_too: false, + boolean_keys: [], + tags_to_ignore_if_value_is: ['Non renseigne'], + tags: { + // Mapping des champs du fichier source vers les tags OSM + // 'champ_source': 'tag_osm_cible', + // 'champ_source_id': 'ref:FR:votre_id', + + // Exemple de transformation plus complexe + // nom: { + // key_converted: 'name', + // convert_to_name: true, + // }, + } +} + +export default ${configName}; +`; +} + +// Fonction pour ajouter l'import et mettre à jour allowed_configs dans convert_to_osm_tags.ts +function updateMainFile(configName: string): void { + const mainFilePath = 'convert_to_osm_tags.ts'; + + try { + let mainFileContent = fs.readFileSync(mainFilePath, 'utf8'); + + // Ajouter l'import + const importStatement = `import ${configName} from './mappings/converters/config${configName.replace(/^Mapping/, '')}'\n`; + const importInsertPos = mainFileContent.indexOf('const limitWarningPercentageChangeInPoints'); + + if (importInsertPos !== -1) { + const beforeInsert = mainFileContent.substring(0, importInsertPos); + const afterInsert = mainFileContent.substring(importInsertPos); + mainFileContent = beforeInsert + importStatement + afterInsert; + } + + // Ajouter à allowed_configs + const allowedConfigsPos = mainFileContent.indexOf('const allowed_configs: any = {'); + + if (allowedConfigsPos !== -1) { + const endOfFirstLinePos = mainFileContent.indexOf('\n', allowedConfigsPos); + const beforeConfig = mainFileContent.substring(0, endOfFirstLinePos + 1); + const afterConfig = mainFileContent.substring(endOfFirstLinePos + 1); + + mainFileContent = beforeConfig + ` ${configName},\n` + afterConfig; + } + + // Écrire le fichier mis à jour + fs.writeFileSync(mainFilePath, mainFileContent, 'utf8'); + console.log(`✅ Fichier ${mainFilePath} mis à jour avec succès.`); + + } catch (error) { + console.error(`❌ Erreur lors de la mise à jour de ${mainFilePath}:`, error); + } +} + +// Fonction principale +async function main() { + console.log('🚀 Création d\'une nouvelle configuration de mapping OSM'); + + // Demander les informations nécessaires + const configName = await askConfigName(); + if (!configName) { + console.error('❌ Le nom de la configuration est obligatoire.'); + rl.close(); + return; + } + + const description = await askConfigDescription(); + const author = await askConfigAuthor(); + + // Créer le dossier si nécessaire + const configDir = path.join('mappings', 'converters'); + if (!fs.existsSync(configDir)) { + fs.mkdirSync(configDir, { recursive: true }); + } + + // Générer le nom du fichier + const fileName = `config${configName.replace(/^Mapping/, '')}.ts`; + const filePath = path.join(configDir, fileName); + + // Vérifier si le fichier existe déjà + if (fs.existsSync(filePath)) { + console.error(`❌ Le fichier ${filePath} existe déjà.`); + rl.close(); + return; + } + + // Générer et écrire le contenu du fichier + const content = generateConfigContent(configName, description, author); + try { + fs.writeFileSync(filePath, content, 'utf8'); + console.log(`✅ Fichier ${filePath} créé avec succès.`); + + // Mettre à jour le fichier principal + updateMainFile(configName); + + console.log(` +📝 Configuration créée avec succès! + +Étapes suivantes: +1. Modifiez ${filePath} pour spécifier vos mappings de données +2. Exécutez la conversion avec: + npx ts-node convert_to_osm_tags.ts --engine-config=${configName} --source=chemin/vers/votre/fichier.geojson +`); + + } catch (error) { + console.error('❌ Erreur lors de la création du fichier:', error); + } + + rl.close(); +} + +// Exécuter le script +main().catch(console.error); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 425df00..4cb18d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ "jest": "^29.6.2", "loglevel": "^1.8.1", "nodemon": "^2.0.22", + "readline": "^1.3.0", "ts-jest": "^29.1.1", "ts-node": "^10.9.1", "tslib": "^2.6.1", @@ -6195,6 +6196,13 @@ "node": ">=8.10.0" } }, + "node_modules/readline": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz", + "integrity": "sha512-k2d6ACCkiNYz222Fs/iNze30rRJ1iIicW7JuX/7/cozvih6YCkFZH+J6mAFDVgv0dRBaAyr4jDqC95R2y4IADg==", + "dev": true, + "license": "BSD" + }, "node_modules/regenerate": { "version": "1.4.2", "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.2.tgz", diff --git a/package.json b/package.json index 9ab6597..20c40b2 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "jest": "^29.6.2", "loglevel": "^1.8.1", "nodemon": "^2.0.22", + "readline": "^1.3.0", "ts-jest": "^29.1.1", "ts-node": "^10.9.1", "tslib": "^2.6.1",