KScript docs
EN

storage fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

Fields

FieldTypeDescription
.appstringThe app the folder belongs to.
.kindstringWhich of the app's folders it is: config, data, cache, state or logs.
.pathstringThe folder's full path.

From KScript.Storage

These need <import package="KScript.Storage" />import KScript.Storage. See KScript.Storage.

#storage->append(name, text) KScript.Storage

Adds text to the end of a file.

Arguments can be named: ->append(…, text: …).

Example
<do>@storage(logs)->append('app.log', 'Started' + @char_of(10))</do>
@storage(logs)->append('app.log', 'Started' + @char_of(10))

#storage->clear() → number KScript.Storage

Deletes everything in the folder. Returns how many files were deleted.
Example
<echo>{ @storage(cache)->clear() } files removed</echo>
echo "{ @storage(cache)->clear() } files removed"

#storage->copy(from, to, overwrite?: bool) KScript.Storage

Copies a file or folder inside the folder. overwrite replaces what is at to.
ParameterTakes
from requiredany value
to requiredany value
overwrite optionala flag: overwrite or not overwrite

Arguments can be named: ->copy(…, to: …, overwrite: …).

Example
<do>@storage(data)->write('a.txt', 'hi'); @storage(data)->copy('a.txt', 'b.txt', overwrite)</do>
<echo>{ @storage(data)->read('b.txt') }</echo>
@storage(data)->write('a.txt', 'hi')
@storage(data)->copy('a.txt', 'b.txt', overwrite)
echo @storage(data)->read('b.txt')

#storage->delete(name) → bool KScript.Storage

Deletes a file, or a folder and everything in it. True if something was deleted.
Example
<do>@storage(cache)->delete('thumbnails')</do>
@storage(cache)->delete('thumbnails')

#storage->exists(name?) → bool KScript.Storage

True if the file (or, without a name, the folder) is there.
Example
<echo>{ @storage(config)->exists('settings.json') }</echo>
echo @storage(config)->exists('settings.json')

#storage->file(name) → file KScript.Storage

A file in the folder as a file value, with .size, .modified, .mime and the file methods.
Example
<do>@storage(data)->write('notes.txt', 'Buy milk')</do>
<echo>{ @storage(data)->file('notes.txt').size } bytes</echo>
@storage(data)->write('notes.txt', 'Buy milk')
echo "{ @storage(data)->file('notes.txt').size } bytes"

#storage->folder(name) → storage KScript.Storage

A folder inside this one, with the same methods.
Example
<let name="notes" value="@storage(data)->folder('notes')" />
<do>$notes->write('today.txt', 'Buy milk')</do>
$notes = @storage(data)->folder('notes')
$notes->write('today.txt', 'Buy milk')

#storage->list(pattern?, recursive?: bool) → list KScript.Storage

The names of the files in the folder, such as ['notes/a.txt', 'settings.json']. pattern is a wildcard like '.json', or a regex like /^notes/..md$/ matched against the whole name.
ParameterTakes
pattern optionalany value
recursive optionala flag: recursive or not recursive

Arguments can be named: ->list(…, recursive: …).

Example
<for each="name" in="@storage(data)->list('*.txt', recursive)"><echo>{ $name }</echo></for>
for name in @storage(data)->list('*.txt', recursive) {
  echo $name
}

#storage->move(from, to, overwrite?: bool) KScript.Storage

Moves (or renames) a file or folder inside the folder. overwrite replaces what is at to.
ParameterTakes
from requiredany value
to requiredany value
overwrite optionala flag: overwrite or not overwrite

Arguments can be named: ->move(…, to: …, overwrite: …).

Example
<do>@storage(data)->write('draft.txt', 'hi'); @storage(data)->move('draft.txt', 'final.txt')</do>
<echo>{ @storage(data)->exists('final.txt') }</echo>
@storage(data)->write('draft.txt', 'hi')
@storage(data)->move('draft.txt', 'final.txt')
echo @storage(data)->exists('final.txt')

#storage->path_of(name) → string KScript.Storage

The full path of a file in the folder. The folder's own path is the field .path.
Example
<echo>{ @storage(data)->path_of('notes/today.txt') }</echo>
echo @storage(data)->path_of('notes/today.txt')

#storage->read(name, default?) → string KScript.Storage

A file's text, or default (null) when it isn't there yet.

Arguments can be named: ->read(…, default: …).

Example
<echo>{ @storage(data)->read('notes.txt', default: '') }</echo>
echo @storage(data)->read('notes.txt', default: '')

#storage->read_bytes(name, default?) → base64 KScript.Storage

Every byte of a file as a base64 value, or default (null) when it isn't there yet.

Arguments can be named: ->read_bytes(…, default: …).

Example
<do>@storage(data)->write('logo.txt', 'hi')</do>
<echo>{ @storage(data)->read_bytes('logo.txt') }</echo>
@storage(data)->write('logo.txt', 'hi')
echo @storage(data)->read_bytes('logo.txt')

#storage->read_json(name, default?, as?: type) → any KScript.Storage

Reads JSON and checks it against the optional as type, mapping a list item by item. A missing file returns default (null if omitted) as supplied.
ParameterTakes
name requiredany value
default optionalany value
as optionaltype

Arguments can be named: ->read_json(…, default: …, as: …).

Example
<type name="Settings" fields="theme: string" />
<let name="settings" value="@storage(config)->read_json('settings.json', as: Settings)" />
<echo>{ $settings?.theme ?? 'light' }</echo>
type Settings {
  theme: string
}
$settings = @storage(config)->read_json('settings.json', as: Settings)
echo $settings?.theme ?? 'light'

#storage->write(name, text) KScript.Storage

Writes text to a file, replacing it safely. Folders are created as needed.

Arguments can be named: ->write(…, text: …).

Example
<do>@storage(data)->write('notes/today.txt', 'Buy milk')</do>
@storage(data)->write('notes/today.txt', 'Buy milk')

#storage->write_bytes(name, bytes: base64) KScript.Storage

Writes the exact bytes of a base64 value (an upload's .bytes, say) to a file, replacing it safely.
ParameterTakes
name requiredany value
bytes requiredbase64

Arguments can be named: ->write_bytes(…, bytes: …).

Example
<do>@storage(data)->write_bytes('avatars/1.bin', 'hi'->to_base64())</do>
@storage(data)->write_bytes('avatars/1.bin', 'hi'->to_base64())

#storage->write_json(name, value) KScript.Storage

Writes a value to a file as indented JSON, replacing it safely.

Arguments can be named: ->write_json(…, value: …).

Example
<do>@storage(config)->write_json('settings.json', { theme: 'dark', size: 14 })</do>
@storage(config)->write_json('settings.json', { theme: 'dark', size: 14 })

Methods from any