-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
Write maid scripts to package.json #26
Changes from 2 commits
5940074
636a120
244d2da
e23da5f
42fe4ed
46605d7
030d503
d4ee5c9
fae6920
81f2e92
9a1b0a2
39b87f8
ad01c9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
#!/usr/bin/env node | ||
const cli = require('cac')() | ||
const chalk = require('chalk') | ||
const MaidError = require('../lib/MaidError') | ||
|
||
cli.command('*', 'Run a task in current working directory', (input, flags) => { | ||
if (flags.updateScripts && input[0]) { | ||
throw new MaidError('Cannot run task and update scripts') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why disallow this 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the updateScripts method, I'm passing all the arguments through transparently. If I allowed this I'd have to find the input and remove it before writing to the package.json. So... laziness. I can remove it. Point might be moot if this turns into its own command though. |
||
} else if (flags.updateScripts) { | ||
const runner = require('..')(flags) | ||
const updateScripts = require('../lib/updateScripts') | ||
updateScripts(runner) | ||
return | ||
} | ||
|
||
const taskName = input[0] | ||
if (!taskName) { | ||
return cli.showHelp() | ||
|
@@ -44,4 +54,9 @@ cli.option('section', { | |
alias: 's' | ||
}) | ||
|
||
cli.option('update-scripts', { | ||
desc: 'Write maid tasks to package.json scripts', | ||
type: 'boolean' | ||
}) | ||
|
||
cli.parse() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const loadFile = require('./loadFile') | ||
const MaidError = require('./MaidError') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const flattenObj = (a, b) => ({ | ||
...a, | ||
...b | ||
}) | ||
|
||
module.exports = maid => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should probably break this function down. |
||
const { path: pkgPath, data: pkg } = loadFile.loadSync(['package.json']) | ||
if (!pkgPath) return null | ||
|
||
const maidExec = process.argv[1].endsWith('.js') | ||
? `node ${path.relative(process.cwd(), process.argv[1])}` | ||
: 'maid' | ||
|
||
const { scripts = {} } = pkg | ||
const tasks = maid.listTasks() | ||
|
||
const passThroughArgs = process.argv | ||
.slice(2) | ||
.filter(arg => arg !== '--update-scripts') | ||
.join(' ') | ||
|
||
const baseScripts = Object.keys(scripts) | ||
.filter(task => !scripts[task].startsWith(maidExec)) | ||
.map(task => ({ [task]: scripts[task] })) | ||
.reduce(flattenObj, {}) | ||
|
||
const conflictingTasks = tasks.filter(task => | ||
Object.keys(baseScripts).includes(task) | ||
) | ||
|
||
if (conflictingTasks.length) { | ||
throw new MaidError( | ||
`Conflicts between maidfile and package.json. Please check these scripts: \n\t | ||
${conflictingTasks.join(', ')}` | ||
) | ||
} | ||
|
||
const finalScripts = tasks | ||
.map(task => ({ | ||
[task]: `${maidExec} ${passThroughArgs} ${task}`.replace(/\s+/g, ' ') | ||
})) | ||
.reduce(flattenObj, baseScripts) | ||
|
||
fs.writeFileSync( | ||
pkgPath, | ||
JSON.stringify({ ...pkg, ...{ scripts: finalScripts } }, null, 2) | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,9 @@ | |
"lib" | ||
], | ||
"scripts": { | ||
"test": "node bin/cli lint && node bin/cli test" | ||
"lint": "node bin/cli.js lint", | ||
"test": "node bin/cli.js test", | ||
"toc": "node bin/cli.js toc" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 AUTOMAGICALLY GENERATED 🎉 |
||
}, | ||
"author": "egoist <[email protected]>", | ||
"license": "MIT", | ||
|
@@ -66,4 +68,4 @@ | |
"git add" | ||
] | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This really gives me pause... the more I think about it the more I think this should be a command