From 7dd41be552edb3058560fb172f03c6015d679500 Mon Sep 17 00:00:00 2001 From: joe-allen-89 <85872286+joe-allen-89@users.noreply.github.com> Date: Fri, 28 Mar 2025 14:40:32 +0000 Subject: [PATCH] New: postinstall script added to allow older courses to be migrated in the authoring tool --- examples/migrations.js | 8 -------- npm_hooks/postinstall.js | 31 +++++++++++++++++++++++++++++++ package.json | 3 +++ 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 npm_hooks/postinstall.js diff --git a/examples/migrations.js b/examples/migrations.js index 99f82a4..2393eb3 100644 --- a/examples/migrations.js +++ b/examples/migrations.js @@ -115,13 +115,6 @@ module.exports = function(grunt) { }); await migrations.migrate({ journal, logger }); - // Todo - { - // display changes success/failure and request user confirmation before completing - // move saving of content outside of the language loop - // only 1 confirmation per course rather than 1 per language - // output journal entries for revert - // } - // group all content items by path const outputFilePathItems = _.groupBy(content, '__path__'); // sort items inside each path @@ -136,7 +129,6 @@ module.exports = function(grunt) { const stripped = isSingleObject ? undressPathIndex(outputItems[0]) // config.json, course.json : outputItems.map(undressPathIndex); // contentObjects.json, articles.json, blocks.json, components.json - // console.log(journal.entries) fs.writeJSONSync(outputPath, stripped, { replacer: null, spaces: 2 }); }); } diff --git a/npm_hooks/postinstall.js b/npm_hooks/postinstall.js new file mode 100644 index 0000000..5f6532b --- /dev/null +++ b/npm_hooks/postinstall.js @@ -0,0 +1,31 @@ +import fs from 'fs/promises'; +import path from 'path'; + +const destinationPath = path.resolve('./../../grunt/tasks/migration.js'); +const sourcePath = path.resolve('./examples/migrations.js'); + +async function checkFileExists () { + try { + await fs.access(destinationPath); + return true; + } catch (error) { + return false; + } +} + +async function copyMigrationFile () { + try { + const destinationDir = path.dirname(destinationPath); + await fs.mkdir(destinationDir, { recursive: true }); + + // Copy the file + await fs.copyFile(sourcePath, destinationPath); + console.log(`File copied successfully to: ${destinationPath}`); + } catch (error) { + console.error('Error copying file:', error.message); + } +} + +if (!await checkFileExists()) { + await copyMigrationFile(); +}; diff --git a/package.json b/package.json index 0f502f6..8a191dd 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "type": "module", "main": "index.js", "version": "1.2.0", + "scripts": { + "postinstall": "node npm_hooks/postinstall.js" + }, "devDependencies": { "eslint": "^8.42.0", "eslint-config-standard": "^17.1.0",