Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Generate list on app start #168

Merged
merged 2 commits into from
Sep 14, 2021
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# app
public/list.json
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ Your url will be `http://linkfree.eddiehub.org/<yourusername>`, for example http

Your `avatar` URL should take the format of `https://github.com/<yourusername>.png`.

### Add your profile to the home page
### Home page profiles

Update the file `public/data/_list.json` with the following object to the collection:

```json
{ "username": "eddiejaoude", "avatar": "https://github.com/eddiejaoude.png" },
```
Your profile will automatically appear on the home page.
24 changes: 24 additions & 0 deletions generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require('path')
const fs = require('fs')

// load json files
const readDirectoryPath = path.join(__dirname, 'public', 'data')
const files = fs.readdirSync(readDirectoryPath)
const profiles = files.map((file) => {
const data = JSON.parse(
fs.readFileSync(`${path.join(__dirname, 'public', 'data', file)}`, 'utf8'),
)
return {
username: file.split('.')[0],
...data,
}
})

// generate list file
const writeDirectoryPath = path.join(__dirname, 'public', 'list.json')
const output = profiles.map((profile) => ({
username: profile.username,
avatar: profile.avatar,
}))

fs.writeFileSync(writeDirectoryPath, JSON.stringify(output))
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
},
"scripts": {
"lint": "eslint public/data --ext .json",
"prestart": "npm run generate",
"start": "react-scripts start",
"start:prod": "serve -s build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"generate": "node generate.js"
},
"eslintConfig": {
"extends": [
Expand Down Expand Up @@ -54,4 +56,4 @@
"eslint-plugin-react": "^7.25.1",
"prettier": "2.4.0"
}
}
}
36 changes: 0 additions & 36 deletions public/data/_list.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/Components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Home() {
}

useEffect(() => {
fetch('/data/_list.json')
fetch('/list.json')
.then((response) => response.json())
.then((data) => setList(data))
}, [])
Expand Down