Skip to content

Commit

Permalink
add assets update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
miaowm5 committed Aug 31, 2020
1 parent bdb6596 commit 185f3e7
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 33 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: checkout main branch
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
Expand All @@ -27,3 +28,16 @@ jobs:
deploy_type: 'backend'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: checkout assets branch
uses: actions/checkout@v2
with:
ref: 'assets'
path: './build/assets'
- name: build assets
run: npm run buildAssets
- name: deploy to assets branch if modify
uses: miaowm5/rmproject2@action
with:
deploy_type: 'backend-assets'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66 changes: 33 additions & 33 deletions assets.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
[
["rule/101.jpg", ""],
["rule/102.jpg", ""],
["rule/103.jpg", ""],
["rule/104.jpg", ""],
["rule/105.jpg", ""],
["rule/106.jpg", ""],
["rule/107.jpg", ""],
["rule/201.jpg", ""],
["rule/202.jpg", ""],
["rule/203.jpg", ""],
["rule/204.jpg", ""],
["rule/205.jpg", ""],
["rule/206.jpg", ""],
["rule/207.jpg", ""],
["rule/301.jpg", ""],
["rule/302.jpg", ""],
["rule/303.jpg", ""],
["rule/304.jpg", ""],
["rule/305.jpg", ""],
["rule/306.jpg", ""],
["rule/307.jpg", ""],
["rule/308.jpg", ""],
["rule/309.jpg", ""],
["rule/310.jpg", ""],
["rule/311.jpg", ""],
["rule/401.jpg", ""],
["rule/402.jpg", ""],
["rule/403.jpg", ""],
["rule/501.jpg", ""],
["rule/502.jpg", ""],
["rule/503.jpg", ""]
]
{
"rule/101.jpg": "",
"rule/102.jpg": "",
"rule/103.jpg": "",
"rule/104.jpg": "",
"rule/105.jpg": "",
"rule/106.jpg": "",
"rule/107.jpg": "",
"rule/201.jpg": "",
"rule/202.jpg": "",
"rule/203.jpg": "",
"rule/204.jpg": "",
"rule/205.jpg": "",
"rule/206.jpg": "",
"rule/207.jpg": "",
"rule/301.jpg": "",
"rule/302.jpg": "",
"rule/303.jpg": "",
"rule/304.jpg": "",
"rule/305.jpg": "",
"rule/306.jpg": "",
"rule/307.jpg": "",
"rule/308.jpg": "",
"rule/309.jpg": "",
"rule/310.jpg": "",
"rule/311.jpg": "",
"rule/401.jpg": "",
"rule/402.jpg": "",
"rule/403.jpg": "",
"rule/501.jpg": "",
"rule/502.jpg": "",
"rule/503.jpg": ""
}
69 changes: 69 additions & 0 deletions deploy/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

const fse = require('fs-extra')
const path = require('path')
const http = require('http')
const fs = require('fs')

const mainPath = path.join(__dirname, '../')

const download = async (url, filename)=>{
console.log(`Download ${filename} from ${url}`)
const dest = path.join(mainPath, './build/assets/assets', filename)
await fse.ensureDir(path.dirname(dest))
const file = fs.createWriteStream(dest)
await new Promise((success, fail)=>{
http.get(url, (response)=>{
response.pipe(file)
file.on('finish', ()=>{ file.close(success) })
}).on('error', (err)=>{
fs.unlink(dest)
fail(err)
})
})
}

const getFileList = async (root, dir)=>{
const fileList = await fse.readdir(path.join(root, dir))
let result = []
const promises = fileList.map((file)=>{
const handleFile = async ()=>{
const filePath = path.join(dir, file)
const stat = await fse.stat(path.join(root, filePath))
if (stat.isFile()){
result = result.concat(filePath)
}else{
const childFile = await getFileList(root, filePath)
result = result.concat(childFile)
}
}
return handleFile()
})
await Promise.all(promises)
return result
}

const main = async ()=>{
const current = JSON.parse((await fse.readFile(path.join(mainPath, './build/assets/assets.json'))).toString())
const latest = JSON.parse((await fse.readFile(path.join(mainPath, 'assets.json'))).toString())
const update = []
Object.keys(latest).forEach((file)=>{
if (current[file] !== latest[file]){
update.push(download(latest[file], file))
}
})
const fileList = await getFileList(path.join(mainPath, './build/assets/assets'), '.')
fileList.forEach((file)=>{
if (latest[file.replace(/\\/g, '/')] === undefined){
console.log(`Remove ${file}`)
update.push(fse.remove(path.join(mainPath, './build/assets/assets', file)))
}
})
if (update.length > 0){
await Promise.all(update)
fse.copy(path.join(mainPath, 'assets.json'), path.join(mainPath, './build/assets/assets.json'))
}else{
fse.remove(path.join(mainPath, './build/assets'))
}
}

main().catch((e)=>{ console.error(e) })
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"scripts": {
"build": "node deploy/index.js",
"buildAssets": "node deploy/assets.js",
"devServer": "npm run build && npx anywhere -s -d build",
"devAssets": "node deploy/devAssets.js",
"lint": "eslint --ext .js deploy",
Expand Down

0 comments on commit 185f3e7

Please sign in to comment.