Working with Assets
Plugins may require external non-source files as part of their functionality. These can be text files, executables, databases, images, etc. The JS SDK provides the “assets.yml” file to allow users to specify files which are to be included as part of the plugin build process.
Example: Reading data from a file included in the assets.yml
- Create an
assets.ymlfile in the root folder of the plugin - Create an
assetsdirectory in the root of the plugin directory- The
assetsdirectory is not required for theassets.ymlto work as files listed inassets.ymlcan specify the exact path. We are only creating theassetsdirectory here to keep our assets organised in the plugin directory
- The
- Create a new text file
my-text-file.txtin theassetsdirectory. Add some text to the file. -
In the
assets.ymlfile, add the following configuration to include the text file as part of the plugin buildnamewill be the name of the folder where your files will be stored in the plugin once it is built. We will use this value during runtime to specify the file locationpathspecifies the location of the file. Path can also accept glob patterns, e.g.assets/*will copy all files and directories in theassetsfolder
To access the file at runtime, the Plugin SDK provides a constant which is the location of the assets folder during runtime, ASSETS_PATH. Use this in combination with the name provided for the assets in the assets.yml to access the file in an action. Below is an example of an action that reads the file and outputs the contents to a command window:
import { CommandAction, ASSETS_PATH } from '@logitech/plugin-sdk';
import { exec } from 'child_process';
import { promises as fs } from 'fs';
export class ReadFileAction extends CommandAction {
name = 'read_file';
displayName = 'Read file';
description = 'Reads the contents of the sample file';
async onKeyDown() {
const data = await fs.readFile(`${ASSETS_PATH}\\files\\my-text-file.txt`, 'utf8');
exec(`start cmd.exe /K echo ${data}`);
}
}