Skip to content

Commit

Permalink
feat: added CLI command to migrate to using openedx scope (#2984)
Browse files Browse the repository at this point in the history
* feat: added migration command to new openedx-paragon npm package

* chore: add default path to the cli

---------

Co-authored-by: Viktor Rusakov <[email protected]>
  • Loading branch information
PKulkoRaccoonGang and viktorrusakov authored Dec 29, 2023
1 parent 51d864a commit 6927788
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
12 changes: 12 additions & 0 deletions bin/paragon-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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.',
Expand Down
62 changes: 62 additions & 0 deletions lib/migrate-to-openedx-scope.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 6927788

Please sign in to comment.