Skip to content

Commit

Permalink
feat: added migration command to new openedx-paragon npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Dec 29, 2023
1 parent 4dcf111 commit 2ddc6a7
Show file tree
Hide file tree
Showing 4 changed files with 125 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.',
required: true,
},
],
},
help: {
executor: helpCommand,
description: 'Displays help for available commands.',
Expand Down
89 changes: 89 additions & 0 deletions lib/migrate-to-openedx-scope.js
Original file line number Diff line number Diff line change
@@ -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;
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 2ddc6a7

Please sign in to comment.