/* eslint-disable max-len */ import type fs from 'fs-extra' import type { PackType } from 'sandstone/pack/packType' import { ContainerNode } from '../nodes' import type { SandstoneCore } from '../sandstoneCore' import type { ResourceClassArguments, ResourceNode } from './resource' import { ResourceClass } from './resource' import type { NamespacedString } from 'sandstone/utils' /** * A node representing a custom resource. */ export class CustomResourceNode extends ContainerNode implements ResourceNode { constructor( sandstoneCore: SandstoneCore, public resource: CustomResourceClass, ) { super(sandstoneCore) } getValue = () => this.resource.getValue() } export type CustomResourceClassArguments = { /** The custom resource type. */ type: string /** Optional. The Pack this resource is exported into. Defaults to the datapack. */ packType?: PackType /** Optional. The folder in the pack where the resource gets stored. Defaults to the root of the pack without a namespace. */ folder?: string[] /** Optional. File extension of the resource. Defaults to `json`. */ extension?: string /** Optional. How `SandstoneCore.getExistingResource` should read this resource. If `false` the encoding will not be provided to `fs.readFile`. Defaults to `'utf8'`. */ encoding?: false | fs.EncodingOption } & ResourceClassArguments<'default'> export abstract class CustomResourceClass extends ResourceClass { public type: string public folder?: string[] constructor(sandstoneCore: SandstoneCore, name: string, args: CustomResourceClassArguments) { sandstoneCore.pack.registerCustomResource(args.type) super( sandstoneCore, { packType: args.packType || sandstoneCore.pack.dataPack(), extension: args.extension || 'json', encoding: args.encoding, }, CustomResourceNode, args.type, // eslint-disable-next-line no-nested-ternary args.folder ? name.includes('/') ? name.split('/') : [name] : sandstoneCore.pack.resourceToPath(name, args.folder), args, ) this.type = args.type this.folder = args.folder this.handleConflicts() } get name() { return `${this.path[0]}:${this.path.slice( this.folder === undefined ? 2 : this.folder.length + 1 ).join('/')}` as NamespacedString } getValue(): string | ArrayBuffer | Buffer | Promise { throw new Error(`Custom Resource '${this.type}' getValue function is not defined!`) } toString = () => this.name }