From 969610b37567ce3416db2c0f99a4ca2f29b60afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20R=C3=B6tsch?= Date: Thu, 21 Sep 2017 14:57:13 +0200 Subject: [PATCH] refactor(data-download): load latest release instead of cloning the data repo --- bin/download-content-model.js | 113 ++++++++++++++++++++++++---------- package.json | 7 ++- 2 files changed, 86 insertions(+), 34 deletions(-) diff --git a/bin/download-content-model.js b/bin/download-content-model.js index da63ebe..cc26c2f 100755 --- a/bin/download-content-model.js +++ b/bin/download-content-model.js @@ -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( - 'git@github.com: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() diff --git a/package.json b/package.json index b80272e..7156a09 100644 --- a/package.json +++ b/package.json @@ -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": {