@lark-project/cli
Version:
飞书项目插件开发工具
61 lines (60 loc) • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.stateGet = exports.stateSet = void 0;
const fs_extra_1 = require("fs-extra");
const get_project_directory_1 = require("../../../utils/get-project-directory");
const workspace_1 = require("../../../v2/utils/workspace");
/**
* Read/write the workflow checkpoint state (`.lpm-cache/state.json`).
*
* Why a CLI command instead of the skill `cat >`-ing the file:
* the path is resolved through `workspacePaths()` (anchored to the plugin
* project root via `getProjectDirectory()`), so combined with `lpm --cwd <dir>`
* the state always lands inside the plugin's `.lpm-cache/` — never in whatever
* directory the shell happened to sit in. `assertPluginRoot` fail-fasts on a
* wrong directory with an actionable hint, so a stale skill doc can't silently
* write the checkpoint to the wrong place.
*/
/** Inline JSON, `@file`, or `@-` (stdin) — same convention as `lpm ai patch-json`. */
function parseStateValue(arg) {
let content = arg;
if (arg.startsWith('@')) {
const src = arg.slice(1);
content = (0, fs_extra_1.readFileSync)(src === '-' ? 0 : src, 'utf8');
}
try {
return JSON.parse(content);
}
catch (e) {
process.stderr.write(`state value is not valid JSON: ${e.message}\n`);
process.stderr.write('Tip: pass an inline JSON object, @file, or @- to read stdin.\n');
process.exit(1);
}
}
function stateSet(rawValue) {
const parsed = parseStateValue(rawValue);
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
process.stderr.write('state must be a JSON object\n');
process.exit(1);
}
const dir = (0, get_project_directory_1.getProjectDirectory)();
try {
(0, workspace_1.assertPluginRoot)(dir);
}
catch (e) {
process.stderr.write(`${e.message}\n`);
process.exit(1);
}
const p = (0, workspace_1.workspacePaths)(dir);
(0, fs_extra_1.ensureDirSync)(p.root);
(0, fs_extra_1.writeFileSync)(p.state, JSON.stringify(parsed, null, 2));
process.stdout.write(`${p.state}\n`);
}
exports.stateSet = stateSet;
function stateGet() {
const p = (0, workspace_1.workspacePaths)((0, get_project_directory_1.getProjectDirectory)());
if (!(0, fs_extra_1.existsSync)(p.state))
return;
process.stdout.write((0, fs_extra_1.readFileSync)(p.state, 'utf8'));
}
exports.stateGet = stateGet;