Skip to content
Merged
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
50 changes: 17 additions & 33 deletions lib/AdaptFrameworkImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'upath'
import semver from 'semver'
import { exec } from 'child_process'

import AdaptFrameworkUtils from './AdaptFrameworkUtils.js'
import AdaptFrameworkUtils from './AdaptFrameworkUtils.js'

import ComponentTransform from './migrations/component.js'
import ConfigTransform from './migrations/config.js'
Expand Down Expand Up @@ -279,7 +279,7 @@ class AdaptFrameworkImport {
try {
await fs.rm(`${this.unzipPath}/package-lock.json`)
} catch (e) {}

await this.patchCustomStyle()

if (!semver.satisfies(this.pkg.version, semver.major(this.framework.version).toString())) {
Expand Down Expand Up @@ -413,54 +413,38 @@ class AdaptFrameworkImport {
runGruntTask (task) {
return this.execPromise(`npx grunt ${task}`)
}

async execPromise (task) {
this.framework.log('debug', 'EXEC', task)
return new Promise((resolve, reject) => {
exec(task, { cwd: this.unzipPath }, (error, stdout, stderr) => {
exec(task, { cwd: this.framework.path }, (error, stdout, stderr) => {
if (stdout) this.framework.log('debug', 'EXEC_STDOUT', task, stdout)
if (stderr) this.framework.log('debug', 'EXEC_STDERR', task, stderr)
error ? reject(error) : resolve(stdout)
})
})
}

/**
* Copies migration scripts from every local plugin being migrated to unzipped course
*/
async addMigrationScripts () {
if (this.pluginsToMigrate.length) {
this.pluginsToMigrate.forEach(async p => {
this.framework.log('debug', `Adding migration scripts for ${p}`)
const [pluginMigrationPaths] = await glob(`${this.framework.path}/**/**/${p}/migrations`)
if (!pluginMigrationPaths) return
try {
let [destinationPath] = await glob(`${this.unzipPath}/**/**/${p}/migrations`)
if (!destinationPath) {
const [pluginPath] = await glob(`${this.unzipPath}/**/**/${p}`)
destinationPath = await fs.ensureDir(path.join(pluginPath, 'migrations'))
}
await fs.copy(pluginMigrationPaths, destinationPath)
} catch (error) {
throw new Error(`Error copying migration scripts for ${p}: ${error.message}`)
}
})
}
}

/**
* Handle migrate course data, installs adapt-migrations/capture data/adds updated scripts/migrates data
*/
async migrateCourseData () {
try {
await this.execPromise('npm install adaptlearning/adapt-migrations')
await fs.copy(path.join(this.framework.path, 'grunt'), path.join(this.unzipPath, 'grunt'), { recursive: true })
await this.addMigrationScripts()
await this.runGruntTask('migration:capture')
await this.runGruntTask('migration:migrate')
const folderName = `${this.userId}-migrations`
const relativePath = path.relative(this.framework.path, this.unzipPath)
const outputDir = fs.existsSync(path.join(this.unzipPath, 'build'))
? path.join(relativePath, 'build')
: path.join(relativePath, 'src')
const captureDir = path.join('./', folderName)

await this.runGruntTask(`migration:capture --outputdir=${outputDir} --capturedir=${captureDir}`)
await this.runGruntTask(`migration:migrate --outputdir=${outputDir} --capturedir=${captureDir}`)

const captureFolder = path.join(this.framework.path, captureDir)
await fs.remove(captureFolder)
} catch (error) {
this.framework.log('error', 'Migration process failed', error)
throw error
throw new Error(`Migration process failed: ${error.message}`)
}
}

Expand Down
28 changes: 24 additions & 4 deletions lib/AdaptFrameworkModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import FWUtils from './AdaptFrameworkUtils.js'
import path from 'path'
import semver from 'semver'
import { unzip } from 'zipper'
import { exec } from 'child_process'
/**
* Module to handle the interface with the Adapt framework
* @memberof adaptframework
Expand Down Expand Up @@ -48,16 +49,19 @@ class AdaptFrameworkModule extends AbstractModule {
*/
this.contentMigrations = []

const content = await this.app.waitForModule('content')
content.accessCheckHook.tap(this.checkContentAccess.bind(this))

this.postInstallHook.tap(this.installFrameworkModules.bind(this))
this.postUpdateHook.tap(this.installFrameworkModules.bind(this))

await this.installFramework()

if (this.app.args['update-framework'] === true) {
await this.updateFramework()
}
await Promise.all([this.loadSchemas(), this.initRoutes()])

const content = await this.app.waitForModule('content')
content.accessCheckHook.tap(this.checkContentAccess.bind(this))

this.logStatus()
}

Expand All @@ -69,6 +73,22 @@ class AdaptFrameworkModule extends AbstractModule {
return AdaptCli.getCurrentFrameworkVersion({ cwd: this.path })
}

/**
* Run npm install on framework directory to install/update any dependencies
* @type {Promise}
*/
async installFrameworkModules () {
try {
exec('npm install', { cwd: this.path }, (stdout, stderr) => {
if (stdout) this.log('debug', stdout)
if (stderr) this.log('debug', stderr)
})
} catch (e) {
this.log('error', `failed to run npm install, ${e.message}`)
throw this.app.errors.FW_INSTALL_FAILED
}
}

/**
* Installs a local copy of the Adapt framework
* @return {Promise}
Expand Down Expand Up @@ -101,7 +121,7 @@ class AdaptFrameworkModule extends AbstractModule {
this.log('error', `failed to install framework, ${e.message}`)
throw this.app.errors.FW_INSTALL_FAILED
}
this.postInstallHook.invoke()
await this.postInstallHook.invoke()
}

/**
Expand Down