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...

As the Dev Ops Manager, I need to start planning our CI/CD release process

Part of the CI/CD Development Series Once we have our Deployment Diagram designed, we need to figure out out to get from here to there. If the end point is the appropriate server environment, the starting point is the developer with his/her hand on the keyboard. These steps take place on a variety of machines, within various process and can changes based on what files are checked in or not. At the moment, we've only created the early basics as seen below. The Beginning of our Deployment Activity Chart Even though there is quite a long way to go there are some elements which we have already been determined. For example We have determined the broad strokes of our technology stack. This is going to be React on the front end and ColdFusion on the server side. We have determined that we are going to be using linting on both the CF and React paths. CFLint for the CF and ESLint for the Javascript We have determined that we are going to be formatters - CFFormat for CF and...

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 ...