-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathutils.js
127 lines (102 loc) · 2.77 KB
/
utils.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
const minimist = require('minimist')
const la = require('lazy-ass')
const is = require('check-more-types')
const path = require('path')
const fs = require('fs')
const execa = require('execa')
/* eslint-disable no-console */
function getNameAndBinary (args = process.argv) {
const options = minimist(args)
la(is.unemptyString(options.npm), 'missing --npm option', options)
la(is.unemptyString(options.binary), 'missing --binary option', options)
let npm = options.npm
if (fs.existsSync(options.npm)) {
console.log('loading NPM url from', options.npm)
npm = require(path.resolve(options.npm)).url
la(is.url(npm), 'not an url', npm)
} else {
console.log('NPM option "%s" is not a file', options.npm)
}
let binary = options.binary
if (fs.existsSync(options.binary)) {
console.log('loading binary url from', options.binary)
binary = require(path.resolve(options.binary)).url
la(is.url(binary), 'not an url', binary)
} else {
console.log('binary option "%s" is not a file', options.binary)
}
return {
npm,
binary,
}
}
function getJustVersion (npmNameOrUrl) {
la(is.unemptyString(npmNameOrUrl), 'missing NPM string', npmNameOrUrl)
if (npmNameOrUrl.startsWith('cypress')) {
return npmNameOrUrl
}
if (is.url(npmNameOrUrl)) {
// try finding semver in the url
// https://something/0.20.3/something...
const re = /\/(\d+\.\d+\.\d+(-\w+)?)\//
const matches = re.exec(npmNameOrUrl)
if (matches) {
return matches[1]
}
}
return npmNameOrUrl
}
const shorten = (s) => {
return s.substr(0, 7)
}
/**
* Grabs the full commit SHA and its short version from CI environment variables
*/
const getShortCommit = () => {
const sha = process.env.CIRCLE_SHA1
if (sha) {
return {
sha,
short: shorten(sha),
}
}
}
/**
* Returns CI name for know CIs
*/
const getCIName = () => {
if (process.env.CIRCLECI) {
return 'Circle'
}
}
/**
* Returns the current CI build url
*/
const getCIBuildUrl = () => {
if (process.env.CIRCLECI) {
// https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables
return process.env.CIRCLE_BUILD_URL
}
}
const seconds = (s) => s * 1000
const minutes = (m) => m * 60 * 1000
const getCurrentBranch = async () => {
const { stdout } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'])
return stdout
}
const getPackagePath = ({ location }) => path.join(location, 'package.json')
const readPackageJson = (pack) => JSON.parse(fs.readFileSync(getPackagePath(pack)))
const independentTagRegex = (name) => new RegExp(`^${name}-v(.+)`)
module.exports = {
getNameAndBinary,
getJustVersion,
getShortCommit,
getCIName,
getCIBuildUrl,
seconds,
minutes,
getCurrentBranch,
getPackagePath,
readPackageJson,
independentTagRegex,
}