Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use spawn instead of exec in build #1057

Merged
merged 2 commits into from
Jan 9, 2025
Merged
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
53 changes: 43 additions & 10 deletions project/gulpfile.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawn } from "node:child_process";
import crypto from "node:crypto";
import path from "node:path";
import pkg from "@yao-pkg/pkg";
Expand Down Expand Up @@ -78,6 +79,35 @@ const modulesToTranspile = [
"when-exit/dist/node/signals.js",
];

/**
* Runs a shell command with spawn, wrapping it in a Promise for async/await usage.
*/
const runCommand = (command, args, options = {}) => {
return new Promise((resolve, reject) => {
const process = spawn(command, args, { shell: true, ...options });

process.stdout.on("data", (data) => {
const message = data.toString().trim();
if (message) console.log(message);
});

process.stderr.on("data", (data) => {
const message = data.toString().trim();
if (message) console.error(message);
});

process.on("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`${command} exited with code ${code}`));
}
});

process.on("error", reject);
});
};

const transpileModules = async () => {
await fs.ensureDir(backupDir);
await fs.ensureDir(transpiledDir);
Expand All @@ -92,25 +122,23 @@ const transpileModules = async () => {
// Backup the original file
await fs.ensureDir(path.dirname(backupPath));
await fs.copy(resolvedPath, backupPath, { overwrite: true });
console.log(`Backed up: ${resolvedPath}`);
console.log(`Backed up: ${path.basename(resolvedPath)}`);

// Ensure the output directory exists
await fs.ensureDir(path.dirname(outputPath));

// Build the SWC command
const swcCommand = `npx swc ${resolvedPath} -o ${outputPath} --config-file .swcrc`;

// Execute the command
// Transpile the module
try {
await exec(swcCommand, { stdio: "inherit" });
await runCommand("npx", ["swc", resolvedPath, "-o", outputPath, "--config-file", ".swcrc"]);
console.log(`Successfully transpiled: ${resolvedPath}`);
} catch (error) {
console.error(`Error transpiling module: ${modulePath}`);
console.error(`Error transpiling module: ${resolvedPath}`);
throw error;
}

// Replace the original file with the transpiled version
await fs.copy(outputPath, resolvedPath, { overwrite: true });
console.log(`Replaced original module: ${resolvedPath} with transpiled version.`);
console.log(`Replaced original module: ${path.basename(resolvedPath)} with transpiled version.\n`);
}
};

Expand All @@ -124,7 +152,7 @@ const restoreModules = async () => {
// Restore the original file
if (await fs.pathExists(backupPath)) {
await fs.copy(backupPath, resolvedPath, { overwrite: true });
console.log(`Restored original module: ${resolvedPath}`);
console.log(`Restored original module: ${path.basename(resolvedPath)}`);
}
}

Expand All @@ -139,7 +167,12 @@ const restoreModules = async () => {
*/
const compile = async () => {
// Compile TypeScript files using SWC
await exec("npx swc src -d obj", { stdio: "inherit" });
try {
await runCommand("npx", ["swc", "src", "-d", "obj", "--config-file", ".swcrc"]);
} catch (error) {
console.error("Error transpiling source:");
throw error;
}

// Merge the contents from the /obj/src directory into /obj
const srcDir = path.join("obj", "src");
Expand Down
Loading