2016-02-11 14:16:34 +00:00
/ * *
* Copyright ( c ) 2015 - present , Facebook , Inc .
* All rights reserved .
*
* This source code is licensed under the BSD - style license found in the
* LICENSE file in the root directory of this source tree . An additional grant
* of patent rights can be found in the PATENTS file in the same directory .
* /
'use strict' ;
2016-02-12 19:57:01 +00:00
var semverCmp = require ( 'semver-compare' ) ;
2017-07-12 22:56:46 +00:00
const {
cd ,
cp ,
echo ,
exec ,
exit ,
ls ,
mkdir ,
rm ,
which ,
} = require ( 'shelljs' ) ;
2016-02-12 19:57:01 +00:00
const CIRCLE _BRANCH = process . env . CIRCLE _BRANCH ;
2016-02-11 14:16:34 +00:00
const CIRCLE _PROJECT _USERNAME = process . env . CIRCLE _PROJECT _USERNAME ;
const CI _PULL _REQUEST = process . env . CI _PULL _REQUEST ;
2017-11-09 21:18:53 +00:00
const CIRCLE _PR _USERNAME = process . env . CIRCLE _PR _USERNAME ;
2016-02-11 14:16:34 +00:00
const GIT _USER = process . env . GIT _USER ;
const remoteBranch = ` https:// ${ GIT _USER } @github.com/facebook/react-native.git ` ;
2017-10-10 00:37:08 +00:00
if ( ! which ( 'git' ) ) {
echo ( 'Sorry, this script requires git' ) ;
2016-02-11 14:16:34 +00:00
exit ( 1 ) ;
}
2017-11-09 21:18:53 +00:00
if ( CIRCLE _PR _USERNAME ) {
echo ( 'Skipping website deployment, this build was triggered from a commit in a pull request.' ) ;
exit ( 0 ) ;
}
if ( CIRCLE _PROJECT _USERNAME !== 'facebook' ) {
echo ( 'Skipping website deployment, this build was not triggered by a commit on the Facebook org.' ) ;
exit ( 0 ) ;
}
2016-02-11 14:16:34 +00:00
let version ;
2016-09-20 22:56:16 +00:00
let areVersionlessSectionsToBeDeployed = false ;
2017-10-10 00:37:08 +00:00
if ( CIRCLE _BRANCH . indexOf ( '-stable' ) !== - 1 ) {
version = CIRCLE _BRANCH . slice ( 0 , CIRCLE _BRANCH . indexOf ( '-stable' ) ) ;
} else if ( CIRCLE _BRANCH === 'master' ) {
version = 'next' ;
2016-09-20 22:56:16 +00:00
areVersionlessSectionsToBeDeployed = true ;
2016-02-11 14:16:34 +00:00
}
2017-10-10 00:37:08 +00:00
rm ( '-rf' , 'build' ) ;
mkdir ( '-p' , 'build' ) ;
2016-02-11 14:16:34 +00:00
// if current commit is tagged "latest" we do a release to gh-pages root
2017-11-09 21:18:53 +00:00
echo ( 'Current commit:' ) ;
2017-10-10 00:37:08 +00:00
const currentCommit = exec ( 'git rev-parse HEAD' ) . stdout . trim ( ) ;
2017-11-09 21:18:53 +00:00
echo ( 'Searching for commit tagged "latest"...' ) ;
2017-10-10 00:37:08 +00:00
const latestTagCommit = exec ( 'git ls-remote origin latest' ) . stdout . split ( /\s/ ) [ 0 ] ;
2016-02-15 16:37:29 +00:00
// pass along which branch contains latest version so that gh-pages root could mark it as latest
2017-11-09 21:18:53 +00:00
echo ( ` Searching for branch that contains commit ${ latestTagCommit } ... ` ) ;
2016-02-15 21:07:36 +00:00
const branchWithLatestTag = exec ( ` git branch -r --contains ${ latestTagCommit } ` ) . stdout . split ( '/' ) [ 1 ] ;
2017-10-10 00:37:08 +00:00
let latestVersion = '' ;
if ( branchWithLatestTag . indexOf ( '-stable' ) !== - 1 ) {
latestVersion = branchWithLatestTag . slice ( 0 , branchWithLatestTag . indexOf ( '-stable' ) ) ;
2016-02-15 16:37:29 +00:00
}
2016-02-11 14:16:34 +00:00
2017-11-09 21:18:53 +00:00
echo ( ` Building static website based on the ${ version === 'next' ? 'master' : version } branch, preparing to push assets to GitHub Pages via the 'gh-pages' branch)... ` ) ;
// if code is running in a branch in CI, commit changes to gh-pages branch
cd ( 'build' ) ;
rm ( '-rf' , 'react-native-gh-pages' ) ;
2016-02-11 14:16:34 +00:00
2017-11-09 21:18:53 +00:00
if ( exec ( ` git clone ${ remoteBranch } react-native-gh-pages ` ) . code !== 0 ) {
echo ( 'Error: Git clone failed' ) ;
exit ( 1 ) ;
}
cd ( 'react-native-gh-pages' ) ;
if ( exec ( 'git checkout origin/gh-pages' ) . code +
exec ( 'git checkout -b gh-pages' ) . code +
exec ( 'git branch --set-upstream-to=origin/gh-pages' ) . code !== 0
) {
echo ( 'Error: Git checkout gh-pages failed' ) ;
exit ( 1 ) ;
}
cd ( 'releases' ) ;
let releasesFolders = ls ( '-d' , '*' ) ;
cd ( '..' ) ;
let versions = releasesFolders . filter ( name => name !== 'next' ) ;
if ( version !== 'next' && versions . indexOf ( version ) === - 1 ) {
versions . push ( version ) ;
}
2016-02-11 14:16:34 +00:00
2017-11-09 21:18:53 +00:00
versions . sort ( semverCmp ) . reverse ( ) ;
2016-02-11 14:16:34 +00:00
2017-11-09 21:18:53 +00:00
// generate to releases/XX when branch name indicates that it is some sort of release
if ( version ) {
echo ( ` ------------ DEPLOYING /releases/ ${ version } ` ) ;
rm ( '-rf' , ` releases/ ${ version } ` ) ;
mkdir ( '-p' , ` releases/ ${ version } ` ) ;
cd ( '../..' ) ;
if ( exec ( ` RN_DEPLOYMENT_PATH=releases/ ${ version } RN_VERSION= ${ version } RN_LATEST_VERSION= ${ latestVersion } \
RN _AVAILABLE _DOCS _VERSIONS = $ { versions . join ( ',' ) } node server / generate . js ` ).code !== 0) {
echo ( 'Error: Generating HTML failed' ) ;
2016-02-11 14:16:34 +00:00
exit ( 1 ) ;
}
2017-11-09 21:18:53 +00:00
cd ( 'build/react-native-gh-pages' ) ;
// blog, showcase, support are copied separately
let toCopy = ls ( '../react-native' )
. filter ( file => ( file !== 'blog' ) && ( file !== 'showcase.html' ) && ( file !== 'support.html' ) )
. map ( file => ` ../react-native/ ${ file } ` ) ;
cp ( '-R' , toCopy , ` releases/ ${ version } ` ) ;
// versions.html is located in root of website and updated with every release
cp ( '../react-native/versions.html' , '.' ) ;
}
// generate to root folder when commit is tagged as latest, i.e. stable and needs to be shown at the root of repo
if ( currentCommit === latestTagCommit ) {
echo ( '------------ DEPLOYING latest' ) ;
// leave only releases and blog folder
rm ( '-rf' , ls ( '*' ) . filter ( name => ( name !== 'releases' ) && ( name !== 'blog' ) && ( name !== 'showcase.html' ) && ( name !== 'support.html' ) ) ) ;
cd ( '../..' ) ;
if ( exec ( ` RN_VERSION= ${ version } RN_LATEST_VERSION= ${ latestVersion } \
RN _AVAILABLE _DOCS _VERSIONS = $ { versions } node server / generate . js ` ).code !== 0) {
echo ( 'Error: Generating HTML failed' ) ;
exit ( 1 ) ;
2016-02-12 19:57:01 +00:00
}
2017-11-09 21:18:53 +00:00
cd ( 'build/react-native-gh-pages' ) ;
// blog, showcase, support are copied separately
let toCopy = ls ( '../react-native' )
. filter ( file => ( file !== 'blog' ) && ( file !== 'showcase.html' ) && ( file !== 'support.html' ) )
. map ( file => ` ../react-native/ ${ file } ` ) ;
cp ( '-R' , toCopy , '.' ) ;
}
// blog, showcase, support are versionless, we always build them in root file
if ( areVersionlessSectionsToBeDeployed ) {
echo ( '------------ COPYING blog' ) ;
rm ( '-rf' , 'blog' ) ;
cp ( '-R' , '../react-native/blog' , '.' ) ;
echo ( '------------ COPYING showcase' ) ;
cp ( '../react-native/showcase.html' , '.' ) ;
echo ( '------------ COPYING support' ) ;
cp ( '../react-native/support.html' , '.' ) ;
}
if ( currentCommit === latestTagCommit || version ) {
exec ( 'git status' ) ;
exec ( 'git add -A .' ) ;
if ( exec ( 'git diff-index --quiet HEAD --' ) . code !== 0 ) {
if ( exec ( ` git commit -m "Updated docs for ${ version } " ` ) . code !== 0 ) {
echo ( 'Error: Git commit gh-pages failed' ) ;
2016-02-11 14:16:34 +00:00
exit ( 1 ) ;
}
2017-11-09 21:18:53 +00:00
if ( exec ( 'git push origin gh-pages' ) . code !== 0 ) {
echo ( 'Error: Git push gh-pages failed' ) ;
2016-02-11 14:16:34 +00:00
exit ( 1 ) ;
}
}
2017-11-09 21:18:53 +00:00
echo ( '------------ gh-pages updated' ) ;
2016-02-11 14:16:34 +00:00
}