All files javadoc.js

91.1% Statements 133/146
74.42% Branches 32/43
92.86% Functions 26/28
90.71% Lines 127/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 3621x 1x 1x 1x   1x 5x                                             3x                                         4x                                       3x                                           4x 4x 4x 8x   4x 4x 12x   4x 4x 12x   4x 8x     4x 4x 4x 12x 12x 4x     4x     4x 12x 12x 12x     12x 3x 3x 3x     12x                                   3x 3x 3x     3x 3x                                 3x             3x   3x 3x       3x 6x 6x 6x 6x   6x   6x 6x   54x   54x 54x   54x   54x   54x 6x     48x   48x 23x     48x   21x     27x     48x 48x       6x   3x                             1x 1x 1x 1x 2x 2x 7x 7x 5x 1x   5x   7x 4x   3x         1x                             3x 3x 3x 3x 3x 3x 3x       3x 1x   2x 2x 2x 2x 2x 2x   2x   2x 2x 2x 2x 1x 1x 1x 1x 1x 1x             1x   1x 1x 1x 1x 1x             1x       2x 2x 2x 2x 2x 2x 2x                                 1x
const fs = require("fs-extra");
const glob = require("glob");
const path = require("path");
const {EOL} = require("os");
 
const capitalizeString = (s) => {
    return s.substr(0,1).toUpperCase() + s.substr(1);
};
 
/**
 * -----------------
 * 
 * @name Javadoc
 * @type Class.
 * @type Function.
 * @description Master class of the `javadoc` package.
 */
class Javadoc {
 
    /**
     * 
     * -------------------
     * 
     * @name Javadoc.REGEX_PATTERNS
     * @type Static property.
     * @type Object.
     * @description Regular expression patterns used by the class.
     */
    static get REGEX_PATTERNS() {
        return {
            JAVADOC_COMMENT: /\/\*\*[\t ]*((\r?\n)([\t ]*\*[\t ]*).*)*/g,
            JAVADOC_ENTRY: /^\/\*\*[\t ]*/g,
            JAVADOC_LINE_ENTRY: /^\r?\n[\t ]*\*[\t ]*/g,
            JAVADOC_PROPERTY: /^\@[^\n\r\t ]*/g,
            JAVADOC_VALUE: /^.*/g,
        }
    }
 
    /**
     * 
     * -------------------------
     * 
     * @name Javadoc.DEFAULT_GLOB_OPTIONS
     * @type Static property.
     * @type Object.
     * @property `cwd`. Defaults to `process.cwd()`. Allowed options at [glob package](https://www.npmjs.com/package/glob#options)
     * @property `dot`. Defaults to `true`. Allowed options at [glob package](https://www.npmjs.com/package/glob#options)
     * @description Default options used by the `glob` package.
     */
    static get DEFAULT_GLOB_OPTIONS() {
        return {
            cwd: process.cwd(),
            dot: true,
        }
    }
 
    /**
     * 
     * ---------------------------
     * 
     * @name Javadoc.DEFAULT_OPTIONS
     * @type Static property.
     * @type Object.
     * @property include:Array<String>. Defaults to `["** /*.js"]`. Allowed rules at [glob package](https://www.npmjs.com/package/glob).
     * @property exclude:Array<String>. Defaults to `["** /node_modules/**.js"]`. Allowed rules at [glob package](https://www.npmjs.com/package/glob).
     * @property format:String. Defaults to `"markdown"`. Allowed values: `"md"` | `"json"`.
     * @property output:String. Defaults to `undefined`. File into which dump the results. **Required value**.
     * @description General options of the `Javadoc.generate` main method.
     */
    static get DEFAULT_OPTIONS() {
        return {
            include: ["**/*.js"],
            exclude: ["**/node_modules/**"],
            format: "markdown",
            output: undefined
        }
    }
 
