mirror of https://github.com/Budibase/budibase.git
5 changed files with 185 additions and 38 deletions
@ -0,0 +1,149 @@ |
|||
const { join } = require("path") |
|||
const { ObjectStoreBuckets } = require("../../constants") |
|||
const fs = require("fs") |
|||
const { upload, retrieveToTmp, streamUpload } = require("./utilities") |
|||
|
|||
/** |
|||
* Client library paths in the object store: |
|||
* Previously, the entire standard-components package was downloaded from NPM |
|||
* as a tarball and extracted to the object store, even though only the manifest |
|||
* was ever needed. Therefore we need to support old apps which may still have |
|||
* the manifest at this location for the first update. |
|||
* |
|||
* The new paths for the in-use version are: |
|||
* {appId}/manifest.json |
|||
* {appId}/budibase-client.js |
|||
* |
|||
* The paths for the backups are: |
|||
* {appId}/manifest.json.bak |
|||
* {appId}/budibase-client.js.bak |
|||
* |
|||
* We don't rely on NPM at all any more, as when updating to the latest version |
|||
* we pull both the manifest and client bundle from the server's dependencies |
|||
* in the local file system. |
|||
*/ |
|||
|
|||
/** |
|||
* Backs up the current client library version by copying both the manifest |
|||
* and client bundle to .bak extensions in the object store. Only the one |
|||
* previous version is stored as a backup, which can be reverted to. |
|||
* @param appId The app ID to backup |
|||
* @returns {Promise<void>} |
|||
*/ |
|||
exports.backupClientLibrary = async appId => { |
|||
let tmpManifestPath |
|||
let tmpClientPath |
|||
|
|||
// Copy existing manifest to tmp
|
|||
try { |
|||
// Try to load the manifest from the new file location
|
|||
tmpManifestPath = await retrieveToTmp( |
|||
ObjectStoreBuckets.APPS, |
|||
join(appId, "manifest.json") |
|||
) |
|||
} catch (error) { |
|||
// Fallback to loading it from the old location for old apps
|
|||
tmpManifestPath = await retrieveToTmp( |
|||
ObjectStoreBuckets.APPS, |
|||
join( |
|||
appId, |
|||
"node_modules", |
|||
"budibase", |
|||
"standard-components", |
|||
"package", |
|||
"manifest.json" |
|||
) |
|||
) |
|||
} |
|||
|
|||
// Copy existing client lib to tmp
|
|||
tmpClientPath = await retrieveToTmp( |
|||
ObjectStoreBuckets.APPS, |
|||
join(appId, "budibase-client.js") |
|||
) |
|||
|
|||
// Upload manifest as backup
|
|||
await upload({ |
|||
bucket: ObjectStoreBuckets.APPS, |
|||
filename: join(appId, "manifest.json.bak"), |
|||
path: tmpManifestPath, |
|||
type: "application/json", |
|||
}) |
|||
|
|||
// Upload client library as backup
|
|||
await upload({ |
|||
bucket: ObjectStoreBuckets.APPS, |
|||
filename: join(appId, "budibase-client.js.bak"), |
|||
path: tmpClientPath, |
|||
type: "application/javascript", |
|||
}) |
|||
} |
|||
|
|||
/** |
|||
* Uploads the latest version of the component manifest and the client library |
|||
* to the object store, overwriting the existing version. |
|||
* @param appId The app ID to update |
|||
* @returns {Promise<void>} |
|||
*/ |
|||
exports.updateClientLibrary = async appId => { |
|||
// Upload latest component manifest
|
|||
await streamUpload( |
|||
ObjectStoreBuckets.APPS, |
|||
join(appId, "manifest.json"), |
|||
fs.createReadStream( |
|||
require.resolve("@budibase/standard-components/manifest.json") |
|||
), |
|||
{ |
|||
ContentType: "application/json", |
|||
} |
|||
) |
|||
|
|||
// Upload latest component library
|
|||
await streamUpload( |
|||
ObjectStoreBuckets.APPS, |
|||
join(appId, "budibase-client.js"), |
|||
fs.createReadStream(require.resolve("@budibase/client")), |
|||
{ |
|||
ContentType: "application/javascript", |
|||
} |
|||
) |
|||
} |
|||
|
|||
/** |
|||
* Reverts the version of the client library and manifest to the previously |
|||
* used version for an app. |
|||
* @param appId The app ID to revert |
|||
* @returns {Promise<void>} |
|||
*/ |
|||
exports.revertClientLibrary = async appId => { |
|||
let tmpManifestPath |
|||
let tmpClientPath |
|||
|
|||
// Copy backup manifest to tmp
|
|||
tmpManifestPath = await retrieveToTmp( |
|||
ObjectStoreBuckets.APPS, |
|||
join(appId, "manifest.json.bak") |
|||
) |
|||
|
|||
// Copy backup client lib to tmp
|
|||
tmpClientPath = await retrieveToTmp( |
|||
ObjectStoreBuckets.APPS, |
|||
join(appId, "budibase-client.js.bak") |
|||
) |
|||
|
|||
// Upload manifest backup
|
|||
await upload({ |
|||
bucket: ObjectStoreBuckets.APPS, |
|||
filename: join(appId, "manifest.json"), |
|||
path: tmpManifestPath, |
|||
type: "application/json", |
|||
}) |
|||
|
|||
// Upload client library backup
|
|||
await upload({ |
|||
bucket: ObjectStoreBuckets.APPS, |
|||
filename: join(appId, "budibase-client.js"), |
|||
path: tmpClientPath, |
|||
type: "application/javascript", |
|||
}) |
|||
} |
|||
@ -1,27 +0,0 @@ |
|||
const { join } = require("path") |
|||
const { ObjectStoreBuckets } = require("../../constants") |
|||
const { streamUpload } = require("./utilities") |
|||
const fs = require("fs") |
|||
|
|||
const BUCKET_NAME = ObjectStoreBuckets.APPS |
|||
|
|||
exports.uploadClientLibrary = async appId => { |
|||
await streamUpload( |
|||
BUCKET_NAME, |
|||
join(appId, "budibase-client.js"), |
|||
fs.createReadStream(require.resolve("@budibase/client")), |
|||
{ |
|||
ContentType: "application/javascript", |
|||
} |
|||
) |
|||
await streamUpload( |
|||
BUCKET_NAME, |
|||
join(appId, "manifest.json"), |
|||
fs.createReadStream( |
|||
require.resolve("@budibase/standard-components/manifest.json") |
|||
), |
|||
{ |
|||
ContentType: "application/javascript", |
|||
} |
|||
) |
|||
} |
|||
Loading…
Reference in new issue