diff --git a/src/commands/theme/component/map.ts b/src/commands/theme/component/map.ts index 3405203b..bde02f5a 100644 --- a/src/commands/theme/component/map.ts +++ b/src/commands/theme/component/map.ts @@ -11,6 +11,7 @@ import path from 'node:path' import Args from '../../../utilities/args.js' import BaseCommand from '../../../utilities/base-command.js' import Flags from '../../../utilities/flags.js' +import { getLastCommitHash } from '../../../utilities/git.js' import { ManifestOptions, generateManifestFiles, getManifest } from '../../../utilities/manifest.js' import { getNameFromPackageJson, getVersionFromPackageJson } from '../../../utilities/package-json.js' @@ -74,8 +75,10 @@ export default class Manifest extends BaseCommand { ) manifest.files = sortObjectKeys(files) - manifest.collections[collectionName] = manifest.collections[collectionName] || {} - manifest.collections[collectionName].version = collectionVersion + manifest.collections[collectionName] = { + commit: getLastCommitHash(collectionDir), + version: collectionVersion + } fs.writeFileSync(manifestPath, JSON.stringify(sortObjectKeys(manifest), null, 2)) } diff --git a/src/utilities/flags.ts b/src/utilities/flags.ts index da648ad2..bf5d7d13 100644 --- a/src/utilities/flags.ts +++ b/src/utilities/flags.ts @@ -120,6 +120,7 @@ export const flagDefinitions: Record = { }), [Flags.SETUP_FILES]: OclifFlags.boolean({ + allowNo: true, char: 's', default: true, description: 'copy setup files to theme directory', diff --git a/src/utilities/git.ts b/src/utilities/git.ts index 47b26d0b..7cbb362b 100644 --- a/src/utilities/git.ts +++ b/src/utilities/git.ts @@ -1,5 +1,24 @@ import {execSync} from 'node:child_process' +import path from 'node:path' export async function cloneTheme(repoUrl: string, targetDir: string): Promise { execSync(`git clone ${repoUrl} ${targetDir}`, { stdio: 'inherit' }) +} + +/** + * Gets the last commit hash for a given directory + * @param directory The directory to get the commit hash for + * @returns The last commit hash or null if not in a git repository + */ +export function getLastCommitHash(directory: string): null | string { + try { + const result = execSync('git rev-parse HEAD', { + cwd: path.resolve(directory), + encoding: 'utf8', + stdio: ['pipe', 'pipe', 'ignore'] + }) + return result.trim() + } catch { + return null + } } \ No newline at end of file diff --git a/src/utilities/types.ts b/src/utilities/types.ts index e04c5782..8d5efd37 100644 --- a/src/utilities/types.ts +++ b/src/utilities/types.ts @@ -35,7 +35,8 @@ export interface PackageJSON { export interface Manifest { collections: { [name: string]: { - [version: string]: string; + commit: null | string; + version: string; }; } files: { diff --git a/test/commands/theme/component/map.test.ts b/test/commands/theme/component/map.test.ts index 6c8e5d20..b924c78e 100644 --- a/test/commands/theme/component/map.test.ts +++ b/test/commands/theme/component/map.test.ts @@ -1,5 +1,6 @@ import {runCommand} from '@oclif/test' import {expect} from 'chai' +import {execSync} from 'node:child_process' import * as fs from 'node:fs' import * as path from 'node:path' import {fileURLToPath} from 'node:url' @@ -310,4 +311,24 @@ describe('theme component map', () => { expect(data.files.assets['commented-script.js']).to.be.undefined }) + + it('adds the last commit hash to the collection in the manifest', async () => { + // Initialize git repo in test collection + execSync('git init', { cwd: testCollectionPath }) + execSync('git config user.email "test@example.com"', { cwd: testCollectionPath }) + execSync('git config user.name "Test User"', { cwd: testCollectionPath }) + execSync('git add .', { cwd: testCollectionPath }) + execSync('git commit -m "Initial commit"', { cwd: testCollectionPath }) + + // Get the commit hash we just created + const expectedHash = execSync('git rev-parse HEAD', { + cwd: testCollectionPath, + encoding: 'utf8' + }).trim() + + await runCommand(['theme', 'component', 'map', testThemePath]) + + const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8')) + expect(data.collections['@archetype-themes/test-collection'].commit).to.equal(expectedHash) + }) }) diff --git a/tsconfig.tsbuildinfo b/tsconfig.tsbuildinfo index 398658f3..faf51a8d 100644 --- a/tsconfig.tsbuildinfo +++ b/tsconfig.tsbuildinfo @@ -1 +1 @@ -{"root":["./src/index.ts","./src/commands/theme/component/clean.ts","./src/commands/theme/component/copy.ts","./src/commands/theme/component/dev.ts","./src/commands/theme/component/index.ts","./src/commands/theme/component/install.ts","./src/commands/theme/component/map.ts","./src/commands/theme/generate/import-map.ts","./src/commands/theme/generate/template-map.ts","./src/utilities/args.ts","./src/utilities/base-command.ts","./src/utilities/files.ts","./src/utilities/flags.ts","./src/utilities/git.ts","./src/utilities/logger.ts","./src/utilities/manifest.ts","./src/utilities/nodes.ts","./src/utilities/package-json.ts","./src/utilities/types.ts"],"version":"5.7.2"} \ No newline at end of file +{"root":["./src/index.ts","./src/commands/theme/component/clean.ts","./src/commands/theme/component/copy.ts","./src/commands/theme/component/dev.ts","./src/commands/theme/component/index.ts","./src/commands/theme/component/install.ts","./src/commands/theme/component/map.ts","./src/commands/theme/generate/import-map.ts","./src/commands/theme/generate/template-map.ts","./src/utilities/args.ts","./src/utilities/base-command.ts","./src/utilities/files.ts","./src/utilities/flags.ts","./src/utilities/git.ts","./src/utilities/logger.ts","./src/utilities/manifest.ts","./src/utilities/nodes.ts","./src/utilities/objects.ts","./src/utilities/package-json.ts","./src/utilities/setup.ts","./src/utilities/types.ts"],"version":"5.7.2"} \ No newline at end of file