<commit_before>/*
 *  1.  Check npm-rescue-config.json exists
 *  2.  Parse JSON, set variables for the git directory and npmPackages
 *  3.  Create nodegit repo object for git directory (promise)
 *  4.  (wait for 3. then) Enumerate npmPackages
    4.1.    git checkout <npmPackage.projectName>
    4.2.    cp package.json to <gitDirectory> from <npmPackage.npmPackage>
 *  4.3.    exec("npm install")
    4.4.    git add .
    4.5.    git commit -m "Npm Rescue Backup at <XYZ> o'clock"
 */
'use strict';
const fs = require('fs-extra');
const git = require('nodegit');
const path = require('path');
const loadConfig = require('./src/loadConfig');


/* Bit of a mess here now. The nodegit API doesn't allow you to do steps as easily as using git CLI.

- make an initial commit in there as well (need the branch.target() to be a commit in its own branch)
- line below is how you stage the entire subdirectory, given an Index object
    //return index.addAll(git.Pathspec.create(['.']), git.Index.ADD_OPTION.ADD_DEFAULT, null);



*/

const repoConfig = loadConfig.then(config => {
    return git.Repository.open(config.gitDirectory).then(repo => {
        return {repo, config};
    }).then(repoConfig => {
        const repo = repoConfig.repo;
        const config = repoConfig.config;
        const signature = repo.defaultSignature();
        const npmRescuePackage = path.resolve(config.gitDirectory, 'package.json');

        let index = undefined;
        let parent = undefined;
        let currentPromise = new Promise((resolve, reject) => { resolve(); });

        config.npmPackages.forEach(npmPackage => {
            var nextPromise = currentPromise.then(() => {
                return git.Branch.lookup(repo, npmPackage.projectName, git.Branch.BRANCH.LOCAL);
            }).then(branch => {
                parent = branch.target();
                repo.checkoutBranch(branch);
            }).then(() => {
                fs.copySync(npmPackage.npmPackage, npmRescuePackage);
                return repo.index();
            }).then(i => {
                index = i;
                return index.addAll(git.Pathspec.create(['.']), git.Index.ADD_OPTION.ADD_DEFAULT);
            }).then(() => {
                return index.write();
            }).then(() => {
                return index.writeTree();
            }).then(tree => {
                return repo.createCommit('HEAD', signature, signature, `npm rescue backup of ${npmPackage.projectName}.`, tree, [parent]);
            }).then(oid => {
                console.log(`New commit for ${npmPackage.projectName}: ${oid.toString()}.`);
            }).catch(error => {
                console.log(error.message);
            });

            currentPromise = nextPromise;
        });
    }).catch(error => {
        console.log(error.message);
        process.exit(1);<commit_msg>More debugging needed to resolve synchronous behaviour.<commit_after>/*
 *  1.  Check npm-rescue-config.json exists
 *  2.  Parse JSON, set variables for the git directory and npmPackages
 *  3.  Create nodegit repo object for git directory (promise)
 *  4.  (wait for 3. then) Enumerate npmPackages
    4.1.    git checkout <npmPackage.projectName>
    4.2.    cp package.json to <gitDirectory> from <npmPackage.npmPackage>
 *  4.3.    exec("npm install")
    4.4.    git add .
    4.5.    git commit -m "Npm Rescue Backup at <XYZ> o'clock"
 */
'use strict';
const fs = require('fs-extra');
const git = require('nodegit');
const path = require('path');
const loadConfig = require('./src/loadConfig');


/* Bit of a mess here now. The nodegit API doesn't allow you to do steps as easily as using git CLI.

- make an initial commit in there as well (need the branch.target() to be a commit in its own branch)
- line below is how you stage the entire subdirectory, given an Index object
    //return index.addAll(git.Pathspec.create(['.']), git.Index.ADD_OPTION.ADD_DEFAULT, null);



*/

const repoConfig = loadConfig.then(config => {
    return git.Repository.open(config.gitDirectory).then(repo => {
        return {repo, config};
    }).then(repoConfig => {
        const repo = repoConfig.repo;
        const config = repoConfig.config;
        const signature = repo.defaultSignature();
        const npmRescuePackage = path.resolve(config.gitDirectory, 'package.json');

        let index = undefined;

/*

Aha, there is asynchronous weirdness going on that needs to be fixed.

Wrote /home/iain/dev/flux/package.json to /home/iain/dev/npm-rescue/npm-rescue-backup/package.json.
Wrote /home/iain/dev/flux-todomvc/package.json to /home/iain/dev/npm-rescue/npm-rescue-backup/package.json.
Wrote /home/iain/dev/npm-rescue/package.json to /home/iain/dev/npm-rescue/npm-rescue-backup/package.json.
Wrote /home/iain/dev/todo-redux-react-webpack/package.json to /home/iain/dev/npm-rescue/npm-rescue-backup/package.json.
Wrote /home/iain/dev/react-tutorial/package.json to /home/iain/dev/npm-rescue/npm-rescue-backup/package.json.
New commit for flux.git: 0000000000000000605d39030000000020000000.
Checkpoint
New commit for flux-todomvc.git: 709839030000000040e733030000000000000000.
Checkpoint
New commit for npm-rescue.git: 50ca39030000000020e433030000000000000000.
Checkpoint
New commit for todo-redux-react-webpack.git: 205b390300000000e0f739030000000000000000.
Checkpoint
New commit for react-tutorial.git: a0f539030000000080e833030000000000000000.
Checkpoint

*/

        config.npmPackages.forEach(npmPackage => {
            Promise.all([
                git.Branch.lookup(repo, npmPackage.projectName, git.Branch.BRANCH.LOCAL).then(branch => {
                    repo.checkoutBranch(branch);
                }).then(() => {
                    fs.copySync(npmPackage.npmPackage, npmRescuePackage);
                    console.log(`Wrote ${npmPackage.npmPackage} to ${npmRescuePackage}.`);
                    return repo.index();
                }).then(i => {
                    index = i;
                    return index.addAll(git.Pathspec.create(['.']), git.Index.ADD_OPTION.ADD_DEFAULT);
                }).then(() => {
                    return index.write();
                }).then(() => {
                    return index.writeTree();
                }).then(tree => {
                    return repo.getHeadCommit().then(parent => {
                        return repo.createCommit('HEAD', signature, signature, `npm rescue backup of ${npmPackage.projectName}.`, tree, [parent]);
                    });
                }).then(oid => {
                    console.log(`New commit for ${npmPackage.projectName}: ${oid.toString()}.`);
                }).catch(error => {
                    console.log(error.message);
                })
            ]).then(s => console.log('Checkpoint'));
        });
    }).catch(error => {
        console.log(error.message);
        process.exit(1);
{'fraction_non_alphanumeric': 0.13118342331825478, 'fraction_numerical': 0.032248650226178316, 'mean_word_length': 2.4133466135458166, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 11, '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': '6378634', 'n_tokens_mistral': 2201, 'n_tokens_neox': 1939, 'n_words': 494}