karabiner-human-config
Version:
The easiest way to write Karabiner-Elements configuration files, ever!
24 lines (20 loc) • 7.3 kB
JavaScript
var Z=Object.defineProperty,q=Object.defineProperties;var G=Object.getOwnPropertyDescriptors;var j=Object.getOwnPropertySymbols;var S=Object.prototype.hasOwnProperty,I=Object.prototype.propertyIsEnumerable;var E=(t,o,e)=>o in t?Z(t,o,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[o]=e,n=(t,o)=>{for(var e in o||(o={}))S.call(o,e)&&E(t,e,o[e]);if(j)for(var e of j(o))I.call(o,e)&&E(t,e,o[e]);return t},O=(t,o)=>q(t,G(o));var C=(t,o)=>{var e={};for(var r in t)S.call(t,r)&&o.indexOf(r)<0&&(e[r]=t[r]);if(t!=null&&j)for(var r of j(t))o.indexOf(r)<0&&I.call(t,r)&&(e[r]=t[r]);return e};import x from"node:fs";import X from"node:os";import g from"node:path";import{parseArgs as Y}from"node:util";var f=t=>{let o=["lazy","repeat","halt"];t=t.replace(/\s+/g," ");let e=t.split(" "),r=e.reduce((i,c)=>(o.includes(c)&&(i[c]=!0),i),{});return[e.filter(i=>!o.includes(i)),r]},d=t=>{t=t.replace(/\s+/g," ");let o=t.split(" "),e=o.filter(i=>parseInt(i)>9),r=e.length===1?{hold_down_milliseconds:parseInt(e[0])}:{};return[o.filter(i=>isNaN(parseInt(i))||parseInt(i)<10),r]},N=t=>`^${t.replace(/\./g,"\\.")}$`,K=(t,o)=>{for(let[e,r]of Object.entries(o))t[e]&&(t[r]=t[e],delete t[e]);return t},p=(t,o)=>{let e=t.split(o);return e.length?[e[0].trim(),t.slice(e[0].length+o.length).trim()]:[t.trim()]},M=t=>/^[A-Z]/.test(t),_=(t,o,e)=>{let r=t[o];r!==void 0&&(t[o]=e(r))};var m={caps:"caps_lock",capslock:"caps_lock",cmd:"left_command",command:"left_command",l_cmd:"left_command",l_command:"left_command",lcmd:"left_command",lcommand:"left_command",left_cmd:"left_command",r_cmd:"right_command",r_command:"right_command",rcmd:"right_command",rcommand:"right_command",right_cmd:"right_command",ctrl:"left_control",control:"left_control",l_control:"left_control",l_ctrl:"left_control",lcontrol:"left_control",lctrl:"left_control",left_ctrl:"left_control",r_control:"right_control",r_ctrl:"right_control",rcontrol:"right_control",rctrl:"right_control",right_ctrl:"right_control",enter:"return_or_enter",esc:"escape",grave:"grave_accent_and_tilde",accent:"grave_accent_and_tilde",tilde:"grave_accent_and_tilde",hyper:"left_command left_control left_option left_shift",opt:"left_option",option:"left_option",l_opt:"left_option",l_option:"left_option",left_opt:"left_option",lopt:"left_option",loption:"left_option",r_opt:"right_option",r_option:"right_option",right_opt:"right_option",ropt:"right_option",roption:"right_option",shift:"left_shift",l_shift:"left_shift",lshift:"left_shift",r_shift:"right_shift",rshift:"right_shift"};var T={t:"to",a:"to_if_alone",alone:"to_if_alone",h:"to_if_held_down",held:"to_if_held_down",u:"to_after_key_up",up:"to_after_key_up",d:"to_delayed_action",delayed:"to_delayed_action",c:"conditions"};import{bundleId as Q}from"bundle-id";var D=t=>{let[o,e]=p(t,"|");for(let[a,$]of Object.entries(m))o=o.replace(new RegExp(`\\b${a}\\b`,"g"),$),e=e==null?void 0:e.replace(new RegExp(`\\b${a}\\b`,"g"),$);let r=o.split(" "),l=r.pop(),i=r,c=e?e.split(" "):[],s=n(n({},i.length&&{mandatory:i}),c.length&&{optional:c});return n({key_code:l},Object.keys(s).length&&{modifiers:s})};var P=t=>{if(typeof t=="object")return t;let[o,e]=p(t,"$");for(let[s,a]of Object.entries(m))o=o.replace(new RegExp(`\\b${s}\\b`,"g"),a);let[r,l]=f(o),[i,c]=d(r.join(" "));return e?[n(n({shell_command:e},l),c)]:[n(n(n({key_code:i.pop()},i.length&&{modifiers:i}),l),c)]};var R=t=>{if(typeof t=="object")return t;let[o,e]=p(t,"$");for(let[s,a]of Object.entries(m))o=o.replace(new RegExp(`\\b${s}\\b`,"g"),a);let[r,l]=f(o),[i,c]=d(r.join(" "));return e?[n(n({shell_command:e},l),c)]:[n(n(n({key_code:i.pop()},i.length&&{modifiers:i}),l),c)]};var J=t=>{if(typeof t=="object")return t;let[o,e]=p(t,"$");for(let[s,a]of Object.entries(m))o=o.replace(new RegExp(`\\b${s}\\b`,"g"),a);let[r,l]=f(o),[i,c]=d(r.join(" "));return e?[n(n({shell_command:e},l),c)]:[n(n(n({key_code:i.pop()},i.length&&{modifiers:i}),l),c)]};var U=t=>{if(typeof t=="object")return t;let[o,e]=p(t,"$");for(let[s,a]of Object.entries(m))o=o.replace(new RegExp(`\\b${s}\\b`,"g"),a);let[r,l]=f(o),[i,c]=d(r.join(" "));return e?[n(n({shell_command:e},l),c)]:[n(n(n({key_code:i.pop()},i.length&&{modifiers:i}),l),c)]};var b=(t,o)=>{if(!o)return t;let e=Array.isArray(o)?o:[o];return[...t||[],...e]},k=async({app:t,bundleId:o,conditions:e,config:r})=>{let l=[];for(let[c,s]of Object.entries(r)){if(typeof s=="string"&&(s={to:s}),M(c)){let h=s.hasOwnProperty("_conditions")?b(e,s._conditions):e;delete s._conditions;let y=await k({app:c.toLowerCase(),bundleId:N(await Q(c)),conditions:h,config:s});l.push(...y);continue}if(s.hasOwnProperty("_self")){let h={[c]:s._self};delete s._self;let y=s.hasOwnProperty("_conditions")?b(e,s._conditions):e;delete s._conditions;let z=Object.fromEntries(Object.entries(s).map(([H,L])=>[`${c} ${H}`,L])),B=await k({app:t,bundleId:o,conditions:y,config:n(n({},h),z)});l.push(...B);continue}if(s.hasOwnProperty("_conditions")){let h=b(e,s._conditions);delete s._conditions;let y=await k({app:t,bundleId:o,conditions:h,config:s});l.push(...y);continue}s=K(s,T);let a=b(b(e,o&&{type:"frontmost_application_if",bundle_identifiers:[o]}),s.conditions),i=s,{conditions:$}=i,W=C(i,["conditions"]),u=n(n({type:"basic",description:o?`${t} ${c}`:c,from:c},W),a&&{conditions:a});_(u,"from",D),_(u,"to_after_key_up",R),_(u,"to_if_alone",J),_(u,"to_if_held_down",U),_(u,"to",P),l.push(u)}return l};var w="karabiner-human-config",F="0.15.0";var v={name:"Neeraj Dalal",email:"admin@nrjdalal.com",url:"https://nrjdalal.com"};import tt from"strip-json-comments";var V=`Version:
${w}@${F}
Usage:
$ ${w} [options]
Options:
-i, --input Input file path (default: konfig.json)
-o, --output Output file path (default: karabiner.json)
-v, --version Display version number
-h, --help Display help message
Author:
${v.name} <${v.email}> (${v.url})`,ot=t=>{try{return Y(t)}catch(o){throw new Error(`Error parsing arguments: ${o.message}`)}},et=async()=>{var t;try{let{values:o}=ot({options:{input:{type:"string",short:"i",default:"auto"},output:{type:"string",short:"o",default:"auto"},help:{type:"boolean",short:"h"},version:{type:"boolean",short:"v"},logfile:{type:"boolean",short:"l"}}});o.version&&(console.log(`${w}@${F}`),process.exit(0)),o.help&&(console.log(V),process.exit(0));let e={input:o.input==="auto"?g.resolve(process.cwd()+"/konfig.json"):g.resolve(o.input),output:o.output==="auto"?g.resolve(process.cwd()+"/karabiner.json")?g.resolve(process.cwd()+"/karabiner.json"):g.resolve(X.homedir()+"/.config/karabiner/karabiner.json"):g.resolve(o.output)};x.existsSync(e.input)||(console.log(V),console.error(`\x1B[31m
Input file doesn't exist: ${e.input}
\x1B[0m`),process.exit(1));let r={};try{r=JSON.parse(x.readFileSync(e.output,"utf-8"))}catch(c){console.log(`\x1B[33m
Output file doesn't exist: ${e.output}
\x1B[0m`)}let l=JSON.parse(tt(x.readFileSync(e.input,"utf-8"))),i=O(n({},r),{profiles:[O(n({},(t=r.profiles)==null?void 0:t[0]),{complex_modifications:{rules:(await k({config:l})).map(c=>({manipulators:[c]}))}})]});o.logfile&&(x.writeFileSync(g.resolve(process.cwd()+"/.logfile"),JSON.stringify(i,null,2)+`
`),process.exit(0)),x.writeFileSync(g.resolve(e.output),JSON.stringify(i,null,2)+`
`),process.exit(0)}catch(o){console.error(`
${o.message}
`),process.exit(1)}};et();