<commit_before>var fs = require('fs')
var path = require('path')
var Bluebird = require('bluebird')
var JSZip = require('jszip')

Bluebird.promisifyAll(fs)

module.exports = function(source) {
	this.cacheable()
	var cb = this.async()
	var loader = this

	var zip = JSZip()
	var completed = source.toString() !== ''
		? zipFile(zip, this)
		: zipDirectory(zip, this).then(Bluebird.all)

	completed.then(function() {
		cb(null, zip.generate({type: 'nodebuffer'}))
	})
}

function zipFile(zip, loader) {
	loader.addDependency(loader.resourcePath)
	return fs.readFileAsync(loader.resourcePath).then(function(buf) {
		zip.file(path.basename(loader.resourcePath), buf)
		return zip
	})
}

function zipDirectory(zip, loader) {
	var fileName = path.basename(loader.resourcePath)
	var directoryName = fileName.substr(0, fileName.indexOf('.'))
	return addFilesToZipDirectory(loader.context, zip.folder(directoryName), loader).then(Bluebird.all)
}

function addFilesToZipDirectory(directory, zip, loader) {
	return fs.readdirAsync(directory).then(function(result) {
		var resultArray = result.toString().split(',')
		var nameFileIndex = resultArray.indexOf(path.basename(loader.resourcePath))
		if (nameFileIndex !== -1 && directory === loader.context) resultArray.splice(nameFileIndex, 1)
		return resultArray.map(function(val) {
			if (val.indexOf('.') !== -1) {
				var filePath = directory + '/' + val
				loader.addDependency(filePath)
				return fs.readFileAsync(filePath).then(function(buf) {
					return zip.file(val, buf)
				})
			} else return addFilesToZipDirectory(directory + '/' + val, zip.folder(val), loader).then(Bluebird.all)
		})<commit_msg>removed addDependency when zipping single file, imported file is already dependency<commit_after>var fs = require('fs')
var path = require('path')
var Bluebird = require('bluebird')
var JSZip = require('jszip')

Bluebird.promisifyAll(fs)

module.exports = function(source) {
	this.cacheable()
	var cb = this.async()
	var loader = this

	var zip = JSZip()
	var completed = source.toString() !== ''
		? zipFile(zip, this.resourcePath)
		: zipDirectory(zip, this).then(Bluebird.all)

	completed.then(function() {
		cb(null, zip.generate({type: 'nodebuffer'}))
	})
}

function zipFile(zip, filePath) {
	return fs.readFileAsync(filePath).then(function(buf) {
		zip.file(path.basename(filePath), buf)
		return zip
	})
}

function zipDirectory(zip, loader) {
	var fileName = path.basename(loader.resourcePath)
	var directoryName = fileName.substr(0, fileName.indexOf('.'))
	return addFilesToZipDirectory(loader.context, zip.folder(directoryName), loader).then(Bluebird.all)
}

function addFilesToZipDirectory(directory, zip, loader) {
	return fs.readdirAsync(directory).then(function(result) {
		var resultArray = result.toString().split(',')
		var nameFileIndex = resultArray.indexOf(path.basename(loader.resourcePath))
		if (nameFileIndex !== -1 && directory === loader.context) resultArray.splice(nameFileIndex, 1)
		return resultArray.map(function(val) {
			if (val.indexOf('.') !== -1) {
				var filePath = directory + '/' + val
				loader.addDependency(filePath)
				return fs.readFileAsync(filePath).then(function(buf) {
					return zip.file(val, buf)
				})
			} else return addFilesToZipDirectory(directory + '/' + val, zip.folder(val), loader).then(Bluebird.all)
		})
{'fraction_non_alphanumeric': 0.1493975903614458, 'fraction_numerical': 0.0024096385542168677, 'mean_word_length': 5.266037735849056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6655612', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1066, 'n_words': 209}