-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcreate.ts
101 lines (80 loc) · 3.98 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
import { flags } from '@oclif/command';
import { join } from 'path';
import { SfdxCommand, core } from '@salesforce/command';
import util = require('util');
import request = require('request-promise-native');
const exec = util.promisify(require('child_process').exec);
const chalk = require('chalk');
const usernameURL = 'https://unique-username-generator.herokuapp.com/unique';
export default class Delete extends SfdxCommand {
public static description = 'create an org with a friendly username. wraps force:org:create';
public static examples = [
`sfdx shane:org:create -u 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 -u 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({ char: 'u', 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.string({ 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.string({ char: 'd', default: '7', description: 'duration of the scratch org (in days) (default:7, min:1, max:30)' }),
wait: flags.string({description: 'the streaming client socket timeout (in minutes) (default:6, 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' }),
targetdevhubusername: flags.boolean({ char: 'v', description: 'username or alias for the dev hub org; overrides default dev hub org' })
};
protected static requiresProject = true;
public async run(): Promise<any> { // tslint:disable-line:no-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 = command + ` -i ${this.flags.clientid}`;
}
if (this.flags.targetdevhubusername) {
command = command + ` -v ${this.flags.targetdevhubusername}`;
}
if (this.flags.setalias) {
command = command + ` -a ${this.flags.setalias}`;
}
// optional boolean
if (this.flags.noancestors) {
command = command + ` -c`;
}
if (this.flags.nonamespace) {
command = command + ` -n`;
}
if (this.flags.setdefaultusername) {
command = command + ` -s`;
}
if (this.flags.setdefaultusername) {
command = command + ` username=${usernameResult.message}`;
}
this.ux.log(`executing ${command}`);
const response = await exec(command);
if (response.stdout){
const success = JSON.parse(response.stdout);
if (success.status == 0){
this.ux.log(chalk.green(`Org created with id ${success.result.orgId} and username ${success.result.username} `));
} else {
this.ux.log(chalk.red(response.stderr));
}
}
return response.stderr || response.stdout;
}
}