summaryrefslogtreecommitdiff
path: root/scripts/lib/skill-resources.js
blob: 5b1e32c003dad9ba4bcf2297bdd37cd5d37bdf27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';

const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');

function stageSkillResources(root, skillName) {
  if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(skillName)) throw new Error('invalid skill name');
  root = fs.realpathSync(root);
  const staged = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-skill-resources-'));
  const skillDir = path.join(staged, 'skills', skillName);
  const sourceEntry = (file) => {
    const entry = fs.lstatSync(file);
    if (entry.isSymbolicLink()) throw new Error(`symbolic link in skill resources: ${file}`);
    return entry;
  };
  const copy = (source, destination) => fs.cpSync(source, destination,
    { recursive: true, filter: (file) => Boolean(sourceEntry(file)) });
  try {
    // Retain the repository layout: both local and ../../references/ links work.
    fs.mkdirSync(path.dirname(skillDir), { recursive: true });
    sourceEntry(path.join(root, 'skills'));
    copy(path.join(root, 'skills', skillName), skillDir);
    const shared = path.join(root, 'references');
    let sharedEntry;
    try { sharedEntry = sourceEntry(shared); } catch (error) { if (error.code !== 'ENOENT') throw error; }
    if (sharedEntry) copy(shared, path.join(staged, 'references'));
    return { root: staged, skillDir };
  } catch (error) {
    fs.rmSync(staged, { recursive: true, force: true });
    throw error;
  }
}

module.exports = { stageSkillResources };