How to export a deployment function for an upgradeable contract for use in other projects without running hardhat?

I'm using the upgradeable contracts in OpenZeppelin with the hardhat-ethers plugin. I'm trying to create a contract deployment function that I can export it so that another project could import and call the function to deploy their own contracts.

My function looks something like in deployment.ts:

import { ethers, upgrades } from "hardhat";

export const deployContract = async (signer: Signer) => {
   const factory = await ethers.getContractFactory('MyContract', signer)
   return upgrades.deployProxy(factory);

This would mean that when running this function, it wouldn't be run from hardhat as a task like with npx hardhat run --network localhost deployment.ts. This function will be run as a regular nodejs function. However, it doesn't seem to work when I don't run it with hardhat.

I have also tried to test the function with this:

import { ethers } from "hardhat";

import { deployContract } from "./deployment";

const provider = new ethers.providers.JsonRpcProvider();
const signer = provider.getSigner();

await deployContract(signer);

But this ends with an error complaining Expected private key to be an Uint8Array.

Strangely, when I run an almost similar code in a test file with npx hardhat test, it works without issues. I'm not sure if OpenZeppelin hardhat plugin did some magic behind the scene though.

Since this is an upgradeable contract and I very much depend on OpenZeppelin's plugin upgrades.deployProxy function, I couldn't directly deploy the the contract using ethers' deploy function directly.

How can I export the deployment function so that it's reusable directly in another project such that the other project importing my deployment function can deploy directly and doesn't need to be run through the commands npx hardhat run or npx hardhat test?

:computer: Environment

OpenZeppelin upgradeable contracts, Hardhat, ethers v5

Can you show the full error including stack trace?