Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxuanzhangsfdx committed Oct 2, 2024
1 parent c4d21fa commit cf11bf5
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 140 deletions.
135 changes: 135 additions & 0 deletions scripts/bundlingUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
const fs = require('fs');
const path = require('path');

// Function to update package.json
function updatePackageJson() {
const packagePath = './package.json';

fs.readFile(packagePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading package.json: ${err}`);
return;
}

try {
const packageJson = JSON.parse(data);

// Update package name if necessary
if (packageJson.name && packageJson.name === '@salesforce/core') {
packageJson.name = '@salesforce/core-bundle';
}

// Remove 'prepack' and 'prepare' scripts
if (packageJson.scripts) {
delete packageJson.scripts.prepack;
delete packageJson.scripts.prepare;
}

// Remove 'exports'
if (packageJson.exports) {
delete packageJson.exports;
}

fs.writeFile(packagePath, JSON.stringify(packageJson, null, 2), 'utf8', (writeErr) => {
if (writeErr) {
console.error(`Error writing to package.json: ${writeErr}`);
} else {
console.log('package.json updated successfully.');
}
});
} catch (parseErr) {
console.error(`Error parsing JSON in package.json: ${parseErr}`);
}
});
}

// Function to check the path to transformStream
function checkTransformStreamPath() {
const loggerPath = './src/logger/logger.ts';
const targetString = "target: path.join('..', '..', 'lib', 'logger', 'transformStream')";
const replacementString = "target: './transformStream'";

fs.readFile(loggerPath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading logger.ts: ${err}`);
return;
}

// Check if the target string exists in the file
if (!data.includes(targetString)) {
console.error(
`Error: The target string "${targetString}" was not found in logger.ts.\n Please make sure to bundle transformStream by referencing the new path or reach out to IDEx Foundations Team.`
);
return;
}
});
}

// Function to update logger.ts
function updateLoggerTs() {
const loggerPath = './src/logger/logger.ts';
const targetString = "target: path.join('..', '..', 'lib', 'logger', 'transformStream')";
const replacementString = "target: './transformStream'";

fs.readFile(loggerPath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading logger.ts: ${err}`);
return;
}
let updatedData = data.replace(targetString, replacementString);

fs.writeFile(loggerPath, updatedData, 'utf8', (writeErr) => {
if (writeErr) {
console.error(`Error writing to logger.ts: ${writeErr}`);
} else {
console.log('Logger.ts updated successfully.');
}
});
});
}

function updateLoadMessagesParam() {
const dirs = ['./src', './test'];
function replaceTextInFile(filePath) {
const data = fs.readFileSync(filePath, 'utf8');
const result = data.replace(
/Messages\.loadMessages\('@salesforce\/core'/g,
"Messages.loadMessages('@salesforce/core-bundle'"
);
fs.writeFileSync(filePath, result, 'utf8');
}
function traverseDirectory(directory) {
fs.readdirSync(directory).forEach((file) => {
const fullPath = path.join(directory, file);
if (fs.lstatSync(fullPath).isDirectory()) {
traverseDirectory(fullPath);
} else if (path.extname(fullPath) === '.ts') {
replaceTextInFile(fullPath);
}
});
}
dirs.forEach((dir) => {
traverseDirectory(dir);
});
}

function addTestSetupToIndex() {
const indexPath = './src/index.ts';
const testSetupExport = "export * from './testSetup';\n";
fs.readFile(indexPath, 'utf8', (err, data) => {
fs.appendFile(indexPath, testSetupExport, 'utf8', (err) => {
if (err) {
console.error(`Error appending to file: ${err}`);
} else {
console.log('Content successfully added to the file.');
}
});
});
}

// Run the update functions
exports.updatePackageJson = updatePackageJson;
exports.checkTransformStreamPath = checkTransformStreamPath;
exports.updateLoggerTs = updateLoggerTs;
exports.updateLoadMessagesParam = updateLoadMessagesParam;
exports.addTestSetupToIndex = addTestSetupToIndex;
2 changes: 1 addition & 1 deletion scripts/scanTs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ if (detected) {
console.log('Consider using import instead or reach out to IDEx Foundations team');
}

const { checkTransformStreamPath } = require('./updateForBundling');
const { checkTransformStreamPath } = require('./bundlingUtils');
checkTransformStreamPath();

console.log('Scan complete');
137 changes: 7 additions & 130 deletions scripts/updateForBundling.js
Original file line number Diff line number Diff line change
@@ -1,136 +1,13 @@
const fs = require('fs');
const path = require('path');
const {
updatePackageJson,
checkTransformStreamPath,
updateLoggerTs,
updateLoadMessagesParam,
addTestSetupToIndex,
} = require('./bundlingUtils');

// Function to update package.json
function updatePackageJson() {
const packagePath = './package.json';

fs.readFile(packagePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading package.json: ${err}`);
return;
}

