From 11e2bc34a426cb6304a5a763d46aa0b2f89565b1 Mon Sep 17 00:00:00 2001 From: Thomas Kelly Date: Wed, 29 Jan 2025 09:22:26 -0500 Subject: [PATCH 1/2] fix: Add commit hash to manifest to know what was installed --- src/commands/theme/component/map.ts | 7 +++-- src/utilities/flags.ts | 1 + src/utilities/git.ts | 19 ++++++++++++++ src/utilities/types.ts | 3 ++- test/commands/theme/component/map.test.ts | 31 +++++++++++++++++++++++ tsconfig.tsbuildinfo | 2 +- 6 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/commands/theme/component/map.ts b/src/commands/theme/component/map.ts index 3405203b..b4938499 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] = { + version: collectionVersion, + commit: getLastCommitHash(collectionDir) + } 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..5ac4e34c 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): string | null { + try { + const result = execSync('git rev-parse HEAD', { + cwd: path.resolve(directory), + encoding: 'utf-8', + stdio: ['pipe', 'pipe', 'ignore'] + }) + return result.trim() + } catch (error) { + return null + } } \ No newline at end of file diff --git a/src/utilities/types.ts b/src/utilities/types.ts index e04c5782..2f0fb161 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; + version: string; + commit: string | null; }; } files: { diff --git a/test/commands/theme/component/map.test.ts b/test/commands/theme/component/map.test.ts index 6c8e5d20..0f3a4fca 100644 --- a/test/commands/theme/component/map.test.ts +++ b/test/commands/theme/component/map.test.ts @@ -3,6 +3,7 @@ import {expect} from 'chai' import * as fs from 'node:fs' import * as path from 'node:path' import {fileURLToPath} from 'node:url' +import {execSync} from 'node:child_process' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const fixturesPath = path.join(__dirname, '../../../fixtures') @@ -310,4 +311,34 @@ 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: 'utf-8' + }).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) + }) + + it('sets commit to null when not in a git repository', async () => { + // Ensure we're not in a git repo + fs.rmSync(path.join(testCollectionPath, '.git'), { recursive: true, force: true }) + + 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.be.null + }) }) 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 From 0f58cb1666ffd5e40d354179dcdf6d48c59eb595 Mon Sep 17 00:00:00 2001 From: Thomas Kelly Date: Wed, 29 Jan 2025 10:25:25 -0500 Subject: [PATCH 2/2] Clean up linting errors --- src/commands/theme/component/map.ts | 4 ++-- src/utilities/git.ts | 6 +++--- src/utilities/types.ts | 2 +- test/commands/theme/component/map.test.ts | 14 ++------------ 4 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/commands/theme/component/map.ts b/src/commands/theme/component/map.ts index b4938499..bde02f5a 100644 --- a/src/commands/theme/component/map.ts +++ b/src/commands/theme/component/map.ts @@ -76,8 +76,8 @@ export default class Manifest extends BaseCommand { manifest.files = sortObjectKeys(files) manifest.collections[collectionName] = { - version: collectionVersion, - commit: getLastCommitHash(collectionDir) + commit: getLastCommitHash(collectionDir), + version: collectionVersion } fs.writeFileSync(manifestPath, JSON.stringify(sortObjectKeys(manifest), null, 2)) diff --git a/src/utilities/git.ts b/src/utilities/git.ts index 5ac4e34c..7cbb362b 100644 --- a/src/utilities/git.ts +++ b/src/utilities/git.ts @@ -10,15 +10,15 @@ export async function cloneTheme(repoUrl: string, targetDir: string): Promise { // Get the commit hash we just created const expectedHash = execSync('git rev-parse HEAD', { cwd: testCollectionPath, - encoding: 'utf-8' + encoding: 'utf8' }).trim() await runCommand(['theme', 'component', 'map', testThemePath]) @@ -331,14 +331,4 @@ describe('theme component map', () => { const data = JSON.parse(fs.readFileSync(path.join(testThemePath, 'component.manifest.json'), 'utf8')) expect(data.collections['@archetype-themes/test-collection'].commit).to.equal(expectedHash) }) - - it('sets commit to null when not in a git repository', async () => { - // Ensure we're not in a git repo - fs.rmSync(path.join(testCollectionPath, '.git'), { recursive: true, force: true }) - - 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.be.null - }) })