<commit_before>// @flow

import merge from 'deepmerge'
import yaml from 'js-yaml'
import type AutoreleaseYml from './autorelease-yml'

/**
 * Generator for circle.yml
 */
export default class CircleYml {

    /**
     * @public
     */
    static generate(arYml: AutoreleaseYml): string {

        const standard = this.standard(arYml)
        const custom = arYml.circle

        const merged = merge(standard, custom)
        return yaml.dump(merged, {indent: 2, lineWidth: 120})
    }


    /**
     * @private
     */
    static standard(arYml: AutoreleaseYml): Object {
        return {
            general: {
                branches: {
                    ignore: [ 'gh-pages', '/release.*/' ]
                }
            },

            machine: {

                environment: this.environment(arYml.config('npm_bin_path')),

                pre: [
                    `git config --global user.name "${arYml.config('git_user_name')}"`,
                    `git config --global user.email "${arYml.config('git_user_email')}"`
                ],
            },

            dependencies: {
                post: flat(
                    arYml.hooks('update_modules', 'pre'),
                    this.updateModulesCommand(arYml.config('npm_update_depth')),
                    arYml.hooks('update_modules', 'post')
                )
            },

            deployment: {
                create_release_branch: {
                    branch: ['master'],
                    commands: flat(
                        arYml.hooks('release', 'pre'),
                        this.releaseCommand(arYml),
                        arYml.hooks('release', 'post'),

                        arYml.hooks('gh_pages', 'pre'),
                        this.ghPagesCommand(arYml.config('create_gh_pages'), arYml.config('gh_pages_dir')),
                        arYml.hooks('gh_pages', 'post'),
                    )
                }
            }
        }
    }

    /**
     * generate command to update node_modules
     * @private
     */
    static environment(enableNpmBinPath: primitive): ?{PATH: string} {
        if (!enableNpmBinPath) return undefined

        return { PATH: './node_modules/.bin:$PATH' }
    }


    /**
     * generate command to update node_modules
     * @private
     */
    static updateModulesCommand(depth: primitive): string {
        if (!depth) {
            return '$(npm bin)/nca notice update-modules'
        }
        return `$(npm bin)/nca update-modules --depth ${depth}`
    }

    /**
     * generate command to release
     * @private
     */
    static releaseCommand(arYml: AutoreleaseYml): string {
        const options = {
            prefix:       arYml.config('version_prefix'),
            branch:     !!arYml.config('create_branch'),
            shrinkwrap: !!arYml.config('npm_shrinkwrap')
        }
        return '$(npm bin)/nca release ' + this.optionStr(options)
    }


    /**
     * generate command to create gh-pages branch
     * @private
     */
    static ghPagesCommand(create: primitive, dir: primitive): string {

        if (!create) {
            return '$(npm bin)/nca notice gh-pages'
        }
        let command = '$(npm bin)/nca gh-pages'
        if (dir) {
            command += ` --dir ${dir}`
        }
        return command
    }

    /**
     * generate command line option string
     * @private
     */
    static optionStr(options: Object): string {
<commit_msg>remove 'npm bin'<commit_after>// @flow

import merge from 'deepmerge'
import yaml from 'js-yaml'
import type AutoreleaseYml from './autorelease-yml'

/**
 * Generator for circle.yml
 */
export default class CircleYml {

    /**
     * @public
     */
    static generate(arYml: AutoreleaseYml): string {

        const standard = this.standard(arYml)
        const custom = arYml.circle

        const merged = merge(standard, custom)
        return yaml.dump(merged, {indent: 2, lineWidth: 120})
    }


    /**
     * @private
     */
    static standard(arYml: AutoreleaseYml): Object {
        return {
            general: {
                branches: {
                    ignore: [ 'gh-pages', '/release.*/' ]
                }
            },

            machine: {

                environment: this.environment(arYml.config('npm_bin_path')),

                pre: [
                    `git config --global user.name "${arYml.config('git_user_name')}"`,
                    `git config --global user.email "${arYml.config('git_user_email')}"`
                ],
            },

            dependencies: {
                post: flat(
                    arYml.hooks('update_modules', 'pre'),
                    this.updateModulesCommand(arYml.config('npm_update_depth')),
                    arYml.hooks('update_modules', 'post')
                )
            },

            deployment: {
                create_release_branch: {
                    branch: ['master'],
                    commands: flat(
                        arYml.hooks('release', 'pre'),
                        this.releaseCommand(arYml),
                        arYml.hooks('release', 'post'),

                        arYml.hooks('gh_pages', 'pre'),
                        this.ghPagesCommand(arYml.config('create_gh_pages'), arYml.config('gh_pages_dir')),
                        arYml.hooks('gh_pages', 'post'),
                    )
                }
            }
        }
    }

    /**
     * generate command to update node_modules
     * @private
     */
    static environment(enableNpmBinPath: primitive): ?{PATH: string} {
        if (!enableNpmBinPath) return undefined

        return { PATH: './node_modules/.bin:$PATH' }
    }


    /**
     * generate command to update node_modules
     * @private
     */
    static updateModulesCommand(depth: primitive): string {
        if (!depth) {
            return 'nca notice update-modules'
        }
        return `nca update-modules --depth ${depth}`
    }

    /**
     * generate command to release
     * @private
     */
    static releaseCommand(arYml: AutoreleaseYml): string {
        const options = {
            prefix:       arYml.config('version_prefix'),
            branch:     !!arYml.config('create_branch'),
            shrinkwrap: !!arYml.config('npm_shrinkwrap')
        }
        return 'nca release ' + this.optionStr(options)
    }


    /**
     * generate command to create gh-pages branch
     * @private
     */
    static ghPagesCommand(create: primitive, dir: primitive): string {

        if (!create) {
            return 'nca notice gh-pages'
        }
        let command = 'nca gh-pages'
        if (dir) {
            command += ` --dir ${dir}`
        }
        return command
    }

    /**
     * generate command line option string
     * @private
     */
    static optionStr(options: Object): string {

{'fraction_non_alphanumeric': 0.12218649517684887, 'fraction_numerical': 0.001169248757673195, 'mean_word_length': 1.5241608262633715, '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': '4188525', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1848, 'n_words': 393}