-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
247 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Import } from '@cordisjs/loader' | ||
|
||
export default Import |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { access, constants, readFile, rename, writeFile } from 'node:fs/promises' | ||
import { pathToFileURL } from 'node:url' | ||
import { remove } from 'cosmokit' | ||
import * as yaml from 'js-yaml' | ||
import { EntryOptions } from './entry.ts' | ||
import { Loader } from '../loader.ts' | ||
import { JsExpr } from './utils.ts' | ||
|
||
export const schema = yaml.JSON_SCHEMA.extend(JsExpr) | ||
|
||
export class LoaderFile { | ||
public suspend = false | ||
public readonly: boolean | ||
public refs: FileRef[] = [] | ||
public writeTask?: NodeJS.Timeout | ||
|
||
constructor(public loader: Loader, public name: string, public type?: string) { | ||
this.readonly = !type | ||
} | ||
|
||
async checkAccess() { | ||
if (!this.type) return | ||
try { | ||
await access(this.name, constants.W_OK) | ||
} catch { | ||
this.readonly = true | ||
} | ||
} | ||
|
||
async read(): Promise<EntryOptions[]> { | ||
if (this.type === 'application/yaml') { | ||
return yaml.load(await readFile(this.name, 'utf8'), { schema }) as any | ||
} else if (this.type === 'application/json') { | ||
// we do not use require / import here because it will pollute cache | ||
return JSON.parse(await readFile(this.name, 'utf8')) as any | ||
} else { | ||
const module = await import(this.name) | ||
return module.default || module | ||
} | ||
} | ||
|
||
private async _write(config: EntryOptions[]) { | ||
this.suspend = true | ||
if (this.readonly) { | ||
throw new Error(`cannot overwrite readonly config`) | ||
} | ||
if (this.type === 'application/yaml') { | ||
await writeFile(this.name + '.tmp', yaml.dump(config, { schema })) | ||
} else if (this.type === 'application/json') { | ||
await writeFile(this.name + '.tmp', JSON.stringify(config, null, 2)) | ||
} | ||
await rename(this.name + '.tmp', this.name) | ||
} | ||
|
||
write(config: EntryOptions[]) { | ||
clearTimeout(this.writeTask) | ||
this.writeTask = setTimeout(() => { | ||
this.writeTask = undefined | ||
this._write(config) | ||
}, 0) | ||
} | ||
|
||
ref() { | ||
return new FileRef(this) | ||
} | ||
} | ||
|
||
export namespace LoaderFile { | ||
export const writable = { | ||
'.json': 'application/json', | ||
'.yaml': 'application/yaml', | ||
'.yml': 'application/yaml', | ||
} | ||
|
||
export const supported = new Set(Object.keys(writable)) | ||
|
||
if (typeof require !== 'undefined') { | ||
// eslint-disable-next-line n/no-deprecated-api | ||
for (const extname in require.extensions) { | ||
supported.add(extname) | ||
} | ||
} | ||
} | ||
|
||
export class FileRef<F extends LoaderFile = LoaderFile> { | ||
public url: string | ||
|
||
constructor(public file: F) { | ||
this.file.refs.push(this) | ||
this.url = pathToFileURL(file.name).href | ||
file.loader.files[this.url] ??= this.file | ||
} | ||
|
||
stop() { | ||
remove(this.file.refs, this) | ||
if (this.file.refs.length) return | ||
delete this.file.loader.files[this.url] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { Context } from '@cordisjs/core' | ||
import { dirname, extname, resolve } from 'node:path' | ||
import { readdir, stat } from 'node:fs/promises' | ||
import { fileURLToPath } from 'node:url' | ||
import { EntryTree } from './tree.ts' | ||
import { FileRef, LoaderFile } from './file.ts' | ||
import Loader from '../loader.ts' | ||
|
||
export class ImportTree<C extends Context = Context> extends EntryTree<C> { | ||
static reusable = true | ||
|
||
public ref!: FileRef | ||
|
||
constructor(public ctx: C) { | ||
super(ctx) | ||
ctx.on('ready', () => this.start()) | ||
ctx.on('dispose', () => this.stop()) | ||
} | ||
|
||
async start() { | ||
await this.refresh() | ||
await this.ref.file.checkAccess() | ||
} | ||
|
||
async refresh() { | ||
this.root.update(await this.ref.file.read()) | ||
} | ||
|
||
stop() { | ||
this.ref?.stop() | ||
return this.root.stop() | ||
} | ||
|
||
write() { | ||
this.context.emit('loader/config-update') | ||
return this.ref.file.write(this.root.data) | ||
} | ||
|
||
async init(baseDir: string, options: Loader.Config) { | ||
if (options.filename) { | ||
const filename = resolve(baseDir, options.filename) | ||
const stats = await stat(filename) | ||
if (stats.isFile()) { | ||
baseDir = dirname(filename) | ||
const ext = extname(filename) | ||
const type = LoaderFile.writable[ext] | ||
if (!LoaderFile.supported.has(ext)) { | ||
throw new Error(`extension "${ext}" not supported`) | ||
} | ||
this.ref = new LoaderFile(this.ctx.loader, filename, type).ref() | ||
} else { | ||
baseDir = filename | ||
await this._init(baseDir, options) | ||
} | ||
} else { | ||
await this._init(baseDir, options) | ||
} | ||
this.ctx.provide('baseDir', baseDir, true) | ||
this.url = this.ref.url | ||
} | ||
|
||
private async _init(baseDir: string, options: Loader.Config) { | ||
const { name, initial } = options | ||
const dirents = await readdir(baseDir, { withFileTypes: true }) | ||
for (const extension of LoaderFile.supported) { | ||
const dirent = dirents.find(dirent => dirent.name === name + extension) | ||
if (!dirent) continue | ||
if (!dirent.isFile()) { | ||
throw new Error(`config file "${dirent.name}" is not a file`) | ||
} | ||
const type = LoaderFile.writable[extension] | ||
const filename = resolve(baseDir, name + extension) | ||
this.ref = new LoaderFile(this.ctx.loader, filename, type).ref() | ||
return | ||
} | ||
if (initial) { | ||
const type = LoaderFile.writable['.yml'] | ||
const filename = resolve(baseDir, name + '.yml') | ||
this.ref = new LoaderFile(this.ctx.loader, filename, type).ref() | ||
return this.ref.file.write(initial as any) | ||
} | ||
throw new Error('config file not found') | ||
} | ||
} | ||
|
||
export namespace Import { | ||
export interface Config { | ||
url: string | ||
} | ||
} | ||
|
||
export class Import extends ImportTree { | ||
constructor(ctx: Context, public config: Import.Config) { | ||
super(ctx) | ||
} | ||
|
||
async start() { | ||
const { url } = this.config | ||
const filename = fileURLToPath(new URL(url, this.ctx.scope.entry!.parent.tree.url)) | ||
const ext = extname(filename) | ||
if (!LoaderFile.supported.has(ext)) { | ||
throw new Error(`extension "${ext}" not supported`) | ||
} | ||
this.ref = new LoaderFile(this.ctx.loader, filename, LoaderFile.writable[ext]).ref() | ||
await super.start() | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.