-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JS script to copy ADRs automatically
The script copies ADRs from the `doc/adr` folder into the web-doc project, so that HTML pages can be generated for them. The script runs automatically when the user starts the project with `yarn dev`. Before copying the files the script compares folders and only copies folders and files that are missing, or that were modified. The script runs synchronously, so that the project only starts after the ADRs have been copied. Singed-off-by: Rodrigo Pinto <rodrigo.pinto@calian.ca>
- Loading branch information
1 parent
d93ea53
commit 026fe28
Showing
3 changed files
with
127 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import fs from 'fs-extra'; | ||
import { compareSync } from 'dir-compare'; | ||
import { once } from 'node:events'; | ||
import { createInterface } from 'node:readline'; | ||
|
||
const src = '../adr/'; | ||
const dest = 'doc-sources/adr-source/'; | ||
|
||
const errorCallback = (err) => { | ||
if (err) throw err; | ||
}; | ||
|
||
const comparisonOptions = { | ||
excludeFilter: '.DS_Store', | ||
compareSize: true, | ||
compareContent: true | ||
}; | ||
|
||
// Sanitizer of markdown files | ||
|
||
const sanitizer = (chunk) => { | ||
return chunk.replace(/{/g, '{').replace(/}/g, '}'); | ||
}; | ||
|
||
const processLineByLine = async (file) => { | ||
try { | ||
let result = ''; | ||
let mermaid = false; | ||
let mermaidBlock = ''; | ||
|
||
const rl = createInterface({ | ||
input: file, | ||
crlfDelay: Infinity | ||
}); | ||
|
||
rl.on('line', (line) => { | ||
if (mermaid) { | ||
mermaidBlock += line + '\n'; | ||
|
||
if (line.includes('```')) { | ||
mermaid = false; | ||
result += sanitizer(mermaidBlock); | ||
mermaidBlock = ''; | ||
} | ||
} else { | ||
result += line + '\n'; | ||
} | ||
|
||
if (line.includes('```mermaid')) { | ||
mermaid = true; | ||
} | ||
}); | ||
|
||
await once(rl, 'close'); | ||
|
||
return result; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
// Compare folders | ||
|
||
if (!fs.existsSync('doc-sources')) { | ||
fs.mkdirSync('doc-sources'); | ||
fs.mkdirSync(dest); | ||
} else if (!fs.existsSync(dest)) { | ||
fs.mkdirSync(dest); | ||
} | ||
|
||
const comparison = compareSync(src, dest, comparisonOptions); | ||
|
||
// Copy missing files and folders | ||
|
||
if (!comparison.same) { | ||
comparison.diffSet.forEach(async (dif) => { | ||
if ((dif.state === 'left' || dif.state === 'distinct') && dif.path1 === src) { | ||
if (dif.type1 === 'file' && dif.name1.slice(-3) === '.md') { | ||
// Remove outdated file | ||
if (dif.state === 'distinct') { | ||
fs.rmSync(dest + dif.name2, { force: true }) | ||
} | ||
|
||
// Sanitize Markdown files | ||
const file = fs.createReadStream(src + dif.name1, 'utf8'); | ||
const sanitized = await processLineByLine(file); | ||
fs.writeFileSync(dest + dif.name1, sanitized.toString()); | ||
} else { | ||
// Copy | ||
fs.copySync(src + dif.name1, dest + dif.name1, { recursive: true }, errorCallback); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters