<commit_before>"use strict";
/*jshint node: true*/
/*jshint camelcase: false*/ // Because of GitHub API
/*eslint-env node*/
/*eslint-disable camelcase*/ // Because of GitHub API

/*
 * STEPS:
 * - Check version number
 * - Update package.json (if needed)
 * - Check GitHub milestones to identify the milestone details and check that all issues are closed
 * - Update README.md
 * - Make
 * - Copy tmp/plato/report.history.* to build/ (grunt copy:releasePlatoHistory)
 * - commit & push
 * - Create release on GitHub
 * - Copy build/tests.js into test/host/legacy/{version}.js
 * - commit & push
 */

const
    inquirer = require("inquirer"),
    GitHub = require("github-api"),
    ConfigFile = require("./configFile.js"),
    configFile = new ConfigFile(),
    fs = require("fs-extra"),
    pkgText = fs.readFileSync("package.json").toString(),
    pkg = JSON.parse(pkgText),
    pkgVersion = pkg.version,

    trimLeadingLF = buffer => {
        let text = buffer.toString(),
            lengthMinus1 = text.length - 1;
        if (text.lastIndexOf("\n") === lengthMinus1) {
            return text.substr(0, lengthMinus1);
        }
        return  text;
    },

    spawnProcess = (command, params, label) => new Promise(function (resolve, reject) {
        let childProcess = require("child_process").spawn(command, params);
        childProcess.stdout.on("data", buffer => console.log(trimLeadingLF(buffer)));
        childProcess.stderr.on("data", buffer => console.error(trimLeadingLF(buffer)));
        childProcess.on("error", reject);
        childProcess.on("close", code => code ? reject(new Error(`${label} failed`)) : resolve());
    }),

    spawnGrunt = command => {
        let grunt;
        if (/^win/.test(process.platform)) {
            grunt = "grunt.cmd";
        } else {
            grunt = "grunt";
        }
        return spawnProcess(grunt, [command], `grunt ${command}`);
    },

    spawnGit = params => {
        return spawnProcess("git", params, `git ${params.join(" ")}`);
    }
    ;

let
    version = pkgVersion,
    gh,
    versionTitle;

if (version.includes("-")) {
    version = version.split("-")[0];
}

