Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ESLint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ jobs:
- name: Install modules
run: yarn
- name: Run ESLint
run: yarn run eslint . --ext .js,.jsx,.ts,.tsx
run: yarn run lint
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"ng": "ng",
"start": "ng serve",
"prebuild": "node scripts/copy-readme.js",
"build": "ng build --configuration=production" ,
"watch": "ng build --watch --configuration development",
"test": "ng test",
Expand Down
80 changes: 80 additions & 0 deletions scripts/copy-readme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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'
}