gen_defaults.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. var path = require('path');
  19. var yaml = require('js-yaml');
  20. var util = require('./util');
  21. // constants
  22. var USAGE = 'Usage: gen_defaults.js [docsRoot] [latestVersion]';
  23. var DEV_VERSION_NAME = 'dev';
  24. // constants for sitemap.xml
  25. // reference:
  26. // http://www.sitemaps.org/protocol.html#xmlTagDefinitions
  27. var LATEST_CHANGE_FREQUENCY = 'monthly';
  28. var LATEST_PAGE_PRIORITY = 0.8;
  29. var DEFAULT_CHANGE_FREQUENCY = 'monthly';
  30. var DEFAULT_PAGE_PRIORITY = LATEST_PAGE_PRIORITY / 2;
  31. var DEV_CHANGE_FREQUENCY = 'daily';
  32. var DEV_PAGE_PRIORITY = LATEST_PAGE_PRIORITY / 4;
  33. function main () {
  34. var rootDir = process.argv[2];
  35. var latestVersionName = process.argv[3];
  36. if (!rootDir) {
  37. console.error(USAGE);
  38. console.error('Please specify the docs root directory from which to generate defaults.');
  39. process.exit(1);
  40. }
  41. if (!latestVersionName) {
  42. console.error(USAGE);
  43. console.error('Please specify the latest version of the docs.');
  44. process.exit(1);
  45. }
  46. // create defaults config
  47. var config = { 'defaults': [] };
  48. // set defaults for each language
  49. util.listdirsSync(rootDir).forEach(function (langName) {
  50. var langPath = path.join(rootDir, langName);
  51. var languageDefaults = {
  52. scope: {
  53. path: 'docs/' + langName
  54. },
  55. values: {
  56. language: langName,
  57. layout: 'docs-' + langName
  58. }
  59. };
  60. config.defaults.push(languageDefaults);
  61. // set defaults for each version
  62. util.listdirsSync(langPath).forEach(function (versionName) {
  63. var tocfile = util.genTocfileName(langName, versionName);
  64. var changeFrequency = DEFAULT_CHANGE_FREQUENCY;
  65. var pagePriority = DEFAULT_PAGE_PRIORITY;
  66. // adjust priority and frequency based on version
  67. if (versionName === latestVersionName) {
  68. changeFrequency = LATEST_CHANGE_FREQUENCY;
  69. pagePriority = LATEST_PAGE_PRIORITY;
  70. } else if (versionName === DEV_VERSION_NAME) {
  71. changeFrequency = DEV_CHANGE_FREQUENCY;
  72. pagePriority = DEV_PAGE_PRIORITY;
  73. }
  74. var current = false;
  75. if (versionName === latestVersionName || versionName === DEV_VERSION_NAME) {
  76. current = true;
  77. }
  78. var versionDefaults = {
  79. scope: {
  80. path: 'docs/' + langName + '/' + versionName
  81. },
  82. values: {
  83. version: versionName,
  84. tocfile: tocfile.replace('.yml', ''),
  85. change_frequency: changeFrequency,
  86. priority: pagePriority,
  87. current: current
  88. }
  89. };
  90. config.defaults.push(versionDefaults);
  91. });
  92. });
  93. console.log(util.generatedBy(__filename));
  94. console.log(yaml.dump(config, { indent: 4 }));
  95. }
  96. main();