@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
82 lines (77 loc) • 2.76 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var dialog_cli_util = require('./util.js');
var string = require('../../string.js');
var runtime = require('../../runtime.js');
var cli = require('../../cli.js');
var dialog_cli_question = require('./question.js');
var cli_common = require('../../cli/common.js');
function createAppleScript(message) {
return string.dedent `
tell application (path to frontmost application as text)
display dialog "${dialog_cli_util.escape(message)}" with title "Confirm"\
buttons {"Cancel", "OK"} default button "OK"
end
`;
}
function createPowerShellScript(message) {
return string.dedent `
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show("${dialog_cli_util.escape(message)}", "Confirm", "YesNo")
`;
}
async function confirm(message, options = {}) {
if ((options === null || options === void 0 ? void 0 : options.gui) && runtime.platform() === "darwin") {
const { code, stderr } = await cli.run("osascript", [
"-e",
createAppleScript(message)
]);
if (code) {
if (stderr.includes("User canceled")) {
return false;
}
else {
throw new Error(stderr);
}
}
else {
return true;
}
}
else if ((options === null || options === void 0 ? void 0 : options.gui) && (runtime.platform() === "windows" || cli_common.isWSL())) {
const { code, stdout, stderr } = await cli.powershell(createPowerShellScript(message));
if (code) {
throw new Error(stderr);
}
else {
return stdout.trim() === "Yes" ? true : false;
}
}
else if ((options === null || options === void 0 ? void 0 : options.gui) && (runtime.platform() === "linux" || await cli.which("zenity"))) {
const args = [
"--question",
"--title", "Confirm",
"--width", "365",
];
if (message) {
args.push("--text", message);
}
const { code, stderr } = await cli.run("zenity", args);
if (!code) {
return true;
}
else if (code === 1) {
return false;
}
else {
throw new Error(stderr);
}
}
else {
const answer = await cli_common.lockStdin(() => dialog_cli_question.default(message + " [Y/n] "));
const ok = answer === null || answer === void 0 ? void 0 : answer.toLowerCase().trim();
return ok === "" || ok === "y" || ok === "yes";
}
}
exports.default = confirm;
//# sourceMappingURL=confirm.js.map