<commit_before>// TODO(Saar) Watch imports for change
'use strict';

var path = require('path');
var util = require('util');
var sass = require('node-sass');
var Batch = require('../batch');
var Processor = require('../processor');
var Promise = require('../promise');

var processor = new Processor('http://sass-lang.com/');

processor.getBatches = function(inputPaths, inputFiles) {
	var log = this.log;
	var projectDir = process.cwd();
	var importMap = {};
	var imported = [];

	// TODO(Saar) Do we need this???
	var promises = inputFiles.map(function(inputFile) {
		var imports = importMap[inputFile.path] = [];
		var inputFileDir = path.dirname(inputFile.path);
		return inputFile.getText();
	});

	// Resolve import paths recursively, but prevent infinite recursion and duplicates
	function getImports(path, paths) {
		paths = paths || [];
		(importMap[path] || []).forEach(function(importPath) {
			if (paths.indexOf(importPath) < 0) {
				paths.push(importPath);
				getImports(importPath, paths);
			}
		});
		return paths;
	}

	return Promise.all(promises).then(function() {
		log.debug('Detected imports: %s', imported.join(', '));
		return inputPaths.filter(function(inputPath) {
			return imported.indexOf(inputPath) === -1;
		}).map(function(inputPath) {
			var outputPath = inputPath.replace(/\.scss$/, '.css');
			var importInputs = getImports(inputPath).map(function(importPath) {
				return { path: importPath, isReadOnly: true, trackRatio: true };
			});
			return new Batch(
				[ { path: inputPath, trackRatio: true } ].concat(importInputs),
				[ { path: outputPath, trackRatio: true }, outputPath + '.map' ],
				{ multiCore: true }
			);
		}).concat([ new Batch(imported, []) ]);
	});
};

processor.process = function(inputs, outputs) {
	// Import removal batch, nothing to do here
	if (outputs.length === 0) {
		return;
	}

	var input = inputs[0];
	var output = outputs[0];
	var mapOutput = outputs[1];
	var text = input.data;

	return sass.render({
		file: input.path,
		sourceMap: true,
		outFile: output.path,
		sourceMapContents: true
	}, function (err, result) {
		if(err) {
			throw err;
		}

		output.resolve(result.css);
		mapOutput.resolve(result.map);
	});
};

module.exports = processor;<commit_msg>#5 Using in memory file data<commit_after>// TODO(Saar) Watch imports for change
'use strict';

var path = require('path');
var util = require('util');
var sass = require('node-sass');
var Batch = require('../batch');
var Processor = require('../processor');
var Promise = require('../promise');

var processor = new Processor('http://sass-lang.com/');

processor.getBatches = function(inputPaths, inputFiles) {
	var log = this.log;
	var projectDir = process.cwd();
	var importMap = {};
	var imported = [];

	// TODO(Saar) Do we need this???
	var promises = inputFiles.map(function(inputFile) {
		var imports = importMap[inputFile.path] = [];
		var inputFileDir = path.dirname(inputFile.path);
		return inputFile.getText().then(function(text) {
			text.split('\n').forEach(function(line) {
				var matches = line.match(/@import (?:'|")(.*?)(?:'|")/);
				if (matches) {
					var filename = matches[1].indexOf('.') > -1 ? matches[1] : matches[1] + '.scss';
					var importPath = path.relative(projectDir, path.resolve(inputFileDir, filename));
					importPath = importPath.replace(/\\/g, '/'); // convert Windows to Unix path
					imports.push(importPath);
					if (imported.indexOf(importPath) === -1) {
						imported.push(importPath);
					}
				}
			});
		});
	});

	// Resolve import paths recursively, but prevent infinite recursion and duplicates
	function getImports(path, paths) {
		paths = paths || [];
		(importMap[path] || []).forEach(function(importPath) {
			if (paths.indexOf(importPath) < 0) {
				paths.push(importPath);
				getImports(importPath, paths);
			}
		});
		return paths;
	}

	return Promise.all(promises).then(function() {
		log.debug('Detected imports: %s', imported.join(', '));
		return inputPaths.filter(function(inputPath) {
			return imported.indexOf(inputPath) === -1;
		}).map(function(inputPath) {
			var outputPath = inputPath.replace(/\.scss$/, '.css');
			var importInputs = getImports(inputPath).map(function(importPath) {
				return { path: importPath, isReadOnly: true, trackRatio: true };
			});
			return new Batch(
				[ { path: inputPath, trackRatio: true } ].concat(importInputs),
				[ { path: outputPath, trackRatio: true }, outputPath + '.map' ],
				{ multiCore: true }
			);
		}).concat([ new Batch(imported, []) ]);
	});
};

processor.process = function(inputs, outputs) {
	// Import removal batch, nothing to do here
	if (outputs.length === 0) {
		return;
	}

	var input = inputs[0];
	var output = outputs[0];
	var mapOutput = outputs[1];
	var text = input.data;

	return sass.render({
		file: input.path,
		data: input.data,
		sourceMap: true,
		outFile: output.path,
		sourceMapContents: true,
		importer: function(url, prev, done) {
			var matches = inputs.filter(function(input) {
				return input.path.indexOf('/' + url + '.scss') > -1;
			});

			if(!matches.length) {
				return;
			}

			return {
				file: matches[0].path,
				contents: matches[0].data
			};
		}
	}, function (err, result) {
		if(err) {
			throw err;
		}

		output.resolve(result.css);
		mapOutput.resolve(result.map);
	});
};

module.exports = processor;
{'fraction_non_alphanumeric': 0.17120841947002444, 'fraction_numerical': 0.003946626573952265, 'mean_word_length': 3.8337874659400546, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6097324', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1738, 'n_words': 405}