mirror of
https://github.com/hanzoai/workbox.git
synced 2026-09-24 17:04:35 +00:00
* The contents of v3, reconciled with master. * Remove a few stray files. * Another extra file. * Two more deletions. * Setting up for stable release * v3.0.0 * Updating CDN and versions * Doc repair for wf (#1358) * Changing doc structure to work with WF * Changing doc structure to work with WF
99 lines
2.5 KiB
JavaScript
99 lines
2.5 KiB
JavaScript
const path = require('path');
|
|
const glob = require('glob');
|
|
const fs = require('fs-extra');
|
|
const oneLine = require('common-tags').oneLine;
|
|
const storage = require('@google-cloud/storage');
|
|
|
|
const cdnDetails = require('../../cdn-details.json');
|
|
const logHelper = require('../../infra/utils/log-helper');
|
|
|
|
const PROJECT_ID = 'workbox-bab1f';
|
|
const SERVICE_ACCOUNT_PATH = path.join(__dirname, '..', '..',
|
|
`workbox-9d39634504ad.json`);
|
|
|
|
const ERROR_SERVICE_ACCOUNT = oneLine`
|
|
Unable to find the service account file that is required to upload
|
|
to the CDN.
|
|
Please get the service account file and put it at the root of the
|
|
workbox repo.
|
|
|
|
File Path Tested: '${SERVICE_ACCOUNT_PATH}'.
|
|
`;
|
|
|
|
class CDNHelper {
|
|
constructor() {
|
|
this._gcs = null;
|
|
}
|
|
|
|
_getReleaseTagPath(tagName) {
|
|
return `${cdnDetails.releasesDir}/${tagName}`;
|
|
}
|
|
|
|
getGCS() {
|
|
if (!this._gcs) {
|
|
try {
|
|
fs.accessSync(SERVICE_ACCOUNT_PATH);
|
|
} catch (err) {
|
|
throw new Error(ERROR_SERVICE_ACCOUNT);
|
|
}
|
|
|
|
this._gcs = storage({
|
|
projectId: PROJECT_ID,
|
|
keyFilename: SERVICE_ACCOUNT_PATH,
|
|
});
|
|
}
|
|
|
|
return this._gcs;
|
|
}
|
|
|
|
async tagExists(tagName) {
|
|
const gcs = this.getGCS();
|
|
try {
|
|
const bucket = gcs.bucket(cdnDetails.bucketName);
|
|
// bucket.file('some/path/').exists() doesn't seem to work
|
|
// for nested directories. Instead we are checking if there are
|
|
// files in the expected release directory.
|
|
const response = await bucket.getFiles({
|
|
prefix: `${this._getReleaseTagPath(tagName)}/`,
|
|
});
|
|
const files = response[0];
|
|
return files.length > 0;
|
|
} catch (err) {
|
|
logHelper.error(err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async upload(tagName, directoryToUpload) {
|
|
const gcs = this.getGCS();
|
|
|
|
const globPattern = path.posix.join(directoryToUpload, '*');
|
|
const filePaths = glob.sync(globPattern, {
|
|
absolute: true,
|
|
});
|
|
|
|
const publicUrls = [];
|
|
const bucket = gcs.bucket(cdnDetails.bucketName);
|
|
for (let filePath of filePaths) {
|
|
const destination =
|
|
`${this._getReleaseTagPath(tagName)}/${path.basename(filePath)}`;
|
|
|
|
try {
|
|
await bucket.upload(filePath, {
|
|
destination,
|
|
public: true,
|
|
});
|
|
} catch (err) {
|
|
logHelper.error(`Failed to upload file to GCS bucket: '${filePath}'`);
|
|
throw err;
|
|
}
|
|
publicUrls.push(
|
|
`${cdnDetails.origin}/${cdnDetails.bucketName}/${destination}`
|
|
);
|
|
}
|
|
return publicUrls;
|
|
}
|
|
}
|
|
|
|
module.exports = new CDNHelper();
|