|
| 1 | +const execa = require('execa') |
| 2 | + |
| 3 | +const channels = require('../channels') |
1 | 4 | const cwd = require('./cwd') |
2 | 5 | const folders = require('./folders') |
| 6 | +const logs = require('./logs') |
| 7 | + |
| 8 | +const { getCommand } = require('../utils/command') |
3 | 9 |
|
4 | | -let tasks = [] |
| 10 | +const tasks = new Map() |
| 11 | + |
| 12 | +function getTasks () { |
| 13 | + const file = cwd.get() |
| 14 | + let list = tasks.get(file) |
| 15 | + if (!list) { |
| 16 | + list = [] |
| 17 | + tasks.set(file, list) |
| 18 | + } |
| 19 | + return list |
| 20 | +} |
5 | 21 |
|
6 | 22 | function list (context) { |
| 23 | + let list = getTasks() |
7 | 24 | const file = cwd.get() |
8 | 25 | const pkg = folders.readPackage(file, context) |
9 | | - tasks = [] |
10 | 26 | if (pkg.scripts) { |
11 | | - tasks = Object.keys(pkg.scripts).map( |
12 | | - name => ({ |
13 | | - id: `${file}:${name}`, |
14 | | - name, |
15 | | - command: pkg.scripts[name], |
16 | | - status: 'idle' |
| 27 | + const existing = new Map() |
| 28 | + |
| 29 | + // Get current valid tasks in project `package.json` |
| 30 | + const currentTasks = Object.keys(pkg.scripts).map( |
| 31 | + name => { |
| 32 | + const id = `${file}:${name}` |
| 33 | + existing.set(id, true) |
| 34 | + return { |
| 35 | + id, |
| 36 | + name, |
| 37 | + command: pkg.scripts[name], |
| 38 | + index: list.findIndex(t => t.id === id) |
| 39 | + } |
| 40 | + } |
| 41 | + ) |
| 42 | + |
| 43 | + // Process existing tasks |
| 44 | + const existingTasks = currentTasks.filter( |
| 45 | + task => task.index !== -1 |
| 46 | + ) |
| 47 | + // Update tasks data |
| 48 | + existingTasks.forEach(task => { |
| 49 | + Object.assign(list[task.index], task) |
| 50 | + }) |
| 51 | + |
| 52 | + // Process new tasks |
| 53 | + const newTasks = currentTasks.filter( |
| 54 | + task => task.index === -1 |
| 55 | + ).map( |
| 56 | + task => ({ |
| 57 | + ...task, |
| 58 | + status: 'idle', |
| 59 | + child: null, |
| 60 | + logs: [] |
17 | 61 | }) |
18 | 62 | ) |
| 63 | + |
| 64 | + // Keep existing or ran tasks |
| 65 | + list = list.filter( |
| 66 | + task => existing.get(task.id) || |
| 67 | + task.status !== 'idle' |
| 68 | + ) |
| 69 | + |
| 70 | + // Add the new tasks |
| 71 | + list = list.concat(newTasks) |
| 72 | + |
| 73 | + tasks.set(file, list) |
19 | 74 | } |
20 | | - return tasks |
| 75 | + return list |
21 | 76 | } |
22 | 77 |
|
23 | 78 | function findOne (id, context) { |
24 | | - return tasks.find( |
| 79 | + return getTasks().find( |
25 | 80 | t => t.id === id |
26 | 81 | ) |
27 | 82 | } |
28 | 83 |
|
| 84 | +function updateOne (data, context) { |
| 85 | + const task = findOne(data.id) |
| 86 | + if (task) { |
| 87 | + Object.assign(task, data) |
| 88 | + context.pubsub.publish(channels.TASK_CHANGED, { |
| 89 | + taskChanged: task |
| 90 | + }) |
| 91 | + } |
| 92 | + return task |
| 93 | +} |
| 94 | + |
| 95 | +function run (id, context) { |
| 96 | + const task = findOne(id, context) |
| 97 | + if (task && task.status !== 'running') { |
| 98 | + const args = ['run', task.name] |
| 99 | + |
| 100 | + const child = execa(getCommand(), args, { |
| 101 | + cwd: cwd.get(), |
| 102 | + stdio: ['inherit', 'pipe', 'pipe'] |
| 103 | + }) |
| 104 | + |
| 105 | + updateOne({ |
| 106 | + id: task.id, |
| 107 | + status: 'running', |
| 108 | + child |
| 109 | + }, context) |
| 110 | + logs.add({ |
| 111 | + message: `Task ${task.id} started`, |
| 112 | + type: 'info' |
| 113 | + }, context) |
| 114 | + |
| 115 | + child.stdout.on('data', buffer => { |
| 116 | + // TODO logs |
| 117 | + console.log(buffer.toString()) |
| 118 | + }) |
| 119 | + |
| 120 | + child.stderr.on('data', buffer => { |
| 121 | + // TODO logs |
| 122 | + console.log(buffer.toString()) |
| 123 | + }) |
| 124 | + |
| 125 | + child.on('close', (code, signal) => { |
| 126 | + if (signal === 'SIGINT') { |
| 127 | + updateOne({ |
| 128 | + id: task.id, |
| 129 | + status: 'error', |
| 130 | + child: null |
| 131 | + }, context) |
| 132 | + logs.add({ |
| 133 | + message: `Task ${task.id} was terminated`, |
| 134 | + type: 'error' |
| 135 | + }, context) |
| 136 | + } else if (code !== 0) { |
| 137 | + updateOne({ |
| 138 | + id: task.id, |
| 139 | + status: 'error', |
| 140 | + child: null |
| 141 | + }, context) |
| 142 | + logs.add({ |
| 143 | + message: `Task ${task.id} ended with error code`, |
| 144 | + type: 'error' |
| 145 | + }, context) |
| 146 | + } else { |
| 147 | + updateOne({ |
| 148 | + id: task.id, |
| 149 | + status: 'done', |
| 150 | + child: null |
| 151 | + }, context) |
| 152 | + logs.add({ |
| 153 | + message: `Task ${task.id} completed`, |
| 154 | + type: 'done' |
| 155 | + }, context) |
| 156 | + } |
| 157 | + }) |
| 158 | + } |
| 159 | + return task |
| 160 | +} |
| 161 | + |
| 162 | +function stop (id, context) { |
| 163 | + const task = findOne(id, context) |
| 164 | + if (task && task.status === 'running') { |
| 165 | + task.child.kill('SIGINT') // TODO not working |
| 166 | + } |
| 167 | + return task |
| 168 | +} |
| 169 | + |
29 | 170 | module.exports = { |
30 | 171 | list, |
31 | | - findOne |
| 172 | + findOne, |
| 173 | + run, |
| 174 | + stop, |
| 175 | + updateOne |
32 | 176 | } |
0 commit comments