<commit_before>#!/usr/bin/env node

var path = require('path'),
    fs = require('fs'),
    readInstalled = require("read-installed");

var dir = process.cwd(),
    cache = path.join(process.env['HOME'], '.npm'),
    tmp = path.resolve('/tmp/.npm');

function relock () {
  var packages = {};

  readInstalled(dir, void 0, function (er, data) {
    if (er) throw er;
    //console.log(data);
    if (data.dependencies) {
      Object.keys(data.dependencies).forEach(function (key) {
        walk(data.dependencies[key], packages);
      });
    }
    fs.writeFile(path.join(process.cwd(), 'lockdown.json'), JSON.stringify(packages, null, '  '));
  });
}<commit_msg>write alpha sorted lockdown.json files for better diff readability<commit_after>#!/usr/bin/env node

var path = require('path'),
    fs = require('fs'),
    readInstalled = require("read-installed");

var dir = process.cwd(),
    cache = path.join(process.env['HOME'], '.npm'),
    tmp = path.resolve('/tmp/.npm');

function relock () {
  var packages = {};

  readInstalled(dir, void 0, function (er, data) {
    if (er) throw er;
    //console.log(data);
    if (data.dependencies) {
      Object.keys(data.dependencies).forEach(function (key) {
        walk(data.dependencies[key], packages);
      });
    }
    fs.writeFile(path.join(process.cwd(), 'lockdown.json'), JSON.stringify(sortObj(packages), null, '  '));
  });
}

// we take advantage of the way JSON.stringify() is implemented to
// write sorted lockdown.json files for better diffs.
function sortObj(obj) {
  if (typeof obj === 'object' && obj !== null) {
    var sorted = {};
    Object.keys(obj).sort().forEach(function(k) {
      sorted[k] = sortObj(obj[k]);
    });
    return sorted;
  } else if (Array.isArray(obj)) {
    return obj.sort().map(sortObj);
  }
  return obj;
}
{'fraction_non_alphanumeric': 0.18221976808393153, 'fraction_numerical': 0.0011043622308117063, 'mean_word_length': 3.3453237410071943, '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': '1809779', 'n_tokens_mistral': 600, 'n_tokens_neox': 580, 'n_words': 131}