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
Create CLI questionnaire #57
Merged
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2c4ed54
WIP: Create CLI questionnaire
macklinu 456cdeb
Replace mustache with custom solution
macklinu 78012c5
Add scripts/update-feather-icons.js
macklinu 6b92afe
Use Map.prototype.clear in fs mock
macklinu d319112
Get tests passing :D
macklinu da14ce9
Update scripts/update-feather-icons.js
JasonEtco dde2810
Update tests/cli.test.js
JasonEtco db2c9cb
More updates / WIP
macklinu 9232edb
More documenatation
macklinu 9a02778
Tweak welcome message spacing
macklinu 27725dd
Fix async write file loop
macklinu fc3dd0b
Address PR comments
macklinu 66c812d
Add and update tests for 100% test coverage
macklinu 863619f
Merge branch 'master' into cli-questionnaire
JasonEtco ee68111
Couple small tweaks
JasonEtco 5ba626d
Whitespace and comments
JasonEtco 9bbf52d
Use Signale
JasonEtco b89f13a
Update tests to use real filesystem
macklinu aa828d6
Merge remote-tracking branch 'upstream/master' into cli-questionnaire
macklinu 8bbe9a0
Fix usage log
JasonEtco 3a4b2dc
Fix test
JasonEtco File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ src | |
tsconfig.json | ||
tslint.json | ||
docs | ||
.github | ||
.github | ||
scripts |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env node | ||
|
||
const createAction = require('./create-action') | ||
|
||
createAction(process.argv.slice(2)) | ||
.then(() => { | ||
process.exit(0) | ||
}) | ||
.catch(error => { | ||
console.error(error) | ||
process.exit(1) | ||
}) |
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,10 @@ | ||
[ | ||
"white", | ||
"yellow", | ||
"blue", | ||
"green", | ||
"orange", | ||
"red", | ||
"purple", | ||
"gray-dark" | ||
] |
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,170 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const { promisify } = require('util') | ||
const minimist = require('minimist') | ||
const { Signale } = require('signale') | ||
const { prompt } = require('enquirer') | ||
const colors = require('./colors.json') | ||
const icons = require('./feather-icons.json') | ||
|
||
const readFile = promisify(fs.readFile) | ||
const writeFile = promisify(fs.writeFile) | ||
const mkdir = promisify(fs.mkdir) | ||
|
||
const signale = new Signale({ | ||
config: { | ||
displayLabel: false | ||
} | ||
}) | ||
|
||
/** | ||
* Reads a template file from disk. | ||
* | ||
* @param {string} filename The template filename to read. | ||
* @returns {Promise<string>} The template file string contents. | ||
*/ | ||
async function readTemplate (filename) { | ||
const templateDir = path.join(__dirname, 'template') | ||
return readFile(path.join(templateDir, filename), 'utf8') | ||
} | ||
|
||
/** | ||
* A predicate function to ensure a string is not empty. | ||
* | ||
* @param {string} value The string value. | ||
* @returns {boolean} Whether the string is empty or not. | ||
*/ | ||
const isNotEmpty = value => value.length > 0 | ||
|
||
/** | ||
* The options object returned from the CLI questionnaire prompt. | ||
* @typedef {object} PromptAnswers | ||
* @property {string} name The action name. | ||
* @property {string} description The action description. | ||
* @property {string} icon The feather icon name. See `bin/feather-icons.json` for options. | ||
* @property {string} color The GitHub Action color. See `bin/colors.json` for options. | ||
*/ | ||
|
||
/** | ||
* Prompts the user with a questionnaire to get key metadata for the GitHub Action. | ||
* | ||
* @returns {Promise<PromptAnswers>} An object containing prompt answers. | ||
*/ | ||
async function getActionMetadata () { | ||
return prompt([ | ||
{ | ||
type: 'input', | ||
name: 'name', | ||
message: 'What is the name of your action?', | ||
initial: 'Your action name', | ||
validate: isNotEmpty | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'description', | ||
message: 'What is a short description of your action?', | ||
initial: 'A description of your action', | ||
validate: isNotEmpty | ||
}, | ||
{ | ||
type: 'autocomplete', | ||
name: 'icon', | ||
message: 'Choose an icon for your action. Visit https://feathericons.com for a visual reference.', | ||
choices: icons, | ||
limit: 10 | ||
}, | ||
{ | ||
type: 'autocomplete', | ||
name: 'color', | ||
message: 'Choose a background color background color used in the visual workflow editor for your action.', | ||
choices: colors | ||
} | ||
]) | ||
} | ||
|
||
/** | ||
* Creates a Dockerfile contents string, replacing variables in the Dockerfile template | ||
* with values passed in by the user from the CLI prompt. | ||
* | ||
* @param {PromptAnswers} answers The CLI prompt answers. | ||
* @returns {Promise<string>} The Dockerfile contents. | ||
*/ | ||
async function createDockerfile (answers) { | ||
const dockerfileTemplate = await readTemplate('Dockerfile') | ||
return dockerfileTemplate | ||
.replace(':NAME', answers.name) | ||
.replace(':DESCRIPTION', answers.description) | ||
.replace(':ICON', answers.icon) | ||
.replace(':COLOR', answers.color) | ||
} | ||
|
||
/** | ||
* Creates a `package.json` object with the latest version | ||
* of `actions-toolkit` ready to be installed. | ||
* | ||
* @param {string} name The action package name. | ||
* @returns {object} The `package.json` contents. | ||
*/ | ||
function createPackageJson (name) { | ||
const { version } = require('../package.json') | ||
return { | ||
name, | ||
private: true, | ||
main: 'index.js', | ||
dependencies: { | ||
'actions-toolkit': `^${version}` | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Runs the create action CLI prompt and bootstraps a new directory for the user. | ||
* | ||
* @public | ||
* @param {string[]} argv The command line arguments to parse. | ||
* @returns {Promise<void>} Nothing. | ||
*/ | ||
module.exports = async function createAction (argv) { | ||
const args = minimist(argv) | ||
const directoryName = args._[0] | ||
if (!directoryName || args.help) { | ||
signale.info(`\nUsage: npx actions-toolkit <name>`) | ||
return process.exit(1) | ||
} | ||
|
||
signale.star('Welcome to actions-toolkit! Let\'s get started creating an action.\n') | ||
|
||
const base = path.join(process.cwd(), directoryName) | ||
try { | ||
signale.info(`Creating folder ${base}...`) | ||
await mkdir(base) | ||
} catch (err) { | ||
if (err.code === 'EEXIST') { | ||
signale.fatal(`Folder ${base} already exists!`) | ||
} | ||
throw err | ||
} | ||
|
||
// Collect answers | ||
const metadata = await getActionMetadata() | ||
|
||
signale.log('\n------------------------------------\n') | ||
|
||
// Create the templated files | ||
const dockerfile = await createDockerfile(metadata) | ||
const packageJson = createPackageJson(directoryName) | ||
const entrypoint = await readTemplate('entrypoint.js') | ||
|
||
await Promise.all([ | ||
['package.json', JSON.stringify(packageJson, null, 2)], | ||
['Dockerfile', dockerfile], | ||
['entrypoint.js', entrypoint] | ||
].map(async ([filename, contents]) => { | ||
signale.info(`Creating ${filename}...`) | ||
await writeFile(path.join(base, filename), contents) | ||
})) | ||
|
||
signale.log('\n------------------------------------\n') | ||
signale.success(`Done! Enjoy building your GitHub Action!`) | ||
signale.info(`Get started with:\n\ncd ${directoryName} && npm install`) | ||
} |
Oops, something went wrong.
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.
This is now the main CLI entrypoint - also defined in
package.json
.