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