Skip to main content

CF: Scripting amidst the tags

Recently I had to do a quick "utility" page where I needed to see how many files from a directory listing had been recorded into a database. I've been writing about 98% of my CF code in script syntax but, since it was quick and easy, I did this quickly in tags because it was outputting directly to the browser. In doing so, I made an interesting discovery in that, when you use closures, even in tag based pages, you can write cfscript. Here's the example

  1. Get the directory listing: 

    <cfset alljs = directoryList(expandpath('/src'),true,"path","*.js")>

  2. Get the database listings and convert it to an array

    <cfquery name="alljsQ" datasource="blah">
    select * from sitefiles where filename like '%.js%'
    </cfquery>
    <cfset recordedFiles = valuelist(alljsQ.filename).listToArray()>

  3. Use a filter function to weed out the files I'd already recorded
    <cfset missingFiles = alljs.filter(function(item){
        var foundFile=item.replacenocase(expandpath('/src\'),'','all').replace("\","/","all");
    return recordedFiles.find(foundFile) eq 0; })>
  4. Use a map function to do what I needed to do with the files not recorded

    <cfset missingFiles.map(function(item){
    var foundFile=item.replacenocase(expandpath('/src\'),'','all').replace("\","/","all");
    writeOutput("<li>#foundFile#</li>");
    })>
A couple of notes about this:
  1. 1. Using the filter and map functions make the code more "functional". If you're looking for a reason to experiment with the idea of functional code, here are two good reasons:
    1.  It really diminishes the number of things you can screw up. 
    2. You can do it, even using tag based code which might ease the process of moving into script

  2. In the filter function in step 3, I used the member function ".find()". This is the equivalent of ArrayFind(recordedFiles,foundFile) but is more readable. Incidentally, I actually used ".indexOf" but then was a bit thrown when I had to use "eq -1" until I remembered that the CF version is ".find" and therefore starts counting at 1 with 0 being "not found". The Java version is ".indexOf" which means it starts counting at 0 and therefore -1 is the "not found".

  3. In the map function, the "callback" or "closure" function that is passed in, can be written in cfscript without worrying about the <cfscript> tag and so on. 

Perhaps obvious to everyone but I did this mostly without thinking and then was pleasantly surprised when it worked. 


Comments

Popular posts from this blog

Creating Stories and Tasks in Jira: Personas and our Software Development Team

Part of the CI/CD Development Series The next step is developing who is on our hypothetical development team. Given that it has a React front end and ColdFusion as the Server Side language, I came up with the following personas, all of which have their own needs and considerations for our development environment. I've listed all the jobs that need doing, not the people involved since, even on a small team or a team of one, these "hats" are all worn by someone, even if it's the same person. Personas for our Project Dev Ops Coordinator - The person responsible for smooth and accurate deployments CF Developer - The person responsible for the API and fulfillment code development and maintenance. React Developer - The person responsible for the front end development Database Coordinator - The person responsible for the schema, data, up time and, presumably the testing databases used by the developers. Lead Developer - The person responsible for coordinat

The Three Deployment Environments: Production, Testing, Development

Part of the CI/CD Development Series A UML Deployment Diagram is a static picture that shows the different "nodes" that work together to create an environment. Typically these nodes consist of hardware, software or other key points. It's a high level overview, just enough detail to get the idea across of the layout without getting too lost in the details. These are the three deployment diagrams for our project. Production The production deployment is more elaborate than the other two below. Our project has a React front end which means that in addition to images and CSS files, it will also have a largish number of Javascript files. All of these are static and do not need any server side processing. As a result, we don't want them on our ColdFusion server taking up space, memory, bandwidth and other resources when we can use those resources for more efficient processing of ColdFusion files. This allows our server to handle more CF requests since they are not busy

As the Dev Ops Coordinator, I need to set up our git repo into several branches with the appropriate permissions for each one

Part of the CI/CD Development Series The core of every CI/CD process is the code repository whether it be Git, Mercurial, SVN or whatever. The general idea is that it allows multiple developers (or whomever) to access your code in the appropriate way in the appropriate level. This can either be the ability for anyone to pull an open source project but not write to the repo directly or full access to a developer on your team to create branches, push to master or anything that needs doing. For our project, we're using git although the hosting provider was up for discussion between Github, Bitbucket by Atlassian or CodeCommit on AWS. We decided to go with AWS for two reasons. 1. We are going use other tools in AWS as part of the build so we decided to keep it all together. 2. We needed to solidify the ins and outs of using IAM for the process. Basic Steps Create the Repo Create the branches we need Use IAM to apply the appropriate permissions to each branch and to set up