Skip to content

Commit

Permalink
Merge pull request #127 from zapier/example-apps-tied-to-version
Browse files Browse the repository at this point in the history
fix(cli) tie example apps to package version
  • Loading branch information
xavdid committed Dec 16, 2019
2 parents 15c891c + f10fbc4 commit d4c9225
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/cli/src/utils/example-apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ const fetch = require('node-fetch');
const path = require('path');
const fse = require('fs-extra');
const AdmZip = require('adm-zip');
const debug = require('debug')('zapier:example-apps');

const xdg = require('./xdg');
const { copyDir } = require('./files');
const { PACKAGE_VERSION } = require('../constants');

const REPO_ZIP_URL =
'https://codeload.github.com/zapier/zapier-platform/zip/master';
const REPO_ZIP_URL = `https://codeload.github.com/zapier/zapier-platform/zip/zapier-platform-cli%40${PACKAGE_VERSION}`;
const zipName = `zapier-platform-zapier-platform-cli-${PACKAGE_VERSION}`;
const folderName = `zapier-platform-cached`; // version independant

const checkCacheUpToDate = async repoDir => {
const etagPath = path.join(repoDir, 'etag');
Expand All @@ -24,7 +27,7 @@ const checkCacheUpToDate = async repoDir => {
};

const downloadRepo = async destDir => {
const destZipPath = path.join(destDir, 'zapier-platform-master.zip');
const destZipPath = path.join(destDir, `${zipName}.zip`);

const res = await fetch(REPO_ZIP_URL);
const dest = fse.createWriteStream(destZipPath);
Expand All @@ -41,24 +44,29 @@ const downloadRepo = async destDir => {
zip.extractAllTo(destDir, true);

// Save etag for cache validation
const etagPath = path.join(destDir, 'zapier-platform-master', 'etag');
// this could probably just be cli version, but this is fine too
const etagPath = path.join(destDir, zipName, 'etag');
fse.writeFileSync(etagPath, res.headers.get('etag'));

fse.removeSync(destZipPath);
fse.renameSync(path.join(destDir, zipName), path.join(destDir, folderName));

return destZipPath;
};

const ensureRepoCached = async () => {
const cacheDir = xdg.ensureCacheDir();
const repoDir = path.join(cacheDir, 'zapier-platform-master');
const repoDir = path.join(cacheDir, folderName);

if (fse.existsSync(repoDir)) {
if (!await checkCacheUpToDate(repoDir)) {
debug('repo exists');
if (!(await checkCacheUpToDate(repoDir))) {
debug('cached repo is stale, re-downloading');
await fse.remove(repoDir);
await downloadRepo(cacheDir);
}
} else {
debug('no cached repo, downloading');
await downloadRepo(cacheDir);
}

Expand Down

0 comments on commit d4c9225

Please sign in to comment.