-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added docs:edit command and support for subcommands
- Loading branch information
Dom Harrington
committed
May 18, 2018
1 parent
410b3b6
commit 36b2059
Showing
9 changed files
with
243 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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,81 @@ | ||
const request = require('request-promise-native'); | ||
|
||
const config = require('config'); | ||
const fs = require('fs'); | ||
const editor = require('editor'); | ||
const { promisify } = require('util'); | ||
|
||
const writeFile = promisify(fs.writeFile); | ||
const readFile = promisify(fs.readFile); | ||
const unlink = promisify(fs.unlink); | ||
|
||
exports.desc = 'Edit a single file from your ReadMe project without saving locally'; | ||
exports.category = 'services'; | ||
exports.weight = 3; | ||
exports.action = 'docs:edit'; | ||
|
||
exports.run = async function({ args, opts }) { | ||
const { key, version } = opts; | ||
|
||
if (!key) { | ||
return Promise.reject(new Error('No api key provided. Please use --key')); | ||
} | ||
|
||
if (!version) { | ||
return Promise.reject(new Error('No version provided. Please use --version')); | ||
} | ||
|
||
if (!args[0]) { | ||
return Promise.reject(new Error('No slug provided. Usage `rdme docs:edit <slug>`')); | ||
} | ||
|
||
const slug = args[0]; | ||
const filename = `${slug}.md`; | ||
|
||
const options = { | ||
auth: { user: key }, | ||
headers: { | ||
'x-readme-version': version, | ||
}, | ||
}; | ||
|
||
const existingDoc = await request.get(`${config.host}/api/v1/docs/${slug}`, { | ||
json: true, | ||
...options, | ||
}).catch(err => { | ||
if (err.statusCode === 404) { | ||
return Promise.reject(err.error); | ||
} | ||
|
||
return Promise.reject(err); | ||
}); | ||
|
||
await writeFile(filename, existingDoc.body); | ||
|
||
return new Promise((resolve, reject) => { | ||
(opts.mockEditor || editor)(filename, async code => { | ||
if (code !== 0) return reject(new Error('Non zero exit code from $EDITOR')); | ||
const updatedDoc = await readFile(filename, 'utf8'); | ||
|
||
return request | ||
.put(`${config.host}/api/v1/docs/${slug}`, { | ||
json: Object.assign(existingDoc, { | ||
body: updatedDoc, | ||
}), | ||
...options, | ||
}) | ||
.then(async () => { | ||
console.log('Doc successfully updated. Cleaning up local file'); | ||
await unlink(filename); | ||
return resolve(); | ||
}) | ||
.catch(err => { | ||
if (err.statusCode === 400) { | ||
return reject(err.error); | ||
} | ||
|
||
return reject(err); | ||
}); | ||
}); | ||
}); | ||
}; |
This file contains 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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