gh-pages.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env node
  2. const ghpages = require('../lib/index.js');
  3. const program = require('commander');
  4. const path = require('path');
  5. const pkg = require('../package.json');
  6. const addr = require('email-addresses');
  7. function publish(config) {
  8. return new Promise((resolve, reject) => {
  9. const basePath = path.resolve(process.cwd(), program.dist);
  10. ghpages.publish(basePath, config, (err) => {
  11. if (err) {
  12. return reject(err);
  13. }
  14. resolve();
  15. });
  16. });
  17. }
  18. function main(args) {
  19. return Promise.resolve().then(() => {
  20. program
  21. .version(pkg.version)
  22. .option('-d, --dist <dist>', 'Base directory for all source files')
  23. .option(
  24. '-s, --src <src>',
  25. 'Pattern used to select which files to publish',
  26. ghpages.defaults.src
  27. )
  28. .option(
  29. '-b, --branch <branch>',
  30. 'Name of the branch you are pushing to',
  31. ghpages.defaults.branch
  32. )
  33. .option(
  34. '-e, --dest <dest>',
  35. 'Target directory within the destination branch (relative to the root)',
  36. ghpages.defaults.dest
  37. )
  38. .option('-a, --add', 'Only add, and never remove existing files')
  39. .option('-x, --silent', 'Do not output the repository url')
  40. .option(
  41. '-m, --message <message>',
  42. 'commit message',
  43. ghpages.defaults.message
  44. )
  45. .option('-g, --tag <tag>', 'add tag to commit')
  46. .option('--git <git>', 'Path to git executable', ghpages.defaults.git)
  47. .option('-t, --dotfiles', 'Include dotfiles')
  48. .option('-r, --repo <repo>', 'URL of the repository you are pushing to')
  49. .option('-p, --depth <depth>', 'depth for clone', ghpages.defaults.depth)
  50. .option(
  51. '-o, --remote <name>',
  52. 'The name of the remote',
  53. ghpages.defaults.remote
  54. )
  55. .option(
  56. '-u, --user <address>',
  57. 'The name and email of the user (defaults to the git config). Format is "Your Name <email@example.com>".'
  58. )
  59. .option(
  60. '-v, --remove <pattern>',
  61. 'Remove files that match the given pattern ' +
  62. '(ignored if used together with --add).',
  63. ghpages.defaults.remove
  64. )
  65. .option('-n, --no-push', 'Commit only (with no push)')
  66. .option(
  67. '-f, --no-history',
  68. 'Push force new commit without parent history'
  69. )
  70. .option(
  71. '--before-add <file>',
  72. 'Execute the function exported by <file> before "git add"'
  73. )
  74. .parse(args);
  75. let user;
  76. if (program.user) {
  77. const parts = addr.parseOneAddress(program.user);
  78. if (!parts) {
  79. throw new Error(
  80. `Could not parse name and email from user option "${program.user}" ` +
  81. '(format should be "Your Name <email@example.com>")'
  82. );
  83. }
  84. user = {name: parts.name, email: parts.address};
  85. }
  86. let beforeAdd;
  87. if (program.beforeAdd) {
  88. const m = require(require.resolve(program.beforeAdd, {
  89. paths: [process.cwd()],
  90. }));
  91. if (typeof m === 'function') {
  92. beforeAdd = m;
  93. } else if (typeof m === 'object' && typeof m.default === 'function') {
  94. beforeAdd = m.default;
  95. } else {
  96. throw new Error(
  97. `Could not find function to execute before adding files in ` +
  98. `"${program.beforeAdd}".\n `
  99. );
  100. }
  101. }
  102. const config = {
  103. repo: program.repo,
  104. silent: !!program.silent,
  105. branch: program.branch,
  106. src: program.src,
  107. dest: program.dest,
  108. message: program.message,
  109. tag: program.tag,
  110. git: program.git,
  111. depth: program.depth,
  112. dotfiles: !!program.dotfiles,
  113. add: !!program.add,
  114. remove: program.remove,
  115. remote: program.remote,
  116. push: !!program.push,
  117. history: !!program.history,
  118. user: user,
  119. beforeAdd: beforeAdd,
  120. };
  121. return publish(config);
  122. });
  123. }
  124. if (require.main === module) {
  125. main(process.argv)
  126. .then(() => {
  127. process.stdout.write('Published\n');
  128. })
  129. .catch((err) => {
  130. process.stderr.write(`${err.message}\n`, () => process.exit(1));
  131. });
  132. }
  133. module.exports = main;
  134. exports = module.exports;