Directory Module
Pengu runtime allows you to access plugins' folder using the import statement.
Importing a directory (folder)
since v1.2.0
You can import a directory by appending ?dir to the import path. The result is an instance of a built-in Directory class, which provides various methods to interact with the folder.
import images from './images?dir'Rules
- Relative Paths Only: Import paths must be relative and cannot navigate to a parent folder (../).
- No Dynamic Imports: Using dynamic imports with
?diris unsupported. - Local Only: Directory imports from remote URLs are not allowed.
See the sections below to use the instance properties and methods.
Directory.url property
const url: stringA read-only property that returns the URL to the directory.
For example, your plugin name is your-plugin and the code is executed in the index.js.
// plugin index.js
import images from './images?dir'
console.log(images.url)
// output: https://plugins/your-plugin/imagesDirectory.exists() method
function exists(): booleanIndicates whether the directory exists or not.
import images from './images?dir'
console.log('does images folder exist?:', images.exists())Directory.files() method
function files(): Promise<string[]>Lists all files in the directory, not including folders and files in subfolders. The method returns a promise with array of file names.
import images from './images?dir'
for (let file of await images.files()) {
console.log('image: %s', file)
}You can concatenate the Directory.url with child file name to get the full file path.
Directory.reveal() method
function reveal(create?: boolean): voidCall this method to open the directory in the system's file explorer, such as Explorer on Windows or Finder on macOS. If the directory doesn't exist, you can pass true to the create param to create it before attempting to reveal it.
That's useful when user needs to add files to the folder, just clicks a button and the folder appears.