Skip to content
This repository has been archived by the owner on Jun 29, 2018. It is now read-only.

Commit

Permalink
More comprehensive update script - Push to npm and GitHub Releases. R…
Browse files Browse the repository at this point in the history
…eferences #69
  • Loading branch information
Daniel15 committed Nov 20, 2016
1 parent d6c4660 commit 155e522
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
5 changes: 5 additions & 0 deletions scripts/config.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$global:config = @{
github_user = 'Daniel15'
github_repo = 'yarn-release-test'
github_token = 'CHANGEME'
}
75 changes: 68 additions & 7 deletions scripts/update.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# Checks for updates to Babel. If a new version is available, upgrades the Babel references in
# Checks for updates to Babel. If a new version is available, upgrades the Babel references in
# package.json to the new version, commits the new package.json, and pushes it to Github.

Set-StrictMode -Version Latest
param(
[switch] $Clean = $false
)

$ErrorActionPreference = "Stop";
Set-StrictMode -Version Latest

. ./scripts/config.ps1

############################


# Formats JSON in a nicer format than the built-in ConvertTo-Json does.
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
Expand All @@ -23,10 +33,10 @@ function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {

# Gets the latest version of a dependency from package.json
function Get-LatestDependencyVersion(
[Parameter(Mandatory)] [PSCustomObject] $package,
[Parameter(Mandatory)] [PSCustomObject] $package,
[Parameter(Mandatory)] [String] $filter
) {
$relevant_packages = $package.devDependencies.PSObject.Members |
$relevant_packages = $package.devDependencies.PSObject.Members |
Where-Object { $_.MemberType -eq 'NoteProperty' -and $_.Name -match $filter}
$latest_version = ([Version] $relevant_packages[0].Value)

Expand All @@ -42,7 +52,7 @@ function Get-LatestDependencyVersion(

# Sets the version number in package.json
function Set-PackageVersion(
[Parameter(Mandatory)] [PSCustomObject] $Package,
[Parameter(Mandatory)] [PSCustomObject] $Package,
[Parameter(Mandatory)] [String] $Version
) {
Write-Output 'Updating version in package.json'
Expand All @@ -56,7 +66,45 @@ function Assert-LastExitCode {
throw 'Non-zero exit code encountered'
}
}


function New-GitHubRelease(
[Parameter(Mandatory)] [String] $Version
) {
Invoke-RestMethod `
-Uri ('https://api.github.com/repos/{0}/{1}/releases?access_token={2}' -f $global:config.github_user, $global:config.github_repo, $global:config.github_token) `
-Method Post `
-Body (@{
body = 'Automated upgrade to Babel ' + $version
draft = $false
name = $version
tag_name = 'v' + $version
} | ConvertTo-Json)
}

function Add-GitHubReleaseAsset(
[Parameter(Mandatory)] [PSCustomObject] $Release,
[Parameter(Mandatory)] [String] $Path
) {
$filename = Split-Path -Path $Path -Leaf
$upload_url = $release.upload_url -replace '{.+', ''
Invoke-WebRequest `
-Uri ('{0}?name={1}&access_token={2}' -f $upload_url, $filename, $global:config.github_token) `
-Method Post `
-ContentType 'text/javascript' `
-Body (Get-Content -Path $Path -Raw) | Out-Null

Write-Output ('Uploaded ' + $filename)
}

############################

if ($Clean) {
Write-Output "Cleaning working directory"
git reset --hard HEAD
git clean -fd
git pull
}

.\node_modules\.bin\npm-check-updates -a; Assert-LastExitCode

$package_json = Get-Content -Path package.json | ConvertFrom-Json
Expand All @@ -69,7 +117,7 @@ if (([Version]$package_json.version) -ge $babel_version) {
Write-Output ('Current version is {0}, latest Babel version is {1}' -f $package_json.version, $babel_version)

npm install; Assert-LastExitCode

Write-Output 'Building and running tests'
npm run build; Assert-LastExitCode
npm run test; Assert-LastExitCode
Expand All @@ -78,6 +126,19 @@ Write-Output 'Build looks okay, committing and pushing changes'
Set-PackageVersion -Package $package_json -Version $babel_version
git commit -m ('Upgrade to Babel {0}' -f $babel_version) --author='DanBuild <[email protected]>' package.json; Assert-LastExitCode
git tag -a ('release-' + $babel_version) -m ('Automated upgrade to Babel {0}' -f $babel_version); Assert-LastExitCode

# Push to Github
git push origin master --follow-tags; Assert-LastExitCode

# Push to npm
npm publish

# Push to GitHub releases
Write-Output "Creating GitHub release..."
$release = New-GitHubRelease -Version $babel_version
Add-GitHubReleaseAsset -Release $release -Path ./babel.js
Add-GitHubReleaseAsset -Release $release -Path ./babel.min.js
Add-GitHubReleaseAsset -Release $release -Path ./packages/babili-standalone/babili.js
Add-GitHubReleaseAsset -Release $release -Path ./packages/babili-standalone/babili.min.js

Write-Output 'DONE!'

0 comments on commit 155e522

Please sign in to comment.