1
1
#!/usr/bin/env node
2
+
3
+ // Import necessary modules for path operations, file system operations, and more
2
4
const path = require ( "path" ) ;
3
5
const fs = require ( "fs" ) ;
4
6
const readline = require ( "readline" ) ;
5
7
const execute = require ( "./execute" ) ;
6
8
const addMeta = require ( "./addMeta" ) ;
7
9
const { color } = require ( "./fonts" ) ;
8
10
11
+ // Configuration object for storing options
9
12
let config = { } ;
10
13
14
+ // Extract arguments from the command line input
11
15
const argv = process . argv . slice ( 2 ) ;
16
+
17
+ // Define available command-line options
12
18
const options = [ "-self" ] ;
19
+
20
+ // Iterate over available options and set configurations if specified in argv
13
21
for ( let option of options ) {
14
22
if ( argv . includes ( option ) ) {
15
23
config [ option . replace ( / ^ - - / , "" ) ] = true ;
@@ -18,12 +26,16 @@ for (let option of options) {
18
26
}
19
27
}
20
28
29
+ // Format command from argv, handling spaces and quotes
21
30
command = argv
22
- . map ( ( part ) =>
23
- part . match ( / | ' | " / ) ? `'${ part . replace ( / ' / , "\\'" ) } '` : part
24
- )
31
+ . map ( ( part ) => ( part . match ( / | ' | " / ) ? `'${ part . replace ( / ' / , "'" ) } '` : part ) )
25
32
. join ( " " ) ;
26
33
34
+ /**
35
+ * Load repository configuration from the given path.
36
+ * @param {string } path - The file path to load repository config from.
37
+ * @returns {Array } - List of repositories.
38
+ */
27
39
function getRepositories ( path ) {
28
40
try {
29
41
const config = require ( path ) ;
@@ -39,28 +51,39 @@ function getRepositories(path) {
39
51
}
40
52
}
41
53
54
+ /**
55
+ * Main function to execute commands across repositories.
56
+ * @param {Object } config - The configuration object.
57
+ * @param {Array } [repos=null] - List of repositories to process.
58
+ * @param {string } [directory=null] - The directory path of the configuration.
59
+ */
42
60
async function main ( config = { } , repos = null , directory = null ) {
43
61
if ( ! repos ) {
44
- // Existing logic to determine repositories and configuration
62
+ // Determine repositories and configuration file paths
45
63
const currentRepoPath = path . resolve (
46
64
process . cwd ( ) ,
47
65
"CoCreate.config.js"
48
66
) ;
49
67
const packageJsonPath = path . resolve ( process . cwd ( ) , "package.json" ) ;
50
68
69
+ // Load repositories from specified config file
51
70
if ( config [ "c" ] && fs . existsSync ( config [ "c" ] ) ) {
52
71
repos = getRepositories ( config [ "c" ] ) ;
53
72
directory = path . dirname ( config [ "c" ] ) ;
54
73
console . warn (
55
74
`${ color . yellow } using ${ config [ "c" ] } configuration${ color . reset } `
56
75
) ;
57
- } else if ( ! config [ "self" ] && fs . existsSync ( currentRepoPath ) ) {
76
+ }
77
+ // Load repositories from default CoCreate.config.js if exists
78
+ else if ( ! config [ "self" ] && fs . existsSync ( currentRepoPath ) ) {
58
79
repos = getRepositories ( currentRepoPath ) ;
59
80
directory = path . dirname ( currentRepoPath ) ;
60
81
console . warn (
61
82
`${ color . yellow } using ${ currentRepoPath } configuration${ color . reset } `
62
83
) ;
63
- } else if ( fs . existsSync ( packageJsonPath ) ) {
84
+ }
85
+ // If package.json exists, load repository details from it
86
+ else if ( fs . existsSync ( packageJsonPath ) ) {
64
87
const repoPath = path . resolve ( process . cwd ( ) ) ;
65
88
const packageObj = require ( packageJsonPath ) ;
66
89
const repoUrl =
@@ -78,20 +101,26 @@ async function main(config = {}, repos = null, directory = null) {
78
101
console . warn (
79
102
`${ color . yellow } using ${ packageJsonPath } configuration${ color . reset } `
80
103
) ;
81
- } else {
104
+ }
105
+ // Error if no configuration can be found
106
+ else {
82
107
console . error (
83
108
`${ color . red } a configuration file cannot be found${ color . reset } `
84
109
) ;
85
110
process . exit ( 1 ) ;
86
111
}
87
112
}
88
113
114
+ // Set default config values
89
115
config = { hideMessage : false , ...config } ;
90
116
117
+ // Add metadata to repos if any are present
91
118
if ( repos && repos . length ) repos = await addMeta ( repos , [ ] , directory ) ;
92
119
120
+ // Execute the command across repositories
93
121
const failed = await execute ( command , repos , config ) ;
94
122
123
+ // Handle any failed command executions
95
124
if ( failed && failed . length > 0 ) {
96
125
console . log (
97
126
color . red +
@@ -104,10 +133,17 @@ async function main(config = {}, repos = null, directory = null) {
104
133
) ;
105
134
}
106
135
136
+ // Prompt user to retry failed commands
107
137
await promptRetry ( failed , config , directory ) ;
108
138
}
109
139
}
110
140
141
+ /**
142
+ * Prompt the user to retry failed commands.
143
+ * @param {Array } failed - List of failed commands.
144
+ * @param {Object } config - Configuration object.
145
+ * @param {string } directory - Path of the configuration directory.
146
+ */
111
147
async function promptRetry ( failed , config , directory ) {
112
148
const rl = readline . createInterface ( {
113
149
input : process . stdin ,
@@ -127,4 +163,5 @@ async function promptRetry(failed, config, directory) {
127
163
) ;
128
164
}
129
165
166
+ // Call the main function with initial configuration
130
167
main ( config ) ;
0 commit comments