My custom module is becoming huge and quite unmanageable each time I want to do something new and was wondering if it is possible to modularise my custom module:
Create a Directory Structure :
Create a directory for my custom module and any sub-modules.
Example structure:
my_custom_module/
├── index.js
├── chordUtils.js
├── noteUtils.js
└── oscHandlers.js
Move Related Functions to Separate Files :
chordUtils.js : Functions related to chords.
noteUtils.js : Functions related to note conversion.
oscHandlers.js : Functions to handle OSC messages.
Is this possible before I embark on what may be quite a big project...
Hi,
Absoutely! I do the same thing as my OSC template has grown.
For example, here in the main.js custom module script. I'm exporting certain methods for OSC to read.
module.exports = {
init: () => _init(),
unload: () => _unload(),
oscInFilter: (data) => _oscInFilter(data),
oscOutFilter: (data) => _oscOutFilter(data),
}
I import them at the top of the main.js script like this
const { _init } = require("./oscFilters/init.js")
const { _oscInFilter } = require("./oscFilters/oscInFilter.js")
const { _oscOutFilter } = require("./oscFilters/oscOutFilter.js")
const { _unload } = require("./oscFilters/unload.js")
And then for example in the unload.js,
const _unload = () => {
// stuff here
}
module.exports = {
_unload,
}
Cheers!
DMDComposer:
I import them at the top of the main.js script like this
const { _init } = require("./oscFilters/init.js")
const { _oscInFilter } = require("./oscFilters/oscInFilter.js")
const { _oscOutFilter } = require("./oscFilters/oscOutFilter.js")
const { _unload } = require("./oscFilters/unload.js")
Great so does this mean if I've got a load of global variables, I can put them all in a separate globalVarables.js
file an then just call that file in at the top of the myCustomModule.js
file?
1 Like
Hi,
Yes. This is what I do if it helps or gives you and idea to build upon.
In my main.js.
const { deleteGlobals, setGlobals } = require("./utils/globals.js")
app.on("open", async (data, client) => {
await setGlobals()
})
In globals.js
const setGlobals = async () => {
// set global clients to empty array
global.clients = global.clients || []
// Cubase Globals
global.cubase = {
timecode: {
primary: "",
secondary: "",
},
mixerChannels: {},
storedMixerChannels: {},
storedTotalChannels: 0,
matrixQuantity: 0,
emptyChannelCount: 0,
mixerPanelScrollPosition: [0, 0],
}
}
module.exports = {
setGlobals
}
It could be better to set the globals manually though for default values in the init.js method so they are initialized rather than in the main.js on app.open()
etc..
1 Like
Solved (previous posts deleted as they were not clear)
const config = {};
const functions = {};
fs.readdirSync(configDir).forEach(file => {
if (file.endsWith('.js')) {
const configPath = path.join(configDir, file);
if (!fs.existsSync(configPath)) {
console.error(`ERROR: ${file} not found at`, configPath);
process.exit(1);
}
const configData = nativeRequire(configPath);
Object.assign(config, configData);
Object.assign(functions, configData);
}
});
// Accessing configurations using helper functions
const getConfig = (path, defaultValue = undefined) => {
return path.split('.').reduce((acc, part) => acc && acc[part], config) || defaultValue;
};
// Accessing functions using helper functions
const getFunction = (path, defaultValue = undefined) => {
return path.split('.').reduce((acc, part) => acc && acc[part], functions) || defaultValue;
};
// Get functions using getFunction
let sendOSCCommand = getFunction('sendOSCCommand');
// let [Other function calls here]
//Access configurations using getConfig
const customModuleVer = getConfig('customModule.fullVersion');
1 Like