@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
52 lines • 2.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startCloudSqlProxyInBackground = exports.startCloudSqlProxyInCurrentShell = exports.ERROR_NOT_INSTALLED = void 0;
const child_process_promise_1 = require("child-process-promise");
const command_exists_promise_1 = __importDefault(require("command-exists-promise"));
exports.ERROR_NOT_INSTALLED = "cloud-sql-proxy not installed";
const getProxyCommandSpawnArgs = async ({ localPort, instanceName, }) => {
const commandString = (await (0, command_exists_promise_1.default)("cloud-sql-proxy"))
? `cloud-sql-proxy --port ${localPort} ${instanceName}`
: (await (0, command_exists_promise_1.default)("cloud_sql_proxy"))
? `cloud_sql_proxy -instances ${instanceName}=tcp:${localPort}`
: null;
if (!commandString) {
throw new Error(exports.ERROR_NOT_INSTALLED);
}
const [cmd, ...args] = commandString.split(" ");
return { cmd, args };
};
const startCloudSqlProxyInCurrentShell = async (opts) => {
const { cmd, args } = await getProxyCommandSpawnArgs(opts);
await (0, child_process_promise_1.spawn)(cmd, args, {
stdio: "inherit",
shell: true,
});
};
exports.startCloudSqlProxyInCurrentShell = startCloudSqlProxyInCurrentShell;
const startCloudSqlProxyInBackground = async (opts) => {
const { cmd, args } = await getProxyCommandSpawnArgs(opts);
const proxyPromise = (0, child_process_promise_1.spawn)(cmd, args, { shell: "bash" });
// wait until it starts
await (0, child_process_promise_1.spawn)(`echo -n "Waiting for proxy"
until echo > /dev/tcp/localhost/${opts.localPort}; do
sleep 0.2
echo -n "."
done 2>/dev/null`, [], { shell: "bash" });
const stop = () => {
proxyPromise.catch(() => {
// ignore
});
proxyPromise.childProcess.kill();
};
// stop if catladder i stopped
process.on("beforeExit", stop);
return {
stop,
};
};
exports.startCloudSqlProxyInBackground = startCloudSqlProxyInBackground;
//# sourceMappingURL=startProxy.js.map