UNPKG

5 kBJavaScriptView Raw
1const UserSettings = require('../user/UserSettings')
2const DcHttpClient = require('../DcHttpClient')
3const UnauthorizedError = require('../errors/UnauthorizedError')
4const AppSettings = require('../app/AppSettings')
5const logger = require('../logger')
6const { SETTINGS_FOLDER, EXTENSIONS_FOLDER, PIPELINES_FOLDER, TRUSTED_PIPELINES_FOLDER, THEMES_FOLDER } = require('../app/Constants')
7const inquirer = require('inquirer')
8const path = require('path')
9const fsEx = require('fs-extra')
10const utils = require('../utils/utils')
11
12const FOLDERS = [SETTINGS_FOLDER, EXTENSIONS_FOLDER, PIPELINES_FOLDER, TRUSTED_PIPELINES_FOLDER, THEMES_FOLDER]
13
14class InitAction {
15 /**
16 * @param {Command} caporal
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 * @param {object} options
32 * @param {function} [cb]
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 * @param {Prompt} prompt
78 * @param {function} cb
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 * @param {Prompt} prompt
87 * @param {String} appId
88 * @param {function} cb
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 * @param {Prompt} prompt
105 * @param {function} cb
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 * @param {String} appId
123 * @param {UserSettings} userSettings
124 * @param {bool} reset
125 * @param {function} cb
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
141module.exports = InitAction