diff --git a/bin/paragon-scripts.js b/bin/paragon-scripts.js index f192f56d9d..212dc0bb2e 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, default to root of the repository', + required: false, + }, + ], + }, 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..64ed76111f --- /dev/null +++ b/lib/migrate-to-openedx-scope.js @@ -0,0 +1,62 @@ +const fs = require('fs'); +const path = require('path'); +const chalk = require('chalk'); + +const isValidFileExtension = (filename) => /(.jsx|.js|.tsx|.ts|.md|.rst|.scss)$/.test(filename.toLowerCase()); + +/** + * 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); + + if (!isValidFileExtension(fileName)) { + 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] || path.resolve(__dirname, '../../../../'); + const stack = [projectPath]; + + 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;