-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.js
161 lines (141 loc) · 4.33 KB
/
setup.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
const argv = require('minimist')(process.argv.slice(2))
const chalk = require('chalk')
const fs = require('fs')
const path = require('path')
const prompt = require('prompt')
const builds = require('../lib/builds')
const config = require('../lib/config')()
const slug = require('../lib/slug')
module.exports = async () => {
const setDefaults =
argv &&
typeof argv.c !== 'undefined' &&
argv.h !== 'undefined' &&
argv.d !== 'undefined' &&
argv.u !== 'undefined' &&
argv.p !== 'undefined'
if (setDefaults && typeof argv.a === 'undefined') {
argv.a = 'sandbox'
}
if (setDefaults && typeof argv.v === 'undefined') {
argv.v = 'develop'
}
prompt.message = ''
prompt.error = ''
prompt.delimiter = ''
prompt.override = argv
// Intentional Blank Line
console.log('')
prompt.start()
prompt.get(
[
{
description: chalk.cyan('Client Name:'),
name: 'c',
pattern: /^[a-zA-Z-_ ]+$/,
required: true,
},
{
description: chalk.cyan('Hostname:'),
name: 'h',
pattern: /^[a-z0-9_.-]+$/,
message: 'Invalid Host Name. ( e.g. dev04-web-mysandbox.demandware.net )',
required: true,
conform: function (hostname) {
if (typeof hostname !== 'string') return false
var parts = hostname.split('.')
if (parts.length <= 1) return false
var tld = parts.pop()
var tldRegex = /^(?:xn--)?[a-zA-Z0-9]+$/gi
if (!tldRegex.test(tld)) return false
var isValid = parts.every(function (host) {
var hostRegex = /^(?!:\/\/)([a-zA-Z0-9]+|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])$/gi
return hostRegex.test(host)
})
return isValid
},
},
{
description: chalk.cyan('Code Version:'),
name: 'v',
pattern: /^[a-zA-Z0-9]+$/,
message: 'Code Version. ( e.g. develop, sitegenesis, etc )',
required: true,
default: 'develop',
},
{
description: chalk.cyan('Instance Alias:'),
name: 'a',
pattern: /^[a-zA-Z0-9]+$/,
message: 'Invalid Instance Alias. ( e.g. dev04, sandbox, staging, etc )',
required: true,
default: 'sandbox',
},
{
description: chalk.cyan('Directory:'),
name: 'd',
required: true,
message: 'Directory does not exist. ( e.g. /Users/Name/Projects/mysandbox )',
conform: function (directory) {
directory = path.normalize(path.resolve(directory.replace(/^\/[a-z]\//, '/')))
return fs.existsSync(directory)
},
},
{
description: chalk.cyan('Username:'),
name: 'u',
pattern: /^[a-zA-Z0-9_@.-]+$/,
message: 'Invalid Username. ( e.g. myusername, my@email.com, etc )',
required: true,
},
{
description: chalk.cyan('Password:'),
name: 'p',
required: true,
hidden: true,
replace: '*',
},
],
function (err, result) {
if (err) {
if (err.message === 'canceled') {
console.log(chalk.red('× Setup Cancelled'))
process.exit(1)
} else {
console.log(chalk.red('× ERROR:', err))
}
} else {
const client = slug(result.c)
const alias = slug(result.a)
const currentConfig = config.get()
let newConfig = {}
let isUpdate = false
// Get current config if it is already there
if (Object.keys(currentConfig).length > 0 && currentConfig.constructor === Object) {
newConfig = Object.assign({}, newConfig, currentConfig)
isUpdate = true
}
// Check if this is a new client
if (typeof newConfig[client] === 'undefined') {
newConfig[client] = {}
}
const directory = path.normalize(path.resolve(result.d.replace(/^\/[a-z]\//, '/')))
// Fetch Build Scripts from Project Directory
const builders = builds(directory)
// Create / Overwrite SFCC Instance for Client
newConfig[client][alias] = {
h: result.h,
v: result.v,
a: result.a,
d: directory,
u: result.u,
p: result.p,
b: builders,
}
// Write Config File
config.set(newConfig, isUpdate)
process.exit()
}
}
)
}