forked from JasonEtco/actions-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-action.js
executable file
路170 lines (154 loc) 路 4.92 KB
/
create-action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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`)
}