FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
frontend/tools/task-runner/runner at master · devhttps/frontend · GitHub
devhttps
/
frontend
Public
forked from
guardian/frontend
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
frontend
/
tools
/
task-runner
/
runner
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
196 lines (173 loc) · 6.91 KB
Breadcrumbs
frontend
/
tools
/
task-runner
/
runner
Copy path
File metadata and controls
executable file
·
196 lines (173 loc) · 6.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
/* eslint-disable import/no-dynamic-require, global-require */
// force any plugins that use `chalk` to output in full colour
process
.
env
.
FORCE_COLOR
=
true
;
const
path
=
require
(
'path'
)
;
const
os
=
require
(
'os'
)
;
const
yargs
=
require
(
'yargs'
)
;
const
Listr
=
require
(
'listr'
)
;
const
execa
=
require
(
'execa'
)
;
const
chalk
=
require
(
'chalk'
)
;
const
figures
=
require
(
'figures'
)
;
const
uniq
=
require
(
'lodash.uniq'
)
;
// name of the tasks directory
const
tasksDirectory
=
'__tasks__'
;
// use yargs to get a useful CLI
const
{
dev
:
IS_DEV
,
teamcity
:
IS_TEAMCITY
,
debug
:
IS_DEBUG
,
verbose
:
IS_VERBOSE
,
stdout
:
IS_STDOUT
,
_
:
TASKS
}
=
yargs
.
option
(
'dev'
,
{
demand
:
false
,
describe
:
'Prefer the dev version of the task, if it exits.'
,
type
:
'boolean'
,
nargs
:
0
,
}
)
.
option
(
'teamcity'
,
{
demand
:
false
,
describe
:
'Output the tasks formatted for teamcity.'
,
type
:
'boolean'
,
nargs
:
0
,
}
)
.
option
(
'debug'
,
{
demand
:
false
,
describe
:
'Log everything there is to log.'
,
type
:
'boolean'
,
nargs
:
0
,
}
)
.
option
(
'verbose'
,
{
demand
:
false
,
describe
:
'Log everything there is to log with a simple format for the output.'
,
type
:
'boolean'
,
nargs
:
0
,
}
)
.
option
(
'stdout'
,
{
demand
:
false
,
describe
:
'Log all stdout once the tasks are finished (errors are logged by default).'
,
type
:
'boolean'
,
nargs
:
0
,
}
)
.
usage
(
'Usage: $0 <task> [<task>] [--dev]'
)
.
command
(
'task'
,
`Run a task defined in '
${
tasksDirectory
}
'.`
)
.
example
(
'$0 copy'
,
'Run all the copy tasks.'
)
.
example
(
'$0 javascript/copy'
,
'Run the javascript copy task.'
)
.
example
(
'$0 javascript/copy --dev'
,
'Run the javascript copy task, and prefer the development version, if it exists.'
)
.
example
(
'$0 javascript/copy css/copy --dev'
,
'Run the javascript and css copy tasks, and prefer the development versions, if they exist.'
)
.
demand
(
1
)
.
help
(
)
.
alias
(
'h'
,
'help'
)
// eslint-disable-line newline-per-chained-call
.
version
(
)
.
alias
(
'v'
,
'version'
)
// eslint-disable-line newline-per-chained-call
.
argv
;
// if this is true, we log as much as we can
const
VERBOSE
=
IS_VERBOSE
||
IS_TEAMCITY
||
IS_DEBUG
;
// look here for tasks that come in from yargs
const
taskSrc
=
path
.
resolve
(
__dirname
,
'..'
,
tasksDirectory
)
;
// we will store tasks that we run in here, to prevent running them more than once
// e.g. if two tasks rely on the same thing
const
cache
=
[
]
;
// use exaca to run simple terminal commands
const
exec
=
(
task
,
onError
,
ctx
)
=>
{
const
[
cmd
,
...
args
]
=
task
.
trim
(
)
.
split
(
' '
)
;
return
execa
(
cmd
,
args
)
.
then
(
(
result
)
=>
{
// store any stdout incase we need it later
if
(
result
.
stdout
)
ctx
.
stdouts
.
push
(
result
.
stdout
)
;
}
)
.
catch
(
(
e
)
=>
{
// if the task supplies an `onError` function, run it
if
(
typeof
onError
===
'function'
)
onError
(
ctx
)
;
// continue with a fake rejected promise
return
Promise
.
reject
(
e
)
;
}
)
;
}
;
const
getCpuCount
=
(
)
=>
os
.
cpus
(
)
.
length
;
// turn a list of our tasks into objects listr can use
function
listrify
(
steps
,
{
concurrent
=
false
}
=
{
}
)
{
const
listrTasks
=
steps
.
map
(
(
step
)
=>
{
const
{
description
:
title
,
task
,
concurrent
:
isConcurrent
,
onError
}
=
step
;
// if another task has included this one, don't run it again
const
skip
=
cache
.
indexOf
(
step
)
!==
-
1
?
(
)
=>
'Skipping: already run by another task'
:
false
;
cache
.
push
(
step
)
;
// if the task is a set of subtasks, prepare them
if
(
Array
.
isArray
(
task
)
)
{
return
{
title
,
task
:
(
)
=>
listrify
(
task
.
map
(
(
_task
)
=>
{
if
(
_task
.
task
)
return
_task
;
if
(
typeof
_task
===
'string'
)
return
{
title
,
task
:
ctx
=>
exec
(
_task
,
onError
,
ctx
)
,
skip
}
;
return
{
title
,
task
:
_task
,
skip
}
;
}
)
,
{
concurrent
:
VERBOSE
?
false
:
(
isConcurrent
?
getCpuCount
(
)
:
false
)
}
)
,
skip
,
}
;
}
// treat tasks that are strings as terminal commands
if
(
typeof
task
===
'string'
)
return
{
title
,
task
:
ctx
=>
exec
(
task
,
onError
,
ctx
)
,
skip
}
;
// assume the task is a function
// if it's not, listr will blow up anyway, which is fine
return
{
title
,
task
:
ctx
=>
new
Promise
(
(
resolve
,
reject
)
=>
{
try
{
resolve
(
task
(
ctx
)
)
;
}
catch
(
e
)
{
if
(
typeof
onError
===
'function'
)
onError
(
ctx
)
;
if
(
VERBOSE
)
console
.
log
(
e
)
;
reject
(
e
)
;
}
}
)
,
skip
,
}
;
}
)
;
let
renderer
=
'default'
;
if
(
IS_TEAMCITY
)
renderer
=
require
(
'./run-task-tc-formater'
)
;
if
(
IS_VERBOSE
)
renderer
=
require
(
'./run-task-verbose-formater'
)
;
return
new
Listr
(
listrTasks
,
{
concurrent
:
concurrent
?
getCpuCount
(
)
:
false
,
collapse
:
true
,
renderer
,
}
)
;
}
// resolve the tasks from yargs to actual files
const
getTasksFromModule
=
(
taskName
)
=>
{
try
{
const
modulePath
=
path
.
resolve
(
taskSrc
,
taskName
)
;
if
(
IS_DEV
)
{
try
{
return
require
(
`
${
modulePath
}
.dev`
)
;
}
catch
(
e
)
{
/* do nothing */
}
try
{
return
require
(
`
${
modulePath
}
/index.dev`
)
;
}
catch
(
e
)
{
/* do nothing */
}
}
return
require
(
modulePath
)
;
}
catch
(
e
)
{
// we can't find any modules, or something else has gone wrong in resolving it
// so output an erroring task
return
{
description
:
`
${
chalk
.
red
(
taskName
)
}
failed:`
,
task
:
(
)
=>
Promise
.
reject
(
e
)
,
}
;
}
}
;
// get a list of the tasks we're going to run
const
taskModules
=
TASKS
.
map
(
getTasksFromModule
)
;
// run them!
listrify
(
taskModules
)
.
run
(
{
// we're adding these to the [listr context](https://github.com/SamVerschueren/listr#context)
messages
:
[
]
,
stdouts
:
[
]
,
}
)
.
catch
(
(
e
)
=>
{
// something went wrong, so log whatever we have
if
(
!
e
.
stderr
&&
!
e
.
stdout
)
console
.
log
(
e
)
;
if
(
e
.
stderr
)
console
.
error
(
`\n
${
e
.
stderr
.
trim
(
)
}
`
)
;
if
(
e
.
stdout
)
console
.
log
(
`\n
${
e
.
stdout
.
trim
(
)
}
`
)
;
return
Object
.
assign
(
e
.
context
,
{
error
:
true
}
)
;
}
)
.
then
(
(
ctx
)
=>
{
if
(
IS_STDOUT
)
ctx
.
stdouts
.
forEach
(
stdout
=>
console
.
log
(
stdout
.
toString
(
)
.
trim
(
)
)
)
;
if
(
ctx
.
messages
.
length
)
{
console
.
log
(
''
)
;
uniq
(
ctx
.
messages
)
.
forEach
(
message
=>
console
.
log
(
chalk
.
dim
(
`
${
figures
.
arrowRight
}
${
message
.
split
(
'\n'
)
.
join
(
'\n '
)
}
`
)
)
)
;
}
if
(
ctx
.
error
)
{
console
.
log
(
''
)
;
// if something's gone wrong, fail hard
process
.
exit
(
1
)
;
}
}
)
;
Back
|
FazBrowse Home
|
New Git URL