From 58b63b6ef049025d3edca1842a62fc46045800db Mon Sep 17 00:00:00 2001 From: Rui Lopes Date: Wed, 1 Nov 2017 11:36:28 +0000 Subject: [PATCH] add windows npm usage example --- Vagrantfile | 1 + .../export-windows-ca-certificates.ps1 | 16 +++ provision/windows/use-npm-repository.ps1 | 117 ++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 provision/windows/export-windows-ca-certificates.ps1 create mode 100644 provision/windows/use-npm-repository.ps1 diff --git a/Vagrantfile b/Vagrantfile index 72068e0..08d44b7 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -53,6 +53,7 @@ Vagrant.configure(2) do |config| config.vm.provision :shell, path: 'provision/windows/ps.ps1', args: ['provision-base.ps1', nexus_domain] config.vm.provision :shell, path: 'provision/windows/ps.ps1', args: ['use-chocolatey-repository.ps1', nexus_domain] config.vm.provision :shell, path: 'provision/windows/ps.ps1', args: ['use-powershell-repository.ps1', nexus_domain] + config.vm.provision :shell, path: 'provision/windows/ps.ps1', args: ['use-npm-repository.ps1', nexus_domain] end config.trigger.before :up, :vm => ['nexus'] do diff --git a/provision/windows/export-windows-ca-certificates.ps1 b/provision/windows/export-windows-ca-certificates.ps1 new file mode 100644 index 0000000..c03ddce --- /dev/null +++ b/provision/windows/export-windows-ca-certificates.ps1 @@ -0,0 +1,16 @@ +# dump all the windows trusted roots into a ca file. +$pems = New-Object System.Text.StringBuilder +Get-ChildItem Cert:\LocalMachine\Root | ForEach-Object { + # $_ is-a System.Security.Cryptography.X509Certificates.X509Certificate2 + Write-Host "Exporting the $($_.Issuer) certificate..." + [void]$pems.AppendLine('-----BEGIN CERTIFICATE-----') + [void]$pems.AppendLine( + [Convert]::ToBase64String( + $_.Export('Cert'), + 'InsertLineBreaks')); + [void]$pems.AppendLine("-----END CERTIFICATE-----"); +} +Set-Content ` + -Encoding Ascii ` + C:\ProgramData\ca-certificates.crt ` + $pems.ToString() diff --git a/provision/windows/use-npm-repository.ps1 b/provision/windows/use-npm-repository.ps1 new file mode 100644 index 0000000..6ae3fdc --- /dev/null +++ b/provision/windows/use-npm-repository.ps1 @@ -0,0 +1,117 @@ +param( + [string]$nexusDomain = 'nexus.example.com' +) + +function external([string]$cmd, [string[]]$arguments) { + $eap = $ErrorActionPreference + $ErrorActionPreference = 'Continue' + try { + &$cmd @arguments + if ($LASTEXITCODE) { + throw "$cmd failed with exit code $LASTEXITCODE" + } + } finally { + $ErrorActionPreference = $eap + } +} +function node { external node.exe $Args } +function npm { external npm.cmd $Args } + +cd $env:USERPROFILE +mkdir tmp | Out-Null +mkdir tmp/use-npm-repository | Out-Null +cd tmp/use-npm-repository + +# +# test the npm repositories. +# see https://help.sonatype.com/display/NXRM3/Node+Packaged+Modules+and+npm+Registries +# see https://docs.npmjs.com/private-modules/ci-server-config +# see https://docs.npmjs.com/cli/adduser + +# install node LTS. +choco install -y nodejs-lts +Import-Module C:\ProgramData\chocolatey\helpers\chocolateyInstaller.psm1 +Update-SessionEnvironment +node --version +npm --version + +# configure npm to trust our system trusted CAs. +# NB never turn off ssl verification with npm config set strict-ssl false +c:\vagrant\provision\windows\export-windows-ca-certificates.ps1 +npm config set cafile c:/ProgramData/ca-certificates.crt + +# +# configure npm to use the npm-group repository. + +npm config set registry https://$nexusDomain/repository/npm-group/ + +# install a package that indirectly uses the npmjs.org-proxy repository. +mkdir hello-world-win-npm | Out-Null +pushd hello-world-win-npm +Set-Content ` + -Encoding Ascii ` + package.json ` + @' +{ + "name": "hello-world-win", + "description": "the classic hello world", + "version": "1.0.0", + "license": "MIT", + "main": "hello-world-win.js", + "repository": { + "type": "git", + "url": "https://git.example.com/hello-world-win.git" + }, + "dependencies": {} +} +'@ +Set-Content ` + -Encoding Ascii ` + hello-world-win.js ` + @' +const leftPad = require('left-pad') +console.log(leftPad('hello world', 40)) +'@ +npm install --save left-pad +node hello-world-win.js + +# +# publish a package to the npm-hosted repository. + +# login. +$env:NPM_USER='alice.doe' +$env:NPM_PASS='password' +$env:NPM_EMAIL='alice.doe@example.com' +$env:NPM_REGISTRY="https://$nexusDomain/repository/npm-hosted/" +npm install npm-registry-client@8.5.0 +$env:NODE_PATH="$PWD/node_modules" +$env:NODE_EXTRA_CA_CERTS='C:\ProgramData\ca-certificates.crt' +$npmAuthToken = node --use-openssl-ca /vagrant/provision/npm-login.js 2>$null +npm set "//$nexusDomain/repository/npm-hosted/:_authToken" $npmAuthToken + +# publish. +npm publish --registry=$env:NPM_REGISTRY +popd + +# use the published package. +mkdir use-hello-world-win-npm | Out-Null +pushd use-hello-world-win-npm +Set-Content ` + -Encoding Ascii ` + hello-world-win.js ` + @' +{ + "name": "use-hello-world-win", + "description": "use the classic hello world", + "version": "1.0.0", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://git.example.com/use-hello-world-win.git" + }, + "dependencies": {} +} +'@ +npm install hello-world-win +node node_modules/hello-world-win/hello-world-win.js +popd