1 | const fs = require('fs')
|
2 | const cli = require('heroku-cli-util')
|
3 | const co = require('co')
|
4 | const BB = require('bluebird')
|
5 | const writeFile = BB.promisify(fs.writeFile)
|
6 | const unlinkFile = BB.promisify(fs.unlink)
|
7 |
|
8 | function* run (context, heroku) {
|
9 | const appJSONPath = `${process.cwd()}/app.json`
|
10 | const appCiJSONPath = `${process.cwd()}/app-ci.json`
|
11 | let action
|
12 |
|
13 | function showWarning () {
|
14 | cli.log(cli.color.green('Please check the contents of your app.json before committing to your repo.'))
|
15 | }
|
16 |
|
17 | function* updateAppJson () {
|
18 | yield cli.action(
|
19 |
|
20 | `${action.charAt(0).toUpperCase() + action.slice(1)} app.json file`,
|
21 | writeFile(appJSONPath, `${JSON.stringify(appJSON, null, ' ')}\n`)
|
22 | )
|
23 | }
|
24 |
|
25 | let appJSON, appCiJSON
|
26 |
|
27 | try {
|
28 | appJSON = require(appJSONPath)
|
29 | action = 'updating'
|
30 | } catch (e) {
|
31 | action = 'creating'
|
32 | appJSON = {}
|
33 | }
|
34 |
|
35 | try {
|
36 | appCiJSON = require(appCiJSONPath)
|
37 | } catch (e) {
|
38 | let msg = `We couldn't find an app-ci.json file in the current directory`
|
39 | if (appJSON.environments == null) {
|
40 | msg += `, but we're ${action} ${action === 'updating' ? 'your' : 'a new'} app.json manifest for you.`
|
41 | appJSON.environments = {}
|
42 | cli.log(msg)
|
43 | yield updateAppJson()
|
44 | showWarning()
|
45 | } else {
|
46 | msg += `, and your app.json already has the environments key.`
|
47 | cli.log(msg)
|
48 | }
|
49 | }
|
50 |
|
51 | if (appCiJSON) {
|
52 | if (appJSON.environments && appJSON.environments.test) {
|
53 | cli.warn(`Your app.json already had a test key. We're overwriting it with the content of your app-ci.json`)
|
54 | }
|
55 |
|
56 | if (appJSON.environments == null) {
|
57 | appJSON.environments = {}
|
58 | }
|
59 |
|
60 | appJSON.environments.test = appCiJSON
|
61 | yield updateAppJson()
|
62 | yield cli.action(
|
63 | 'Deleting app-ci.json file',
|
64 | unlinkFile(appCiJSONPath)
|
65 | )
|
66 | showWarning()
|
67 | }
|
68 |
|
69 | cli.log(`You're all set! 🎉`)
|
70 | }
|
71 |
|
72 | module.exports = {
|
73 | topic: 'ci',
|
74 | command: 'migrate-manifest',
|
75 | needsApp: false,
|
76 | needsAuth: false,
|
77 | description: 'app-ci.json is deprecated. Run this command to migrate to app.json with an environments key.',
|
78 | help: `Example:
|
79 |
|
80 | $ heroku ci:migrate-manifest
|
81 | Writing app.json file... done
|
82 | Deleting app-ci.json file... done
|
83 | Please check the contents of your app.json before committing to your repo
|
84 | You're all set! 🎉.`,
|
85 | run: cli.command(co.wrap(run))
|
86 | }
|