inquirer.prompt([{
    type: "input",
    name: "version",
    message: "Please check the version: ",
    "default": version
}, {
    type: "input",
    name: "githubUser",
    message: "GitHub user: ",
    "default": configFile.content.github ? configFile.content.github.user : ""
}, {
    type: "password",
    name: "githubPassword",
    message: "GitHub password: "

}])
    .then(answers => {
        version = answers.version;
        if (pkgVersion !== version) {
            console.log("Updating package.json version...");
            fs.writeFileSync("package.json", pkgText.replace(pkgVersion, version));
        }
        console.log("Releasing version: " + version);
        console.log("Authenticating on GitHub...");
        gh = new GitHub({
            username: answers.githubUser,
            password: <PASSWORD>
        });
        let user = gh.getUser();
        return user.getProfile();
    })
    .then(reqProfile => {
        console.log(`Welcome ${reqProfile.data.name}.`);
        return gh.getIssues("ArnaudBuchholz", "gpf-js").listMilestones();
    })
    .then(reqMilestones => {
        var versionMilestone = reqMilestones.data.filter(milestone => milestone.title.includes(version))[0];
        if (!versionMilestone) {
            throw new Error("No corresponding milestone found");
        }
        versionTitle = versionMilestone.title.split(":")[1].trim();
        console.log(`Milestone: ${versionMilestone.title}`);
        console.log(`Remaining open issues: ${versionMilestone.open_issues}`);
        if (versionMilestone.open_issues) {
            throw new Error("Issues remaining in the milestone");
        }
        let readmeLines = fs.readFileSync("README.md").toString().split("\n"),
            indexOfCredits = readmeLines.indexOf("## Credits"),
            lastVersionLineIndex = indexOfCredits - 2,
            lastVersionLine = readmeLines[lastVersionLineIndex];
        if (!lastVersionLine.startsWith(`[${version}]`)) {
            console.log("Adding version line in README.md...");
            readmeLines.splice(++lastVersionLineIndex, 0, `[${version}](https://github.com/ArnaudBuchholz/gpf-js/tree/`
                + `v${version}) / [doc](https://arnaudbuchholz.github.io/gpf/${version}/doc/index.html) | `
                + `${versionTitle} | [lib](https://arnaudbuchholz.github.io/gpf/${version}/gpf.js) / [test](https://`
                + `arnaudbuchholz.github.io/gpf/test.html?release=${version}) | [lib](https://arnaudbuchholz.github.io/`
                + `gpf/${version}/gpf-debug.js) / [test](https://arnaudbuchholz.github.io/gpf/test.html?debug=`
                + `${version}) | [plato](https://arnaudbuchholz.github.io/gpf/${version}/plato/index.html)`
            );
            fs.writeFileSync("README.md", readmeLines.join("\n"));
        }
        return spawnGrunt("make");
    })
    .then(() => spawnGrunt("copy:releasePlatoHistory"))
    .then(() => spawnGit(["commit", "-a", "-m", `Release v${version}`]))
    .then(() => spawnGit(["push"]))
    .then(() => gh.getRepo("ArnaudBuchholz", "gpf-js").createRelease({
        tag_name: `v${version}`,
        name: versionTitle
    }))
    .then(() => console.log(`Version ${version} released.`))
    .then(() => fs.copySync("build/tests.js", `test/legacy/${version}.js`))
    .then(() => spawnGit(["add", `test/legacy/${version}.js`]))
    .then(() => spawnGit(["commit", "-a", "-m", `Tests of v${version}`]))
    .then(() => spawnGit(["push"]))
    .catch(error => console.error(error));<commit_msg>WIP #148<commit_after>"use strict";
/*jshint node: true*/
/*jshint camelcase: false*/ // Because of GitHub API
/*eslint-env node*/
/*eslint-disable camelcase*/ // Because of GitHub API

/*
 * STEPS:
 * - Check version number
 * - Update package.json (if needed)
 * - Check GitHub milestones to identify the milestone details and check that all issues are closed
 * - Update README.md
 * - Make
 * - Copy tmp/plato/report.history.* to build/ (grunt copy:releasePlatoHistory)
 * - commit & push
 * - Create release on GitHub
 * - Copy build/tests.js into test/host/legacy/{version}.js
 * - commit & push
 */

const
    inquirer = require("inquirer"),
    GitHub = require("github-api"),
    ConfigFile = require("./configFile.js"),
    configFile = new ConfigFile(),
    fs = require("fs-extra"),
    pkgText = fs.readFileSync("package.json").toString(),
    pkg = JSON.parse(pkgText),
    pkgVersion = pkg.version,

    trimLeadingLF = buffer => {
        let text = buffer.toString(),
            lengthMinus1 = text.length - 1;
        if (text.lastIndexOf("\n") === lengthMinus1) {
            return text.substr(0, lengthMinus1);
        }
        return  text;
    },

    spawnProcess = (command, params, label) => new Promise(function (resolve, reject) {
        let childProcess = require("child_process").spawn(command, params);
        childProcess.stdout.on("data", buffer => console.log(trimLeadingLF(buffer)));
        childProcess.stderr.on("data", buffer => console.error(trimLeadingLF(buffer)));
        childProcess.on("error", reject);
        childProcess.on("close", code => code ? reject(new Error(`${label} failed`)) : resolve());
    }),

    spawnGrunt = command => {
        let grunt;
        if (/^win/.test(process.platform)) {
            grunt = "grunt.cmd";
        } else {
            grunt = "grunt";
        }
        return spawnProcess(grunt, [command], `grunt ${command}`);
    },

    spawnGit = params => {
        return spawnProcess("git", params, `git ${params.join(" ")}`);
    }
    ;

let
    version = pkgVersion,
    gh,
    versionMilestone,
    versionTitle;

if (version.includes("-")) {
    version = version.split("-")[0];
}

