@lark-project/cli
Version:
飞书项目插件开发工具
73 lines (72 loc) • 2.85 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = require("fs-extra");
const validate_point_schema_1 = require("../../../utils/validate-point-schema");
/**
* Validate `point.config.local.json` against the plugin schema.
*
* Why native: weak AI keeps fabricating prop_type enums (e.g. `multiSelect`
* camelCase, `workItemTypeWithField` not in enum) and skipping required
* fields (e.g. `multi_select` missing `options`). Letting AI ask the user
* to spot these is the wrong end of the loop — the schema is authoritative,
* so we surface schema errors mechanically.
*
* Output sinks (intentional split, do not "normalize"):
* - text: human path → result + count summary on stdout, fatal causes on stderr
* - json: structured `{ valid, errors: [{ path, message }] }` on stdout for AI auto-parse
*
* Exit 0 when valid, 1 on any error (file missing / bad JSON / schema
* mismatch).
*/
async function validate(file, opts) {
const format = opts.format === 'json' ? 'json' : 'text';
const filePath = file || 'point.config.local.json';
const absPath = path_1.default.resolve(process.cwd(), filePath);
if (!(0, fs_extra_1.existsSync)(absPath)) {
emitFatal(format, `file not found: ${filePath}`);
process.exit(1);
}
let config;
try {
config = JSON.parse((0, fs_extra_1.readFileSync)(absPath, 'utf8'));
}
catch (e) {
emitFatal(format, `invalid JSON in ${filePath}: ${e.message}`);
process.exit(1);
}
let result;
try {
result = await (0, validate_point_schema_1.validatePointConfig)(config);
}
catch (e) {
emitFatal(format, `schema unavailable: ${e.message}`);
process.exit(1);
}
if (format === 'json') {
const formatted = (0, validate_point_schema_1.formatValidationErrors)(result, 'json');
process.stdout.write(formatted.stdout + '\n');
process.exit(result.valid ? 0 : 1);
}
if (result.valid) {
process.stdout.write(`ok (${filePath} valid)\n`);
process.exit(0);
}
const formatted = (0, validate_point_schema_1.formatValidationErrors)(result, 'text');
process.stdout.write(formatted.stdout + '\n');
process.stdout.write(`\n${formatted.structured.length} validation error(s) in ${filePath}.\n`);
process.exit(1);
}
exports.validate = validate;
function emitFatal(format, message) {
if (format === 'json') {
process.stdout.write(JSON.stringify({ valid: false, errors: [{ path: '', message }] }) + '\n');
}
else {
process.stderr.write(`${message}\n`);
}
}