build-and-publish-nightly.js 3.0 KB

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