This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(data-download): load latest release instead of cloning the d…
…ata repo
- Loading branch information
Showing
2 changed files
with
86 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters