UNPKG

1.98 kBJavaScriptView Raw
1const cli = require('heroku-cli-util')
2const Sanbashi = require('../lib/sanbashi')
3
4let usage = `
5 ${cli.color.bold.underline.magenta('Usage:')}
6 ${cli.color.cmd('heroku container:run web bash')} # Runs bash on the local web docker container
7 ${cli.color.cmd('heroku container:run worker')} # Runs the container CMD on the local worker container`
8
9module.exports = function (topic) {
10 return {
11 topic: topic,
12 command: 'run',
13 description: 'builds, then runs the docker image locally',
14 needsApp: true,
15 needsAuth: true,
16 variableArgs: true,
17 help: usage,
18 flags: [
19 {
20 name: 'port',
21 char: 'p',
22 hasValue: true,
23 description: 'port the app will run on'
24 },
25 {
26 name: 'verbose',
27 char: 'v',
28 hasValue: false
29 }
30 ],
31 run: cli.command(run)
32 }
33}
34
35let run = async function (context, heroku) {
36 if (context.args.length === 0) {
37 cli.error(`Error: Requires one process type\n ${usage} `, 1)
38 return
39 }
40
41 let processType = context.args.shift()
42 let command = context.args
43
44 let herokuHost = process.env.HEROKU_HOST || 'heroku.com'
45 let registry = `registry.${herokuHost}`
46 let dockerfiles = Sanbashi.getDockerfiles(process.cwd(), false)
47 let possibleJobs = Sanbashi.getJobs(`${registry}/${context.app}`, dockerfiles)
48
49 let jobs = []
50 if (possibleJobs.standard) {
51 possibleJobs.standard.forEach((pj) => { pj.resource = pj.resource.replace(/standard$/, processType) })
52 jobs = possibleJobs.standard || []
53 }
54 if (!jobs.length) {
55 cli.error('No images to run', 1)
56 return
57 }
58 let job = jobs[0]
59
60 if (command.length === 0) {
61 cli.styledHeader(`Running ${job.resource}`)
62 } else {
63 cli.styledHeader(`Running '${command}' on ${job.resource}`)
64 }
65 try {
66 await Sanbashi.runImage(job.resource, command, context.flags.port || 5000, context.flags.verbose)
67 } catch (err) {
68 cli.error(`Error: docker run exited with ${err}`, 1)
69 }
70}