build-and-publish-nightly.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env node
  2. /**
  3. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  4. * For licensing, see LICENSE.md.
  5. */
  6. /* eslint-env node */
  7. 'use strict';
  8. /*
  9. This script is to be used on CI to automatically update https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/.
  10. */
  11. // Build the documentation only when master branch is updated.
  12. if ( process.env.TRAVIS_BRANCH !== 'master' ) {
  13. process.exit();
  14. }
  15. // Build the documentation only when a cron task triggered the CI.
  16. if ( process.env.TRAVIS_EVENT_TYPE !== 'cron' ) {
  17. process.exit();
  18. }
  19. const path = require( 'path' );
  20. const { tools } = require( '@ckeditor/ckeditor5-dev-utils' );
  21. const mainRepoUrl = 'https://github.com/CKEditor5/ckeditor5.github.io';
  22. // The assumption here is that the script is called from ckeditor5/.
  23. const projectVersion = require( path.join( process.cwd(), 'package.json' ) ).version;
  24. // Clone the CKEditor 5 page.
  25. console.log( 'Cloning ckeditor5.github.io repository...' );
  26. exec( `git clone ${ mainRepoUrl }.git` );
  27. // Build the documentation.
  28. console.log( 'Building documentation...' );
  29. exec( 'npm run docs -- --production' );
  30. console.log( 'Copying files...' );
  31. // Remove existing documentation.
  32. exec( `rm -rf ckeditor5.github.io/docs/nightly/ckeditor5/${ projectVersion }` );
  33. exec( 'rm -rf ckeditor5.github.io/docs/nightly/ckeditor5/latest' );
  34. // Copy built documentation to the new destination.
  35. exec( 'cp -R build/docs/* ckeditor5.github.io/docs/nightly/' );
  36. // Umberto makes a symlink between the latest version (`projectVersion`) and "latest/" directory.
  37. // The symlink must be deleted in order to copy a documentation for `projectVersion` as `latest`.
  38. exec( 'rm -rf ckeditor5.github.io/docs/nightly/ckeditor5/latest' );
  39. // Copy the versioned documentation to latest/.
  40. exec( `cp -R ckeditor5.github.io/docs/nightly/ckeditor5/${ projectVersion } ckeditor5.github.io/docs/nightly/ckeditor5/latest` );
  41. // Change work directory in order to make a commit in CKEditor 5 page's repository.
  42. process.chdir( path.join( process.cwd(), 'ckeditor5.github.io' ) );
  43. exec( `echo "https://${ process.env.GITHUB_TOKEN }:@github.com" > .git/credentials 2> /dev/null` );
  44. exec( 'git config credential.helper "store --file=.git/credentials"' );
  45. // Commit the documentation.
  46. if ( exec( 'git diff --name-only docs/' ).trim().length ) {
  47. exec( 'git add docs/' );
  48. exec( 'git commit -m "Documentation build."' );
  49. exec( 'git push origin master --quiet' );
  50. const lastCommit = exec( 'git log -1 --format="%h"' );
  51. console.log( `Successfully published the documentation under ${ mainRepoUrl }/commit/${ lastCommit }` );
  52. } else {
  53. console.log( 'Nothing to commit. Documentation is up to date.' );
  54. }
  55. function exec( command ) {
  56. try {
  57. return tools.shExec( command, { verbosity: 'error' } );
  58. }
  59. catch ( error ) {
  60. console.error( error );
  61. process.exit( 1 );
  62. }
  63. }