Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(velo)!: use the @betagouv/aides-velo package #246

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
1 change: 0 additions & 1 deletion data-fetch/miniatures/aides-jeunes-repo
Submodule aides-jeunes-repo deleted from 553945
82 changes: 0 additions & 82 deletions data-fetch/miniatures/extract-from-aides-jeunes.js

This file was deleted.

17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"yaml": "^2.3.4"
},
"dependencies": {
"@betagouv/aides-velo": "^0.1.0-12",
"@etalab/decoupage-administratif": "^3.1.1",
"@betagouv/aides-velo": "^0.1.0",
"@etalab/decoupage-administratif": "^4.0.0",
"fuzzysort": "^2.0.4",
"jsdom": "^23.0.1",
"playwright-aws-lambda": "^0.10.0",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/components/DetailsLine.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { engine as baseEngine, getEngine } from '$lib/engine';
import { formatDescription } from '$lib/utils';
import miniaturesManifest from '$lib/data/miniatures.json';
import miniatures from '$lib/data/miniatures';

import AnimatedAmount from './AnimatedAmount.svelte';
import Badge from './Badge.svelte';
Expand Down Expand Up @@ -37,13 +37,13 @@

{#if aide.nodeValue !== null}
<div class="flex flex-row">
{#if miniaturesManifest[ruleName]}
{#if miniatures[ruleName]}
<div
class="basis-12 sm:basis-18 py-4 pl-3 pr-0 flex-shrink-0 opacity-85 cursor-pointer"
jclass="basis-12 sm:basis-18 py-4 pl-3 pr-0 flex-shrink-0 opacity-85 cursor-pointer"
EmileRolley marked this conversation as resolved.
Show resolved Hide resolved
on:click={() => rawNode.lien && window.open(rawNode.lien, '_blank')}
>
<img
src="/miniatures/{miniaturesManifest[ruleName]}"
src="/miniatures/{miniatures[ruleName]}"
class="object-fill"
alt="Logo {title.toLowerCase()}"
/>
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/MiniatureCollectivite.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script>
import miniaturesManifest from '$lib/data/miniatures.json';
import miniatures from '$lib/data/miniatures';

export let ruleName;
export let titre;
</script>

{#if miniaturesManifest[ruleName]}
{#if miniatures[ruleName]}
<img
src="/miniatures/{miniaturesManifest[ruleName]}"
src="/miniatures/{miniatures[ruleName]}"
alt="Logo de {titre}"
class="float-left pt-4 mr-6 !mb-6 max-h-[120px] w-[140px] object-contain"
/>
Expand Down
6 changes: 3 additions & 3 deletions src/routes/(front)/(about)/liste-aides/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { data } from '@betagouv/aides-velo';
import { engine } from '$lib/engine';
import { aidesPerVeloKind } from '$lib/textUtils';
import { formatValue } from 'publicodes';
import aidesCollectivites from '$lib/data/aides-collectivities.json';

export const prerender = true;

Expand Down Expand Up @@ -34,9 +34,9 @@ const formatAideForClient = (aide) => ({

/** @type {import('./$types').PageServerLoad} */
export async function load() {
const associatedCollectivities = Object.keys(data.aidesAvecLocalisation).map((ruleName) => ({
const associatedCollectivities = Object.keys(aidesCollectivites).map((ruleName) => ({
...engine.getRule(ruleName),
...data.aidesAvecLocalisation[ruleName],
...aidesCollectivites[ruleName],
}));

const aidesEtat = associatedCollectivities
Expand Down
7 changes: 4 additions & 3 deletions src/routes/(front)/(search)/ville/[slug]/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { data } from '@betagouv/aides-velo';
import { compile } from 'mdsvex';
import labelTourDeFrance from '/src/content/label-tour-de-france.json';
import labelTourDeFranceCommentairesSource from '/src/content/label-tour-de-france-commentaires.txt?raw';
Expand All @@ -7,6 +6,8 @@ import { error } from '@sveltejs/kit';
import { engine } from '$lib/engine';
import { rawCityToFullLocalisation } from '$lib/utils';
import classementVilleplus from '$lib/data/classement-villeplus.json';
import communes from '$lib/data/communes.json';
import aidesCollectivites from '$lib/data/aides-collectivities.json';

const barometreFubPerCity = Object.fromEntries(
barometreFubRawCsv
Expand All @@ -27,7 +28,7 @@ const commentairesLabelTourDeFrance = labelTourDeFranceCommentairesLines.reduce(
}
}, {});

const ruleNamePerCollectivity = Object.entries(data.aidesAvecLocalisation).reduce(
const ruleNamePerCollectivity = Object.entries(aidesCollectivites).reduce(
(manifest, [ruleName, { collectivity }]) => {
manifest[collectivity.kind][collectivity.value] = ruleName;
return manifest;
Expand Down Expand Up @@ -75,7 +76,7 @@ const prependPartialSentence = (content, { prepend } = {}) =>
/** @type {import('./$types').PageServerLoad} */
export async function load({ params }) {
const slug = params.slug;
const city = data.communes.find((c) => c.slug === slug);
const city = communes.find((c) => c.slug === slug);
if (!city) {
error(404);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { data } from '@betagouv/aides-velo';
import { compile } from 'mdsvex';
import playwright from 'playwright-aws-lambda';
import template from './template.html?raw';
import communes from '$lib/data/communes.json';
import aidesCollectivites from '$lib/data/aides-collectivities.json';
import miniatures from '$lib/data/miniatures.json';

/** @type {import('./$types').RequestHandler} */
export async function GET({ params: { slug } }) {
// NOTE: communes may not be exported by the publicodes package in the
// future.
const ville = data.communes.find((c) => c.slug === slug);
const ville = communes.find((c) => c.slug === slug);

if (!ville) {
throw error(404);
}

const localRuleName = Object.entries(data.aidesAvecLocalisation).find(
const localRuleName = Object.entries(aidesCollectivites).find(
([, { collectivity }]) =>
(collectivity.kind === 'code insee' && collectivity.value === ville.code) ||
(collectivity.kind === 'epci' && collectivity.value === ville.epci),
Expand All @@ -24,8 +26,8 @@ export async function GET({ params: { slug } }) {
localRuleName &&
// NOTE: should really be the publicodes package that provides this
// information?
data.miniatures[localRuleName]
? `https://mesaidesvelo.fr/miniatures/${data.miniatures[localRuleName]}`
miniatures[localRuleName]
? `https://mesaidesvelo.fr/miniatures/${miniatures[localRuleName]}`
: '',
);
const res = await compile(html);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/(front)/departement/[slug]/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { data } from '@betagouv/aides-velo';
import { engine } from '$lib/engine';
import { slugify } from '$lib/utils';
import departements from '@etalab/decoupage-administratif/data/departements.json';
import regions from '@etalab/decoupage-administratif/data/regions.json';
import { error, redirect } from '@sveltejs/kit';
import classementVilleplus from '$lib/data/classement-villeplus.json';
import aidesCollectivites from '$lib/data/aides-collectivities.json';
import { _getCorrespondingContent } from '../../(search)/ville/[slug]/+page.server.js';

// https://www.insee.fr/fr/information/2114773#:~:text=TNCC%20%2D%20Type%20de%20nom%20en%20clair
Expand All @@ -29,7 +29,7 @@ export async function load({ params }) {
}

const region = regions.find((r) => departement.region === r.code);
const aidesAvecLocalisationEntries = Object.entries(data.aidesAvecLocalisation);
const aidesAvecLocalisationEntries = Object.entries(aidesCollectivites);

const aideDepartement = aidesAvecLocalisationEntries.find(
([, { collectivity }]) =>
Expand Down
6 changes: 2 additions & 4 deletions src/routes/(front)/sitemap.txt/+server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { data } from '@betagouv/aides-velo';
import { _departementWithSlug } from '../departement/[slug]/+page.server';
import communes from '$lib/data/communes.json' assert { type: 'json' };

/** @type {import('./$types').RequestHandler} */
export async function GET() {
Expand All @@ -10,9 +10,7 @@ export async function GET() {
'/forfait-mobilite-durable',
'/liste-aides',
..._departementWithSlug.map(({ slug }) => `/departement/${slug}`),
...data.communes
.sort((a, b) => b.population - a.population)
.map(({ slug }) => `/ville/${slug}`),
...communes.sort((a, b) => b.population - a.population).map(({ slug }) => `/ville/${slug}`),
]
.map((path) => `https://mesaidesvelo.fr${path}`)
.join('\n'),
Expand Down
5 changes: 3 additions & 2 deletions src/routes/api/collectivites/+server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
// - associer les communes à une "métropole" (Bordeaux Métropole, Grand Lyon
// etc.) ou une intercommunalité. En effet, c'est à cet échelon que sont
// souvent définies les aides à la mobilité.
// NOTE(Emile): pourrait être fait avec epci.json
// - chercher par le nom de la commune ou par le code postal via le même champ
// de recherche
// - « recherche approximative » pour gérer les erreurs de saisie de
// l'utilisateur.

import { rawCityToFullLocalisation, removeAccents } from '$lib/utils';
import { data } from '@betagouv/aides-velo';
import fuzzysort from 'fuzzysort';
import communes from '$lib/data/communes.json' assert { type: 'json' };

const indexedData = data.communes.flatMap(({ codesPostaux, ...rest }) =>
const indexedData = communes.flatMap(({ codesPostaux, ...rest }) =>
codesPostaux.map((codePostal, i) => {
const cpPrincipal = i === 0;
return {
Expand Down
7 changes: 2 additions & 5 deletions src/scripts/associate-collectivities.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import fs from 'node:fs';
import { rules } from '@betagouv/aides-velo';
import { writeJsonData } from './writeData.js';
import Publicodes, { reduceAST } from 'publicodes';
import { parse } from 'yaml';

import communes from '../lib/data/communes.json' assert { type: 'json' };
import epci from '@etalab/decoupage-administratif/data/epci.json' assert { type: 'json' };
import departements from '@etalab/decoupage-administratif/data/departements.json' assert { type: 'json' };
import regions from '@etalab/decoupage-administratif/data/regions.json' assert { type: 'json' };

const sourceRules = fs.readFileSync(new URL('../aides.yaml', import.meta.url).pathname, 'utf8');

const engine = new Publicodes(parse(sourceRules));
const engine = new Publicodes(rules);

const aidesRuleNames = Object.keys(engine.getParsedRules()).filter(
(ruleName) => ruleName.startsWith('aides .') && engine.getRule(ruleName).rawNode.titre,
Expand Down
44 changes: 44 additions & 0 deletions src/scripts/generate-miniatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Download the images from github (aides-jeune repo) and create the miniatures
* from them.
*/
import fs from 'fs';
import { data } from '@betagouv/aides-velo';
import sharp from 'sharp';
import { join } from 'path';

import { writeJsonData } from '../scripts/writeData.js';
import aidesWithCollectivities from '../lib/data/aides-collectivities.json' assert { type: 'json' };

const currentPath = new URL('./', import.meta.url).pathname;
const rootPath = join(currentPath, '../../');

const miniatureDirectory = join(rootPath, 'static/miniatures/');
if (fs.existsSync(miniatureDirectory)) {
fs.rmSync(miniatureDirectory, { recursive: true });
}
fs.mkdirSync(miniatureDirectory, { recursive: true });

const thumbnailsManifest = Object.keys(aidesWithCollectivities).reduce((acc, id) => {
const imgSrc = data.miniatures[id];

if (!imgSrc) {
return acc;
}

const imgName = imgSrc.split('/').at(-1).split('.')[0] + '.webp';
generateThumbnail(imgSrc, imgName);

return { ...acc, [id]: imgName };
}, {});

async function generateThumbnail(imgSrc, imgName) {
const resp = await fetch(imgSrc);
const blob = await resp.blob();
const buffer = await blob.arrayBuffer();
const img = sharp(buffer);
img.resize({ fit: 'inside', height: 170, width: 120 });
img.webp().toFile(join(miniatureDirectory, imgName));
}

writeJsonData('miniatures.json', thumbnailsManifest);
7 changes: 3 additions & 4 deletions src/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: to remove
// await import('./transform-communes-data.js');
// await import('./associate-collectivities.js');
// await import('../../data-fetch/miniatures/extract-from-aides-jeunes.js');
await import('./transform-communes-data.js');
await import('./associate-collectivities.js');
await import('./generate-miniatures.js');
Loading