Files
ECC/scripts/lib/install-targets/claude-project.js
T

132 lines
3.6 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const {
createInstallTargetAdapter,
createRemappedOperation,
isForeignPlatformPath,
normalizeRelativePath,
} = require('./helpers');
const CLAUDE_ECC_NAMESPACE = 'ecc';
const CLAUDE_HOOKS_CONFIG_PATH = 'hooks/hooks.json';
function planClaudeHooksOperations(adapter, module, input) {
const sourceHooksRoot = path.join(input.repoRoot || '', 'hooks');
const operations = [
createRemappedOperation(
adapter,
module.id,
CLAUDE_HOOKS_CONFIG_PATH,
path.join(adapter.resolveRoot(input), 'settings.json'),
{
kind: 'update-claude-settings',
strategy: 'merge-hook-ids',
}
),
];
if (!input.repoRoot || !fs.existsSync(sourceHooksRoot)) {
return operations;
}
return [
...operations,
...fs.readdirSync(sourceHooksRoot, { withFileTypes: true })
.filter(entry => entry.name !== 'hooks.json')
.sort((left, right) => left.name.localeCompare(right.name))
.map(entry => adapter.createScaffoldOperation(
module.id,
path.join('hooks', entry.name),
input
)),
];
}
function getClaudeManagedDestinationPath(adapter, sourceRelativePath, input) {
const normalizedSourcePath = normalizeRelativePath(sourceRelativePath);
const targetRoot = adapter.resolveRoot(input);
if (normalizedSourcePath === 'rules') {
return path.join(targetRoot, 'rules', CLAUDE_ECC_NAMESPACE);
}
if (normalizedSourcePath.startsWith('rules/')) {
return path.join(
targetRoot,
'rules',
CLAUDE_ECC_NAMESPACE,
normalizedSourcePath.slice('rules/'.length)
);
}
if (normalizedSourcePath === 'skills') {
return path.join(targetRoot, 'skills');
}
if (normalizedSourcePath.startsWith('skills/')) {
return path.join(
targetRoot,
'skills',
normalizedSourcePath.slice('skills/'.length)
);
}
if (normalizedSourcePath === 'docs' || normalizedSourcePath.startsWith('docs/')) {
return path.join(targetRoot, normalizedSourcePath);
}
return null;
}
module.exports = createInstallTargetAdapter({
id: 'claude-project',
target: 'claude-project',
kind: 'project',
rootSegments: ['.claude'],
installStatePathSegments: ['ecc', 'install-state.json'],
nativeRootRelativePath: '.claude-plugin',
planOperations(input, adapter) {
const modules = Array.isArray(input.modules)
? input.modules
: (input.module ? [input.module] : []);
const planningInput = {
repoRoot: input.repoRoot,
projectRoot: input.projectRoot,
homeDir: input.homeDir,
};
return modules.flatMap(module => {
const paths = Array.isArray(module.paths) ? module.paths : [];
return paths
.filter(p => !isForeignPlatformPath(p, 'claude'))
.flatMap(sourceRelativePath => {
if (
module.id === 'hooks-runtime'
&& normalizeRelativePath(sourceRelativePath) === 'hooks'
) {
return planClaudeHooksOperations(adapter, module, planningInput);
}
const managedDestinationPath = getClaudeManagedDestinationPath(
adapter,
sourceRelativePath,
planningInput
);
if (managedDestinationPath) {
return [createRemappedOperation(
adapter,
module.id,
sourceRelativePath,
managedDestinationPath,
{ strategy: 'preserve-relative-path' }
)];
}
return [adapter.createScaffoldOperation(module.id, sourceRelativePath, planningInput)];
});
});
},
});