Skip to content

Commit 0da3437

Browse files
committed
chore: refactor remote provider config gen for easier setup
1 parent 0fcdff7 commit 0da3437

File tree

4 files changed

+91
-71
lines changed

4 files changed

+91
-71
lines changed

packages/create-app/src/lib/bin.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import {
99
RockError,
1010
spawn,
1111
spinner,
12+
type SupportedRemoteCacheProviders,
1213
} from '@rock-js/tools';
1314
import { gitInitStep, hasGitClient, isGitRepo } from './steps/git-init.js';
14-
import type { RemoteCacheTemplateInfo, TemplateInfo } from './templates.js';
15+
import type { TemplateInfo } from './templates.js';
1516
import {
1617
BUNDLERS,
1718
PLATFORMS,
1819
PLUGINS,
19-
REMOTE_CACHE_PROVIDERS,
20+
remoteCacheProviderToConfigTemplate,
21+
remoteCacheProviderToImportTemplate,
2022
resolveTemplate,
2123
TEMPLATES,
2224
} from './templates.js';
@@ -47,7 +49,7 @@ import {
4749
promptPlugins,
4850
promptProjectName,
4951
promptRemoteCacheProvider,
50-
promptRemoteCacheProvidersConfig,
52+
promptRemoteCacheProviderArgs,
5153
promptTemplate,
5254
} from './utils/prompts.js';
5355
import {
@@ -146,10 +148,10 @@ export async function run() {
146148
options.remoteCacheProvider !== undefined ||
147149
options.remoteCacheProvider === false
148150
? null
149-
: await promptRemoteCacheProvider(REMOTE_CACHE_PROVIDERS);
151+
: await promptRemoteCacheProvider();
150152

151-
const remoteCacheProviderConfig = remoteCacheProvider
152-
? await promptRemoteCacheProvidersConfig(remoteCacheProvider.name)
153+
const remoteCacheProviderArgs = remoteCacheProvider
154+
? await promptRemoteCacheProviderArgs(remoteCacheProvider)
153155
: null;
154156

155157
const shouldInstallDependencies =
@@ -178,10 +180,10 @@ export async function run() {
178180
platforms,
179181
plugins,
180182
bundler,
181-
remoteCacheProvider && remoteCacheProviderConfig
183+
remoteCacheProvider && remoteCacheProviderArgs
182184
? {
183-
provider: remoteCacheProvider,
184-
args: remoteCacheProviderConfig,
185+
name: remoteCacheProvider,
186+
args: remoteCacheProviderArgs,
185187
}
186188
: null,
187189
);
@@ -304,7 +306,7 @@ function createConfig(
304306
plugins: TemplateInfo[] | null,
305307
bundler: TemplateInfo,
306308
remoteCacheProvider: {
307-
provider: RemoteCacheTemplateInfo;
309+
name: SupportedRemoteCacheProviders;
308310
args: Record<string, unknown>;
309311
} | null,
310312
) {
@@ -320,7 +322,7 @@ export function formatConfig(
320322
plugins: TemplateInfo[] | null,
321323
bundler: TemplateInfo,
322324
remoteCacheProvider: {
323-
provider: RemoteCacheTemplateInfo;
325+
name: SupportedRemoteCacheProviders;
324326
args: Record<string, unknown>;
325327
} | null,
326328
) {
@@ -330,17 +332,13 @@ export function formatConfig(
330332
const pluginsWithImports = plugins
331333
? plugins.filter((template) => template.importName)
332334
: null;
333-
return `${[
334-
...platformsWithImports,
335-
...(pluginsWithImports ?? []),
336-
bundler,
337-
...(remoteCacheProvider ? [remoteCacheProvider.provider] : []),
338-
]
335+
return `${[...platformsWithImports, ...(pluginsWithImports ?? []), bundler]
339336
.map(
340337
(template) =>
341338
`import { ${template.importName} } from '${template.packageName}';`,
342339
)
343340
.join('\n')}
341+
${[remoteCacheProvider ? remoteCacheProviderToImportTemplate(remoteCacheProvider.name) : '']}
344342
345343
export default {${
346344
pluginsWithImports && pluginsWithImports.length > 0
@@ -358,18 +356,14 @@ export default {${
358356
.map((template) => `${template.name}: ${template.importName}(),`)
359357
.join('\n ')}
360358
},
361-
remoteCacheProvider: ${
362-
remoteCacheProvider === null
363-
? null
364-
: `${remoteCacheProvider.provider.importName}({\n${Object.entries(
359+
${
360+
remoteCacheProvider
361+
? remoteCacheProviderToConfigTemplate(
362+
remoteCacheProvider.name,
365363
remoteCacheProvider.args,
366364
)
367-
.map(
368-
([key, value]) =>
369-
` ${JSON.stringify(key)}: ${JSON.stringify(value)},`,
370-
)
371-
.join('\n')}\n })`
372-
},
365+
: ''
366+
}
373367
};
374368
`;
375369
}

packages/create-app/src/lib/templates.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import * as path from 'node:path';
2-
import { resolveAbsolutePath, type SupportedRemoteCacheProviders } from '@rock-js/tools';
2+
import {
3+
resolveAbsolutePath,
4+
type SupportedRemoteCacheProviders,
5+
} from '@rock-js/tools';
36

47
export type TemplateInfo = NpmTemplateInfo | LocalTemplateInfo;
58

@@ -22,13 +25,6 @@ export type LocalTemplateInfo = {
2225
importName?: string;
2326
};
2427

25-
export type RemoteCacheTemplateInfo = {
26-
name: SupportedRemoteCacheProviders;
27-
displayName: string;
28-
packageName: string;
29-
importName: string;
30-
}
31-
3228
export const TEMPLATES: TemplateInfo[] = [
3329
{
3430
type: 'npm',
@@ -96,20 +92,35 @@ export const PLATFORMS: TemplateInfo[] = [
9692
},
9793
];
9894

99-
export const REMOTE_CACHE_PROVIDERS: RemoteCacheTemplateInfo[] = [
100-
{
101-
name: 'github-actions',
102-
displayName: 'GitHub Actions',
103-
packageName: '@rock-js/provider-github-actions',
104-
importName: 'providerGithubActions',
105-
},
106-
{
107-
name: 's3',
108-
displayName: 'S3',
109-
packageName: '@rock-js/provider-s3',
110-
importName: 'providerS3',
111-
},
112-
];
95+
export function remoteCacheProviderToImportTemplate(
96+
provider: SupportedRemoteCacheProviders,
97+
) {
98+
switch (provider) {
99+
case 'github-actions':
100+
return `import { providerGithubActions } from '@rock-js/provider-github-actions';`;
101+
case 's3':
102+
return `import { providerS3 } from '@rock-js/provider-s3';`;
103+
}
104+
}
105+
106+
export function remoteCacheProviderToConfigTemplate(
107+
provider: SupportedRemoteCacheProviders,
108+
args: any,
109+
) {
110+
switch (provider) {
111+
case 'github-actions':
112+
return `remoteCacheProvider: providerGithubActions({
113+
owner: "${args.owner}",
114+
repo: "${args.repo}",
115+
token: ${process.env['GITHUB_TOKEN']},
116+
}),`;
117+
case 's3':
118+
return `remoteCacheProvider: providerS3({
119+
bucket: "${args.bucket}",
120+
region: "${args.region}",
121+
}),`;
122+
}
123+
}
113124

114125
export function resolveTemplate(
115126
templates: TemplateInfo[],

packages/create-app/src/lib/utils/prompts.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import {
77
promptConfirm,
88
promptGroup,
99
promptMultiselect,
10-
promptPassword,
1110
promptSelect,
1211
promptText,
1312
relativeToCwd,
1413
RockError,
1514
type SupportedRemoteCacheProviders,
1615
} from '@rock-js/tools';
1716
import { vice } from 'gradient-string';
18-
import type { RemoteCacheTemplateInfo, TemplateInfo } from '../templates.js';
17+
import type { TemplateInfo } from '../templates.js';
1918
import { validateProjectName } from './project-name.js';
2019
import { getRockVersion } from './version.js';
2120

@@ -160,37 +159,51 @@ export function promptBundlers(
160159
});
161160
}
162161

163-
export function promptRemoteCacheProvider(
164-
providers: RemoteCacheTemplateInfo[],
165-
): Promise<RemoteCacheTemplateInfo | null> {
166-
return promptSelect({
167-
message: 'Which remote cache provider do you want to use?',
168-
initialValue: providers[0],
169-
options: providers.map((provider) => ({
170-
value: provider,
171-
label: provider.displayName,
172-
})),
162+
export function promptRemoteCacheProvider() {
163+
return promptSelect<SupportedRemoteCacheProviders | null>({
164+
message: 'Where do you store or intend to store remote build cache?',
165+
initialValue: 'github-actions',
166+
options: [
167+
{
168+
value: 'github-actions',
169+
label: 'GitHub Actions',
170+
},
171+
{
172+
value: 's3',
173+
label: 'Amazon S3',
174+
},
175+
{
176+
value: null,
177+
label: 'None',
178+
hint: 'Local cache only',
179+
},
180+
],
173181
});
174182
}
175183

176-
export function promptRemoteCacheProvidersConfig(
184+
export function promptRemoteCacheProviderArgs(
177185
provider: SupportedRemoteCacheProviders,
178186
) {
179187
switch (provider) {
180188
case 'github-actions':
181189
return promptGroup({
182-
owner: () => promptText({ message: 'GitHub owner' }),
183-
repo: () => promptText({ message: 'GitHub repo' }),
190+
owner: () => promptText({ message: 'GitHub repository owner' }),
191+
repo: () => promptText({ message: 'GitHub repository name' }),
184192
token: () =>
185-
promptPassword({ message: 'GitHub Personal Access Token (PAT)' }),
193+
promptText({
194+
message: 'GitHub Personal Access Token (PAT)',
195+
placeholder: 'GITHUB_TOKEN',
196+
}),
186197
});
187198
case 's3':
188199
return promptGroup({
189-
bucket: () => promptText({ message: 'S3 bucket' }),
190-
region: () => promptText({ message: 'S3 region' }),
191-
accessKeyId: () => promptText({ message: 'S3 access key ID' }),
192-
secretAccessKey: () =>
193-
promptPassword({ message: 'S3 secret access key' }),
200+
bucket: () =>
201+
promptText({ message: 'Bucket name', placeholder: 'bucket-name' }),
202+
region: () =>
203+
promptText({
204+
message: 'Region',
205+
placeholder: 'us-west-1',
206+
}),
194207
});
195208
}
196209
}

packages/tools/src/lib/build-cache/common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { spinner } from '../prompts.js';
1010

1111
export const BUILD_CACHE_DIR = 'remote-build';
1212

13-
export type SupportedRemoteCacheProviders = 'github-actions' | 's3';
13+
export const supportedRemoteCacheProviders = ['github-actions', 's3'] as const;
14+
export type SupportedRemoteCacheProviders = typeof supportedRemoteCacheProviders[number];
15+
1416

1517
export type RemoteArtifact = {
1618
name: string;

0 commit comments

Comments
 (0)