<commit_before>'use strict';

var ensureString   = require('es5-ext/object/validate-stringifiable-value')
  , startsWith     = require('es5-ext/string/#/starts-with')
  , deferred       = require('deferred')
  , debug          = require('debug-ext')('rename-module:directory')
  , readdir        = require('fs2/readdir')
  , stat           = require('fs2/stat')
  , rmdir          = require('fs2/rmdir')
  , path           = require('path')
  , resolveRoot    = require('cjs-module/resolve-package-root')
  , moveModule     = require('./module')

  , stringify = JSON.stringify
  , sep = path.sep, basename = path.basename, resolve = path.resolve
  , readdirOpts = { depth: Infinity, type: { file: true },
		ignoreRules: 'git',
		dirFilter: function (path) { return (basename(path) !== 'node_modules'); } };

module.exports = function (from, to) {
	from = resolve(ensureString(from));
	to = resolve(ensureString(to));

	// Validate arguments and resolve initial data
	return deferred(resolveRoot(from), stat(to).then(function (stats) {
		if (!stat.isDirectory()) {
			throw new Error("Target path " + stringify(to) + " is taken and it's not a directory");
		}
	}, function (err) {
		if (err.code === 'ENOENT') return null;
		throw err;
	}))(function (data) {
		var root = data[0];
		if (!root) {
			throw new Error("Unable to resolve package root (renamed directory is expected to be " +
				"placed in scope of some package");
		}
		if (root === from) throw new Error("Renamed directory should not be package root directory");
		if (!startsWith.call(to, root + sep)) {
			throw new Error("Cannot reliably move modules out of current package");
		}

		debug('%s -> %s', from.slice(root.length), to.slice(root.length));
		return readdir(from, readdirOpts).reduce(function (ignore, file, index, files) {
			debug('process %s/%s', index + 1, files.length);
			return moveModule(resolve(from, file), resolve(to, file));
		}, null);
	})(function () {
		return rmdir(from, { recursive: true }).catch(function (e) {
			if (e.code !== 'ENOTEMPTY') throw e;<commit_msg>Do not scan for requires of non modules<commit_after>'use strict';

var ensureString   = require('es5-ext/object/validate-stringifiable-value')
  , startsWith     = require('es5-ext/string/#/starts-with')
  , deferred       = require('deferred')
  , debug          = require('debug-ext')('rename-module:directory')
  , rename         = require('fs2/rename')
  , readdir        = require('fs2/readdir')
  , stat           = require('fs2/stat')
  , rmdir          = require('fs2/rmdir')
  , path           = require('path')
  , resolveRoot    = require('cjs-module/resolve-package-root')
  , moveModule     = require('./module')
  , isModule       = require('./lib/is-module')

  , stringify = JSON.stringify
  , sep = path.sep, basename = path.basename, resolve = path.resolve
  , readdirOpts = { depth: Infinity, type: { file: true },
		ignoreRules: 'git',
		dirFilter: function (path) { return (basename(path) !== 'node_modules'); } };

module.exports = function (from, to) {
	from = resolve(ensureString(from));
	to = resolve(ensureString(to));

	// Validate arguments and resolve initial data
	return deferred(resolveRoot(from), stat(to).then(function (stats) {
		if (!stat.isDirectory()) {
			throw new Error("Target path " + stringify(to) + " is taken and it's not a directory");
		}
	}, function (err) {
		if (err.code === 'ENOENT') return null;
		throw err;
	}))(function (data) {
		var root = data[0];
		if (!root) {
			throw new Error("Unable to resolve package root (renamed directory is expected to be " +
				"placed in scope of some package");
		}
		if (root === from) throw new Error("Renamed directory should not be package root directory");
		if (!startsWith.call(to, root + sep)) {
			throw new Error("Cannot reliably move modules out of current package");
		}

		debug('%s -> %s', from.slice(root.length), to.slice(root.length));
		return readdir(from, readdirOpts).reduce(function (ignore, file, index, files) {
			var filename = resolve(from, file), targetFilename = resolve(to, file);
			debug('process %s/%s', index + 1, files.length);
			return isModule(filename)(function (is) {
				if (is) return moveModule(filename, targetFilename);
				debug('rename %s to %s', filename.slice(root.length), targetFilename.slice(root.length));
				return rename(filename, targetFilename, { intermediate: true });
			});
		}, null);
	})(function () {
		return rmdir(from, { recursive: true }).catch(function (e) {
			if (e.code !== 'ENOTEMPTY') throw e;
{'fraction_non_alphanumeric': 0.15988500663423263, 'fraction_numerical': 0.0033171163202122956, 'mean_word_length': 3.7114583333333333, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1618327', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1398, 'n_words': 377}