1 | const UserSettings = require('../user/UserSettings')
|
2 | const DcHttpClient = require('../DcHttpClient')
|
3 | const UnauthorizedError = require('../errors/UnauthorizedError')
|
4 | const AppSettings = require('../app/AppSettings')
|
5 | const logger = require('../logger')
|
6 | const { SETTINGS_FOLDER, EXTENSIONS_FOLDER, PIPELINES_FOLDER, TRUSTED_PIPELINES_FOLDER, THEMES_FOLDER } = require('../app/Constants')
|
7 | const inquirer = require('inquirer')
|
8 | const path = require('path')
|
9 | const fsEx = require('fs-extra')
|
10 | const utils = require('../utils/utils')
|
11 |
|
12 | const FOLDERS = [SETTINGS_FOLDER, EXTENSIONS_FOLDER, PIPELINES_FOLDER, TRUSTED_PIPELINES_FOLDER, THEMES_FOLDER]
|
13 |
|
14 | class InitAction {
|
15 | |
16 |
|
17 |
|
18 | static register (caporal) {
|
19 | caporal
|
20 | .command('init')
|
21 | .description('Init the sdk')
|
22 | .option('--appId <appId>', 'set the Sandbox App ID you want to initialize')
|
23 | .action((args, options) => new InitAction().run(options, (err, appId, aborted) => {
|
24 | if (err) return logger.error(err.message)
|
25 | if (aborted) return logger.info('Init was aborted by user')
|
26 | logger.info(`The Application "${appId}" was successfully initialized`)
|
27 | }))
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 | async run (options, cb) {
|
35 | this.options = options
|
36 | const userSettings = new UserSettings().validate()
|
37 | const appSettings = new AppSettings()
|
38 |
|
39 | let appSettingsExists = true
|
40 | try {
|
41 | appSettings.validate()
|
42 | } catch (err) {
|
43 | appSettingsExists = false
|
44 | logger.debug('App settings are not present')
|
45 | }
|
46 |
|
47 | if (appSettingsExists) {
|
48 | return this.checkForRunningProcesses(inquirer.prompt, (err, stopInit) => {
|
49 | if (err) return cb(err)
|
50 | if (stopInit) return cb(null, null, true)
|
51 |
|
52 | return this.permitDeletion(inquirer.prompt, appSettings.getId(), (err, overwrite) => {
|
53 | if (err) return cb(err)
|
54 | if (!overwrite) return cb(null, null, true)
|
55 | this.getAppId(inquirer.prompt, (err, appId) => {
|
56 | if (err) return cb(err)
|
57 | this.initApplication(appId, userSettings, true, cb)
|
58 | })
|
59 | })
|
60 | })
|
61 | }
|
62 |
|
63 | return this.getAppId(inquirer.prompt, (err, appId) => {
|
64 | if (err) return cb(err)
|
65 | if (!appId) return cb(new Error('Sandbox App ID (--appId) is invalid'))
|
66 |
|
67 | FOLDERS.forEach(folder => {
|
68 | if (process.env.APP_PATH) folder = path.join(process.env.APP_PATH, folder)
|
69 | fsEx.ensureDir(folder)
|
70 | })
|
71 |
|
72 | this.initApplication(appId, userSettings, false, cb)
|
73 | })
|
74 | }
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 | getAppId (prompt, cb) {
|
81 | if (this.options.appId) return cb(null, this.options.appId)
|
82 | prompt([{type: 'input', name: 'appId', message: 'Enter your Sandbox App ID:'}]).then(answers => cb(null, answers.appId))
|
83 | }
|
84 |
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 | permitDeletion (prompt, appId, cb) {
|
91 | const contentToBeDeleted = [
|
92 | ' - project related settings',
|
93 | ' - generated extension/theme config files',
|
94 | ' - active (trusted) pipelines'
|
95 | ].join('\n')
|
96 |
|
97 | logger.info(`The application ${appId} is currently initialized.\n\nReinit will delete the following project content:\n${contentToBeDeleted}`)
|
98 | prompt({type: 'input', name: 'overwrite', default: 'n', message: `Do you really want to overwrite your current application (${appId})? (y/N)`}).then(answers => {
|
99 | return cb(null, answers.overwrite.charAt(0).toLowerCase() === 'y')
|
100 | })
|
101 | }
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 | async checkForRunningProcesses (prompt, cb) {
|
108 | const processes = []
|
109 | if (await utils.previousProcess('backend', SETTINGS_FOLDER)) processes.push('backend')
|
110 | if (await utils.previousProcess('frontend', SETTINGS_FOLDER)) processes.push('frontend')
|
111 |
|
112 | if (processes.length > 0) {
|
113 | return prompt({type: 'input', name: 'ignore', default: 'n', message: `Processe(s): ${processes.join(', ')} running, reinit can cause problems in these processes; ignore? (y,N)`}).then(answers => {
|
114 | return cb(null, answers.ignore.charAt(0).toLowerCase() === 'n')
|
115 | })
|
116 | }
|
117 |
|
118 | return cb(null, false)
|
119 | }
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | initApplication (appId, userSettings, reset, cb) {
|
128 | new DcHttpClient(userSettings).getApplicationData(appId, (err) => {
|
129 | if (err) {
|
130 | logger.debug(err)
|
131 | if (err instanceof UnauthorizedError) return cb(new Error('You\'re not logged in! Please run `sgcloud login` again.'))
|
132 | if (cb) return cb(new Error(`The application ${appId} is not available or permissions are missing (message: ${err.message}). Please check the application at developer.shopgate.com!`))
|
133 | }
|
134 | if (reset) utils.resetProject()
|
135 | new AppSettings().setId(appId)
|
136 | cb(null, appId, false)
|
137 | })
|
138 | }
|
139 | }
|
140 |
|
141 | module.exports = InitAction
|