Skip to content

Commit

Permalink
ci: post-bundling scripts
Browse files Browse the repository at this point in the history
* chore: update workflow to bundle from lib

* fix: update postbundling name

* fix: delete yarn install

* chore: delete exports for bundling workflow
  • Loading branch information
mingxuanzhangsfdx committed Apr 17, 2024
1 parent f9b390f commit bf118f6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/esbuild-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
node-version: ${{ inputs.nodeVersion }}
registry-url: 'https://registry.npmjs.org'
cache: yarn
- uses: salesforcecli/github-workflows/.github/actions/yarnInstallWithRetries@main
- name: Install esbuild Dependencies
run: |
yarn add -D esbuild@^0.19.5 esbuild-plugin-pino@^2.1.0 npm-dts@^1.3.12 esbuild-plugin-tsc@^0.4.0
Expand All @@ -35,7 +36,11 @@ jobs:
node scripts/updateForBundling.js
- name: Generate Bundle
run: |
yarn build
node scripts/build.js
- name: Post Bundling Update
run: |
node scripts/postBundlingUpdate.js
- name: Publish a Package
run: |
npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
Expand Down
17 changes: 10 additions & 7 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ const esbuildPluginPino = require('esbuild-plugin-pino');
const esbuildPluginTsc = require('esbuild-plugin-tsc');
const { Generator } = require('npm-dts');
const fs = require('fs');
const CONSTANTS = require('./constants.json');
const outputFolder = CONSTANTS.outputFilesFolder;
const tmpOutputFolder = CONSTANTS.outputFilesTmpFolder;

new Generator({
output: 'lib/index.d.ts',
output: `${tmpOutputFolder}/index.d.ts`,
}).generate();

const sharedConfig = {
entryPoints: ['src/index.ts'],
entryPoints: [`${outputFolder}/index.js`],
bundle: true,
// minify: true,
plugins: [
Expand All @@ -31,22 +34,22 @@ const sharedConfig = {
...sharedConfig,
// external: ['src/logger/transformStream.ts'],
platform: 'node', // for CJS
outdir: 'lib',
outdir: tmpOutputFolder,
});
const filePath = 'lib/index.js';
const filePath = `${tmpOutputFolder}/index.js`;
let bundledEntryPoint = fs.readFileSync(filePath, 'utf8');

const searchString = /\$\{process\.cwd\(\)\}\$\{require\("path"\)\.sep\}lib/g;
const searchString = /\$\{process\.cwd\(\)\}\$\{require\("path"\)\.sep\}tmp-lib/g;
const replacementString = `\${__dirname}\${require("path").sep}`;

bundledEntryPoint = bundledEntryPoint.replace(searchString, replacementString);
fs.writeFileSync(filePath, bundledEntryPoint, 'utf8');

await build({
entryPoints: ['src/logger/transformStream.ts'],
entryPoints: [`${outputFolder}/logger/transformStream.js`],
bundle: true,
minify: true,
outdir: 'lib',
outdir: tmpOutputFolder,
platform: 'node', // for CJS
plugins: [
// esbuildPluginPino({ transports: ['pino-pretty'] }),
Expand Down
4 changes: 4 additions & 0 deletions scripts/constants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"outputFilesTmpFolder": "tmp-lib",
"outputFilesFolder": "lib"
}
37 changes: 37 additions & 0 deletions scripts/postBundlingUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs');
const path = require('path');

// Path to the directories
const CONSTANTS = require('./constants.json');
const outputDir = `./${CONSTANTS.outputFilesFolder}`;
const tmpOutputDir = `./${CONSTANTS.outputFilesTmpFolder}`;

// Function to rename a directory
function renameBundledFolder() {
if (fs.existsSync(tmpOutputDir)) {
fs.rename(tmpOutputDir, outputDir, (err) => {
if (err) {
return console.error(`Error renaming folder: ${err}`);
}
console.log(`Folder renamed from ${tmpOutputDir} to ${outputDir}`);
});
} else {
console.error(`${tmpOutputDir} does not exist, cannot rename.`);
}
}

// delete the original compiled files
function deleteOriginalLib() {
if (fs.existsSync(outputDir)) {
fs.rm(outputDir, { recursive: true, force: true }, (err) => {
if (err) {
return console.error(`Error deleting folder: ${err}`);
}
console.log(`${outputDir} is deleted.`);
renameBundledFolder();
});
}
}

// Start the process
deleteOriginalLib();
5 changes: 5 additions & 0 deletions scripts/updateForBundling.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ function updatePackageJson() {
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}`);
Expand Down

0 comments on commit bf118f6

Please sign in to comment.