11#!/usr/bin/env node
2+
3+ // Import necessary modules for path operations, file system operations, and more
24const path = require ( "path" ) ;
35const fs = require ( "fs" ) ;
46const readline = require ( "readline" ) ;
57const execute = require ( "./execute" ) ;
68const addMeta = require ( "./addMeta" ) ;
79const { color } = require ( "./fonts" ) ;
810
11+ // Configuration object for storing options
912let config = { } ;
1013
14+ // Extract arguments from the command line input
1115const argv = process . argv . slice ( 2 ) ;
16+
17+ // Define available command-line options
1218const options = [ "-self" ] ;
19+
20+ // Iterate over available options and set configurations if specified in argv
1321for ( let option of options ) {
1422 if ( argv . includes ( option ) ) {
1523 config [ option . replace ( / ^ - - / , "" ) ] = true ;
@@ -18,12 +26,16 @@ for (let option of options) {
1826 }
1927}
2028
29+ // Format command from argv, handling spaces and quotes
2130command = argv
22- . map ( ( part ) =>
23- part . match ( / | ' | " / ) ? `'${ part . replace ( / ' / , "\\'" ) } '` : part
24- )
31+ . map ( ( part ) => ( part . match ( / | ' | " / ) ? `'${ part . replace ( / ' / , "'" ) } '` : part ) )
2532 . join ( " " ) ;
2633
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+ */
2739function getRepositories ( path ) {
2840 try {
2941 const config = require ( path ) ;
@@ -39,28 +51,39 @@ function getRepositories(path) {
3951 }
4052}
4153
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+ */
4260async function main ( config = { } , repos = null , directory = null ) {
4361 if ( ! repos ) {
44- // Existing logic to determine repositories and configuration
62+ // Determine repositories and configuration file paths
4563 const currentRepoPath = path . resolve (
4664 process . cwd ( ) ,
4765 "CoCreate.config.js"
4866 ) ;
4967 const packageJsonPath = path . resolve ( process . cwd ( ) , "package.json" ) ;
5068
69+ // Load repositories from specified config file
5170 if ( config [ "c" ] && fs . existsSync ( config [ "c" ] ) ) {
5271 repos = getRepositories ( config [ "c" ] ) ;
5372 directory = path . dirname ( config [ "c" ] ) ;
5473 console . warn (
5574 `${ color . yellow } using ${ config [ "c" ] } configuration${ color . reset } `
5675 ) ;
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 ) ) {
5879 repos = getRepositories ( currentRepoPath ) ;
5980 directory = path . dirname ( currentRepoPath ) ;
6081 console . warn (
6182 `${ color . yellow } using ${ currentRepoPath } configuration${ color . reset } `
6283 ) ;
63- } else if ( fs . existsSync ( packageJsonPath ) ) {
84+ }
85+ // If package.json exists, load repository details from it
86+ else if ( fs . existsSync ( packageJsonPath ) ) {
6487 const repoPath = path . resolve ( process . cwd ( ) ) ;
6588 const packageObj = require ( packageJsonPath ) ;
6689 const repoUrl =
@@ -78,20 +101,26 @@ async function main(config = {}, repos = null, directory = null) {
78101 console . warn (
79102 `${ color . yellow } using ${ packageJsonPath } configuration${ color . reset } `
80103 ) ;
81- } else {
104+ }
105+ // Error if no configuration can be found
106+ else {
82107 console . error (
83108 `${ color . red } a configuration file cannot be found${ color . reset } `
84109 ) ;
85110 process . exit ( 1 ) ;
86111 }
87112 }
88113
114+ // Set default config values
89115 config = { hideMessage : false , ...config } ;
90116
117+ // Add metadata to repos if any are present
91118 if ( repos && repos . length ) repos = await addMeta ( repos , [ ] , directory ) ;
92119
120+ // Execute the command across repositories
93121 const failed = await execute ( command , repos , config ) ;
94122
123+ // Handle any failed command executions
95124 if ( failed && failed . length > 0 ) {
96125 console . log (
97126 color . red +
@@ -104,10 +133,17 @@ async function main(config = {}, repos = null, directory = null) {
104133 ) ;
105134 }
106135
136+ // Prompt user to retry failed commands
107137 await promptRetry ( failed , config , directory ) ;
108138 }
109139}
110140
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+ */
111147async function promptRetry ( failed , config , directory ) {
112148 const rl = readline . createInterface ( {
113149 input : process . stdin ,
@@ -127,4 +163,5 @@ async function promptRetry(failed, config, directory) {
127163 ) ;
128164}
129165
166+ // Call the main function with initial configuration
130167main ( config ) ;
0 commit comments