"use strict";

/*eslint-env node, es6 */
/*eslint-disable no-magic-numbers */

const assert = require("assert");
const path = require("path");
const fs = require("fs");

const Template = require(path.resolve(path.join(__dirname, "..")));

function buildTemplateList(registrationDir, cb) {
    assert.strictEqual(typeof registrationDir, "string", "registrationDir is required");
    assert.strictEqual(typeof cb, "function", "cb must be a callback function");

    const templates = [];

    fs.readdir(registrationDir, (err, files) => {
        if (err) {
            console.log("template list, readdir", err);
            return cb(err);
        }

        for (let i = 0; i < files.length; i++) {
            const file = files[i];

            if (file.match(/^template-.*\.json$/)) {
                try {
                    templates.push({
                        file,
                        config: new Template(path.resolve(path.join(registrationDir, file)))
                    });
                }
                catch (errFile) {
                    console.log("template list, add to list", err);
                    return cb(errFile);
                }
            }
        }

        return cb(null, templates);
    });

}

module.exports = buildTemplateList;

@@ -1,9 +1,9 @@
-%22
+'
 use stri
@@ -8,14 +8,15 @@
 rict
-%22
+'
 ;%0A%0A/*
+ 
 esli
@@ -38,53 +38,16 @@
 6 */
-%0A/*eslint-disable no-magic-numbers */
 %0A%0Aconst 
 asse
@@ -42,22 +42,20 @@
 %0A%0Aconst 
-assert
+path
  = requi
@@ -61,16 +61,14 @@
 ire(
-%22assert%22
+'path'
 );%0Ac
@@ -68,28 +68,26 @@
 th');%0Aconst 
-path
+fs
  = require(%22
@@ -89,25 +89,26 @@
 ire(
-%22path%22
+'fs'
 );%0A
+%0A
 const 
-fs
+cosi
  = r
@@ -118,16 +118,57 @@
 ire(
-%22fs%22
+path.resolve(path.join(__dirname, '..', '..'))
 );%0A
-%0A
 cons
@@ -215,23 +215,32 @@
 oin(
-__dirname, %22..%22
+cosi.lib_dir, 'template'
 )));
@@ -268,29 +268,17 @@
 ateList(
-registrationD
+d
 ir, cb) 
@@ -287,200 +287,221 @@
     
-assert.strictEqual(typeof registrationDir, %22string%22, %22registrationDir is required%22);%0A    assert.strictEqual(typeof cb, %22function%22, %22cb must be a callback function%22);%0A%0A    const templates = %5B%5D;
+let templateDir = cosi.reg_dir;%0A    let callback = null;%0A    const templates = %5B%5D;%0A%0A    if (typeof dir === 'function') %7B%0A        callback = dir;%0A    %7D else %7B%0A        templateDir = dir;%0A        callback = cb;%0A    %7D
 %0A%0A  
@@ -513,28 +513,24 @@
 readdir(
-registration
+template
 Dir, (er
@@ -579,33 +579,33 @@
     console.log(
-%22
+'
 template list, r
@@ -610,17 +610,17 @@
  readdir
-%22
+'
 , err);%0A
@@ -635,22 +635,41 @@
     
-return cb(err)
+callback(err);%0A            return
 ;%0A  
@@ -976,21 +976,18 @@
 oin(
-registrationD
+cosi.reg_d
 ir, 
@@ -1035,32 +1035,16 @@
        %7D
-%0A               
  catch (
@@ -1086,17 +1086,17 @@
 ole.log(
-%22
+'
 template
@@ -1117,9 +1117,9 @@
 list
-%22
+'
 , er
@@ -1142,25 +1142,24 @@
         
-return cb
+callback
 (errFile
@@ -1159,32 +1159,54 @@
 errFile);%0A      
+     
+         return;%0A
           %7D%0A    
@@ -1191,32 +1191,38 @@
 turn;%0A          
+      
 %7D%0A            %7D%0A
@@ -1244,17 +1244,16 @@
     
-return cb
+callback
 (nul
@@ -1267,16 +1267,32 @@
 lates);%0A
+        return;%0A
     %7D);%0A

make template directory optionsl