forked from cypress-io/cypress-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (134 loc) · 4.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
process.on('unhandledRejection', function (reason, p) {
/* eslint-disable no-console */
console.error('Unhandled Rejection at: Promise ', p)
console.error('reason: ', reason)
process.exit(-1)
})
const { copyUntranslatedDocs } = require('./copy-english-docs')
const Hexo = require('hexo')
const chalk = require('chalk')
const minimist = require('minimist')
const Contentful = require('contentful')
const moment = require('moment')
const yaml = require('js-yaml')
const fs = require('fs')
const { documentToHtmlString } = require('@contentful/rich-text-html-renderer')
// these are the args like --port
const args = minimist(process.argv.slice(2))
// this is the command passed in such as 'server' or 'generate'
const cmd = args._.shift()
const hexo = new Hexo(process.cwd(), args)
function initHexo () {
// defaults outside of _config.yml
Object.assign(hexo.config, {
hfc_useref: {
enable: true,
concat: true,
exclude: null,
},
hfc_html: {
enable: false,
exclude: null,
},
hfc_css: {
enable: true,
exclude: ['*.min.css'],
},
hfc_favicons: {
enable: false,
},
hfc_js: {
enable: true,
mangle: true,
compress: {},
exclude: ['*.min.js'],
},
hfc_img: {
enable: true,
interlaced: false,
multipass: false,
optimizationLevel: 2,
pngquant: false,
progressive: false,
webp: true,
webpQuality: 75,
gifslice: true,
jpegtran: true,
jpegrecompress: false,
jpegrecompressQuality: 'medium',
optipng: true,
svgo: false,
},
})
// hexo does this weird thing where it literally sets
// an 'env' property on the 'env' object
// so we take that into account (and any other way it is set)
const env = hexo.env.NODE_ENV || hexo.env.env || process.env.NODE_ENV || 'development'
// https://github.com/mamboer/hexo-filter-cleanup
// only run the filter_cleanup if we are in
// production mode -- deploying static asset
// this will combine multiple JS files into single "application.js"
// note that --debug mode in hexo disables clean up automatically
if (env !== 'production' && env !== 'staging') {
console.log('disabling filter cleanup for environment', env)
hexo.config.filter_cleanup = false
}
let url
if (env === 'staging') {
url = 'https://docs-staging.cypress.io'
} else {
url = 'https://docs.cypress.io'
}
console.log('in environment %s site url is %s', env, url)
hexo.config.url = url
// set this on both our process + Hexo
process.env.NODE_ENV = hexo.env.NODE_ENV = env
console.log('NODE_ENV is:', chalk.cyan(env))
return new Promise((resolve, reject) => {
const space = hexo.env.GATSBY_CONTENTFUL_SPACE_ID || process.env.GATSBY_CONTENTFUL_SPACE_ID
const accessToken = hexo.env.GATSBY_CONTENTFUL_ACCESS_TOKEN || process.env.GATSBY_CONTENTFUL_ACCESS_TOKEN
const environment = hexo.env.GATSBY_CONTENTFUL_ENVIRONMENT || process.env.GATSBY_CONTENTFUL_ENVIRONMENT || 'master'
if (typeof space === 'undefined' || typeof accessToken === 'undefined') {
return reject({
message: 'No Contentful space variables.',
})
}
return Contentful.createClient({ space, accessToken, environment })
.getEntries({ content_type: 'docsTopBanner' })
.then(({ items }) => {
const data = items.reduce((filtered, { sys: { id }, fields }) => {
if (moment(fields.endDate).isSameOrAfter(moment())) {
filtered.push({ id, ...fields, text: documentToHtmlString(fields.text) })
}
return filtered
}, [])
fs.writeFile(
`${__dirname}/source/_data/banners.yml`,
yaml.safeDump(data),
(error) => {
// log if writeFile ends with error, but don't block hexo init process
if (error) {
console.warn(error)
return reject(error)
}
return resolve()
},
)
})
.catch((err) => {
return reject({
message: err,
})
})
})
// start Hexo
.then(() => hexo.init().then(() => hexo.call(cmd, args)))
.catch((error) => {
// log error object
console.error(error)
// but start Hexo anyway
return hexo.init().then(() => hexo.call(cmd, args))
})
}
copyUntranslatedDocs()
.then(initHexo)