midi-shell-commands 1.0.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/release.yml +54 -0
- package/.github/workflows/verify-pr.yml +37 -0
- package/.nvmrc +1 -0
- package/.releaserc.json +20 -0
- package/README.md +20 -2
- package/coverage/base.css +224 -0
- package/coverage/block-navigation.js +87 -0
- package/coverage/clover.xml +343 -0
- package/coverage/coverage-final.json +16 -0
- package/coverage/favicon.png +0 -0
- package/coverage/index.html +131 -0
- package/coverage/midi-shell-commands/index.html +116 -0
- package/coverage/midi-shell-commands/lib/check-inputs.js.html +112 -0
- package/coverage/midi-shell-commands/lib/clean-up-inputs.js.html +112 -0
- package/coverage/midi-shell-commands/lib/clean-up-watcher.js.html +115 -0
- package/coverage/midi-shell-commands/lib/escape-path.js.html +106 -0
- package/coverage/midi-shell-commands/lib/index.html +311 -0
- package/coverage/midi-shell-commands/lib/initialize-midi.js.html +112 -0
- package/coverage/midi-shell-commands/lib/initialize-scripts-directory.js.html +196 -0
- package/coverage/midi-shell-commands/lib/invoke-scripts.js.html +139 -0
- package/coverage/midi-shell-commands/lib/listen-to-input.js.html +118 -0
- package/coverage/midi-shell-commands/lib/log.js.html +94 -0
- package/coverage/midi-shell-commands/lib/map-message-to-file-names.js.html +106 -0
- package/coverage/midi-shell-commands/lib/post-install.js.html +490 -0
- package/coverage/midi-shell-commands/lib/refresh-scripts.js.html +124 -0
- package/coverage/midi-shell-commands/lib/report-errors.js.html +100 -0
- package/coverage/midi-shell-commands/lib/state.js.html +106 -0
- package/coverage/midi-shell-commands/midi-shell-commands.js.html +106 -0
- package/coverage/prettify.css +1 -0
- package/coverage/prettify.js +2 -0
- package/coverage/sort-arrow-sprite.png +0 -0
- package/coverage/sorter.js +210 -0
- package/eslint.config.js +12 -0
- package/lib/check-inputs.js +9 -0
- package/lib/clean-up-inputs.js +9 -0
- package/lib/clean-up-watcher.js +10 -0
- package/lib/escape-path.js +7 -0
- package/lib/initialize-midi.js +9 -0
- package/lib/initialize-scripts-directory.js +37 -0
- package/lib/invoke-scripts.js +18 -0
- package/lib/listen-to-input.js +11 -0
- package/lib/log.js +3 -0
- package/lib/map-message-to-file-names.js +7 -0
- package/lib/post-install.js +135 -0
- package/lib/refresh-scripts.js +13 -0
- package/lib/report-errors.js +5 -0
- package/lib/state.js +7 -0
- package/midi-shell-commands.js +8 -0
- package/package.json +29 -9
- package/renovate.json +26 -0
- package/index.js +0 -67
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
var addSorting = (function() {
|
|
3
|
+
'use strict';
|
|
4
|
+
var cols,
|
|
5
|
+
currentSort = {
|
|
6
|
+
index: 0,
|
|
7
|
+
desc: false
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// returns the summary table element
|
|
11
|
+
function getTable() {
|
|
12
|
+
return document.querySelector('.coverage-summary');
|
|
13
|
+
}
|
|
14
|
+
// returns the thead element of the summary table
|
|
15
|
+
function getTableHeader() {
|
|
16
|
+
return getTable().querySelector('thead tr');
|
|
17
|
+
}
|
|
18
|
+
// returns the tbody element of the summary table
|
|
19
|
+
function getTableBody() {
|
|
20
|
+
return getTable().querySelector('tbody');
|
|
21
|
+
}
|
|
22
|
+
// returns the th element for nth column
|
|
23
|
+
function getNthColumn(n) {
|
|
24
|
+
return getTableHeader().querySelectorAll('th')[n];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function onFilterInput() {
|
|
28
|
+
const searchValue = document.getElementById('fileSearch').value;
|
|
29
|
+
const rows = document.getElementsByTagName('tbody')[0].children;
|
|
30
|
+
|
|
31
|
+
// Try to create a RegExp from the searchValue. If it fails (invalid regex),
|
|
32
|
+
// it will be treated as a plain text search
|
|
33
|
+
let searchRegex;
|
|
34
|
+
try {
|
|
35
|
+
searchRegex = new RegExp(searchValue, 'i'); // 'i' for case-insensitive
|
|
36
|
+
} catch (error) {
|
|
37
|
+
searchRegex = null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < rows.length; i++) {
|
|
41
|
+
const row = rows[i];
|
|
42
|
+
let isMatch = false;
|
|
43
|
+
|
|
44
|
+
if (searchRegex) {
|
|
45
|
+
// If a valid regex was created, use it for matching
|
|
46
|
+
isMatch = searchRegex.test(row.textContent);
|
|
47
|
+
} else {
|
|
48
|
+
// Otherwise, fall back to the original plain text search
|
|
49
|
+
isMatch = row.textContent
|
|
50
|
+
.toLowerCase()
|
|
51
|
+
.includes(searchValue.toLowerCase());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
row.style.display = isMatch ? '' : 'none';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// loads the search box
|
|
59
|
+
function addSearchBox() {
|
|
60
|
+
var template = document.getElementById('filterTemplate');
|
|
61
|
+
var templateClone = template.content.cloneNode(true);
|
|
62
|
+
templateClone.getElementById('fileSearch').oninput = onFilterInput;
|
|
63
|
+
template.parentElement.appendChild(templateClone);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// loads all columns
|
|
67
|
+
function loadColumns() {
|
|
68
|
+
var colNodes = getTableHeader().querySelectorAll('th'),
|
|
69
|
+
colNode,
|
|
70
|
+
cols = [],
|
|
71
|
+
col,
|
|
72
|
+
i;
|
|
73
|
+
|
|
74
|
+
for (i = 0; i < colNodes.length; i += 1) {
|
|
75
|
+
colNode = colNodes[i];
|
|
76
|
+
col = {
|
|
77
|
+
key: colNode.getAttribute('data-col'),
|
|
78
|
+
sortable: !colNode.getAttribute('data-nosort'),
|
|
79
|
+
type: colNode.getAttribute('data-type') || 'string'
|
|
80
|
+
};
|
|
81
|
+
cols.push(col);
|
|
82
|
+
if (col.sortable) {
|
|
83
|
+
col.defaultDescSort = col.type === 'number';
|
|
84
|
+
colNode.innerHTML =
|
|
85
|
+
colNode.innerHTML + '<span class="sorter"></span>';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return cols;
|
|
89
|
+
}
|
|
90
|
+
// attaches a data attribute to every tr element with an object
|
|
91
|
+
// of data values keyed by column name
|
|
92
|
+
function loadRowData(tableRow) {
|
|
93
|
+
var tableCols = tableRow.querySelectorAll('td'),
|
|
94
|
+
colNode,
|
|
95
|
+
col,
|
|
96
|
+
data = {},
|
|
97
|
+
i,
|
|
98
|
+
val;
|
|
99
|
+
for (i = 0; i < tableCols.length; i += 1) {
|
|
100
|
+
colNode = tableCols[i];
|
|
101
|
+
col = cols[i];
|
|
102
|
+
val = colNode.getAttribute('data-value');
|
|
103
|
+
if (col.type === 'number') {
|
|
104
|
+
val = Number(val);
|
|
105
|
+
}
|
|
106
|
+
data[col.key] = val;
|
|
107
|
+
}
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
// loads all row data
|
|
111
|
+
function loadData() {
|
|
112
|
+
var rows = getTableBody().querySelectorAll('tr'),
|
|
113
|
+
i;
|
|
114
|
+
|
|
115
|
+
for (i = 0; i < rows.length; i += 1) {
|
|
116
|
+
rows[i].data = loadRowData(rows[i]);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// sorts the table using the data for the ith column
|
|
120
|
+
function sortByIndex(index, desc) {
|
|
121
|
+
var key = cols[index].key,
|
|
122
|
+
sorter = function(a, b) {
|
|
123
|
+
a = a.data[key];
|
|
124
|
+
b = b.data[key];
|
|
125
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
126
|
+
},
|
|
127
|
+
finalSorter = sorter,
|
|
128
|
+
tableBody = document.querySelector('.coverage-summary tbody'),
|
|
129
|
+
rowNodes = tableBody.querySelectorAll('tr'),
|
|
130
|
+
rows = [],
|
|
131
|
+
i;
|
|
132
|
+
|
|
133
|
+
if (desc) {
|
|
134
|
+
finalSorter = function(a, b) {
|
|
135
|
+
return -1 * sorter(a, b);
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (i = 0; i < rowNodes.length; i += 1) {
|
|
140
|
+
rows.push(rowNodes[i]);
|
|
141
|
+
tableBody.removeChild(rowNodes[i]);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
rows.sort(finalSorter);
|
|
145
|
+
|
|
146
|
+
for (i = 0; i < rows.length; i += 1) {
|
|
147
|
+
tableBody.appendChild(rows[i]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// removes sort indicators for current column being sorted
|
|
151
|
+
function removeSortIndicators() {
|
|
152
|
+
var col = getNthColumn(currentSort.index),
|
|
153
|
+
cls = col.className;
|
|
154
|
+
|
|
155
|
+
cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
|
|
156
|
+
col.className = cls;
|
|
157
|
+
}
|
|
158
|
+
// adds sort indicators for current column being sorted
|
|
159
|
+
function addSortIndicators() {
|
|
160
|
+
getNthColumn(currentSort.index).className += currentSort.desc
|
|
161
|
+
? ' sorted-desc'
|
|
162
|
+
: ' sorted';
|
|
163
|
+
}
|
|
164
|
+
// adds event listeners for all sorter widgets
|
|
165
|
+
function enableUI() {
|
|
166
|
+
var i,
|
|
167
|
+
el,
|
|
168
|
+
ithSorter = function ithSorter(i) {
|
|
169
|
+
var col = cols[i];
|
|
170
|
+
|
|
171
|
+
return function() {
|
|
172
|
+
var desc = col.defaultDescSort;
|
|
173
|
+
|
|
174
|
+
if (currentSort.index === i) {
|
|
175
|
+
desc = !currentSort.desc;
|
|
176
|
+
}
|
|
177
|
+
sortByIndex(i, desc);
|
|
178
|
+
removeSortIndicators();
|
|
179
|
+
currentSort.index = i;
|
|
180
|
+
currentSort.desc = desc;
|
|
181
|
+
addSortIndicators();
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
for (i = 0; i < cols.length; i += 1) {
|
|
185
|
+
if (cols[i].sortable) {
|
|
186
|
+
// add the click event handler on the th so users
|
|
187
|
+
// dont have to click on those tiny arrows
|
|
188
|
+
el = getNthColumn(i).querySelector('.sorter').parentElement;
|
|
189
|
+
if (el.addEventListener) {
|
|
190
|
+
el.addEventListener('click', ithSorter(i));
|
|
191
|
+
} else {
|
|
192
|
+
el.attachEvent('onclick', ithSorter(i));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// adds sorting functionality to the UI
|
|
198
|
+
return function() {
|
|
199
|
+
if (!getTable()) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
cols = loadColumns();
|
|
203
|
+
loadData();
|
|
204
|
+
addSearchBox();
|
|
205
|
+
addSortIndicators();
|
|
206
|
+
enableUI();
|
|
207
|
+
};
|
|
208
|
+
})();
|
|
209
|
+
|
|
210
|
+
window.addEventListener('load', addSorting);
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import js from '@eslint/js';
|
|
2
|
+
import { defineConfig } from 'eslint/config';
|
|
3
|
+
import globals from 'globals';
|
|
4
|
+
|
|
5
|
+
export default defineConfig([
|
|
6
|
+
{
|
|
7
|
+
files: ['**/*.{js,mjs,cjs}'],
|
|
8
|
+
plugins: { js },
|
|
9
|
+
extends: ['js/recommended'],
|
|
10
|
+
languageOptions: { globals: globals.node },
|
|
11
|
+
},
|
|
12
|
+
]);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { checkInputs } from './check-inputs.js';
|
|
2
|
+
import { cleanUpInputs } from './clean-up-inputs.js';
|
|
3
|
+
|
|
4
|
+
export function initializeMidi() {
|
|
5
|
+
const checkDelay = 60 * 1000 + (Math.random() * 3000) | 0;
|
|
6
|
+
checkInputs();
|
|
7
|
+
setInterval(checkInputs, checkDelay);
|
|
8
|
+
process.on('exit', cleanUpInputs);
|
|
9
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { cleanUpWatcher } from './clean-up-watcher.js';
|
|
5
|
+
import { log } from './log.js';
|
|
6
|
+
import { refreshScripts } from './refresh-scripts.js';
|
|
7
|
+
import { state } from './state.js';
|
|
8
|
+
|
|
9
|
+
export function initializeScriptsDirectory() {
|
|
10
|
+
// Default to ~/Documents/MidiShellCommands if no directory is provided
|
|
11
|
+
let targetDirArg = process.argv[process.argv.length - 1];
|
|
12
|
+
if (process.argv.length === 2 || targetDirArg === '--daemon') {
|
|
13
|
+
targetDirArg = path.join(os.homedir(), 'Documents', 'MidiShellCommands');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Ensure the directory exists
|
|
17
|
+
state.watchDir = path.resolve(targetDirArg);
|
|
18
|
+
try {
|
|
19
|
+
fs.mkdirSync(state.watchDir, { recursive: true });
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
console.error('Failed to create or access scripts directory:', state.watchDir);
|
|
23
|
+
console.error(e);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
refreshScripts();
|
|
28
|
+
try {
|
|
29
|
+
state.watcher = fs.watch(state.watchDir, { persistent: true }, refreshScripts);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// Non-fatal on some platforms
|
|
33
|
+
}
|
|
34
|
+
process.on('exit', cleanUpWatcher);
|
|
35
|
+
|
|
36
|
+
log(`Watching ${targetDirArg}`);
|
|
37
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import childProcess from 'node:child_process';
|
|
2
|
+
import { log } from './log.js';
|
|
3
|
+
import { mapMessageToFileNames } from './map-message-to-file-names.js';
|
|
4
|
+
import { reportErrors } from './report-errors.js';
|
|
5
|
+
import { state } from './state.js';
|
|
6
|
+
|
|
7
|
+
export function invokeScripts(msg) {
|
|
8
|
+
const possibleFileNames = mapMessageToFileNames(msg);
|
|
9
|
+
log(possibleFileNames[0]);
|
|
10
|
+
for (const possibleFileName of possibleFileNames) {
|
|
11
|
+
for (let i = 0; i < state.scriptsWithoutExtension.length; i++) {
|
|
12
|
+
if (state.scriptsWithoutExtension[i] === possibleFileName) {
|
|
13
|
+
log('Executing ' + state.scripts[i]);
|
|
14
|
+
childProcess.exec(`./${state.scripts[i]}`, { cwd: state.watchDir }, reportErrors);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import easyMIDI from 'easymidi';
|
|
2
|
+
import { invokeScripts } from './invoke-scripts.js';
|
|
3
|
+
import { state } from './state.js';
|
|
4
|
+
|
|
5
|
+
export function listenToInput(inputName) {
|
|
6
|
+
if (!state.watchedInputs[inputName]) {
|
|
7
|
+
const input = state.watchedInputs[inputName] = new easyMIDI.Input(inputName);
|
|
8
|
+
input.on('noteon', invokeScripts);
|
|
9
|
+
input.on('noteoff', invokeScripts);
|
|
10
|
+
}
|
|
11
|
+
}
|
package/lib/log.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
#!/usr/local/bin/node
|
|
2
|
+
import childProcess from 'node:child_process';
|
|
3
|
+
/**
|
|
4
|
+
* Postinstall script to set up a macOS LaunchAgent which runs midi-shell-commands
|
|
5
|
+
* pointing at ~/Documents/MidiShellCommands.
|
|
6
|
+
*/
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import os from 'node:os';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { escapePath } from './escape-path.js';
|
|
11
|
+
import { log } from './log.js';
|
|
12
|
+
|
|
13
|
+
(function main() {
|
|
14
|
+
const platform = process.platform;
|
|
15
|
+
if (platform !== 'darwin') {
|
|
16
|
+
log('Postinstall daemon setup skipped (not macOS).');
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const home = os.homedir();
|
|
21
|
+
const scriptsDir = path.join(home, 'Documents', 'MidiShellCommands');
|
|
22
|
+
try {
|
|
23
|
+
fs.mkdirSync(scriptsDir, { recursive: true });
|
|
24
|
+
// Add a sample README the first time, without overwriting user files.
|
|
25
|
+
const readmePath = path.join(scriptsDir, 'README.txt');
|
|
26
|
+
if (!fs.existsSync(readmePath)) {
|
|
27
|
+
fs.writeFileSync(readmePath, 'Place executable scripts here. Names should match patterns like:\nnoteon.<note>\nnoteon.<note>.<channel>\nnoteon.<note>.<channel>.<velocity>\nMake files executable (chmod +x).');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
log(`Failed to create scripts directory at ${scriptsDir}: ${e.message}`);
|
|
32
|
+
// Continue; LaunchAgent will still start and the app will create it on run.
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Determine bin path where npm linked the CLI
|
|
36
|
+
const npmPrefix = process.env.npm_config_prefix || '';
|
|
37
|
+
let binPath = '';
|
|
38
|
+
if (npmPrefix) {
|
|
39
|
+
// Typical global install
|
|
40
|
+
binPath = path.join(npmPrefix, 'bin', 'midi-shell-commands');
|
|
41
|
+
}
|
|
42
|
+
if (!binPath || !fs.existsSync(binPath)) {
|
|
43
|
+
// Fallback to local project binary path
|
|
44
|
+
binPath = path.join(process.cwd(), 'midi-shell-commands.js');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const launchAgentsDir = path.join(home, 'Library', 'LaunchAgents');
|
|
48
|
+
const label = 'com.midi-shell-commands';
|
|
49
|
+
const plistPath = path.join(launchAgentsDir, `${label}.plist`);
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
fs.mkdirSync(launchAgentsDir, { recursive: true });
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
log(`Failed to ensure LaunchAgents directory: ${e.message}`);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const stdoutPath = path.join(home, 'Library', 'Logs', 'midi-shell-commands.stdout.log');
|
|
60
|
+
const stderrPath = path.join(home, 'Library', 'Logs', 'midi-shell-commands.stderr.log');
|
|
61
|
+
|
|
62
|
+
const plist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
63
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
64
|
+
<plist version="1.0">
|
|
65
|
+
<dict>
|
|
66
|
+
<key>Label</key>
|
|
67
|
+
<string>${label}</string>
|
|
68
|
+
<key>ProgramArguments</key>
|
|
69
|
+
<array>
|
|
70
|
+
<string>${binPath}</string>
|
|
71
|
+
<string>${scriptsDir}</string>
|
|
72
|
+
</array>
|
|
73
|
+
<key>RunAtLoad</key>
|
|
74
|
+
<true/>
|
|
75
|
+
<key>KeepAlive</key>
|
|
76
|
+
<true/>
|
|
77
|
+
<key>StandardOutPath</key>
|
|
78
|
+
<string>${stdoutPath}</string>
|
|
79
|
+
<key>StandardErrorPath</key>
|
|
80
|
+
<string>${stderrPath}</string>
|
|
81
|
+
<key>EnvironmentVariables</key>
|
|
82
|
+
<dict>
|
|
83
|
+
<key>PATH</key>
|
|
84
|
+
<string>/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
|
|
85
|
+
</dict>
|
|
86
|
+
</dict>
|
|
87
|
+
</plist>`;
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
fs.writeFileSync(plistPath, plist, { encoding: 'utf8' });
|
|
91
|
+
log(`Installed LaunchAgent at ${plistPath}`);
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
log(`Failed to write LaunchAgent plist: ${e.message}`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Try to (re)load the LaunchAgent so it starts now
|
|
99
|
+
try {
|
|
100
|
+
if (process.env.SUDO_UID) {
|
|
101
|
+
// Avoid trying to load as root for a user agent
|
|
102
|
+
log('Skipping launchctl load because running under sudo. You can load it later with your user session.');
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Unload if already loaded to pick up updates
|
|
107
|
+
try {
|
|
108
|
+
childProcess.execSync(`launchctl unload ${escapePath(plistPath)}`, { stdio: 'ignore' });
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// Unload failed, proceed forward.
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Preferred modern approach: bootstrap into the current user session
|
|
115
|
+
const uid = process.getuid && process.getuid();
|
|
116
|
+
if (uid) {
|
|
117
|
+
try {
|
|
118
|
+
childProcess.execSync(`launchctl bootstrap gui/${uid} ${escapePath(plistPath)}`, { stdio: 'ignore' });
|
|
119
|
+
log('LaunchAgent bootstrapped.');
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
console.log('Current user session bootstrap failed, falling back to legacy load.');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Fallback to legacy load
|
|
128
|
+
childProcess.execSync(`launchctl load -w ${escapePath(plistPath)}`, { stdio: 'ignore' });
|
|
129
|
+
log('LaunchAgent loaded.');
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
log(`Could not load LaunchAgent automatically: ${e.message}\nYou can load it manually with:\n launchctl load -w ${plistPath}`);
|
|
133
|
+
}
|
|
134
|
+
})();
|
|
135
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { state } from './state.js';
|
|
3
|
+
|
|
4
|
+
export function refreshScripts() {
|
|
5
|
+
try {
|
|
6
|
+
state.scripts = fs.readdirSync(state.watchDir).filter(f => f[0] !== '.');
|
|
7
|
+
state.scriptsWithoutExtension = state.scripts.map(script => script.split('.').slice(0, -1).join('.'));
|
|
8
|
+
}
|
|
9
|
+
catch (e) {
|
|
10
|
+
console.error('Failed to read scripts directory:', state.watchDir);
|
|
11
|
+
console.error(e);
|
|
12
|
+
}
|
|
13
|
+
}
|
package/lib/state.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { initializeMidi } from './lib/initialize-midi.js';
|
|
3
|
+
import { initializeScriptsDirectory } from './lib/initialize-scripts-directory.js';
|
|
4
|
+
import { log } from './lib/log.js';
|
|
5
|
+
|
|
6
|
+
log('Starting up!');
|
|
7
|
+
initializeScriptsDirectory();
|
|
8
|
+
initializeMidi();
|
package/package.json
CHANGED
|
@@ -1,19 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "midi-shell-commands",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"midi-shell-commands": "index.js"
|
|
7
|
-
},
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"author": "Dawson Toth",
|
|
5
|
+
"repository": "https://github.com/dawsontoth/midi-shell-commands.git",
|
|
8
6
|
"scripts": {
|
|
9
|
-
"start": "node
|
|
10
|
-
"
|
|
7
|
+
"start": "node midi-shell-commands.js ./scripts",
|
|
8
|
+
"postinstall": "node lib/post-install.js",
|
|
9
|
+
"test": "vitest",
|
|
10
|
+
"test:ci": "vitest run",
|
|
11
|
+
"test:coverage": "vitest run --coverage",
|
|
12
|
+
"lint": "eslint .",
|
|
13
|
+
"lint:fix": "eslint . --fix"
|
|
11
14
|
},
|
|
15
|
+
"description": "Execute shell scripts on MIDI note events; supports macOS daemon mode via LaunchAgent.",
|
|
16
|
+
"type": "module",
|
|
12
17
|
"keywords": [],
|
|
13
|
-
"
|
|
18
|
+
"main": "midi-shell-commands.js",
|
|
19
|
+
"bin": {
|
|
20
|
+
"midi-shell-commands": "midi-shell-commands.js"
|
|
21
|
+
},
|
|
14
22
|
"license": "ISC",
|
|
15
|
-
"description": "",
|
|
16
23
|
"dependencies": {
|
|
17
24
|
"easymidi": "^3.1.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@eslint/js": "^9.36.0",
|
|
28
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
29
|
+
"@semantic-release/git": "^10.0.1",
|
|
30
|
+
"@semantic-release/github": "^11.0.6",
|
|
31
|
+
"@semantic-release/npm": "^12.0.2",
|
|
32
|
+
"@semantic-release/release-notes-generator": "^14.1.0",
|
|
33
|
+
"@vitest/coverage-v8": "^3.0.0",
|
|
34
|
+
"eslint": "^9.36.0",
|
|
35
|
+
"globals": "^16.4.0",
|
|
36
|
+
"semantic-release": "^24.2.9",
|
|
37
|
+
"vitest": "^3.0.0"
|
|
18
38
|
}
|
|
19
39
|
}
|
package/renovate.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
|
3
|
+
"extends": [
|
|
4
|
+
"config:recommended"
|
|
5
|
+
],
|
|
6
|
+
"lockFileMaintenance": {
|
|
7
|
+
"enabled": true,
|
|
8
|
+
"automerge": true
|
|
9
|
+
},
|
|
10
|
+
"packageRules": [
|
|
11
|
+
{
|
|
12
|
+
"matchDepTypes": [
|
|
13
|
+
"action"
|
|
14
|
+
],
|
|
15
|
+
"pinDigests": true
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"matchUpdateTypes": [
|
|
19
|
+
"minor",
|
|
20
|
+
"patch"
|
|
21
|
+
],
|
|
22
|
+
"matchCurrentVersion": "!/^0/",
|
|
23
|
+
"automerge": true
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
package/index.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
const childProcess = require('child_process');
|
|
3
|
-
const easyMIDI = require('easymidi');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
|
|
7
|
-
if (process.argv.length === 2) {
|
|
8
|
-
console.error('Please pass the path to the directory containing your scripts.');
|
|
9
|
-
process.exit(1);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const watchDir = path.resolve(process.argv[process.argv.length - 1]);
|
|
13
|
-
const scripts = fs.readdirSync(watchDir).filter(f => f[0] !== '.');
|
|
14
|
-
const scriptsWithoutExtension = scripts.map(script => script.split('.').slice(0, -1).join('.'));
|
|
15
|
-
const checkDelay = 60 * 1000 + (Math.random() * 3000) | 0;
|
|
16
|
-
|
|
17
|
-
const watchedInputs = {};
|
|
18
|
-
checkInputs();
|
|
19
|
-
setInterval(checkInputs, checkDelay);
|
|
20
|
-
|
|
21
|
-
function checkInputs() {
|
|
22
|
-
const inputNames = easyMIDI.getInputs();
|
|
23
|
-
for (const inputName of inputNames) {
|
|
24
|
-
listenToInput(inputName);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function listenToInput(inputName) {
|
|
29
|
-
if (watchedInputs[inputName]) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const input = watchedInputs[inputName] = new easyMIDI.Input(inputName);
|
|
33
|
-
input.on('noteon', invokeScripts);
|
|
34
|
-
input.on('noteoff', invokeScripts);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function invokeScripts(msg) {
|
|
38
|
-
const possibleFileNames = mapMessageToFileNames(msg);
|
|
39
|
-
for (const possibleFileName of possibleFileNames) {
|
|
40
|
-
for (let i = 0; i < scriptsWithoutExtension.length; i++) {
|
|
41
|
-
if (scriptsWithoutExtension[i] === possibleFileName) {
|
|
42
|
-
console.log('Executing ' + scripts[i]);
|
|
43
|
-
childProcess.exec(`./${scripts[i]}`, { cwd: watchDir }, reportErrors);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function mapMessageToFileNames(msg) {
|
|
50
|
-
return [
|
|
51
|
-
`${msg._type}.${msg.note}.${msg.channel}.${msg.velocity}`,
|
|
52
|
-
`${msg._type}.${msg.note}.${msg.channel}`,
|
|
53
|
-
`${msg._type}.${msg.note}`,
|
|
54
|
-
];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function reportErrors(err) {
|
|
58
|
-
if (err) {
|
|
59
|
-
console.error(err);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
process.on('exit', () => {
|
|
64
|
-
for (const input of Object.values(watchedInputs)) {
|
|
65
|
-
input.close();
|
|
66
|
-
}
|
|
67
|
-
});
|