Commit 7d912c49 authored by 🕺 Winnie 🕺's avatar 🕺 Winnie 🕺

Cache sound files

parent 1212c202
Pipeline #16143176 passed with stage
in 3 minutes and 43 seconds
/_book/
/gitbook/
/generated-book/
/node_modules/
/public/
/book.pdf
......
......@@ -5,6 +5,7 @@ image: node:8.9
cache:
paths:
- node_modules/ # Node modules and dependencies
- generated-book/
before_script:
- npm install gitbook-cli -g # install gitbook
......@@ -22,7 +23,7 @@ test:
- branches # this job will affect only the 'master' branch
except:
- master
# the 'pages' job will deploy and build your site to the 'public' path
pages:
stage: deploy
......
......@@ -37,25 +37,41 @@ function consumeApi (url) {
})
}
function downloadFile (url, destination) {
console.error(`Downloading ${url} to ${destination}`)
return new Promise((resolve, reject) => {
https.get(url, (res) => {
const { statusCode } = res
if (statusCode !== 200) {
return reject(new Error(`Request Failed.\nStatus Code: ${statusCode}`))
function downloadFile (project, filePath, outputPath) {
// temporary workaround because API doesn't work with LFS (https://gitlab.com/gitlab-org/gitlab-ce/issues/41843)
// const url = `https://gitlab.com/api/v4/projects/${project.id}/repository/files/${filePath}/raw?ref=${project.default_branch}`
const url = `${project.web_url}/raw/${project.default_branch}/${filePath}`
console.error(`Downloading ${url} to ${outputPath}`)
return consumeApi(`https://gitlab.com/api/v4/projects/${project.id}/repository/files/${filePath}?ref=${project.default_branch}`)
.then(fileMetadata => new Promise((resolve, reject) => {
const commitIdFile = `${outputPath}.last_commit_id`
if (fs.existsSync(outputPath) && fs.existsSync(commitIdFile)) {
const lastCommitId = fs.readFileSync(commitIdFile).toString()
if (lastCommitId === fileMetadata.last_commit_id) {
console.log(`${outputPath} is already up to date.`)
resolve()
return
}
}
let rawData = []
res.on('data', chunk => rawData.push(chunk))
res.on('end', () => {
const buffer = Buffer.concat(rawData)
fs.writeFileSync(destination, buffer)
resolve()
})
}).on('error', (error) => reject(error))
})
https.get(url, (res) => {
const { statusCode } = res
if (statusCode !== 200) {
return reject(new Error(`Request Failed.\nStatus Code: ${statusCode}`))
}
let rawData = []
res.on('data', chunk => rawData.push(chunk))
res.on('end', () => {
const buffer = Buffer.concat(rawData)
fs.writeFileSync(outputPath, buffer)
fs.writeFileSync(commitIdFile, fileMetadata.last_commit_id)
resolve()
})
}).on('error', (error) => reject(error))
}))
}
function fetchSubgroups (group) {
......@@ -133,12 +149,10 @@ ${dateRange}
summaryLines.push(`${indentation} * [${project.name}](${fullPath}/README.md)`)
const downloadSoundFile = extension => {
// temporary workaround because API doesn't work with LFS
// const url = `https://gitlab.com/api/v4/projects/${project.id}/repository/files/${project.path}.${extension}/raw?ref=${project.default_branch}`
const url = `${project.web_url}/raw/${project.default_branch}/${project.path}.${extension}`
const filePath = `${project.path}.${extension}`
const outputPath = `${path}/${project.path}.${extension}`
return downloadFile(url, outputPath)
.catch(error => console.error(`${outputPath} is missing!`)) // ignore missing sound files
return downloadFile(project, filePath, outputPath)
.catch(() => console.error(`${outputPath} is missing!`)) // ignore missing sound files
}
return Promise.all(soundFileExtension.map(downloadSoundFile))
})))
......
......@@ -2,9 +2,9 @@
"private": true,
"scripts": {
"prebuild": "npm run generate",
"build": "gitbook build gitbook public",
"generate": "node generate.js gitbook",
"start": "gitbook serve gitbook",
"build": "gitbook build generated-book public",
"generate": "node generate.js generated-book",
"start": "gitbook serve generated-book",
"test": "standard"
},
"devDependencies": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment