1- import { CreatePlan , Resource , ResourceSettings } from 'codify-plugin-lib' ;
1+ import { CreatePlan , DestroyPlan , Resource , ResourceSettings } from 'codify-plugin-lib' ;
22import { ResourceConfig } from 'codify-schemas' ;
33import path from 'node:path' ;
44
@@ -12,8 +12,7 @@ export interface GitCloneConfig extends ResourceConfig {
1212 autoVerifySSH : boolean
1313 directory ?: string ,
1414 parentDirectory ?: string ,
15- remote ?: string ,
16- repository ?: string ,
15+ repository : string ,
1716}
1817
1918export class GitCloneResource extends Resource < GitCloneConfig > {
@@ -22,65 +21,52 @@ export class GitCloneResource extends Resource<GitCloneConfig> {
2221 id : 'git-clone' ,
2322 schema : Schema ,
2423 parameterSettings : {
25- autoVerifySSH : { default : true } ,
24+ parentDirectory : { type : 'directory' } ,
25+ directory : { type : 'directory' } ,
26+ autoVerifySSH : { type : 'setting' , default : true } ,
2627 } ,
27- }
28- }
29-
30- override async validate ( parameters : Partial < GitCloneConfig > ) : Promise < void > {
31- if ( parameters . parentDirectory && parameters . directory ) {
32- throw new Error ( 'Cannot specify both parentDirectory and directory together' )
33- }
34-
35- if ( parameters . remote && parameters . repository ) {
36- throw new Error ( 'Cannot specify both remote and repository together' )
28+ import : {
29+ requiredParameters : [ 'directory' ]
30+ }
3731 }
3832 }
3933
4034 override async refresh ( parameters : Partial < GitCloneConfig > ) : Promise < Partial < GitCloneConfig > | null > {
41- const repositoryUrl = parameters . repository ?? parameters . remote ! ;
42-
4335 if ( parameters . parentDirectory ) {
44- const parentDirectory = path . resolve ( untildify ( parameters . parentDirectory ) ) ;
45-
46- const folderName = this . extractBasename ( repositoryUrl ) ;
36+ const folderName = this . extractBasename ( parameters . repository ! ) ;
4737 if ( ! folderName ) {
4838 throw new Error ( 'Invalid git repository or remote name. Un-able to parse' ) ;
4939 }
5040
51- const fullPath = path . join ( parentDirectory , folderName ) ;
41+ const fullPath = path . join ( parameters . parentDirectory , folderName ) ;
5242
5343 const exists = await FileUtils . checkDirExistsOrThrowIfFile ( fullPath ) ;
5444 if ( ! exists ) {
5545 return null ;
5646 }
5747
5848 const { data : url } = await codifySpawn ( 'git config --get remote.origin.url' , { cwd : fullPath } ) ;
59- if ( this . extractBasename ( url ) !== folderName ) {
60- console . log ( this . extractBasename ( url ) )
61- console . log ( folderName )
62- throw new Error ( `Folder found at location: '${ fullPath } '. However the remote url '${ url } ' repo does not match desired repo '${ repositoryUrl } '` ) ;
63- }
6449
65- return parameters ;
50+ return {
51+ parentDirectory : parameters . parentDirectory ,
52+ repository : url . trim ( ) ,
53+ autoVerifySSH : parameters . autoVerifySSH ,
54+ }
6655 }
6756
6857 if ( parameters . directory ) {
69- const directory = path . resolve ( untildify ( parameters . directory ) ) ;
70-
71- const exists = await FileUtils . checkDirExistsOrThrowIfFile ( directory ) ;
58+ const exists = await FileUtils . checkDirExistsOrThrowIfFile ( parameters . directory ) ;
7259 if ( ! exists ) {
7360 return null ;
7461 }
7562
76- const { data : url } = await codifySpawn ( 'git config --get remote.origin.url' , { cwd : directory } ) ;
77- if ( this . extractBasename ( url ) !== this . extractBasename ( repositoryUrl ) ) {
78- console . log ( this . extractBasename ( url ) )
79- console . log ( this . extractBasename ( url ) )
80- throw new Error ( `Folder found at location: '${ directory } '. However the remote url '${ url } ' does not match desired url '${ repositoryUrl } '` ) ;
81- }
63+ const { data : url } = await codifySpawn ( 'git config --get remote.origin.url' , { cwd : parameters . directory } ) ;
8264
83- return parameters ;
65+ return {
66+ directory : parameters . directory ,
67+ repository : url . trim ( ) ,
68+ autoVerifySSH : parameters . autoVerifySSH ,
69+ }
8470 }
8571
8672 throw new Error ( 'Either directory or parent directory must be supplied' ) ;
@@ -89,24 +75,25 @@ export class GitCloneResource extends Resource<GitCloneConfig> {
8975
9076 override async create ( plan : CreatePlan < GitCloneConfig > ) : Promise < void > {
9177 const config = plan . desiredConfig ;
92- const repositoryUrl = config . repository ?? config . remote ! ;
9378
9479 if ( plan . desiredConfig . autoVerifySSH ) {
95- await this . autoVerifySSHForFirstAttempt ( repositoryUrl )
80+ await this . autoVerifySSHForFirstAttempt ( config . repository )
9681 }
9782
9883 if ( config . parentDirectory ) {
9984 const parentDirectory = path . resolve ( untildify ( config . parentDirectory ) ) ;
10085 await FileUtils . createDirIfNotExists ( parentDirectory ) ;
101- await codifySpawn ( `git clone --progress ${ repositoryUrl } ` , { cwd : parentDirectory } ) ;
86+ await codifySpawn ( `git clone --progress ${ config . repository } ` , { cwd : parentDirectory } ) ;
10287 } else {
10388 const directory = path . resolve ( untildify ( config . directory ! ) ) ;
104- await codifySpawn ( `git clone --progress ${ repositoryUrl } ${ directory } ` ) ;
89+ await codifySpawn ( `git clone --progress ${ config . repository } ${ directory } ` ) ;
10590 }
10691 }
10792
108- override async destroy ( ) : Promise < void > {
93+ override async destroy ( plan : DestroyPlan < GitCloneConfig > ) : Promise < void > {
10994 // Do nothing here. We don't want to destroy a user's repository.
95+ throw new Error ( `The git-clone resource is not designed to delete folders.
96+ Please delete ${ plan . currentConfig . directory ?? ( plan . currentConfig . parentDirectory ! + this . extractBasename ( plan . currentConfig . repository ) ) } manually and re-apply` ) ;
11097 }
11198
11299 // Converts https://github.com/kevinwang5658/codify-homebrew-plugin.git => codify-homebrew-plugin
0 commit comments