Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/lib/test/build.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) 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 SFC 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
18
'use strict'
19
20
const fs = require('node:fs')
21
const path = require('node:path')
22
const { spawn } = require('node:child_process')
23
const PROJECT_ROOT = path.normalize(path.join(__dirname, '../../../..'))
24
const WORKSPACE_FILE = path.join(PROJECT_ROOT, 'WORKSPACE')
25
26
function isDevMode() {
27
return fs.existsSync(WORKSPACE_FILE)
28
}
29
30
function checkIsDevMode() {
31
if (!isDevMode()) {
32
throw Error('Cannot execute build; not running in dev mode')
33
}
34
}
35
36
/**
37
* Targets that have been previously built.
38
* @type {!Object}
39
*/
40
let builtTargets = {}
41
42
/**
43
* @param {!Array.<string>} targets The targets to build.
44
* @throws {Error} If not running in dev mode.
45
* @constructor
46
*/
47
const Build = function (targets) {
48
checkIsDevMode()
49
this.targets_ = targets
50
}
51
52
/** @private {boolean} */
53
Build.prototype.cacheResults_ = false
54
55
/**
56
* Configures this build to only execute if it has not previously been
57
* run during the life of the current process.
58
* @return {!Build} A self reference.
59
*/
60
Build.prototype.onlyOnce = function () {
61
this.cacheResults_ = true
62
return this
63
}
64
65
/**
66
* Executes the build.
67
* @return {!Promise} A promise that will be resolved when
68
* the build has completed.
69
* @throws {Error} If no targets were specified.
70
*/
71
Build.prototype.go = function () {
72
let targets = this.targets_
73
if (!targets.length) {
74
throw Error('No targets specified')
75
}
76
77
// Filter out cached results.
78
if (this.cacheResults_) {
79
targets = targets.filter(function (target) {
80
return !Object.prototype.hasOwnProperty.call(builtTargets, target)
81
})
82
83
if (!targets.length) {
84
return Promise.resolve()
85
}
86
}
87
88
console.log('\nBuilding', targets.join(' '), '...')
89
90
let cmd,
91
args = targets
92
if (process.platform === 'win32') {
93
cmd = 'cmd.exe'
94
args.unshift('/c', path.join(PROJECT_ROOT, 'go.bat'))
95
} else {
96
cmd = path.join(PROJECT_ROOT, 'go')
97
}
98
99
return new Promise((resolve, reject) => {
100
spawn(cmd, args, {
101
cwd: PROJECT_ROOT,
102
env: process.env,
103
stdio: ['ignore', process.stdout, process.stderr],
104
}).on('exit', function (code, signal) {
105
if (code === 0) {
106
targets.forEach(function (target) {
107
builtTargets[target] = 1
108
})
109
return resolve()
110
}
111
112
let msg = 'Unable to build artifacts'
113
if (code) {
114
// May be null.
115
msg += '; code=' + code
116
}
117
if (signal) {
118
msg += '; signal=' + signal
119
}
120
121
reject(Error(msg))
122
})
123
})
124
}
125
126
// PUBLIC API
127
128
exports.isDevMode = isDevMode
129
130
/**
131
* Creates a build of the listed targets.
132
* @param {...string} var_args The targets to build.
133
* @return {!Build} The new build.
134
* @throws {Error} If not running in dev mode.
135
*/
136
exports.of = function (_) {
137
let targets = Array.prototype.slice.call(arguments, 0)
138
return new Build(targets)
139
}
140
141
/**
142
* @return {string} Absolute path of the project's root directory.
143
* @throws {Error} If not running in dev mode.
144
*/
145
exports.projectRoot = function () {
146
return PROJECT_ROOT
147
}
148
149