Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use execa for better windows support #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
*.log
15 changes: 13 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class Maid {
}

async runTask(taskName, throwWhenNoMatchedTask = true) {
// type Task = { name: string, type: string, script: string }
// type Task = { name: 'lint', type: 'js', script: 'var bar = 123' }
// type Task = { name: 'test', type: 'bash', script: 'tap *.js' }
// type Task = { name: 'meow', type: 'python', script: 'print("meooow")' }
const task =
taskName &&
this.maidfile &&
Expand All @@ -58,23 +62,30 @@ class Maid {
const start = Date.now()

logger.log(`Starting '${chalk.cyan(task.name)}'...`)

await new Promise((resolve, reject) => {
const handleError = err => {
throw new MaidError(`Task '${task.name}' failed.\n${err.stack}`)
}
// task.type = bash|sh
if (checkTypes(task, ['sh', 'bash'])) {
return runCLICommand({ task, resolve, reject })
runCLICommand({ task, resolve, reject })
return
}
// task.type = python|py
if (checkTypes(task, ['py', 'python'])) {
return runCLICommand({ type: 'python', task, resolve, reject })
runCLICommand({ task, resolve, reject })
return
}
if (checkTypes(task, ['js', 'javascript'])) {
let res

try {
res = requireFromString(task.script, this.maidfile.filepath)
} catch (err) {
return handleError(err)
}

res = res.default || res
return resolve(
typeof res === 'function'
Expand Down
2 changes: 1 addition & 1 deletion lib/parseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ module.exports = (content, { section, filepath } = {}) => {
// Currently only use the first one
for (const token of sectionTokens) {
if (token.type === 'fence') {
task.script = token.content
task.script = token.content.trim()
task.type = token.info
break
}
Expand Down
30 changes: 25 additions & 5 deletions lib/runCLICommand.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
const path = require('path')
const spawn = require('cross-spawn')
const execa = require('execa')
const MaidError = require('./MaidError')

module.exports = ({ task, type = task.type, resolve, reject }) => {
const cmd = spawn(type, ['-c', task.script, ...process.argv.slice(2)], {
const modulesBin = path.resolve(path.join('node_modules', '.bin'))
const isShell = /(ba|z)?sh/.test(type)
const isPython = /py(thon)?/.test(type)
const args = process.argv.slice(2)

let script = null

if (isShell) {
script = [task.script, ...args]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm does this work? maid log hello world:

## log

```sh
echo $1;
echo $2;
```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably yes. the readme scripts works, so. but not sure how to handle the windows yet ;/

} else if (isPython) {
script = ['python', '-c', task.script, ...args]
} else {
script = [type, task.script, ...args]
}

const promise = execa.shell(script.join(' '), {
stdio: 'inherit',
env: Object.assign({}, process.env, {
PATH: `${path.resolve('node_modules/.bin')}:${process.env.PATH}`
PATH: `${modulesBin}:${process.env.PATH}`
})
})

cmd.on('close', code => {
promise.on('close', code => {
if (code === 0) {
resolve()
} else {
reject(new MaidError(`task "${task.name}" exited with code ${code}`))
}
})

return cmd
// we don't need to return
// 1. because this function is executed in new Promise(() => {})
// 2. we have its resolve and reject, so it's better to use them
promise.then(resolve).catch(err => {
reject(new MaidError(`task "${task.name}" failed with "${err.message}"`))
})
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"cac": "^5.0.10",
"chalk": "^2.4.1",
"cross-spawn": "^6.0.5",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanna take this out if it isn't being used anymore?

Copy link
Contributor Author

@tunnckoCore tunnckoCore Jun 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yea, forgot about that.. thanks.

"execa": "^0.10.0",
"fancy-log": "^1.3.2",
"joycon": "^1.0.4",
"markdown-it": "^8.4.1",
Expand Down
16 changes: 6 additions & 10 deletions test/snapshots/parseMarkdown.test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ Generated by [AVA](https://ava.li).
before: [],
description: '',
name: 'key',
script: `console.log('key')␊
`,
script: 'console.log(\'key\')',
type: 'js',
},
{
after: [],
before: [],
description: 'hehehe',
name: 'goodbye',
script: `echo goodbye␊
`,
script: 'echo goodbye',
type: 'sh',
},
],
Expand Down Expand Up @@ -55,21 +53,19 @@ Generated by [AVA](https://ava.li).
But it's very complex so I'm writing some instructions.`,
name: 'hey',
script: `console.log('key')␊
`,
script: 'console.log(\'key\')',
type: 'js',
},
{
after: [],
before: [],
description: 'hehehe',
name: 'goodbye',
script: `echo goodbye␊
`,
script: 'echo goodbye',
type: 'sh',
},
],

}

## use readme

Expand All @@ -85,4 +81,4 @@ Generated by [AVA](https://ava.li).
name: 'dev',
},
],
}
}
Binary file modified test/snapshots/parseMarkdown.test.js.snap
Binary file not shown.
14 changes: 13 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ cross-spawn@^5.0.1, cross-spawn@^5.1.0:
shebang-command "^1.2.0"
which "^1.2.9"

cross-spawn@^6.0.5:
cross-spawn@^6.0.0, cross-spawn@^6.0.5:
version "6.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
dependencies:
Expand Down Expand Up @@ -1427,6 +1427,18 @@ esutils@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"

execa@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.10.0.tgz#ff456a8f53f90f8eccc71a96d11bdfc7f082cb50"
dependencies:
cross-spawn "^6.0.0"
get-stream "^3.0.0"
is-stream "^1.1.0"
npm-run-path "^2.0.0"
p-finally "^1.0.0"
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777"
Expand Down