KScript 文档
ZH

项目与 kscript.json

项目设置:kscript.json

kscript serve、kscript check、kscript fmt、kscript make 以及编辑器会读取其工作文件夹或上级文件夹中最近的 kscript.json,因此无论在项目的哪个位置,它们的行为都一致。每个设置都是可选的:

JSON
{
  "name": "my-site",
  "kscript": "0.51.0",                             // the KScript release the project is written for (see below)
  "timezone": "Europe/London",                     // the zone stored dates are in (see docs/data.md); this is the default
  "standard": "kscript:recommended",               // lint rules and format settings to start from (see Standards)
  "packages": ["KScript.Terminal", "KScript.IO"],   // imported by every script in the project
  "extensions": ["extensions", "shared/**/*.kscript"], // where the project's extend files are (default: extensions/)
  "natives": ["natives"], // where the project's native types are, used without an import (default: natives/)
  "serve": {
    "root": ".",          // the site folder, relative to kscript.json
    "port": 8080,         // --port and --host on the command line still win
    "host": "localhost",
    "docs": true          // also serve the docs, with a page about this project (see docs/web.md)
  },
  "http": {
    "middleware": ["log"],                     // routes, middleware and their settings (see docs/http.md)
    "groups": { "api": ["cors", "throttle"] }
  },
  "lint": {
    "include": ["**/*.html", "**/*.md", "**/*.kscript"],  // what `kscript check` looks at
    "exclude": ["_templates", "node_modules", "drafts/**"], // never checked, on the command line or in the editor
    "rules": { "KS3003": "off", "KS4*": "error" },          // off, hint, info, warning or error, by code or prefix
    "warnings_as_errors": false                               // same as kscript check --warnings-as-errors
  },
  "test": {
    "include": ["tests/**/*.test.kscript"],  // what `kscript test` runs (default: **/*.test.kscript and **/*.test.ks.xml)
    "exclude": ["tests/slow"],
    "timeout": 10,                          // seconds per test, 0 for no limit
    "packages": ["KScript.Terminal"],       // imported by test files only
    "env": "test"                           // the environment tests run in, so kscript.test.json applies
  },
  "format": {
    "indent": 2,           // spaces per level, or "tab"; used by kscript fmt and Format Document
    "print_width": 80,      // the longest a line should be; longer lines are wrapped
    "rules": { "statement_per_line": false },  // any rule can be turned off
    "exclude": ["vendor"]  // see docs/formatting.md for every format setting
  },
  "app": {
    "main": "app.kscript",      // what `kscript run` and `kscript compile` start from (default: main.kscript, then index.kscript)
    "main_function": "start",   // the function a script runs by itself after its top-level code (default: main)
    "name": "My App",           // shown in the Dock, Start menu or launcher (default: the project's name)
    "id": "com.example.myapp",  // the macOS bundle id
    "version": "1.0.0",
    "icon": "icon.png",         // a .png, .ico or .icns, used for every platform
    "user_config": true         // lay the person's own config.json (KScript.Storage) over $config
  },
  "types": {
    "uuid": { "v": 4 },                     // arguments for every use of a built-in type (see Types and validation)
    "password": { "min": 12 }
  },
  "config": {
    "app": { "name": "Shop" },              // the app's own settings: $config.app.name
    "database": { "connections": { "main": { "url": "sqlite:data/shop.sqlite" } } }
  }
}
  • 不含 / 的模式(例如 _templates 或 *.draft.html)会匹配任意位置同名的文件或文件夹。否则,它会与来自 kscript.json 的路径进行匹配:* 只在一个文件夹内匹配,** 可跨越多个文件夹,文件夹则匹配其中的所有内容。
  • kscript 指定项目所用的 KScript 发行版:精确的版本如 "0.51.0",或 "0.51" 表示最新的 0.51.x 版本。项目中的每个 kscript 命令都会运行该版本,首次使用时会自动下载;会自动获取 KScript 的编辑器(如 FlowIDE)也是如此。kscript version pin 0.51 会写入该版本,kscript version unpin 会将其移除(参见 切换版本)。若未设置,项目将使用计算机上的版本。
  • standard 选择项目的 lint 和 format 设置所基于的 标准:kscript:recommended(默认)、kscript:strict,或你可以在项目之间共享的自定义标准文件。在 kscript.json 中直接设置的内容优先于标准。kscript check --print-config 会显示最终结果以及每个设置的来源。
  • rules 用于更改诊断的严重程度(参见代码列表)。采用最长匹配规则,因此 "KS3*": "warning" 配合 "KS3001": "error" 会将未知元素保留为错误。
  • 注释和末尾逗号均被允许。未知设置会以警告形式报告,而不会被静默忽略;VS Code 扩展会校验文件,并在您输入时补全设置。
  • format 设置 kscript fmt 和编辑器“格式化文档”的样式,format.rules 用于关闭单个规则。所有设置及忽略注释请参见 格式化。
  • 在项目内部,即使从子文件夹运行 kscript make page about,也会在站点根目录中创建页面。
  • config 保存应用自身的设置,以 $config 形式读取;kscript.json 旁边的 kscript.<env>.json 和 config.kscript 会覆盖在其之上(参见 设置)。
  • kscript serve 从不提供 kscript.json、kscript.<env>.json、.env 文件,也不提供 config.* JSON 文件。
  • http 为 KScript.Http 配置路由文件、中间件、CORS、身份验证和速率限制(参见 路由、控制器和中间件)。
  • types 为项目中所有位置的 uuid、email、password 等内置类型提供相同的参数。在使用类型处写下的参数(如 uuid(v: 8))优先生效(参见 类型与校验)。
  • app.name(或 name)同时用于命名应用自身的设置、数据和缓存文件夹(参见 应用文件夹)。
  • queue 指定作业等待的连接、工作进程检查的队列,以及失败作业的保留时长(参见 队列与作业)。
  • app 由 kscript compile 读取,它将脚本(通常是桌面应用)构建为适用于 macOS、Windows 和 Linux 的程序(参见 程序)。