<commit_before>/*
 *  Copyright (c) 2016 pro!vision GmbH and Contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

"use strict";

var async = require("async");
var path = require("path");
var _ = require("lodash");
var fs = require("fs");
var fse = require("fs-extra");

/**
 * @typedef {Object} ClientLibItem
 * @property {String} path - Clientlib root path (optional if `options.clientLibRoot` is set)
 * @property {String} name - Clientlib name
 * @property {Array<String>} [embed] - other Clientlib names that should be embedded
 * @property {Array<String>} [dependencies] - other Clientlib names that should be included
 * @property {Array<Object>} assets - content that should be copied to the clientlib folder, more details below
 */

/**
 * Check if the given file exists
 * @param file
 * @returns {boolean}
 */
function fileExists(file) {
  try {
    fs.accessSync(file);
    return true;
  } catch(e) {
    return false;
  }
}

/**
 * Removes clientlib folder and configuration file (JSON) for the given
 * clientlib item.
 * @param {ClientLibItem} item - clientlib properties
 * @param {Function} done - callback to be invoked after
 */
function removeClientLib(item, done) {
  var configJson = path.join(item.path, item.name + ".json");
  var clientLibPath = path.join(item.path, item.name);
  var files = [];
  if (fileExists(configJson)) {
    files.push(configJson);
  }
  if (fileExists(clientLibPath)) {
    files.push(clientLibPath);
  }

  if (files.length === 0) {
    return done();
  }

  async.eachSeries(files, function(file, doneClean) {
    fse.remove(file, doneClean);
  }, done);
}

/**
 * Write the clientlib asset TXT file (js or css) that describes the
 * base and contains all resource paths.
 * @param {String} clientLibPath - path to the clientlib folder
 * @param {Object} asset - asset object
 */
function writeAssetTxt(clientLibPath, asset) {

  if (!asset || !asset.type || !_.isArray(asset.files)) {
    return;
  }
  var outputFile = path.join(clientLibPath, asset.type + ".txt");
  var basePath = path.join(clientLibPath, asset.base);

  // determines file path relative to the base
  var filenames = [];
  asset.files.forEach(function(file){
    var rel = path.relative(basePath, file.dest);
    filenames.push(rel);<commit_msg>add file extension check to JS/CSS config files<commit_after>/*
 *  Copyright (c) 2016 pro!vision GmbH and Contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

"use strict";

var async = require("async");
var path = require("path");
var _ = require("lodash");
var fs = require("fs");
var fse = require("fs-extra");

/**
 * @typedef {Object} ClientLibItem
 * @property {String} path - Clientlib root path (optional if `options.clientLibRoot` is set)
 * @property {String} name - Clientlib name
 * @property {Array<String>} [embed] - other Clientlib names that should be embedded
 * @property {Array<String>} [dependencies] - other Clientlib names that should be included
 * @property {Array<Object>} assets - content that should be copied to the clientlib folder, more details below
 */

/**
 * Check if the given file exists
 * @param file
 * @returns {boolean}
 */
function fileExists(file) {
  try {
    fs.accessSync(file);
    return true;
  } catch(e) {
    return false;
  }
}

/**
 * Removes clientlib folder and configuration file (JSON) for the given
 * clientlib item.
 * @param {ClientLibItem} item - clientlib properties
 * @param {Function} done - callback to be invoked after
 */
function removeClientLib(item, done) {
  var configJson = path.join(item.path, item.name + ".json");
  var clientLibPath = path.join(item.path, item.name);
  var files = [];
  if (fileExists(configJson)) {
    files.push(configJson);
  }
  if (fileExists(clientLibPath)) {
    files.push(clientLibPath);
  }

  if (files.length === 0) {
    return done();
  }

  async.eachSeries(files, function(file, doneClean) {
    fse.remove(file, doneClean);
  }, done);
}

/**
 * Write the clientlib asset TXT file (js or css) that describes the
 * base and contains all resource paths.
 * @param {String} clientLibPath - path to the clientlib folder
 * @param {Object} asset - asset object
 */
function writeAssetTxt(clientLibPath, asset) {

  if (!asset || !asset.type || !_.isArray(asset.files)) {
    return;
  }
  var outputFile = path.join(clientLibPath, asset.type + ".txt");
  var basePath = path.join(clientLibPath, asset.base);

  // determines file path relative to the base
  var filenames = [];
  var typeExt = "." + asset.type;

  asset.files.forEach(function(file){

    // inject only files that correspondents to the asset type
    if (path.extname(file.dest) === typeExt) {
      var rel = path.relative(basePath, file.dest);
      filenames.push(rel);
    }
{'fraction_non_alphanumeric': 0.12017241379310345, 'fraction_numerical': 0.003103448275862069, 'mean_word_length': 3.712428919577579, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '269522', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1705, 'n_words': 609}