-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcreate.ts
130 lines (113 loc) · 4.62 KB
/
create.ts
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
import { flags, SfdxCommand } from '@salesforce/command';
import chalk from 'chalk';
import { exec2JSON } from '@mshanemc/plugin-helpers';
import request = require('request-promise-native');
const usernameURL = 'https://unique-username-generator.herokuapp.com/unique';
export default class CreateOrg extends SfdxCommand {
public static description = 'create an org with a friendly username. wraps force:org:create';
public static examples = [
`sfdx shane:org:create --userprefix shane -o org.test
// creates an org from the default project config/project-scratch-def.json but with username shane[i]@org.test where i is a unique sequence number for that -u/-o combination
`,
`sfdx shane:org:create --userprefix shane -o org.test -a sydneyBristow -d 30 -v myOtherHub -f config/thatOtherFile.json
// above, but with an alias, a longer duration, and not the default hub, and not the default config file
`
];
protected static flagsConfig = {
userprefix: flags.string({
required: true,
description: "first part of the generated username. Example: 'shane' produces shane1@demo.org, shane2@demo.org"
}),
userdomain: flags.string({
char: 'o',
required: true,
description: "last part of the generated username (after the @ sign). Example: 'demo.org' produces shane1@demo.org, shane2@demo.org"
}),
clientid: flags.string({
char: 'i',
description: 'connected app consumer key'
}),
definitionfile: flags.filepath({
char: 'f',
default: 'config/project-scratch-def.json',
description: 'path to a scratch org definition file. Default = config/project-scratch-def.json'
}),
setalias: flags.string({
char: 'a',
description: 'set an alias for for the created scratch org'
}),
durationdays: flags.integer({
char: 'd',
default: 7,
min: 1,
max: 30,
description: 'duration of the scratch org (in days) (default:7, min:1, max:30)'
}),
wait: flags.integer({
description: 'the streaming client socket timeout (in minutes) (default:20, min:2)',
default: 20,
min: 2
}),
noancestors: flags.boolean({
char: 'c',
description: 'do not include second-generation package ancestors in the scratch org'
}),
nonamespace: flags.boolean({
char: 'n',
description: 'creates the scratch org with no namespace'
}),
setdefaultusername: flags.boolean({
char: 's',
description: 'set the created org as the default username'
}),
verbose: flags.builtin()
// targetdevhubusername: flags.boolean({description: 'username or alias for the dev hub org; overrides default dev hub org' })
};
protected static requiresProject = true;
public async run(): Promise<any> {
// gets the unique username
const usernameResult = await request.post({
uri: usernameURL,
body: {
prefix: this.flags.userprefix,
domain: this.flags.userdomain
},
json: true
});
// required flags
let command = `sfdx force:org:create --json -f ${this.flags.definitionfile} -d ${this.flags.durationdays} -w ${this.flags.wait || 20}`;
// optional value flags without defaults
if (this.flags.clientid) {
command += ` -i ${this.flags.clientid}`;
}
if (this.flags.targetdevhubusername) {
command += ` -v ${this.flags.targetdevhubusername}`;
}
if (this.flags.setalias) {
command += ` -a ${this.flags.setalias}`;
}
// optional boolean
if (this.flags.noancestors) {
command += ' -c';
}
if (this.flags.nonamespace) {
command += ' -n';
}
if (this.flags.setdefaultusername) {
command += ' -s';
}
if (this.flags.setdefaultusername) {
command += ` username=${usernameResult.message}`;
}
this.ux.log(`executing ${command}`);
const response = await exec2JSON(command);
if (!this.flags.json && this.flags.verbose) {
this.ux.logJson(response);
}
if (response.status === 0) {
this.ux.log(chalk.green(`Org created with id ${response.result.orgId} and username ${response.result.username} `));
return response.result;
}
throw new Error(response.message);
}
}