-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtaskfile.js
134 lines (127 loc) · 3.96 KB
/
taskfile.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
128
129
130
131
132
133
134
const fs = require('fs')
const resolve = require('path').resolve
const _ = require('lodash')
const commands = require('./utils/commands')
const { TASK_RUNNERS } = require('./constants')
let LIB_PATHS = null
const createLibPaths = () => {
const [utilFormat, utilDir] =
process.platform === 'win32'
? ['.cmd', 'node_modules\\.bin\\']
: ['', 'node_modules/.bin/']
LIB_PATHS = _.reduce(
fs
.readdirSync(resolve('node_modules/.bin'))
.filter(file => !file.match(/.*\.cmd$/)),
(bin, name) => (bin[name] = `${utilDir}${name}${utilFormat}`) && bin,
{}
)
}
/**
* load task files (package.json or Procfile)
* @param {object} config
* @param {string} procfilePath
*/
function load(config, procfilePath) {
const allCommands = []
if (config.web || config.useOnlyPackageJson) {
try {
const packageJson = JSON.parse(fs.readFileSync('package.json').toString())
config.name = packageJson.name || 'flamebird'
_.forEach(packageJson.scripts, (command, taskName) => {
if (!config.tasks.length || _.includes(config.tasks, taskName)) {
allCommands.push(
commands.createCommand(config.id, taskName, command, 'npm')
)
}
})
} catch (e) {
console.warn('package.json not found')
}
}
if (config.web || !config.useOnlyPackageJson) {
try {
const data = fs.readFileSync(procfilePath)
_.each(_.compact(data.toString().split(/[\n\r]/g)), (line, i) => {
if (line && line[0] !== '#') {
const tuple = /^([A-Za-z0-9_-]+):\s*(.+)$/m.exec(line)
const name = tuple[1].trim()
const task = tuple[2].trim()
if (!name || !task) {
throw new Error(
'Syntax Error in Procfile, Line %d: No ' +
(!tuple ? 'Procfile' : 'Command') +
' Found'
)
}
if (!config.tasks.length || _.includes(config.tasks, name)) {
allCommands.push(
commands.createCommand(config.id, name, task, 'procfile')
)
}
}
})
} catch (e) {
console.warn('Procfile not found')
}
}
console.log('allCommands', allCommands)
return updateCommands(config, allCommands)
}
function setAbsolutePathsToTask(command, commandsWithoutPms) {
const spaceChar = ' '
const fixedTaskData = []
const words = command.task.split(spaceChar)
for (let x = 0; x < words.length; ) {
const word = words[x]
if (word === 'npm' || word === 'yarn') {
const nextWordIsRun = words[x + 1] === 'run'
const updatedCommand =
commandsWithoutPms[words[x + (nextWordIsRun ? 2 : 1)]]
if (updatedCommand) {
fixedTaskData.push(updatedCommand)
x += 2
} else {
fixedTaskData.push(word)
x++
}
} else {
fixedTaskData.push(LIB_PATHS[word] || word)
x++
}
}
return (command.rawTask = fixedTaskData.join(spaceChar))
}
function updateCommands(config, commands) {
if (config.withoutTaskRunner) {
if (LIB_PATHS === null) createLibPaths()
_.reduce(
_.sortBy(
commands,
({ task }) => _.includes(task, 'yarn') || _.includes(task, 'npm')
),
(commandsWithoutPms, command) => {
// TODO: fix problem with displaying cross-env in .task property
command.task = _.replace(command.task, 'cross-env ', '')
commandsWithoutPms[command.name] = setAbsolutePathsToTask(
command,
commandsWithoutPms
)
return commandsWithoutPms
},
{}
)
} else {
const taskRunner =
TASK_RUNNERS[`${config.taskRunner}`.toUpperCase()] || config.taskRunner
_.each(commands, command => {
command.rawTask =
command.type === 'procfile'
? command.task
: `${taskRunner || command.type} ${command.name}`
})
}
console.log('update Commands -> commands', commands)
return config.sortByName ? _.sortBy(commands, 'name', 'asc') : commands
}
module.exports.load = load