    /**
     * 
     * ------------------------
     * 
     * @name Javadoc.findFiles
     * @type Static method.
     * @type Function.
     * @parameter parameterIncludes:Array<String>. Replaces `DEFAULT_OPTIONS.include` as value.
     * @parameter parameterExcludes:Array<String>. Replaces `DEFAULT_OPTIONS.exclude` as value.
     * @parameter parameterOptions:Object. Overrides `DEFAULT_GLOB_OPTIONS` as object.
     * @returns files:Promise<Array<String>>. Asynchronously, returns an array of matched files (as Strings).
     * @description Finds files based on glob patterns ([more info](https://www.npmjs.com/package/glob#usage)) included, excluded and glob options ([more info](https://www.npmjs.com/package/glob#options)).
     */
    static findFiles(parameterIncludes, parameterExcludes = [], parameterOptions = {}) {
        return new Promise((success, failure) => {
            const options = Object.assign({}, this.DEFAULT_GLOB_OPTIONS, parameterOptions);
            const excludes = [].concat(parameterExcludes).map(f => {
                return path.resolve(options.cwd, f);
            });
            options.ignore = excludes;
            const includez = [].concat(parameterIncludes).map(f => {
                return path.resolve(options.cwd, f);
            });
            console.log("[javadoc] Patterns:");
            includez.forEach(includezItem => {
                console.log("[javadoc]   - (include) " + includezItem);
            });
            excludes.forEach(excluded => {
                console.log("[javadoc]   - (exclude) " + excluded);
            });
            // @TODO: match all included glob patterns in a loop...
            const files = [];
            let counterIncluded = 0;
            const finalize = () => {
                counterIncluded++;
                if(counterIncluded >= includez.length) {
                    return success(files);
                }
            }
            Iif(includez.length === 0) {
                return finalize();
            }
            for(let includeIndex = 0; includeIndex < includez.length; includeIndex++) {
                const includePattern = includez[includeIndex];
                glob(includePattern, options, (error, filesMatched) => {
                    Iif (error) {
                        return finalize();
                    }
                    for(let indexMatched = 0; indexMatched < filesMatched.length; indexMatched++) {
                        const fileMatched = filesMatched[indexMatched];
                        Eif(files.indexOf(fileMatched) === -1) {
                            files.push(fileMatched);
                        }
                    }
                    return finalize();
                });
            }
        });
    }
 
    /**
     * 
     * -------------------------------
     * 
     * @name Javadoc.findCommentsInFile
     * @type Static method.
     * @type Function.
     * @parameter file:String. File into which look for the javadoc-comments.
     * @returns matches:Promise<Array<Object>>. Asynchronously, returns a list of matched javadoc-comments (as Objects) found in the passed file.
     * @description From a file, it returns (asynchronously, by a Promise) javadoc-comments represented as Objects.
     */
    static findCommentsInFile(file) {
        return new Promise((success, failure) => {
            return fs.readFile(file, "utf8", (error, contents) => {
                Iif (error) {
                    return failure(error);
                }
                const matches = this.findCommentsInString(contents);
                return success(matches);
            });
        });
    }
 
