/
react.ts
143 lines (126 loc) · 4.26 KB
/
react.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
131
132
133
134
135
136
137
138
139
140
141
142
143
import {CLICommand} from '@coveo/cli-commons/command/cliCommand';
import {Flags} from '@oclif/core';
import {Config} from '@coveo/cli-commons/config/config';
import {platformUrl} from '@coveo/cli-commons/platform/environment';
import {AuthenticatedClient} from '@coveo/cli-commons/platform/authenticatedClient';
import {spawnProcess, spawnProcessOutput} from '../../../lib/utils/process';
import {getPackageVersion} from '../../../lib/utils/misc';
import {appendCmdIfWindows} from '@coveo/cli-commons/utils/os';
import {
Preconditions,
IsAuthenticated,
HasNecessaryCoveoPrivileges,
AuthenticationType,
} from '@coveo/cli-commons/preconditions/index';
import {
createApiKeyPrivilege,
impersonatePrivilege,
listSearchHubsPrivilege,
} from '@coveo/cli-commons/preconditions/platformPrivilege';
import {Trackable} from '@coveo/cli-commons/preconditions/trackable';
import {
IsNodeVersionInRange,
IsNpxInstalled,
} from '../../../lib/decorators/preconditions';
import {promptForSearchHub} from '../../../lib/ui/shared';
type ReactProcessEnv = {
orgId: string;
apiKey: string;
user: string;
platformUrl: string;
platformEnvironment: string;
};
export default class React extends CLICommand {
public static templateName = '@coveo/cra-template';
public static cliPackage = 'create-react-app@latest';
/**
* "You’ll need to have Node 18.18.1 or later version on your local development machine"
* https://github.com/facebook/create-react-app#creating-an-app
*/
public static requiredNodeVersion = '^18.18.1 || ^20.9.0';
public static description =
'Create a Coveo Headless-powered search page with the React web framework. See <https://docs.coveo.com/headless> and <https://reactjs.org/>.';
public static examples = [
'$ coveo ui:create:react myapp',
'$ coveo ui:create:react --help',
];
public static flags = {
version: Flags.string({
char: 'v',
description: `Version of ${React.templateName} to use.`,
default: getPackageVersion(React.templateName),
}),
};
public static args = [
{
name: 'name',
description: 'The name of the application to create.',
required: true,
},
];
@Trackable({
eventName: 'ui create',
overrideEventProperties: {framework: 'react'},
})
@Preconditions(
IsAuthenticated([AuthenticationType.OAuth]),
IsNodeVersionInRange(React.requiredNodeVersion),
IsNpxInstalled(),
HasNecessaryCoveoPrivileges(
createApiKeyPrivilege,
impersonatePrivilege,
listSearchHubsPrivilege
)
)
public async run() {
const {args} = await this.parse(React);
await this.createProject(args.name);
await this.setupEnvironmentVariables(args.name);
}
private async setupEnvironmentVariables(name: string) {
const authenticatedClient = new AuthenticatedClient();
const platformClient = await authenticatedClient.getClient();
const searchHub = await promptForSearchHub(platformClient);
const {args} = await this.parse(React);
const cfg = this.configuration.get();
const username = await authenticatedClient.getUsername();
const apiKey = await authenticatedClient.createImpersonateApiKey(
args.name,
searchHub
);
const env: ReactProcessEnv = {
orgId: cfg.organization,
apiKey: apiKey.value!,
user: username,
platformUrl: platformUrl({environment: cfg.environment}),
platformEnvironment: cfg.environment,
};
await spawnProcessOutput(appendCmdIfWindows`npm`, ['run', 'setup-env'], {
cwd: name,
env: {...process.env, ...env},
});
}
private async createProject(name: string) {
const {flags} = await this.parse(React);
const templateVersion =
flags.version || getPackageVersion(React.templateName);
const exitCode = await this.runReactCliCommand([
name,
'--template',
`${React.templateName}@${templateVersion}`,
]);
if (exitCode !== 0) {
this.error(
new Error(
'create-react-app was unable to create the project. See the logs above for more information.'
)
);
}
}
private runReactCliCommand(args: string[]) {
return spawnProcess(appendCmdIfWindows`npx`, [React.cliPackage, ...args]);
}
private get configuration() {
return new Config(this.config.configDir);
}
}