From 2ddc6a73fc3854617cc188a10ac4a9fe4b5d03fa Mon Sep 17 00:00:00 2001 From: PKulkoRaccoonGang Date: Fri, 29 Dec 2023 15:08:20 +0200 Subject: [PATCH] feat: added migration command to new openedx-paragon npm package --- bin/paragon-scripts.js | 12 +++++ lib/migrate-to-openedx-scope.js | 89 +++++++++++++++++++++++++++++++++ package-lock.json | 23 +++++++++ package.json | 1 + 4 files changed, 125 insertions(+) create mode 100644 lib/migrate-to-openedx-scope.js diff --git a/bin/paragon-scripts.js b/bin/paragon-scripts.js index f192f56d9d..8291981aae 100755 --- a/bin/paragon-scripts.js +++ b/bin/paragon-scripts.js @@ -3,6 +3,7 @@ const chalk = require('chalk'); const themeCommand = require('../lib/install-theme'); const helpCommand = require('../lib/help'); const versionCommand = require('../lib/version'); +const migrateToOpenEdxScopeCommand = require('../lib/migrate-to-openedx-scope'); const HELP_COMMAND = 'help'; const commandAliases = { @@ -48,6 +49,17 @@ const COMMANDS = { }, ], }, + 'migrate-to-openedx-scope': { + executor: migrateToOpenEdxScopeCommand, + description: 'CLI for migrate from "@edx/paragon" to "@openedx/paragon".', + parameters: [ + { + name: 'path', + description: 'Path to the directory where to replace Paragon package name.', + required: true, + }, + ], + }, help: { executor: helpCommand, description: 'Displays help for available commands.', diff --git a/lib/migrate-to-openedx-scope.js b/lib/migrate-to-openedx-scope.js new file mode 100644 index 0000000000..e83d12c6df --- /dev/null +++ b/lib/migrate-to-openedx-scope.js @@ -0,0 +1,89 @@ +const fs = require('fs'); +const path = require('path'); +const chalk = require('chalk'); +const sizeOf = require('image-size'); + +/** + * Checks if a given file is an image by analyzing its dimensions. + * + * @param {string} fileName - The name or path of the file to check. + * @returns {boolean} - Returns `true` if the file is determined to be an image, and `false` otherwise. + * @throws {Error} - Throws an error if there is an issue determining the image dimensions. + * + * @example + * const result = isImage('example.jpg'); + * console.log(result); // true or false + */ +function isImage(fileName) { + try { + const imagePath = path.join(__dirname, fileName); + const dimensions = sizeOf(imagePath); + + return dimensions.width > 0 && dimensions.height > 0; + } catch (error) { + return false; + } +} + +/** + * Processes the content of a file by replacing occurrences of '@edx/paragon' with '@openedx/paragon'. + * + * @param {string} filePath - The path to the file to process. + */ +function processFileContent(filePath) { + const fileName = path.basename(filePath); + const isInvalidFile = fileName === 'package-lock.json' || fileName === 'package.json' || isImage(fileName); + + if (isInvalidFile) { + return; + } + + const fileContent = fs.readFileSync(filePath, 'utf-8'); + const updatedContent = fileContent.replace(/@edx\/paragon/g, '@openedx/paragon'); + + if (fileContent !== updatedContent) { + fs.writeFileSync(filePath, updatedContent, 'utf-8'); + console.log(`Updated file: ${filePath}`); // eslint-disable-line no-console + } +} + +/** + * Performs a migration from "@edx/paragon" to "@openedx/paragon" NPM package name. + */ +function migrateToOpenEdxScopeCommand() { + const projectPath = process.argv[3]; + const stack = [projectPath]; + + if (!projectPath) { + console.error(`${chalk.red.bold('Error: Specify the path to the project.')}`); // eslint-disable-line no-console + process.exit(1); + } + + while (stack.length > 0) { + const currentDir = stack.pop(); + const files = fs.readdirSync(currentDir); + + files.forEach(file => { + const filePath = path.join(currentDir, file); + const fileStats = fs.statSync(filePath); + + if (fileStats.isDirectory()) { + if (file === 'node_modules') { + return; + } + + if (file.startsWith('.') && file !== '.' && file !== '..') { + return; + } + + stack.push(filePath); + } else { + processFileContent(filePath); + } + }); + } + + console.error(`${chalk.green.bold('Paragon migration to Openedx scope completed successfully.')}`); // eslint-disable-line no-console +} + +module.exports = migrateToOpenEdxScopeCommand; diff --git a/package-lock.json b/package-lock.json index 70c9eebd6b..e11c785f62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "file-selector": "^0.6.0", "font-awesome": "^4.7.0", "glob": "^8.0.3", + "image-size": "^1.1.0", "inquirer": "^8.2.5", "lodash.uniqby": "^4.7.0", "mailto-link": "^2.0.0", @@ -21196,6 +21197,20 @@ "url": "https://opencollective.com/webpack" } }, + "node_modules/image-size": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/image-size/-/image-size-1.1.0.tgz", + "integrity": "sha512-asnTHw2K8OlqT5kVnQwX+AGKQqpvLo95LbNzQ/C0ln3yzentZmAdd0ygoD004VC4Kkd4PV7J2iaPQkqwp9yuTw==", + "dependencies": { + "queue": "6.0.2" + }, + "bin": { + "image-size": "bin/image-size.js" + }, + "engines": { + "node": ">=18.0.0" + } + }, "node_modules/imask": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/imask/-/imask-7.1.3.tgz", @@ -31434,6 +31449,14 @@ "dev": true, "license": "MIT" }, + "node_modules/queue": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/queue/-/queue-6.0.2.tgz", + "integrity": "sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==", + "dependencies": { + "inherits": "~2.0.3" + } + }, "node_modules/queue-microtask": { "version": "1.2.3", "funding": [ diff --git a/package.json b/package.json index 74ff0d7cfd..ddac7f57c0 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "file-selector": "^0.6.0", "font-awesome": "^4.7.0", "glob": "^8.0.3", + "image-size": "^1.1.0", "inquirer": "^8.2.5", "lodash.uniqby": "^4.7.0", "mailto-link": "^2.0.0",