Skip to content

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.

js
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 ?dir is 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

ts
const url: string

A 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.

ts
// plugin index.js
import images from './images?dir'

console.log(images.url)
// output: https://plugins/your-plugin/images

Directory.exists() method

ts
function exists(): boolean

Indicates whether the directory exists or not.

js
import images from './images?dir'

console.log('does images folder exist?:', images.exists())

Directory.files() method

ts
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.

js
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

ts
function reveal(create?: boolean): void

Call 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.

Released under the MIT License.