try {
const packageJson = JSON.parse(data);

// Update package name if necessary
if (packageJson.name && packageJson.name === '@salesforce/core') {
packageJson.name = '@salesforce/core-bundle';
}

// Remove 'prepack' and 'prepare' scripts
if (packageJson.scripts) {
delete packageJson.scripts.prepack;
delete packageJson.scripts.prepare;
}

// Remove 'exports'
if (packageJson.exports) {
delete packageJson.exports;
}

fs.writeFile(packagePath, JSON.stringify(packageJson, null, 2), 'utf8', (writeErr) => {
if (writeErr) {
console.error(`Error writing to package.json: ${writeErr}`);
} else {
console.log('package.json updated successfully.');
}
});
} catch (parseErr) {
console.error(`Error parsing JSON in package.json: ${parseErr}`);
}
});
}

// Function to check the path to transformStream
function checkTransformStreamPath() {
const loggerPath = './src/logger/logger.ts';
const targetString = "target: path.join('..', '..', 'lib', 'logger', 'transformStream')";
const replacementString = "target: './transformStream'";

fs.readFile(loggerPath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading logger.ts: ${err}`);
return;
}

// Check if the target string exists in the file
if (!data.includes(targetString)) {
console.error(
`Error: The target string "${targetString}" was not found in logger.ts.\n Please make sure to bundle transformStream by referencing the new path or reach out to IDEx Foundations Team.`
);
return;
}
});
}

// Function to update logger.ts
function updateLoggerTs() {
const loggerPath = './src/logger/logger.ts';
const targetString = "target: path.join('..', '..', 'lib', 'logger', 'transformStream')";
const replacementString = "target: './transformStream'";

fs.readFile(loggerPath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading logger.ts: ${err}`);
return;
}
let updatedData = data.replace(targetString, replacementString);

fs.writeFile(loggerPath, updatedData, 'utf8', (writeErr) => {
if (writeErr) {
console.error(`Error writing to logger.ts: ${writeErr}`);
} else {
console.log('Logger.ts updated successfully.');
}
});
});
}

function updateLoadMessagesParam() {
const dirs = ['./src', './test'];
function replaceTextInFile(filePath) {
const data = fs.readFileSync(filePath, 'utf8');
const result = data.replace(
/Messages\.loadMessages\('@salesforce\/core'/g,
"Messages.loadMessages('@salesforce/core-bundle'"
);
fs.writeFileSync(filePath, result, 'utf8');
}
function traverseDirectory(directory) {
fs.readdirSync(directory).forEach((file) => {
const fullPath = path.join(directory, file);
if (fs.lstatSync(fullPath).isDirectory()) {
traverseDirectory(fullPath);
} else if (path.extname(fullPath) === '.ts') {
replaceTextInFile(fullPath);
}
});
}
dirs.forEach((dir) => {
traverseDirectory(dir);
});
}

function addTestSetupToIndex() {
const indexPath = './src/index.ts';
const testSetupExport = "export * from './testSetup';\n";
fs.readFile(indexPath, 'utf8', (err, data) => {
fs.appendFile(indexPath, testSetupExport, 'utf8', (err) => {
if (err) {
console.error(`Error appending to file: ${err}`);
} else {
console.log('Content successfully added to the file.');
}
});
});
}

// Run the update functions
updatePackageJson();
checkTransformStreamPath();
updateLoggerTs();
updateLoadMessagesParam();
addTestSetupToIndex();
exports.checkTransformStreamPath = checkTransformStreamPath;
15 changes: 6 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3892,15 +3892,7 @@ merge@^2.1.1:
resolved "https://registry.yarnpkg.com/merge/-/merge-2.1.1.tgz#59ef4bf7e0b3e879186436e8481c06a6c162ca98"
integrity sha512-jz+Cfrg9GWOZbQAnDQ4hlVnQky+341Yk5ru8bZSe6sIDTCIg8n9i/u7hSQGSVOF3C7lH6mGtqjkiT9G4wFLL0w==

micromatch@^4.0.2:
version "4.0.8"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.8.tgz#d66fa18f3a47076789320b9b1af32bd86d9fa202"
integrity sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==
dependencies:
braces "^3.0.3"
picomatch "^2.3.1"

micromatch@^4.0.4:
micromatch@^4.0.2, micromatch@^4.0.4:
version "4.0.8"
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-4.0.8.tgz#d66fa18f3a47076789320b9b1af32bd86d9fa202"
integrity sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==
Expand Down Expand Up @@ -5168,6 +5160,11 @@ srcset@^5.0.0:
resolved "https://registry.yarnpkg.com/srcset/-/srcset-5.0.1.tgz#e660a728f195419e4afa95121099bc9efb7a1e36"
integrity sha512-/P1UYbGfJVlxZag7aABNRrulEXAwCSDo7fklafOQrantuPTDmYgijJMks2zusPCVzgW9+4P69mq7w6pYuZpgxw==

[email protected]:
version "0.0.10"
resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.10.tgz#547c70b347e8d32b4e108ea1a2a159e5fdde19c0"
integrity sha512-KGzahc7puUKkzyMt+IqAep+TVNbKP+k2Lmwhub39m1AsTSkaDutx56aDCo+HLDzf/D26BIHTJWNiTG1KAJiQCg==

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
Expand Down

0 comments on commit cf11bf5

Please sign in to comment.