-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
184 lines (161 loc) · 5.75 KB
/
index.ts
File metadata and controls
184 lines (161 loc) · 5.75 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import * as path from 'path';
import { execSync, spawn } from 'child_process';
import { cliux, sanitizePath } from '@contentstack/cli-utilities';
import { default as ContentStackSeed } from '@contentstack/cli-cm-seed/lib/commands/cm/stacks/seed';
import { AppConfig } from '../config';
import messageHandler from '../messages';
import { setupEnvironments } from './utils';
import GithubError from './github/github-error';
import GitHubClient, { Repo } from './github/client';
export const ENGLISH_LOCALE = 'en-us';
export interface BootstrapOptions {
cloneDirectory: string;
seedParams: SeedParams;
appConfig: AppConfig;
managementAPIClient: any;
region: any;
accessToken?: string;
appType: string;
livePreviewEnabled?: boolean;
runDevServer?: boolean;
master_locale: any;
}
export interface SeedParams {
stackAPIKey?: string;
org?: string;
stackName?: string;
yes?: boolean;
managementTokenAlias?: string | undefined;
managementToken?: string | undefined;
}
/**
* @description Bootstraps the sample app
* Clone the repo
* Create the stack from the source
* Setup the environment
* Ready to use!!
*/
export default class Bootstrap {
private readonly ghClient: GitHubClient;
private repo: Repo;
private appConfig: AppConfig;
private region: any;
private managementAPIClient: any;
private cloneDirectory: string;
constructor(public options: BootstrapOptions) {
this.region = options.region;
this.appConfig = options.appConfig;
this.managementAPIClient = options.managementAPIClient;
this.repo = GitHubClient.parsePath(options.appConfig.source);
if (options.appConfig.branch) {
this.repo.branch = options.appConfig.branch;
}
this.cloneDirectory = path.join(sanitizePath(options.cloneDirectory), sanitizePath(this.repo.name));
this.ghClient = new GitHubClient(this.repo, options.appConfig.private, options.accessToken);
this.options = options;
}
async run(): Promise<any> {
cliux.loader(messageHandler.parse('CLI_BOOTSTRAP_START_CLONE_APP'));
try {
await this.ghClient.getLatest(this.cloneDirectory);
} catch (error) {
if (error instanceof GithubError) {
if (error.status === 404) {
cliux.error(messageHandler.parse('CLI_BOOTSTRAP_REPO_NOT_FOUND', this.appConfig.source));
}
}
throw error;
} finally {
cliux.loader();
}
// seed plugin start
try {
const cmd = ['--repo', this.appConfig.stack];
if (this.options.seedParams.stackAPIKey) {
cmd.push('--stack-api-key', this.options.seedParams.stackAPIKey);
}
if (this.options.seedParams.org) {
cmd.push('--org', this.options.seedParams.org);
}
if (this.options.seedParams.stackName) {
cmd.push('-n', this.options.seedParams.stackName);
}
if (this.options.seedParams.yes) {
cmd.push('-y');
}
if (this.options.seedParams.managementTokenAlias) {
cmd.push('--alias', this.options.seedParams.managementTokenAlias);
}
if (this.options.master_locale) {
cmd.push('--locale', this.options.master_locale);
}
const result = await ContentStackSeed.run(cmd);
if (result && result.api_key) {
await setupEnvironments(
this.managementAPIClient,
result.api_key,
this.appConfig,
this.cloneDirectory,
this.region,
this.options.livePreviewEnabled as boolean,
this.options.seedParams.managementToken as string,
);
} else {
throw new Error(messageHandler.parse('CLI_BOOTSTRAP_NO_API_KEY_FOUND'));
}
if (this.options.livePreviewEnabled) {
cliux.print(
messageHandler.parse('CLI_BOOTSTRAP_SUCCESS_LIVE_PREVIEW_NOTE'),
{
color: 'yellow',
},
);
}
cliux.print(messageHandler.parse('CLI_BOOTSTRAP_SUCCESS'));
// Install dependencies and start development server if requested (after all other operations)
if (this.options.runDevServer) {
// Install project dependencies
cliux.loader(messageHandler.parse('CLI_BOOTSTRAP_INSTALLING_DEPENDENCIES'));
try {
execSync('npm install', {
cwd: this.cloneDirectory,
stdio: 'inherit'
});
cliux.loader();
cliux.print(messageHandler.parse('CLI_BOOTSTRAP_DEPENDENCIES_INSTALLED'));
// Start development server
cliux.print(messageHandler.parse('CLI_BOOTSTRAP_STARTING_DEV_SERVER'));
cliux.print(messageHandler.parse('CLI_BOOTSTRAP_DEV_SERVER_STARTED'));
cliux.print('You can now access your application. Check the output above for the local URL.');
// Run npm run dev using spawn for long-running process
const devProcess = spawn('npm', ['run', 'dev'], {
cwd: this.cloneDirectory,
stdio: 'inherit',
shell: true
});
devProcess.on('error', (error) => {
cliux.print(messageHandler.parse('CLI_BOOTSTRAP_DEV_SERVER_FAILED'), {
color: 'yellow',
});
console.error('Failed to start dev server:', error);
});
// Handle process exit
devProcess.on('exit', (code) => {
if (code !== 0 && code !== null) {
cliux.print(messageHandler.parse('CLI_BOOTSTRAP_DEV_SERVER_FAILED'), {
color: 'yellow',
});
}
});
} catch (installError: any) {
cliux.loader();
cliux.print(messageHandler.parse('CLI_BOOTSTRAP_DEPENDENCIES_INSTALL_FAILED'), {
color: 'yellow',
});
}
}
} catch (error) {
cliux.error(messageHandler.parse('CLI_BOOTSTRAP_STACK_CREATION_FAILED', this.appConfig.stack));
}
}
}