Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
16 changes: 16 additions & 0 deletions conf/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
"type": "string",
"isDirectory": true,
"default": "$DATA/contentplugins"
},
"autoUpdate": {
"description": "Enable/disable automatic plugin updates",
"type": "boolean",
"default": false
},
"updateCron": {
"description": "CRON expression for automatic update check interval",
"type": "string",
"default": "0 0 * * *"
},
"semverSpecifier": {
"description": "Semver range specifier for automatic updates (* for major, ^ for minor, ~ for patch)",
"type": "string",
"enum": ["~", "^", "*"],
"default": "^"
}
}
}
107 changes: 107 additions & 0 deletions lib/ContentPluginModule.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AbstractApiModule from 'adapt-authoring-api'
import apidefs from './apidefs.js'
import cron from 'node-cron'
import fs from 'fs/promises'
import { glob } from 'glob'
import path from 'path'
Expand Down Expand Up @@ -84,6 +85,20 @@ class ContentPluginModule extends AbstractApiModule {
}
this.framework.postInstallHook.tap(this.syncPluginData.bind(this))
this.framework.postUpdateHook.tap(this.syncPluginData.bind(this))

// Set up auto-update CRON task
if (this.getConfig('autoUpdate')) {
const cronExpression = this.getConfig('updateCron')
this.log('info', 'AUTO_UPDATE', `scheduling auto-update checks with cron expression: ${cronExpression}`)
cron.schedule(cronExpression, async () => {
this.log('info', 'AUTO_UPDATE', 'running scheduled plugin update check')
try {
await this.runAutoUpdate()
} catch (e) {
this.log('error', 'AUTO_UPDATE', e)
}
})
}
}

/** @override */
Expand Down Expand Up @@ -302,6 +317,7 @@ class ContentPluginModule extends AbstractApiModule {
* @returns Resolves with plugin DB data
*/
async installPlugin (pluginName, versionOrPath, options = { strict: false, force: false }) {
this.log('debug', 'INSTALL', `installing ${pluginName}@${versionOrPath}`)
const [pluginData] = await this.find({ name: String(pluginName) }, { includeUpdateInfo: true })
const { name, version, sourcePath, isLocalInstall } = await this.processPluginFiles({ ...pluginData, sourcePath: versionOrPath })
const [existingPlugin] = await this.find({ name })
Expand All @@ -327,6 +343,7 @@ class ContentPluginModule extends AbstractApiModule {
.setData({ name })
}
await this.processPluginSchemas(data)
this.log('info', 'INSTALL', `installed ${name}@${version}`)
return info
}

Expand Down Expand Up @@ -377,6 +394,96 @@ class ContentPluginModule extends AbstractApiModule {
return p
}

/**
* Checks for updates for one or more plugins
* @param {string|Array<string>} pluginName The name (or array of names) for the plugins to check
* @return {Object|Array<Object>} Result(s) from getPluginInfos
*/
async checkForPluginUpdate (pluginName) {
const pluginNames = Array.isArray(pluginName) ? pluginName : [pluginName]
const semverSpecifier = this.getConfig('semverSpecifier')

this.log('verbose', 'UPDATE', `checking for updates for ${pluginNames.join(', ')}`)

const results = []
for (const name of pluginNames) {
const [pluginData] = await this.find({ name }, { includeUpdateInfo: true })
if (!pluginData) {
this.log('warn', 'UPDATE', `plugin ${name} not found in database`)
continue
}

if (pluginData.canBeUpdated && pluginData.latestCompatibleVersion) {
const currentVersion = pluginData.version
const latestVersion = pluginData.latestCompatibleVersion

// Check if the update is within the configured semver range
let shouldUpdate = false
if (semverSpecifier === '*') {
// Allow any version update
shouldUpdate = true
} else {
const range = `${semverSpecifier}${currentVersion}`
shouldUpdate = semver.satisfies(latestVersion, range)
}

if (shouldUpdate) {
this.log('info', 'UPDATE', `update found for ${name} (${latestVersion})`)
results.push({
name,
currentVersion,
updateVersion: latestVersion,
canUpdate: true
})
} else {
const range = semverSpecifier === '*' ? 'any' : `${semverSpecifier}${currentVersion}`
this.log('verbose', 'UPDATE', `update available for ${name} (${latestVersion}) but outside semver range ${range}`)
}
} else {
this.log('verbose', 'UPDATE', `no updates found for ${name}`)
}
}

return Array.isArray(pluginName) ? results : (results[0] || null)
}

/**
* Runs the automated update process for all non-local plugins
*/
async runAutoUpdate () {
// Get all plugins that are not locally installed
const plugins = await this.find({ isLocalInstall: false })

if (!plugins.length) {
this.log('verbose', 'AUTO_UPDATE', 'no plugins to check for updates')
return
}

const pluginNames = plugins.map(p => p.name)
this.log('info', 'AUTO_UPDATE', `checking ${pluginNames.length} plugins for updates`)

const updatesAvailable = await this.checkForPluginUpdate(pluginNames)

if (!updatesAvailable || updatesAvailable.length === 0) {
this.log('info', 'AUTO_UPDATE', 'no updates available')
return
}

this.log('info', 'AUTO_UPDATE', `found ${updatesAvailable.length} plugin(s) with updates`)

// Install updates
for (const update of updatesAvailable) {
if (update.canUpdate) {
try {
await this.installPlugin(update.name, update.updateVersion, { force: true })
this.log('info', 'AUTO_UPDATE', `successfully updated ${update.name} from ${update.currentVersion} to ${update.updateVersion}`)
} catch (e) {
this.log('error', 'AUTO_UPDATE', `failed to update ${update.name}:`, e)
}
}
}
}

/** @override */
serveSchema () {
return async (req, res, next) => {
Expand Down
25 changes: 23 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"adapt-cli": "^3.3.3",
"glob": "^13.0.0",
"node-cron": "^4.2.1",
"semver": "^7.6.0"
},
"peerDependencies": {
Expand Down