Add modifying text commit only when some files are changed

This commit is contained in:
Emmanuel 2024-12-18 22:13:29 +01:00
parent ebe10b3d69
commit 900cf7b201

View file

@ -618,7 +618,7 @@ async function exportConsolidatedTextToGit(
}) })
// Generate main LICENCE.md file. // Generate main LICENCE.md file.
await fs.writeFile( await writeTextFileIfChanged(
path.join(targetDir, "LICENCE.md"), path.join(targetDir, "LICENCE.md"),
dedent` dedent`
# Textes juridiques consolidés français sous Git # Textes juridiques consolidés français sous Git
@ -694,7 +694,7 @@ async function exportConsolidatedTextToGit(
.replace(/\s+/g, " ") .replace(/\s+/g, " ")
.trim() .trim()
const consolidatedTextDirName = slugify(consolidatedTextTitle, "_") const consolidatedTextDirName = slugify(consolidatedTextTitle, "_")
await fs.writeFile( await writeTextFileIfChanged(
path.join(targetDir, "README.md"), path.join(targetDir, "README.md"),
dedent` dedent`
# Textes juridiques consolidés français sous Git # Textes juridiques consolidés français sous Git
@ -878,7 +878,7 @@ async function exportConsolidatedTextToGit(
} }
const t2 = performance.now() const t2 = performance.now()
await generateTextGitDirectory( const changedFilesCount = await generateTextGitDirectory(
context, context,
2, 2,
tree, tree,
@ -887,106 +887,108 @@ async function exportConsolidatedTextToGit(
) )
const t3 = performance.now() const t3 = performance.now()
let messageLines: string | undefined = undefined if (changedFilesCount > 0) {
let summary: string | undefined = undefined let messageLines: string | undefined = undefined
if (modifyingTextId.startsWith("JORFTEXT")) { let summary: string | undefined = undefined
const jorfModifyingTexteVersion = if (modifyingTextId.startsWith("JORFTEXT")) {
modifyingTexteVersion as JorfTexteVersion const jorfModifyingTexteVersion =
messageLines = [ modifyingTexteVersion as JorfTexteVersion
[ messageLines = [
"Autorité", [
jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION "Autorité",
.AUTORITE, jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION
], .AUTORITE,
[ ],
"Ministère", [
jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION "Ministère",
.MINISTERE, jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION
], .MINISTERE,
["Nature", jorfModifyingTexteVersion.META.META_COMMUN.NATURE], ],
[ ["Nature", jorfModifyingTexteVersion.META.META_COMMUN.NATURE],
"Date de début", [
jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION "Date de début",
.DATE_DEBUT, jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION
], .DATE_DEBUT,
[ ],
"Date de fin", [
jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION "Date de fin",
.DATE_FIN, jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION
], .DATE_FIN,
["Identifiant", jorfModifyingTexteVersion.META.META_COMMUN.ID], ],
[ ["Identifiant", jorfModifyingTexteVersion.META.META_COMMUN.ID],
"NOR", [
jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_CHRONICLE.NOR, "NOR",
], jorfModifyingTexteVersion.META.META_SPEC.META_TEXTE_CHRONICLE.NOR,
[ ],
"Ancien identifiant", [
jorfModifyingTexteVersion.META.META_COMMUN.ANCIEN_ID, "Ancien identifiant",
], jorfModifyingTexteVersion.META.META_COMMUN.ANCIEN_ID,
// TODO: Mettre l'URL dans Légifrance et(?) le Git Tricoteuses ],
["URL", jorfModifyingTexteVersion.META.META_COMMUN.URL], // TODO: Mettre l'URL dans Légifrance et(?) le Git Tricoteuses
] ["URL", jorfModifyingTexteVersion.META.META_COMMUN.URL],
.filter(([, value]) => value !== undefined) ]
.map(([key, value]) => `${key}: ${value}`) .filter(([, value]) => value !== undefined)
.join("\n") .map(([key, value]) => `${key}: ${value}`)
summary = jorfModifyingTexteVersion.SM?.CONTENU?.replace( .join("\n")
/<br\s*\/>/gi, summary = jorfModifyingTexteVersion.SM?.CONTENU?.replace(
"\n", /<br\s*\/>/gi,
) "\n",
} else if (modifyingTextId.startsWith("LEGITEXT")) { )
const legiModifyingTexteVersion = } else if (modifyingTextId.startsWith("LEGITEXT")) {
modifyingTexteVersion as LegiTexteVersion const legiModifyingTexteVersion =
messageLines = [ modifyingTexteVersion as LegiTexteVersion
[ messageLines = [
"État", [
legiModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION.ETAT, "État",
], legiModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION.ETAT,
["Nature", legiModifyingTexteVersion.META.META_COMMUN.NATURE], ],
[ ["Nature", legiModifyingTexteVersion.META.META_COMMUN.NATURE],
"Date de début", [
legiModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION "Date de début",
.DATE_DEBUT, legiModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION
], .DATE_DEBUT,
[ ],
"Date de fin", [
legiModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION "Date de fin",
.DATE_FIN, legiModifyingTexteVersion.META.META_SPEC.META_TEXTE_VERSION
], .DATE_FIN,
["Identifiant", legiModifyingTexteVersion.META.META_COMMUN.ID], ],
[ ["Identifiant", legiModifyingTexteVersion.META.META_COMMUN.ID],
"NOR", [
legiModifyingTexteVersion.META.META_SPEC.META_TEXTE_CHRONICLE.NOR, "NOR",
], legiModifyingTexteVersion.META.META_SPEC.META_TEXTE_CHRONICLE.NOR,
[ ],
"Ancien identifiant", [
legiModifyingTexteVersion.META.META_COMMUN.ANCIEN_ID, "Ancien identifiant",
], legiModifyingTexteVersion.META.META_COMMUN.ANCIEN_ID,
// TODO: Mettre l'URL dans le Git Tricoteuses ],
["URL", legiModifyingTexteVersion.META.META_COMMUN.URL], // TODO: Mettre l'URL dans le Git Tricoteuses
] ["URL", legiModifyingTexteVersion.META.META_COMMUN.URL],
.filter(([, value]) => value !== undefined) ]
.map(([key, value]) => `${key}: ${value}`) .filter(([, value]) => value !== undefined)
.join("\n") .map(([key, value]) => `${key}: ${value}`)
.join("\n")
}
await git.commit({
dir: targetDir,
fs,
author: {
email: "republique@tricoteuses.fr",
name: "République française",
timestamp,
timezoneOffset,
},
committer: {
email: "republique@tricoteuses.fr",
name: "République française",
timestamp,
timezoneOffset,
},
message: [modifyingTextTitle, summary, messageLines]
.filter((block) => block !== undefined)
.join("\n\n"),
})
} }
await git.commit({
dir: targetDir,
fs,
author: {
email: "republique@tricoteuses.fr",
name: "République française",
timestamp,
timezoneOffset,
},
committer: {
email: "republique@tricoteuses.fr",
name: "République française",
timestamp,
timezoneOffset,
},
message: [modifyingTextTitle, summary, messageLines]
.filter((block) => block !== undefined)
.join("\n\n"),
})
const t4 = performance.now() const t4 = performance.now()
console.log(`Durations: ${t1 - t0} ${t2 - t1} ${t3 - t2} ${t4 - t3}`) console.log(`Durations: ${t1 - t0} ${t2 - t1} ${t3 - t2} ${t4 - t3}`)
@ -1002,7 +1004,8 @@ async function generateSectionTaGitDirectory(
parentRepositoryRelativeDir: string, parentRepositoryRelativeDir: string,
modifyingTextId: string, modifyingTextId: string,
obsoleteRepositoryRelativeFilesPaths: Set<string>, obsoleteRepositoryRelativeFilesPaths: Set<string>,
) { ): Promise<number> {
let changedFilesCount = 0
const sectionTaTitle = sectionTa.TITRE_TA ?? sectionTa.ID const sectionTaTitle = sectionTa.TITRE_TA ?? sectionTa.ID
let sectionTaSlug = slugify(sectionTaTitle.split(":")[0].trim(), "_") let sectionTaSlug = slugify(sectionTaTitle.split(":")[0].trim(), "_")
if (sectionTaSlug.length > 255) { if (sectionTaSlug.length > 255) {
@ -1039,7 +1042,7 @@ async function generateSectionTaGitDirectory(
repositoryRelativeDir, repositoryRelativeDir,
articleFilename, articleFilename,
) )
await fs.writeFile( const fileChanged = await writeTextFileIfChanged(
path.join(context.targetDir, articleRepositoryRelativeFilePath), path.join(context.targetDir, articleRepositoryRelativeFilePath),
dedent` dedent`
--- ---
@ -1063,11 +1066,14 @@ async function generateSectionTaGitDirectory(
${await cleanHtmlFragment(article.BLOC_TEXTUEL?.CONTENU)} ${await cleanHtmlFragment(article.BLOC_TEXTUEL?.CONTENU)}
` + "\n", ` + "\n",
) )
await git.add({ if (fileChanged) {
dir: context.targetDir, await git.add({
filepath: articleRepositoryRelativeFilePath, dir: context.targetDir,
fs, filepath: articleRepositoryRelativeFilePath,
}) fs,
})
changedFilesCount++
}
obsoleteRepositoryRelativeFilesPaths.delete( obsoleteRepositoryRelativeFilesPaths.delete(
articleRepositoryRelativeFilePath, articleRepositoryRelativeFilePath,
) )
@ -1093,7 +1099,7 @@ async function generateSectionTaGitDirectory(
const sectionTaDirName = sectionTaSlug const sectionTaDirName = sectionTaSlug
readmeLinks.push({ href: sectionTaDirName, title: sectionTaTitle }) readmeLinks.push({ href: sectionTaDirName, title: sectionTaTitle })
await generateSectionTaGitDirectory( changedFilesCount += await generateSectionTaGitDirectory(
context, context,
depth + 1, depth + 1,
child, child,
@ -1109,7 +1115,7 @@ async function generateSectionTaGitDirectory(
repositoryRelativeDir, repositoryRelativeDir,
"README.md", "README.md",
) )
await fs.writeFile( const fileChanged = await writeTextFileIfChanged(
path.join(context.targetDir, readmeRepositoryRelativeFilePath), path.join(context.targetDir, readmeRepositoryRelativeFilePath),
dedent` dedent`
--- ---
@ -1132,12 +1138,17 @@ async function generateSectionTaGitDirectory(
${readmeLinks.map(({ href, title }) => `- [${title}](${href})`).join("\n")} ${readmeLinks.map(({ href, title }) => `- [${title}](${href})`).join("\n")}
` + "\n", ` + "\n",
) )
await git.add({ if (fileChanged) {
dir: context.targetDir, await git.add({
filepath: readmeRepositoryRelativeFilePath, dir: context.targetDir,
fs, filepath: readmeRepositoryRelativeFilePath,
}) fs,
})
changedFilesCount++
}
obsoleteRepositoryRelativeFilesPaths.delete(readmeRepositoryRelativeFilePath) obsoleteRepositoryRelativeFilesPaths.delete(readmeRepositoryRelativeFilePath)
return changedFilesCount
} }
async function generateTextGitDirectory( async function generateTextGitDirectory(
@ -1146,7 +1157,8 @@ async function generateTextGitDirectory(
tree: TextelrNode, tree: TextelrNode,
texteVersion: LegiTexteVersion, texteVersion: LegiTexteVersion,
modifyingTextId: string, modifyingTextId: string,
) { ): Promise<number> {
let changedFilesCount = 0
const texteTitle = ( const texteTitle = (
texteVersion.META.META_SPEC.META_TEXTE_VERSION.TITREFULL ?? texteVersion.META.META_SPEC.META_TEXTE_VERSION.TITREFULL ??
texteVersion.META.META_SPEC.META_TEXTE_VERSION.TITRE ?? texteVersion.META.META_SPEC.META_TEXTE_VERSION.TITRE ??
@ -1186,7 +1198,7 @@ async function generateTextGitDirectory(
repositoryRelativeDir, repositoryRelativeDir,
articleFilename, articleFilename,
) )
await fs.writeFile( const fileChanged = await writeTextFileIfChanged(
path.join(context.targetDir, articleRepositoryRelativeFilePath), path.join(context.targetDir, articleRepositoryRelativeFilePath),
dedent` dedent`
--- ---
@ -1210,11 +1222,14 @@ async function generateTextGitDirectory(
${await cleanHtmlFragment(article.BLOC_TEXTUEL?.CONTENU)} ${await cleanHtmlFragment(article.BLOC_TEXTUEL?.CONTENU)}
` + "\n", ` + "\n",
) )
await git.add({ if (fileChanged) {
dir: context.targetDir, await git.add({
filepath: articleRepositoryRelativeFilePath, dir: context.targetDir,
fs, filepath: articleRepositoryRelativeFilePath,
}) fs,
})
changedFilesCount++
}
obsoleteRepositoryRelativeFilesPaths.delete( obsoleteRepositoryRelativeFilesPaths.delete(
articleRepositoryRelativeFilePath, articleRepositoryRelativeFilePath,
) )
@ -1240,7 +1255,7 @@ async function generateTextGitDirectory(
const sectionTaDirName = sectionTaSlug const sectionTaDirName = sectionTaSlug
readmeLinks.push({ href: sectionTaDirName, title: sectionTaTitle }) readmeLinks.push({ href: sectionTaDirName, title: sectionTaTitle })
await generateSectionTaGitDirectory( changedFilesCount += await generateSectionTaGitDirectory(
context, context,
depth + 1, depth + 1,
child, child,
@ -1262,7 +1277,7 @@ async function generateTextGitDirectory(
repositoryRelativeDir, repositoryRelativeDir,
"README.md", "README.md",
) )
await fs.writeFile( const fileChanged = await writeTextFileIfChanged(
path.join(context.targetDir, readmeRepositoryRelativeFilePath), path.join(context.targetDir, readmeRepositoryRelativeFilePath),
dedent` dedent`
--- ---
@ -1291,11 +1306,14 @@ async function generateTextGitDirectory(
${readmeBlocks.join("\n\n")} ${readmeBlocks.join("\n\n")}
` + "\n", ` + "\n",
) )
await git.add({ if (fileChanged) {
dir: context.targetDir, await git.add({
filepath: readmeRepositoryRelativeFilePath, dir: context.targetDir,
fs, filepath: readmeRepositoryRelativeFilePath,
}) fs,
})
changedFilesCount++
}
obsoleteRepositoryRelativeFilesPaths.delete(readmeRepositoryRelativeFilePath) obsoleteRepositoryRelativeFilesPaths.delete(readmeRepositoryRelativeFilePath)
// Delete obsolete files and directories. // Delete obsolete files and directories.
@ -1308,6 +1326,7 @@ async function generateTextGitDirectory(
filepath: obsoleteRepositoryRelativeFilePath, filepath: obsoleteRepositoryRelativeFilePath,
fs, fs,
}) })
changedFilesCount++
if ( if (
obsoleteRepositoryRelativeFilePath === "README.md" || obsoleteRepositoryRelativeFilePath === "README.md" ||
obsoleteRepositoryRelativeFilePath.endsWith("/README.md") obsoleteRepositoryRelativeFilePath.endsWith("/README.md")
@ -1319,6 +1338,8 @@ async function generateTextGitDirectory(
) )
} }
} }
return changedFilesCount
} }
async function getOrLoadArticle( async function getOrLoadArticle(
@ -2124,6 +2145,20 @@ async function* walkStructureTree(
} }
} }
async function writeTextFileIfChanged(
filePath: string,
text: string,
): Promise<boolean> {
if (await fs.pathExists(filePath)) {
const existingText = await fs.readFile(filePath, { encoding: "utf-8" })
if (text === existingText) {
return false
}
}
await fs.writeFile(filePath, text, { encoding: "utf-8" })
return true
}
sade("export_consolidated_text_to_git <consolidatedTextId> <targetDir>", true) sade("export_consolidated_text_to_git <consolidatedTextId> <targetDir>", true)
.describe( .describe(
"Convert a consolidated LEGI texte (Constitution, code, law, etc) to a Git repository", "Convert a consolidated LEGI texte (Constitution, code, law, etc) to a Git repository",