forked from cypress-io/cypress-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.js
90 lines (79 loc) · 2.36 KB
/
scrape.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
/* eslint-disable no-console */
const path = require('path')
const chalk = require('chalk')
const Promise = require('bluebird')
const request = require('request-promise')
const {
configFromEnvOrJsonFile,
filenameToShellVariable,
} = require('@cypress/env-or-json-file')
const { stripIndent } = require('common-tags')
const R = require('ramda')
function checkToken (token) {
if (!token) {
const example = JSON.stringify({
token: 'foobarbaz',
})
console.log(
chalk.red(
stripIndent`Cannot scrape docs.
You are missing your Circle CI API token.
It should look like this:
${example}
`
)
)
throw new Error('missing token')
}
}
function getCircleCredentials () {
// the JSON file should have an object like
// { "token": "abc123..." }
// where the token is your personal API token from CircleCI
// alternatively, put the JSON object into environment variable
// with file shown by filenameToShellVariable(jsonFile) call
// which is something like _circle_credentials_json
// you can load such environment variable quickly from local terminal with
// https://github.com/bahmutov/as-a
const jsonFile = path.join('support', '.circle-credentials.json')
const config = configFromEnvOrJsonFile(jsonFile)
if (!config) {
console.error('⛔️ Cannot find CircleCI credentials')
console.error('Using @cypress/env-or-json-file module')
console.error('and key', filenameToShellVariable(jsonFile))
console.error('from file path', jsonFile)
throw new Error('Cannot load CircleCI credentials')
}
return config.token
}
function scrape () {
return Promise.resolve()
.then(getCircleCredentials)
.then(R.tap(checkToken))
.then((token) => {
// hmm, how do we trigger workflow?
// seems this is not supported yet as of July 10th 2017
// https://discuss.circleci.com/t/trigger-workflow-through-rest-api/13931
return request({
url: 'https://circleci.com/api/v1.1/project/github/cypress-io/docsearch-scraper/',
method: 'POST',
json: true,
auth: {
user: token,
},
}).then((body) => {
console.log(
'\n',
'Started Circle CI build:',
chalk.green(body.build_url),
'\n'
)
})
})
}
// if we're not being required
// then kick off scraping
if (!module.parent) {
scrape()
}
module.exports = scrape