<commit_before>module Travis
  module Build
    class Script
      class NodeJs < Script
        DEFAULT_VERSION = '0.10'

        NVM_VERSION     = '0.32.0' # to coincide with ../files/nvm.sh version

        YARN_REQUIRED_NODE_VERSION = '4'

        def export
          super
          if node_js_given_in_config?
            sh.export 'TRAVIS_NODE_VERSION', version, echo: false
          end
        end

        def setup
          super
          prepend_path './node_modules/.bin'
          convert_legacy_nodejs_config
          update_nvm
          nvm_install
          npm_disable_prefix
          npm_disable_spinner
          npm_disable_progress
          npm_disable_strict_ssl unless npm_strict_ssl?
          setup_npm_cache if use_npm_cache?
        end

        def announce
          super
          if iojs_3_plus?
            sh.cmd 'echo -e "#include <array>\nstd::array<int, 1> arr = {0}; int main() {return 0;}" > /tmp/foo-$$.cpp', echo: false
            sh.raw "if ! ($CXX -std=c++11 -o /dev/null /tmp/foo-$$.cpp >&/dev/null || g++ -std=c++11 -o /dev/null /tmp/foo-$$.cpp >&/dev/null); then"
            sh.echo "Starting with io.js 3 and Node.js 4, building native extensions requires C++11-compatible compiler, which seems unavailable on this VM. Please read https://docs.travis-ci.com/user/languages/javascript-with-nodejs#Node.js-v4-(or-io.js-v3)-compiler-requirements.", ansi: :yellow
            sh.raw "fi"
            sh.cmd 'rm -f /tmp/foo-$$.cpp', echo: false
          end
          sh.cmd 'node --version'
          sh.cmd 'npm --version'
          sh.cmd 'nvm --version'
        end

        def install
          sh.if '-f package.json' do
            sh.if "-f yarn.lock" do
              sh.if "$(vers2int $(echo `node --version` | tr -d 'v')) -lt $(vers2int #{YARN_REQUIRED_NODE_VERSION})" do
                sh.echo "Node.js version $(node --version) does not meet requirement for yarn." \
                  " Please use Node.js #{YARN_REQUIRED_NODE_VERSION} or later.", ansi: :red
                npm_install config[:npm_args]
              end
              sh.else do
                sh.if "-z \"$(command -v yarn)\"" do
                  install_yarn
                end
                sh.cmd "yarn", retry: true, fold: 'install'
              end
            end
            sh.else do
              npm_install config[:npm_args]
            end
          end
        end

        def script
          sh.if '-f package.json' do
            sh.cmd 'npm test'
          end
          sh.else do
            sh.cmd 'make test'
          end
        end

        def cache_slug
          super << '--node-' << version
        end

        def setup_cache
          if data.cache?(:yarn)
            sh.fold 'cache.yarn' do
              sh.echo ''
              directory_cache.add '$HOME/.cache/yarn'
            end
          end
        end

        def use_directory_cache?
          super || data.cache?(:yarn)
        end

        private

          def convert_legacy_nodejs_config
            # TODO deprecate :nodejs
            # some old projects use language: nodejs. MK.
            if config[:nodejs] && !config[:node_js]
              config[:node_js] = config[:nodejs]
            end
          end

          def node_js_given_in_config?
            !!config[:node_js]
          end

          def version
            @version ||= begin
              version = Array(config[:node_js]).first
              version == 0.1 ? '0.10' : version.to_s
            end
          end

          def nvm_install
            if node_js_given_in_config?
              use_nvm_version
            else
              use_nvm_default
            end
          end

          def use_nvm_default
            sh.if '-f .nvmrc' do
              sh.echo "Using nodejs version from .nvmrc", ansi: :yellow
              install_version '$(< .nvmrc)'
            end
            sh.else do
              install_version DEFAULT_VERSION
            end
          end

          def use_nvm_version
            install_version version
          end

          def install_version(ver)
            sh.cmd "nvm install #{ver}", assert: false
            sh.if '$? -ne 0' do
              sh.echo "Failed to install #{ver}. Remote repository may not be reachable.", ansi: :red
              sh.echo "Using locally available version #{ver}, if applicable."
              sh.cmd "nvm use #{ver}", assert: false, timing: false
              sh.if '$? -ne 0' do
                sh.echo "Unable to use #{ver}", ansi: :red
                sh.cmd "false", assert: true, echo: false, timing: false
              end
            end
            sh.export 'TRAVIS_NODE_VERSION', ver, echo: false
          end

          def update_nvm
            return if app_host.empty?
            sh.if "$(vers2int `nvm --version`) -lt $(vers2int #{NVM_VERSION})" do
              sh.echo "Updating nvm to v#{NVM_VERSION}", ansi: :yellow, timing: false
              sh.raw "mkdir -p $HOME/.nvm"
              ['nvm.sh', 'nvm-exec'].each do |f|
                sh.raw "curl -s -o $HOME/.nvm/#{f} https://#{app_host}/files/#{f}".untaint, assert: false
              end
              sh.raw "source $HOME/.nvm/nvm.sh", assert: false
            end
          end

          def app_host
            @app_host ||= Travis::Build.config.app_host.to_s.strip.untaint
          end

          def npm_disable_prefix
            sh.if "$(command -v sw_vers) && -f $HOME/.npmrc" do
              sh.cmd "npm config delete prefix"
            end
          end

          def npm_disable_spinner<commit_msg>Be more explicit on which files to fetch from web app<commit_after>module Travis
  module Build
    class Script
      class NodeJs < Script
        DEFAULT_VERSION = '0.10'

        NVM_VERSION     = '0.32.0' # to coincide with ../files/nvm.sh version

        YARN_REQUIRED_NODE_VERSION = '4'

        def export
          super
          if node_js_given_in_config?
            sh.export 'TRAVIS_NODE_VERSION', version, echo: false
          end
        end

        def setup
          super
          prepend_path './node_modules/.bin'
          convert_legacy_nodejs_config
          update_nvm
          nvm_install
          npm_disable_prefix
          npm_disable_spinner
          npm_disable_progress
          npm_disable_strict_ssl unless npm_strict_ssl?
          setup_npm_cache if use_npm_cache?
        end

        def announce
          super
          if iojs_3_plus?
            sh.cmd 'echo -e "#include <array>\nstd::array<int, 1> arr = {0}; int main() {return 0;}" > /tmp/foo-$$.cpp', echo: false
            sh.raw "if ! ($CXX -std=c++11 -o /dev/null /tmp/foo-$$.cpp >&/dev/null || g++ -std=c++11 -o /dev/null /tmp/foo-$$.cpp >&/dev/null); then"
            sh.echo "Starting with io.js 3 and Node.js 4, building native extensions requires C++11-compatible compiler, which seems unavailable on this VM. Please read https://docs.travis-ci.com/user/languages/javascript-with-nodejs#Node.js-v4-(or-io.js-v3)-compiler-requirements.", ansi: :yellow
            sh.raw "fi"
            sh.cmd 'rm -f /tmp/foo-$$.cpp', echo: false
          end
          sh.cmd 'node --version'
          sh.cmd 'npm --version'
          sh.cmd 'nvm --version'
        end

        def install
          sh.if '-f package.json' do
            sh.if "-f yarn.lock" do
              sh.if "$(vers2int $(echo `node --version` | tr -d 'v')) -lt $(vers2int #{YARN_REQUIRED_NODE_VERSION})" do
                sh.echo "Node.js version $(node --version) does not meet requirement for yarn." \
                  " Please use Node.js #{YARN_REQUIRED_NODE_VERSION} or later.", ansi: :red
                npm_install config[:npm_args]
              end
              sh.else do
                sh.if "-z \"$(command -v yarn)\"" do
                  install_yarn
                end
                sh.cmd "yarn", retry: true, fold: 'install'
              end
            end
            sh.else do
              npm_install config[:npm_args]
            end
          end
        end

        def script
          sh.if '-f package.json' do
            sh.cmd 'npm test'
          end
          sh.else do
            sh.cmd 'make test'
          end
        end

        def cache_slug
          super << '--node-' << version
        end

        def setup_cache
          if data.cache?(:yarn)
            sh.fold 'cache.yarn' do
              sh.echo ''
              directory_cache.add '$HOME/.cache/yarn'
            end
          end
        end

        def use_directory_cache?
          super || data.cache?(:yarn)
        end

        private

          def convert_legacy_nodejs_config
            # TODO deprecate :nodejs
            # some old projects use language: nodejs. MK.
            if config[:nodejs] && !config[:node_js]
              config[:node_js] = config[:nodejs]
            end
          end

          def node_js_given_in_config?
            !!config[:node_js]
          end

          def version
            @version ||= begin
              version = Array(config[:node_js]).first
              version == 0.1 ? '0.10' : version.to_s
            end
          end

          def nvm_install
            if node_js_given_in_config?
              use_nvm_version
            else
              use_nvm_default
            end
          end

          def use_nvm_default
            sh.if '-f .nvmrc' do
              sh.echo "Using nodejs version from .nvmrc", ansi: :yellow
              install_version '$(< .nvmrc)'
            end
            sh.else do
              install_version DEFAULT_VERSION
            end
          end

          def use_nvm_version
            install_version version
          end

          def install_version(ver)
            sh.cmd "nvm install #{ver}", assert: false
            sh.if '$? -ne 0' do
              sh.echo "Failed to install #{ver}. Remote repository may not be reachable.", ansi: :red
              sh.echo "Using locally available version #{ver}, if applicable."
              sh.cmd "nvm use #{ver}", assert: false, timing: false
              sh.if '$? -ne 0' do
                sh.echo "Unable to use #{ver}", ansi: :red
                sh.cmd "false", assert: true, echo: false, timing: false
              end
            end
            sh.export 'TRAVIS_NODE_VERSION', ver, echo: false
          end

          def update_nvm
            return if app_host.empty?
            sh.if "$(vers2int `nvm --version`) -lt $(vers2int #{NVM_VERSION})" do
              sh.echo "Updating nvm to v#{NVM_VERSION}", ansi: :yellow, timing: false
              nvm_dir = "$HOME/.nvm"
              sh.raw "mkdir -p #{nvm_dir}"
              sh.raw "curl -s -o #{nvm_dir}/nvm.sh   https://#{app_host}/files/nvm.sh".untaint,   assert: false
              sh.raw "curl -s -o #{nvm_dir}/nvm-exec https://#{app_host}/files/nvm-exec".untaint, assert: false
              sh.raw "source #{nvm_dir}/nvm.sh", assert: false
            end
          end

          def app_host
            @app_host ||= Travis::Build.config.app_host.to_s.strip.untaint
          end

          def npm_disable_prefix
            sh.if "$(command -v sw_vers) && -f $HOME/.npmrc" do
              sh.cmd "npm config delete prefix"
            end
          end

          def npm_disable_spinner
{'fraction_non_alphanumeric': 0.10179640718562874, 'fraction_numerical': 0.005811905600563579, 'mean_word_length': 1.3884332281808622, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, 'https://': 5, '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': '2344740', 'n_tokens_mistral': 3425, 'n_tokens_neox': 3261, 'n_words': 912}