Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 56 additions & 25 deletions scripts/utils/bdlClassnameManager
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const colors = require('colors/safe');
const { promisify } = require('util');
const {promisify} = require('util');
const invert = require('lodash/invert');
const trimStart = require('lodash/trimStart');

Expand Down Expand Up @@ -99,7 +100,7 @@ async function handleWriteFile(fileContentsToParse, fileName, isVerbose, matches
}
}

async function processFileContents({ fileContentsToParse, fileName, isVerbose, sourceNameRegex }, handleMatch) {
async function processFileContents({fileContentsToParse, fileName, isVerbose, sourceNameRegex}, handleMatch) {
const matches = [...new Set(fileContentsToParse.match(sourceNameRegex))];

if (isVerbose) {
Expand All @@ -111,7 +112,7 @@ async function processFileContents({ fileContentsToParse, fileName, isVerbose, s
await handleWriteFile(fileContentsToParse, fileName, isVerbose, matches);
}

async function swap({ currentConversionMap, fileType, ...operationParams }) {
async function swap({currentConversionMap, fileType, ...operationParams}) {
processFileContents(operationParams, (fileContents, match) => {
if (fileType === 'scss') {
fileContents = findAndReplaceSCSS(
Expand All @@ -137,7 +138,7 @@ async function swap({ currentConversionMap, fileType, ...operationParams }) {
});
}

async function append({ currentConversionMap, fileType, ...operationParams }) {
async function append({currentConversionMap, fileType, ...operationParams}) {
processFileContents(operationParams, (fileContents, match) => {
if (fileType === 'scss') {
fileContents = findAndReplaceSCSS(
Expand All @@ -163,7 +164,7 @@ async function append({ currentConversionMap, fileType, ...operationParams }) {
});
}

async function extend({ currentConversionMap, fileType, ...operationParams }) {
async function extend({currentConversionMap, fileType, ...operationParams}) {
processFileContents(operationParams, (fileContents, match, index) => {
if (fileType === 'scss') {
const matchReplace = match.replace('.', '');
Expand All @@ -185,22 +186,7 @@ async function extend({ currentConversionMap, fileType, ...operationParams }) {
});
}

async function main() {
// argv 0 and 1 are the node instance and the script name respectively.
// argv 2 is the operation: check, swap, append, extend
const operation = process.argv[2];

// argv 3 is the file (and extension)
const fileName = process.argv[3];

if (!fileName) {
console.error(colors.red('Missing parameter:'), colors.white('fileName'));
process.exit(1);
}
if (!Object.keys(conversionMap).length) {
console.error(colors.red('Missing values in: '), colors.white('conversionMap'));
process.exit(1);
}
async function execute(fileName, operation) {
const fileType = fileName.split('.').pop();
const isVerbose = process.argv[4] === '--verbose';
const isExtend = operation === 'extend';
Expand Down Expand Up @@ -230,11 +216,11 @@ async function main() {
);
} else {
console.error(colors.yellow('Unrecognized file type for this tool. Skipping', fileName, '...'));
process.exit(0);
return;
}

try {
const fileContentsToParse = await readFile(fileName, { encoding: 'utf8' });
const fileContentsToParse = await readFile(fileName, {encoding: 'utf8'});
const operationParams = {
currentConversionMap,
fileContentsToParse,
Expand All @@ -253,7 +239,6 @@ async function main() {
if (isVerbose) {
console.error(colors.red('Bad BDL class name found in', fileName));
}
process.exit(1);
}

break;
Expand All @@ -277,16 +262,62 @@ async function main() {
'use "swap" to replace names, "append" to add new classes, "extend" to add @extend property, or "check" to verify if a file contains deprecated values',
),
);
process.exit(1);
break;
}
} catch (error) {
console.error(
colors.red('Cannot read file, because it does not exist or the wrong path is specified\n'),
error,
);
}
}

async function loopThroughDirectory(dirPath, operation) {
fs.readdir(dirPath, (err, files) => {
if (err) {
console.error(`Error reading directory: ${dirPath}`);
return;
}

files.forEach((file) => {
const filePath = path.join(dirPath, file);
// Check if it's a file or a directory
fs.stat(filePath, (err, stats) => {
if (err) {
console.error(`Error checking file stats: ${filePath}`);
process.exit(0);
}

if (stats.isDirectory()) {
const directoryPath = path.dirname(filePath);
if (!directoryPath.startsWith('__') && !directoryPath.endsWith('__')){
loopThroughDirectory(filePath,operation);
}
} else {
execute(filePath, operation)
}
});
});
});
}

async function main() {
// argv 0 and 1 are the node instance and the script name respectively.
// argv 2 is the operation: check, swap, append, extend
const operation = process.argv[2];

// argv 3 is the file (and extension)
const dirPath = process.argv[3];
if (!dirPath) {
console.error(colors.red('Missing parameter:'), colors.white('path'));
process.exit(1);
}
if (!Object.keys(conversionMap).length) {
console.error(colors.red('Missing values in: '), colors.white('conversionMap'));
process.exit(1);
}

loopThroughDirectory(dirPath, operation);
}

if (process.argv.length < 3) {
Expand Down