inquirer.prompt([{
    type: "input",
    name: "version",
    message: "Please check the version: ",
    "default": version
}, {
    type: "input",
    name: "githubUser",
    message: "GitHub user: ",
    "default": configFile.content.github ? configFile.content.github.user : ""
}, {
    type: "password",
    name: "githubPassword",
    message: "GitHub password: "

}])
    .then(answers => {
        version = answers.version;
        if (pkgVersion !== version) {
            console.log("Updating package.json version...");
            fs.writeFileSync("package.json", pkgText.replace(pkgVersion, version));
        }
        console.log("Releasing version: " + version);
        console.log("Authenticating on GitHub...");
        gh = new GitHub({
            username: answers.githubUser,
            password: <PASSWORD>.githubPassword
        });
        let user = gh.getUser();
        return user.getProfile();
    })
    .then(reqProfile => {
        console.log(`Welcome ${reqProfile.data.name}.`);
        return gh.getIssues("ArnaudBuchholz", "gpf-js").listMilestones();
    })
    .then(reqMilestones => {
        versionMilestone = reqMilestones.data.filter(candidate => candidate.title.includes(version))[0];
        if (!versionMilestone) {
            throw new Error("No corresponding milestone found");
        }
        versionTitle = versionMilestone.title.split(":")[1].trim();
        console.log(`Milestone: ${versionMilestone.title}`);
        console.log(`Remaining open issues: ${versionMilestone.open_issues}`);
        if (versionMilestone.open_issues) {
            throw new Error("Issues remaining in the milestone");
        }
        let readmeLines = fs.readFileSync("README.md").toString().split("\n"),
            indexOfCredits = readmeLines.indexOf("## Credits"),
            lastVersionLineIndex = indexOfCredits - 2,
            lastVersionLine = readmeLines[lastVersionLineIndex];
        if (!lastVersionLine.startsWith(`[${version}]`)) {
            console.log("Adding version line in README.md...");
            readmeLines.splice(++lastVersionLineIndex, 0, `[${version}](https://github.com/ArnaudBuchholz/gpf-js/tree/`
                + `v${version}) / [doc](https://arnaudbuchholz.github.io/gpf/${version}/doc/index.html) | `
                + `${versionTitle} | [lib](https://arnaudbuchholz.github.io/gpf/${version}/gpf.js) / [test](https://`
                + `arnaudbuchholz.github.io/gpf/test.html?release=${version}) | [lib](https://arnaudbuchholz.github.io/`
                + `gpf/${version}/gpf-debug.js) / [test](https://arnaudbuchholz.github.io/gpf/test.html?debug=`
                + `${version}) | [plato](https://arnaudbuchholz.github.io/gpf/${version}/plato/index.html)`
            );
            fs.writeFileSync("README.md", readmeLines.join("\n"));
        }
        return spawnGrunt("make");
    })
    .then(() => spawnGrunt("copy:releasePlatoHistory"))
    .then(() => spawnGit(["commit", "-a", "-m", `Release v${version}`]))
    .then(() => spawnGit(["push"]))
    .then(() => gh.getIssues("ArnaudBuchholz", "gpf-js").editMilestone(versionMilestone.id, {
        state: "closed"
    }))
    .then(() => gh.getRepo("ArnaudBuchholz", "gpf-js").createRelease({
        tag_name: `v${version}`,
        name: versionTitle
    }))
    .then(() => console.log(`Version ${version} released.`))
    .then(() => fs.copySync("build/tests.js", `test/legacy/${version}.js`))
    .then(() => spawnGit(["add", `test/legacy/${version}.js`]))
    .then(() => spawnGit(["commit", "-a", "-m", `Tests of v${version}`]))
    .then(() => spawnGit(["push"]))
    .catch(error => console.error(error));
{'fraction_non_alphanumeric': 0.17824275047751345, 'fraction_numerical': 0.001996874457371071, 'mean_word_length': 2.7521172638436484, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 14, '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': '1149482', 'n_tokens_mistral': 3607, 'n_tokens_neox': 3541, 'n_words': 657}