Skip to content

Commit 82c07d5

Browse files
committed
Copy README from root to assets when newer
1 parent 6c54aee commit 82c07d5

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"scripts": {
55
"ng": "ng",
66
"start": "ng serve",
7+
"prebuild": "node scripts/copy-readme.js",
78
"build": "ng build --configuration=production" ,
89
"watch": "ng build --watch --configuration development",
910
"test": "ng test",

scripts/copy-readme.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
// Comment to add at the beginning of the copied readme file
7+
const comment = `<!--
8+
NB! This file will be OVERWRITTEN in the build process.
9+
NB!
10+
NB! Do not edit this file. Changes will be lost.
11+
NB! Edit the main README.md in the root folder.
12+
-->
13+
14+
`;
15+
16+
17+
const sourceFile = path.join(__dirname, '..', 'README.md');
18+
const targetDir = path.join(__dirname, '..', 'src/assets/Markdown Files');
19+
const targetFile = path.join(targetDir, 'README.md');
20+
21+
try {
22+
// Ensure target directory exists
23+
if (!fs.existsSync(targetDir)) {
24+
fs.mkdirSync(targetDir, { recursive: true });
25+
}
26+
27+
// Warn the programmer if readme copy is newer than source
28+
let copyNeeded = true;
29+
const sourceStats = fs.statSync(sourceFile);
30+
if (fs.existsSync(targetFile)) {
31+
const targetStats = fs.statSync(targetFile);
32+
33+
if (targetStats.mtimeMs == sourceStats.mtimeMs) {
34+
copyNeeded = false;
35+
}
36+
if (targetStats.mtimeMs > sourceStats.mtimeMs) {
37+
console.warn('===============================================================');
38+
console.warn('⚠️ WARNING: Did you edit the README.md copy under `assets`?');
39+
console.warn('The assets readme gets overwritten during the build process.');
40+
console.warn('Edit the main README.md in root folder to avoid losing changes.');
41+
console.warn('===============================================================');
42+
fs.copyFileSync(targetFile, targetFile + '.bak');
43+
console.warn('Made you a backup, though: ' + targetFile + '.bak');
44+
}
45+
}
46+
47+
if (copyNeeded) {
48+
// Read the source README
49+
const readmeContent = fs.readFileSync(sourceFile, 'utf8');
50+
const nl = getLineBreakChar(readmeContent);
51+
52+
// Write to target with comment prepended
53+
fs.writeFileSync(targetFile, setLineBreak(comment, nl) + readmeContent, 'utf8');
54+
fs.utimesSync(targetFile, sourceStats.atime, sourceStats.mtime);
55+
56+
console.log('✔ Copied README.md to src/assets/Markdown Files/');
57+
}
58+
} catch (error) {
59+
console.error('Error copying README.md to assets:', error);
60+
process.exit(1);
61+
}
62+
63+
64+
function setLineBreak(input, nl) {
65+
return input.replace(/\r\n|\r|\n/g, nl);
66+
}
67+
68+
function getLineBreakChar(string) {
69+
const indexOfLF = string.indexOf('\n', 1) // No need to check first-character
70+
71+
if (indexOfLF === -1) {
72+
if (string.indexOf('\r') !== -1) return '\r'
73+
74+
return '\n'
75+
}
76+
77+
if (string[indexOfLF - 1] === '\r') return '\r\n'
78+
79+
return '\n'
80+
}

0 commit comments

Comments
 (0)