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,6 +887,7 @@ async function exportConsolidatedTextToGit(
) )
const t3 = performance.now() const t3 = performance.now()
if (changedFilesCount > 0) {
let messageLines: string | undefined = undefined let messageLines: string | undefined = undefined
let summary: string | undefined = undefined let summary: string | undefined = undefined
if (modifyingTextId.startsWith("JORFTEXT")) { if (modifyingTextId.startsWith("JORFTEXT")) {
@ -987,6 +988,7 @@ async function exportConsolidatedTextToGit(
.filter((block) => block !== undefined) .filter((block) => block !== undefined)
.join("\n\n"), .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",
) )
if (fileChanged) {
await git.add({ await git.add({
dir: context.targetDir, dir: context.targetDir,
filepath: articleRepositoryRelativeFilePath, filepath: articleRepositoryRelativeFilePath,
fs, 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",
) )
if (fileChanged) {
await git.add({ await git.add({
dir: context.targetDir, dir: context.targetDir,
filepath: readmeRepositoryRelativeFilePath, filepath: readmeRepositoryRelativeFilePath,
fs, 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",
) )
if (fileChanged) {
await git.add({ await git.add({
dir: context.targetDir, dir: context.targetDir,
filepath: articleRepositoryRelativeFilePath, filepath: articleRepositoryRelativeFilePath,
fs, 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",
) )
if (fileChanged) {
await git.add({ await git.add({
dir: context.targetDir, dir: context.targetDir,
filepath: readmeRepositoryRelativeFilePath, filepath: readmeRepositoryRelativeFilePath,
fs, 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",