    /**
     * 
     * --------------------------------
     * 
     * @name Javadoc.findCommentsInString
     * @type Static method.
     * @type Function.
     * @parameter text:String. Text into which look for the javadoc-comments.
     * @returns totalMatches:Array<Object>. List of javadoc-comments (as Objects) found in the passed String.
     * @description Finds javadoc-comments (as Objects) from a String.
     */
    static findCommentsInString(text) {
        const totalMatches = [];
        const {
            JAVADOC_COMMENT,
            JAVADOC_ENTRY,
            JAVADOC_LINE_ENTRY,
            JAVADOC_PROPERTY,
            JAVADOC_VALUE,
        } = this.REGEX_PATTERNS;
        // 1. Get all javadoc-comment matches.
        const allMatches = text.match(JAVADOC_COMMENT);
        Iif(allMatches === null) {
            return totalMatches;
        }
        // 2. For each javadoc-comment...
        for (let indexMatch = 0; indexMatch < allMatches.length; indexMatch++) {
            const matches = {};
            let lastProperty = "_";
            let wasStarted = false;
            let token = allMatches[indexMatch];
            // 3. Remove the entry
            token = token.replace(JAVADOC_ENTRY, "");
            // 4. For each line of the javadoc-comment... starts the extraction.
            Extraction:
            while (JAVADOC_LINE_ENTRY.test(token)) {
                // 5. Remove the line entry.
                token = token.replace(JAVADOC_LINE_ENTRY, "");
                // 6. Get the current property of the line (description by default)
                const isPropertyLine = JAVADOC_PROPERTY.test(token);
                const property = isPropertyLine ? token.match(JAVADOC_PROPERTY)[0] : lastProperty;
                // 7. Remove the property.
                token = token.replace(JAVADOC_PROPERTY, "");
                // 8. Get the current value. Also, replace "* /" for "*/" (in order to escape closed comments).
                const value = token.match(JAVADOC_VALUE)[0].replace(/^[\t ]+/g, "").replace(/\* \//g, "*/");
                // 9. If it is the last line of the javadoc, get out of the extraction of that javadoc-comment.
                if (value === "/") {
                    continue Extraction;
                }
                // 10. Remove the value.
                token = token.replace(JAVADOC_VALUE, "");
                // 11. If the property did not exist, create it.
                if (!(property in matches)) {
                    matches[property] = [];
                }
                // 12. Add the value to that property.
                if ((!isPropertyLine) && wasStarted) {
                    // a. Inline if it is a new-property-line
                    matches[property][matches[property].length - 1] += "\n" + value;
                } else {
                    // b. Otherwise as a new entry
                    matches[property].push(value);
                }
                // 13. Assign the last property to the current property.
                lastProperty = property;
                wasStarted = true;
                // 14. Go for the next javadoc-comment line extraction.
            }
            // 15. Once all the lines were extracted, push the data for this javadoc-comment.
            totalMatches.push(matches);
        }
        return totalMatches;
    }
 
    /**
     * 
     * -----------------------------------
     * 
     * @name Javadoc.formatJsonToMarkdown
     * @type Static method.
     * @type Function.
     * @parameter fileComments:Array<Object>. List of javadoc-comments (as Object) to convert to markdown format.
     * @returns formatted:String. Markdown code from passed javadoc-comments.
     * @description Generates `markdown` code from a list of javadoc-comments.
     */
    static formatJsonToMarkdown(filesAndComments) {
        let formatted = "";
        for (let indexFiles = 0; indexFiles < filesAndComments.length; indexFiles++) {
            const fileComments = filesAndComments[indexFiles];
            for(let indexComments = 0; indexComments < fileComments.length; indexComments++) {
                const fileComment = fileComments[indexComments];
                Object.keys(fileComment).forEach(property => {
                    const value = fileComment[property].join(EOL + EOL + EOL);
                    if (property !== "_") {
                        const propertyTransformed = capitalizeString(property.replace(/^@/g, "").replace(/\-[a-z]/g, (match) => {
                            return " " + match.substr(1).toUpperCase();
                        }));
                        formatted += `**${propertyTransformed}**`;
                    }
                    if (value) {
                        formatted += (property.startsWith("@") ? ": " : "") + value + "" + EOL + EOL + EOL;
                    } else {
                        formatted += "." + EOL + EOL + EOL;
                    }
                });
            }
        }
        return formatted;
    }
 
    /**
     * 
     * -----------------------------------
     * 
     * @name Javadoc.generate
     * @type Static method.
     * @type Function.
     * @parameter parameters:Object. Overrides `Javadoc.DEFAULT_OPTIONS` as Object. To see more about properties, go to that static property of Javadoc class.
     * @returns output:Promise<String|Array<Object>>. Depending on `parameters.format` (`"json"`|`"markdown"`), it can return an Array or a String.
     * @description Finds included files, dismisses excluded files, extracts javadoc-comments, formats them adecuately, and dumps the results into output file, while returning the output asynchronously (as a Promise).
     */
    static generate(parameters = {}) {
        return new Promise((success, failure) => {
            const options = Object.assign({}, this.DEFAULT_OPTIONS, parameters);
            console.log("[javadoc] Start extracting javadoc comments.");
            return this.findFiles(options.include, options.exclude).then(files => {
                console.log(`[javadoc] Total of files matched: ${files.length}.`);
                let counter = 0;
                const output = {
                    success: [],
                    error: []
                };
                if(files.length === 0) {
                    return success([]);
                }
                const endExecution = (result) => {
                    console.log("[javadoc] Final javadoc report per file:");
                    console.log(`[javadoc]   - ${output.success.length} success(es).`);
                    console.log(`[javadoc]   - ${output.error.length} error(s).`);
                    Eif(output.error.length === 0) {
                        console.log("[javadoc] Successfully finished.");
                    }
                    return success(result);
                }
                const finalize = () => {
                    counter++;
                    Eif (counter >= files.length) {
                        if (["md", "markdown"].indexOf(options.format) !== -1) {
                            const formatted = this.formatJsonToMarkdown(output.success);
                            Eif (options.output) {
                                const outputFile = path.resolve(options.output);
                                try {
                                    fs.outputFileSync(outputFile, formatted, "utf8");
                                    console.log(`[javadoc] File output (markdown) written successfully at ${path.basename(outputFile)}.`);
                                } catch (error) {
                                    console.log("[javadoc] Error writing the (markdown) output.", error);
                                }
                            } else {
                                console.log("[javadoc] CAUTION: No output file was specified.");
                            }
                            return endExecution(formatted);
                        } else {
                            Eif(options.output) {
                                const outputFile = path.resolve(options.output);
                                try {
                                    fs.outputFileSync(outputFile, JSON.stringify(output, null, 2), "utf8");
                                    console.log(`[javadoc] File output (json) written successfully at ${path.basename(outputFile)}.`);
                                } catch (error) {
                                    console.log("[javadoc] Error writing the (json) output.", error);
                                }
                            } else {
                                console.log("[javadoc] CAUTION: No output file was specified.");
                            }
                            return endExecution(output);
                        }
                    }
                }
                for (let index = 0; index < files.length; index++) {
                    const file = files[index];
                    ((file) => {
                        return this.findCommentsInFile(file).then(comments => {
                            console.log(`[javadoc] File ${path.basename(file)} has ${comments.length} javadoc comments.`);
                            output.success.push(comments);
                            return finalize();
                        }).catch(error => {
                            console.log(`[javadoc] Error finding comments on file: ${path.basename(file)}.`);
                            output.error.push(error);
                            return finalize();
                        });
                    })(file);
                }
            }).catch(error => {
                console.log("[javadoc] Failed on finding files.", error);
                return failure(error);
            });
        });
    }
 
}
 
module.exports = Javadoc;