ColdBox / coldbox-cli

The official ColdBox CLI for CommandBox
https://www.coldbox.org
3 stars 4 forks source link

Framework Reinit Doesn't Recognize Webroot Setting in server.json #9

Open homestar9 opened 2 months ago

homestar9 commented 2 months ago

What are the steps to reproduce this issue?

  1. Create a new app using the Ortus Module Template
  2. Add a server.json file to the project root and make sure the web.webroot value is set to test-harness
    
    // example server.json
    {
    "name":"myapp",
    "app":{
        "serverHomeDirectory":".engine/lucee5",
        "cfengine":"lucee@5"
    },
    "web":{
        "http":{
            "port":"60301"
        },
        "rewrites":{
            "enable":"true"
        },
        "webroot":"test-harness",
        "aliases":{
            "/moduleroot/myapp":"./",
            "/modules/myapp":"./"
        }
    },
    "openBrowser":"false",
    "cfconfig":{
        "file":".cfconfig.json"
    }
    }

3. Start the server from the root
4. Try typing `fwreinit` in Commandbox

## What happens?

You will see the error _No server configurations found for '[your project folder]', so have no clue what to reinit buddy!_ 

## What were you expecting to happen?

The Coldbox-CLI should see the web root and attempt to reinit the framework.

## What versions are you using?

Tested with coldbox-cli@be and Commandbox 6.0.0
homestar9 commented 2 months ago

Update: If I make the following change to Line 29 of reinit.cfc: var serverInfo = serverService.getServerInfoByDiscovery( name = arguments.name );

The reinit will work if you specify the name of the server like this: fwreinit name=mymodule

However, I think fwreinit should be smart enough to infer the name from the current working directory server.json file or box.json, if present.

Therefore, I think the fix involves making the change I mentioned above and also changing the default name argument in the run() method to look for a value in the server.json file followed next by box.json.

homestar9 commented 2 months ago

Update 2: What about this possible solution?

/**
 * Reinitialize a running ColdBox app if a server was started with CommandBox.
 * This command must be ran from the root of the ColdBox Application
 * .
 * {code:bash}
 * coldbox reinit
 * {code}
 * .
 * {code:bash}
 * coldbox reinit password="mypass"
 * {code}
 **/
component aliases="fwreinit" {

    // DI
    property name="serverService" inject="ServerService";
    property name="formatter"     inject="formatter";

    /**
     * @password The FWReinit password
     * @name     Name of the CommandBox server to reinit, will default to the name listed in server.json file
     * @showUrl  Show the Url to reinit
     **/
    function run(
        password = "1",
        name     = getDefaultServerName(),
        showUrl  = true
    ){
        var serverInfo = serverService.getServerInfoByDiscovery( name = arguments.name );

        if ( !structCount( serverInfo ) ) {
            print.boldRedLine(
                "No server configurations found for '#getCWD()#' and '#arguments.name#', so have no clue what to reinit buddy!"
            );
        } else {
            var thisURL = "#serverInfo.host#:#serverInfo.port#/?fwreinit=#arguments.password#";
            if ( arguments.showUrl ) {
                print.greenLine( "Hitting... #thisURL#" );
            }
            http result="local.results" url="#thisURL#";

            if ( findNoCase( "200", local.results.statusCode ) ) {
                print.boldGreenLine( "App Reinited!" );
            } else {
                print
                    .redLine( "status code: #local.results.statusCode#" )
                    .redline( "error detail: " & local.results.errorDetail )
                    .line( trim( formatter.HTML2ANSI( local.results.filecontent ) ) );
            }
        }
    }

    private function getDefaultServerName() {
        return serverService.getServerInfoByDiscovery( serverConfigFile = 'server.json' ).name;
    }

}