@@ -2,92 +2,86 @@ import { fs } from 'zx'
22import { exec , log } from './core.ts'
33
44/**
5- * Generate Deptrac image and add to git
5+ * Result of artifact generation
66 */
7- export async function generateDeptracImage ( ) : Promise < void > {
7+ export interface ArtifactResult {
8+ generated : boolean
9+ changed : boolean
10+ path ?: string
11+ }
12+
13+ /**
14+ * Generate Deptrac architecture diagram
15+ *
16+ * Generates a PNG image showing the dependency graph.
17+ * Does NOT auto-commit - leaves that to the developer or CI.
18+ */
19+ export async function generateDeptracImage ( ) : Promise < ArtifactResult > {
820 // Check if deptrac is installed
921 if ( ! ( await fs . pathExists ( './vendor/bin/deptrac' ) ) ) {
10- return
22+ log . skip ( 'Deptrac not installed, skipping image generation' )
23+ return { generated : false , changed : false }
1124 }
1225
1326 try {
14- // Use graphviz-image formatter to generate PNG directly
27+ log . tool ( 'Deptrac' , 'Generating architecture diagram...' )
28+
1529 await exec (
1630 [ './vendor/bin/deptrac' , '--formatter=graphviz-image' , '--output=deptrac.png' ] ,
1731 { type : 'php' } ,
1832 )
19- if ( await fs . pathExists ( './deptrac.png' ) ) {
20- await exec ( [ 'git' , 'add' , 'deptrac.png' ] , { quiet : true } )
2133
22- // Check if there are staged changes for the image
23- try {
24- await exec ( [ 'git' , 'diff' , '--cached' , '--quiet' , 'deptrac.png' ] , { quiet : true } )
25- } catch {
26- // Changes detected, commit them
27- await exec ( [ 'git' , 'commit' , '-m' , 'chore: update deptrac image' ] )
28- log . success ( 'Deptrac image updated and committed' )
29- }
34+ if ( await fs . pathExists ( './deptrac.png' ) ) {
35+ log . success ( 'Deptrac image generated: deptrac.png' )
36+ return { generated : true , changed : true , path : 'deptrac.png' }
3037 }
38+
39+ return { generated : false , changed : false }
3140 } catch ( error : unknown ) {
32- // Image generation is optional, don't fail if it doesn't work
3341 const errorMessage = error instanceof Error ? error . message : String ( error )
3442 log . warn ( `Deptrac image generation failed: ${ errorMessage } ` )
43+ return { generated : false , changed : false }
3544 }
3645}
3746
3847/**
39- * Generate API documentation if OpenAPI spec has changed
48+ * Generate API documentation from OpenAPI annotations
49+ *
50+ * Generates both the YAML spec and HTML documentation.
51+ * Does NOT auto-commit - leaves that to the developer or CI.
4052 */
41- export async function generateApiDocs ( ) : Promise < void > {
53+ export async function generateApiDocs ( ) : Promise < ArtifactResult > {
54+ // Check if swagger-php is installed
55+ if ( ! ( await fs . pathExists ( './vendor/bin/openapi' ) ) ) {
56+ log . skip ( 'swagger-php not installed, skipping API docs generation' )
57+ return { generated : false , changed : false }
58+ }
59+
4260 log . tool ( 'API Documentation' , 'Generating OpenAPI specification...' )
43- try {
44- // Check if swagger-php is installed by looking for the binary
45- // This avoids reading/parsing composer.lock
46- if ( await fs . pathExists ( './vendor/bin/openapi' ) ) {
47- await exec ( [ 'composer' , 'generate-api-spec' ] , { type : 'php' } )
4861
49- const diffResult = await exec ( [ 'git' , 'diff' , '--name-only' ] , { quiet : true } )
50- const modifiedFiles = diffResult . toString ( ) . trim ( ) . split ( '\n' )
62+ try {
63+ await exec ( [ 'composer' , 'generate-api-spec' ] , { type : 'php' } )
64+ log . success ( 'OpenAPI specification generated' )
5165
52- if ( modifiedFiles . includes ( 'openapi/openapi.yml' ) ) {
53- log . info ( 'API spec changed, regenerating HTML...' )
66+ // Check if spec changed
67+ const diffResult = await exec ( [ 'git' , 'diff' , '--name-only' ] , { quiet : true } )
68+ const modifiedFiles = diffResult . toString ( ) . trim ( ) . split ( '\n' )
5469
55- try {
56- await exec ( [ 'pnpm' , 'generate:api-doc:html' ] )
57- log . success ( 'HTML documentation generated' )
70+ if ( modifiedFiles . includes ( 'openapi/openapi.yml' ) ) {
71+ log . info ( 'API spec changed, regenerating HTML...' )
5872
59- // Stage the generated files
60- await exec (
61- [ 'git' , 'add' , 'openapi/openapi.html' , 'openapi/openapi.yml' ] ,
62- { quiet : true } ,
63- )
73+ await exec ( [ 'pnpm' , 'generate:api-doc:html' ] )
74+ log . success ( 'HTML documentation generated' )
6475
65- // Check if there are staged changes and commit them
66- try {
67- await exec ( [ 'git' , 'diff' , '--cached' , '--quiet' ] , { quiet : true } )
68- // If we get here, there are no staged changes
69- log . info ( 'No staged changes for API documentation' )
70- } catch {
71- // There are staged changes, commit them
72- await exec ( [ 'git' , 'commit' , '-m' , 'chore: update API documentation' ] )
73- log . success ( 'API documentation committed' )
74- }
75- } catch ( error : unknown ) {
76- const errorMessage = error instanceof Error ? error . message : String ( error )
77- log . error ( `HTML documentation generation failed: ${ errorMessage } ` )
78- throw error
79- }
80- } else {
81- log . info ( 'No changes to OpenAPI specification, skipping HTML generation' )
82- }
83- } else {
84- log . info ( 'swagger-php not installed -> skipping API docs.' )
76+ log . info ( 'Artifacts generated. Remember to commit: openapi/openapi.yml, openapi/openapi.html' )
77+ return { generated : true , changed : true , path : 'openapi/openapi.yml' }
8578 }
79+
80+ log . info ( 'No changes to OpenAPI specification' )
81+ return { generated : true , changed : false }
8682 } catch ( error : unknown ) {
8783 const errorMessage = error instanceof Error ? error . message : String ( error )
88- log . error ( `API spec generation failed: ${ errorMessage } ` )
89- // Don't throw, just log error, as this might be optional?
90- // Original pre-push returned false on error.
84+ log . error ( `API documentation generation failed: ${ errorMessage } ` )
9185 throw error
9286 }
9387}
0 commit comments