-
-
Notifications
You must be signed in to change notification settings - Fork 35
Write maid scripts to package.json #26
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5940074
Add cli option for updating package.json scripts
zephraph 636a120
Enable automatically writing scripts to package.json with --update-sc…
zephraph 244d2da
Move updateScripts to command, update on commit
zephraph e23da5f
Start documentation
zephraph 42fe4ed
Key maid executable off of first arg, undo my pathing snaffle
zephraph 46605d7
Add note about package.json staging
zephraph 030d503
Undo try 2
zephraph d4ee5c9
Make passthrough more explicit, add noop for lint staged
zephraph fae6920
Add no-write command for debugging
zephraph 81f2e92
Remove noop being as explicit passthrough makes it unnecessary
zephraph 9a1b0a2
Remove noop references
zephraph 39b87f8
Don't write pre/post tasks if their base command exists
zephraph ad01c9a
Merge remote-tracking branch 'upstream/master' into update-scripts
zephraph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const loadFile = require('./loadFile') | ||
const MaidError = require('./MaidError') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const { exec } = require('./runCLICommand') | ||
|
||
const flattenObj = (a, b) => ({ | ||
...a, | ||
...b | ||
}) | ||
|
||
module.exports = (maid, flags) => { | ||
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 => !['update-scripts', '--git-add'].includes(arg)) | ||
.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) | ||
) | ||
if (flags.gitAdd) { | ||
exec('git', ['add', pkgPath]) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.