"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.readPackageVersion = exports.NpmVersionIncrementerRegistration = exports.NpmVersionIncrementer = exports.NpmVersionerRegistration = exports.NpmVersioner = void 0;
const semver = require("semver");
const logInterpreters_1 = require("../../../api-helper/log/logInterpreters");
const child_process_1 = require("../../../api-helper/misc/child_process");
const dateFormat_1 = require("../../../api-helper/misc/dateFormat");
const projectConfiguration_1 = require("../../../api-helper/project/configuration/projectConfiguration");
const GoalNameGenerator_1 = require("../../../api/goal/GoalNameGenerator");
const nodePushTests_1 = require("../pushtest/nodePushTests");
const executePublish_1 = require("./executePublish");
const packageJson = "package.json";
/**
 * Create timestamped pre-prelease, branch-aware version based on
 * version in package.json file.
 */
const NpmVersioner = async (sdmGoal, p, log) => {
    const version = await readPackageVersion(p, log);
    log.write(`Using base version '${version}'`);
    const branch = sdmGoal.branch.split("/").join(".");
    const tagMaster = await projectConfiguration_1.projectConfigurationValue("npm.publish.tag.defaultBranch", p, false);
    let branchSuffix = "";
    if (tagMaster) {
        branchSuffix = `${branch}.`;
    }
    else {
        branchSuffix = branch !== sdmGoal.push.repo.defaultBranch ? `${branch}.` : "";
    }
    const prereleaseVersion = `${version}-${executePublish_1.gitBranchToNpmVersion(branchSuffix)}${dateFormat_1.formatDate()}`;
    log.write(`Calculated pre-release version '${prereleaseVersion}'`);
    return prereleaseVersion;
};
exports.NpmVersioner = NpmVersioner;
/**
 * Versioner function registration for the [[Version]] goal.
 */
exports.NpmVersionerRegistration = {
    logInterpreter: logInterpreters_1.LogSuppressor,
    name: GoalNameGenerator_1.DefaultGoalNameGenerator.generateName("npm-versioner"),
    pushTest: nodePushTests_1.IsNode,
    versioner: exports.NpmVersioner,
};
/**
 * Command for incrementing the patch value in package.json.
 *
 * @param args Standard project incrementer arguments
 * @return Goal execution result
 */
const NpmVersionIncrementer = async (args) => {
    const slug = `${args.id.owner}/${args.id.repo}`;
    try {
        const currentVersion = await readPackageVersion(args.project, args.log);
        if (semver.gt(currentVersion, args.currentVersion)) {
            const msg = `Version in ${slug} appears to have already been incremented, expected '${args.currentVersion}' ` +
                `but found '${currentVersion}'`;
            args.log.write(msg);
            return { code: 1, message: msg };
        }
        else if (currentVersion === defaultVersion) {
            if (!(await args.project.hasFile(packageJson))) {
                try {
                    await args.project.addFile(packageJson, `{"name":"@${slug}","version":"${args.currentVersion}"}\n`);
                }
                catch (e) {
                    const msg = `The ${packageJson} file did not exist in ${slug} and failed to create one: ${e.message}`;
                    args.log.write(msg);
                    return { code: 1, message: msg };
                }
            }
        }
        const newVersion = semver.inc(args.currentVersion, args.increment);
        const incrementResult = await child_process_1.spawnLog("npm", ["version", "--no-git-tag-version", newVersion], {
            cwd: args.project.baseDir,
            log: args.log,
        });
        if (incrementResult.code !== 0) {
            throw incrementResult.error;
        }
        const message = `Incremented ${args.increment} version in ${slug}: ${args.currentVersion} => ${newVersion}`;
        args.log.write(message);
        return { code: 0, message };
    }
    catch (e) {
        const message = `Failed to increment ${args.increment} version in ${slug}: ${e.message}`;
        args.log.write(message);
        return { code: 1, message };
    }
};
exports.NpmVersionIncrementer = NpmVersionIncrementer;
/**
 * Increment version registration for [[NpmVersionIncrementer]].
 */
exports.NpmVersionIncrementerRegistration = {
    logInterpreter: logInterpreters_1.LogSuppressor,
    name: GoalNameGenerator_1.DefaultGoalNameGenerator.generateName("npm-version-incrementer"),
    pushTest: nodePushTests_1.IsNode,
    versionIncrementer: exports.NpmVersionIncrementer,
};
const defaultVersion = "0.0.0";
/**
 * Read version from package.json in project p.  If it fails to
 * determine the version from the package.json, it returns "0.0.0".
 */
async function readPackageVersion(p, log) {
    let pjVersion;
    try {
        const pjFile = await p.getFile(packageJson);
        if (pjFile) {
            const pj = JSON.parse(await pjFile.getContent());
            pjVersion = pj.version;
        }
    }
    catch (e) {
        log.write(`Error reading version from package.json: ${e.message}`);
    }
    if (!pjVersion) {
        pjVersion = defaultVersion;
        log.write(`Failed to read version from package.json, using '${pjVersion}'`);
    }
    return pjVersion;
}
exports.readPackageVersion = readPackageVersion;
//# sourceMappingURL=npmVersioner.js.map