npm install -g @johnlindquist/kit | 全局安装Script Kitkit | 启动Script Kit界面~/.kit | 默认脚本存储目录import "@johnlindquist/kit";
let name = await arg("请输入名字");
await showToast(`你好,${name}!`);
await npm("package") | 安装并导入包appInstallMultiple(pkgs) | 批量安装包installMissingPackage(pkg) | 条件安装npmInstall(pkg) | 仅安装不导入import "@johnlindquist/kit";
// 安装并导入
let lodash = await npm("lodash")
let axios = await npm("axios")
// 批量安装
await appInstallMultiple(["axios", "lodash", "dayjs"])
// 条件安装(仅当缺失时)
await installMissingPackage("lodash")
// 仅安装不导入
await npmInstall("package-name")
// 作用域包
let kit = await npm("@johnlindquist/kit")
let babel = await npm("@babel/core")
await arg(prompt) | 文本输入框await select(options) | 选择列表await confirm(message) | 确认对话框await multi-select(options) | 多选列表import "@johnlindquist/kit";
// 文本输入
let name = await arg("请输入你的名字:")
// 单选
let choice = await select(["选项1", "选项2", "选项3"])
// 确认
let confirmed = await confirm("确定继续吗?")
// 多选
let tools = await multi-select(["git", "npm", "docker", "k8s"])
// 带默认值
let email = await arg("邮箱:", {defaultValue: "user@example.com"})
await showToast(message) | 桌面通知await notify(title, body) | 系统通知await div(html) | HTML内容显示await md(content) | Markdown显示import "@johnlindquist/kit";
// 简单通知
await showToast("任务完成!")
// 系统通知
await notify("任务状态", "构建任务已完成,耗时2分钟")
// HTML内容
await div("HTML标题
段落内容
")
// Markdown内容
await md(`
# 标题
- 列表项1
- 列表项2
**粗体文本**
`)
// 带进度条
let progress = 0
let interval = setInterval(async () => {
progress += 10
await showToast(`进度:${progress}%`)
if (progress >= 100) clearInterval(interval)
}, 1000)
await readFile(path) | 读取文件await writeFile(path, content) | 写入文件await getFiles(pattern) | 文件搜索await pathExists(path) | 路径检查import "@johnlindquist/kit";
// 读取文件
let content = await readFile("config.json")
let data = JSON.parse(content)
// 写入文件
await writeFile("output.txt", "Hello World!")
// 搜索文件
let jsFiles = await getFiles("*.js", { recursive: true })
let allFiles = await getFiles("**/*", { includeDirectories: true })
// 路径操作
let configPath = path.join(home(), ".config", "app.json")
let exists = await pathExists(configPath)
// 读取JSON
let packageJson = await readJson("package.json")
await writeJson("package-backup.json", packageJson)
await clipboard.read() | 读取剪贴板await clipboard.write(text) | 写入剪贴板await clipboard.readText() | 读文本内容await clipboard.readImage() | 读图像内容import "@johnlindquist/kit";
// 读取剪贴板
let clipboardContent = await clipboard.read()
await showToast(`剪贴板内容:${clipboardContent}`)
// 写入剪贴板
await clipboard.write("Hello from Script Kit!")
// 处理复制内容
let text = await arg("选择要复制的文本:")
await clipboard.write(text)
await showToast("内容已复制到剪贴板")
// 剪贴板格式转换
let content = await clipboard.read()
await clipboard.write(content.toUpperCase())
// JSON内容处理
let jsonData = await clipboard.read()
try {
let parsed = JSON.parse(jsonData)
await clipboard.write(JSON.stringify(parsed, null, 2))
await showToast("JSON格式化完成")
} catch {
await showToast("无效的JSON格式")
}
env.VAR_NAME | 访问环境变量home() | 获取用户主目录cwd() | 获取当前工作目录exec(command) | 执行系统命令import "@johnlindquist/kit";
// 环境变量访问
let apiKey = env.API_KEY || "default-key"
let nodeEnv = env.NODE_ENV || "development"
// 设置环境变量
env.NODE_ENV = "production"
env.DEBUG = "true"
// 目录操作
let homeDir = home()
let cwdPath = cwd()
let projectPath = path.join(homeDir, "projects")
// 执行系统命令
let gitStatus = await exec("git status")
await showToast(div(`${gitStatus}`))
// 带参数的命令
let version = await exec("node --version")
await showToast(`Node.js版本:${version.trim()}`)
// 开发环境检查
let configs = {
git: path.join(home(), ".gitconfig"),
ssh: path.join(home(), ".ssh"),
npm: path.join(home(), ".npmrc")
}
let missing = []
for (let [name, configPath] of Object.entries(configs)) {
if (!await pathExists(configPath)) {
missing.push(name)
}
}
if (missing.length > 0) {
await showToast(`缺少配置:${missing.join(", ")}`)
}
await degit(template) | 克隆模板await readJson(path) | 读取JSON文件await writeJson(path, data) | 写入JSON文件await copy(source, target) | 复制文件import "@johnlindquist/kit";
async function createProject(projectName, template) {
console.log(`创建 ${projectName} 从 ${template}...`)
try {
// 克隆模板
await degit(template).clone(`./${projectName}`)
// 更新 package.json
let pkgPath = path.join(projectName, 'package.json')
let pkg = await readJson(pkgPath)
pkg.name = projectName
pkg.version = "1.0.0"
pkg.author = "Your Name"
await writeJson(pkgPath, pkg, { spaces: 2 })
// 安装依赖
await exec(`cd ${projectName} && npm install`)
// 初始化Git
await exec(`cd ${projectName} && git init`)
console.log(`✅ 项目 ${projectName} 创建成功!`)
await showToast(`项目${projectName}已创建`)
} catch (error) {
console.error('创建失败:', error)
await showToast(`创建失败:${error.message}`)
}
}
// 使用示例
let projectName = await arg("项目名称:")
let template = await select([
"vite/vanilla",
"vite/vue-ts",
"vite/react-ts",
"create-next-app"
])
await createProject(projectName, template)