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

Commit

Permalink
refactor(data-download): load latest release instead of cloning the d…
Browse files Browse the repository at this point in the history
…ata repo
  • Loading branch information
Benedikt Rötsch authored and axe312ger committed Sep 22, 2017
1 parent b0051bb commit 969610b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 34 deletions.
113 changes: 82 additions & 31 deletions bin/download-content-model.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,93 @@
const fs = require('fs')
const path = require('path')
const zlib = require('zlib')

const clone = require('git-clone')
const ora = require('ora')
const fetch = require('node-fetch')
const Listr = require('listr')
const tar = require('tar')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')

const REPO_ROOT = path.resolve(__dirname, '..')
const DATA_DIR = path.resolve(REPO_ROOT, 'data')

const permissions = ora('Checking permissions to write files to the disk').start()

fs.access(REPO_ROOT, fs.constants.W_OK, (err) => {
if (err) {
permissions.fail(`Script is unable to write to project directory. Please check your users file permissions for ${REPO_ROOT}`)
process.exit(1)
}

permissions.succeed()

const cloning = ora('Cloning content model').start()

rimraf(DATA_DIR, (error) => {
if (error) {
cloning.fail('An error happend while removing existing content model')
process.exit(1)
const tasks = new Listr([
{
title: `Checking permissions to write files to the disk`,
task: (ctx) => {
return new Promise((resolve, reject) => {
fs.access(REPO_ROOT, fs.constants.W_OK, (err) => {
if (err) {
reject()
console.error(`Script is unable to write to project directory. Please check your users file permissions for ${REPO_ROOT}`)
process.exit(1)
}
resolve()
})
})
}
clone(
'[email protected]:contentful/content-models.git',
DATA_DIR,
(error) => {
if (error) {
cloning.fail('An error happend during cloning')
console.log(error)
process.exit(1)
},
{
title: `Clean up directory`,
task: (ctx) => {
return new Promise((resolve, reject) => {
rimraf(DATA_DIR, (err) => {
if (err) {
reject(err)
}
resolve()
})
}).then(() => {
return new Promise((resolve, reject) => {
mkdirp(DATA_DIR, (err) => {
if (err) {
reject(err)
}
resolve()
})
})
})
}
},
{
title: `Fetching release information of contentful/content-models`,
task: (ctx) => {
return fetch(`https://api.github.com/repos/contentful/content-models/releases/latest`)
.then((response) => response.json())
.then((json) => {
ctx.latestReleaseInfo = json
})
}
},
{
title: `Downloading latest release of contentful/content-models`,
task: (ctx) => {
ctx.latestReleaseZipLocation = path.join(DATA_DIR, 'latest-release.tar.gz')
return fetch(ctx.latestReleaseInfo.tarball_url)
.then((response) => {
ctx.latestReleaseTarballStream = response.body
})
}
},
{
title: `Unpacking latest release of contentful/content-models`,
task: (ctx) => {
return new Promise((resolve, reject) => {
try {
ctx.latestReleaseTarballStream
.pipe(zlib.Unzip())
.pipe(new tar.Unpack({
cwd: DATA_DIR,
strip: 1
}))
.on('error', reject)
.on('close', resolve)
} catch (err) {
reject(err)
}
})
}
}
])

cloning.succeed('Cloned data to ./data\n')
}
)
})
})
tasks.run()
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
"eslint-plugin-promise": "^3.4.1",
"eslint-plugin-standard": "^2.0.1",
"gh-pages": "^1.0.0",
"git-clone": "^0.1.0",
"listr": "^0.12.0",
"node-fetch": "^1.7.3",
"now": "^5.3.0",
"ora": "^1.3.0",
"rimraf": "^2.6.1"
"rimraf": "^2.6.1",
"tar": "^4.0.1"
},
"config": {
"commitizen": {
Expand Down

0 comments on commit 969610b

Please sign in to comment.