Skip to content

Commit 0fcdff7

Browse files
committed
feat: add remote cache provider setup to the create-app command
1 parent 72bcd0b commit 0fcdff7

File tree

4 files changed

+99
-25
lines changed

4 files changed

+99
-25
lines changed

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import {
99
RockError,
1010
spawn,
1111
spinner,
12-
type SupportedRemoteCacheProviders,
1312
} from '@rock-js/tools';
1413
import { gitInitStep, hasGitClient, isGitRepo } from './steps/git-init.js';
15-
import type { TemplateInfo } from './templates.js';
14+
import type { RemoteCacheTemplateInfo, TemplateInfo } from './templates.js';
1615
import {
1716
BUNDLERS,
1817
PLATFORMS,
1918
PLUGINS,
19+
REMOTE_CACHE_PROVIDERS,
2020
resolveTemplate,
2121
TEMPLATES,
2222
} from './templates.js';
@@ -47,6 +47,7 @@ import {
4747
promptPlugins,
4848
promptProjectName,
4949
promptRemoteCacheProvider,
50+
promptRemoteCacheProvidersConfig,
5051
promptTemplate,
5152
} from './utils/prompts.js';
5253
import {
@@ -145,7 +146,11 @@ export async function run() {
145146
options.remoteCacheProvider !== undefined ||
146147
options.remoteCacheProvider === false
147148
? null
148-
: await promptRemoteCacheProvider();
149+
: await promptRemoteCacheProvider(REMOTE_CACHE_PROVIDERS);
150+
151+
const remoteCacheProviderConfig = remoteCacheProvider
152+
? await promptRemoteCacheProvidersConfig(remoteCacheProvider.name)
153+
: null;
149154

150155
const shouldInstallDependencies =
151156
options.install || isInteractive()
@@ -173,7 +178,12 @@ export async function run() {
173178
platforms,
174179
plugins,
175180
bundler,
176-
remoteCacheProvider,
181+
remoteCacheProvider && remoteCacheProviderConfig
182+
? {
183+
provider: remoteCacheProvider,
184+
args: remoteCacheProviderConfig,
185+
}
186+
: null,
177187
);
178188
loader.stop('Applied template, platforms and plugins.');
179189

@@ -293,7 +303,10 @@ function createConfig(
293303
platforms: TemplateInfo[],
294304
plugins: TemplateInfo[] | null,
295305
bundler: TemplateInfo,
296-
remoteCacheProvider: SupportedRemoteCacheProviders | null,
306+
remoteCacheProvider: {
307+
provider: RemoteCacheTemplateInfo;
308+
args: Record<string, unknown>;
309+
} | null,
297310
) {
298311
const rockConfig = path.join(absoluteTargetDir, 'rock.config.mjs');
299312
fs.writeFileSync(
@@ -306,15 +319,23 @@ export function formatConfig(
306319
platforms: TemplateInfo[],
307320
plugins: TemplateInfo[] | null,
308321
bundler: TemplateInfo,
309-
remoteCacheProvider: SupportedRemoteCacheProviders | null,
322+
remoteCacheProvider: {
323+
provider: RemoteCacheTemplateInfo;
324+
args: Record<string, unknown>;
325+
} | null,
310326
) {
311327
const platformsWithImports = platforms.filter(
312328
(template) => template.importName,
313329
);
314330
const pluginsWithImports = plugins
315331
? plugins.filter((template) => template.importName)
316332
: null;
317-
return `${[...platformsWithImports, ...(pluginsWithImports ?? []), bundler]
333+
return `${[
334+
...platformsWithImports,
335+
...(pluginsWithImports ?? []),
336+
bundler,
337+
...(remoteCacheProvider ? [remoteCacheProvider.provider] : []),
338+
]
318339
.map(
319340
(template) =>
320341
`import { ${template.importName} } from '${template.packageName}';`,
@@ -338,7 +359,16 @@ export default {${
338359
.join('\n ')}
339360
},
340361
remoteCacheProvider: ${
341-
remoteCacheProvider === null ? null : `'${remoteCacheProvider}'`
362+
remoteCacheProvider === null
363+
? null
364+
: `${remoteCacheProvider.provider.importName}({\n${Object.entries(
365+
remoteCacheProvider.args,
366+
)
367+
.map(
368+
([key, value]) =>
369+
` ${JSON.stringify(key)}: ${JSON.stringify(value)},`,
370+
)
371+
.join('\n')}\n })`
342372
},
343373
};
344374
`;

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

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

44
export type TemplateInfo = NpmTemplateInfo | LocalTemplateInfo;
55

@@ -22,6 +22,13 @@ export type LocalTemplateInfo = {
2222
importName?: string;
2323
};
2424

25+
export type RemoteCacheTemplateInfo = {
26+
name: SupportedRemoteCacheProviders;
27+
displayName: string;
28+
packageName: string;
29+
importName: string;
30+
}
31+
2532
export const TEMPLATES: TemplateInfo[] = [
2633
{
2734
type: 'npm',
@@ -89,6 +96,21 @@ export const PLATFORMS: TemplateInfo[] = [
8996
},
9097
];
9198

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+
];
113+
92114
export function resolveTemplate(
93115
templates: TemplateInfo[],
94116
name: string,

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

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import {
55
note,
66
outro,
77
promptConfirm,
8+
promptGroup,
89
promptMultiselect,
10+
promptPassword,
911
promptSelect,
1012
promptText,
1113
relativeToCwd,
1214
RockError,
1315
type SupportedRemoteCacheProviders,
1416
} from '@rock-js/tools';
1517
import { vice } from 'gradient-string';
16-
import type { TemplateInfo } from '../templates.js';
18+
import type { RemoteCacheTemplateInfo, TemplateInfo } from '../templates.js';
1719
import { validateProjectName } from './project-name.js';
1820
import { getRockVersion } from './version.js';
1921

@@ -23,9 +25,9 @@ export function printHelpMessage(
2325
) {
2426
console.log(`
2527
Usage: create-rock [options]
26-
28+
2729
Options:
28-
30+
2931
-h, --help Display help for command
3032
-v, --version Output the version number
3133
-d, --dir Create project in specified directory
@@ -36,7 +38,7 @@ export function printHelpMessage(
3638
--remote-cache-provider Specify remote cache provider
3739
--override Override files in target directory
3840
--install Install Node.js dependencies
39-
41+
4042
Available templates:
4143
${templates.map((t) => t.name).join(', ')}
4244
@@ -158,19 +160,39 @@ export function promptBundlers(
158160
});
159161
}
160162

161-
export function promptRemoteCacheProvider(): Promise<SupportedRemoteCacheProviders | null> {
163+
export function promptRemoteCacheProvider(
164+
providers: RemoteCacheTemplateInfo[],
165+
): Promise<RemoteCacheTemplateInfo | null> {
162166
return promptSelect({
163167
message: 'Which remote cache provider do you want to use?',
164-
initialValue: 'github-actions',
165-
options: [
166-
{
167-
value: 'github-actions',
168-
label: 'GitHub Actions',
169-
hint: 'Enable builds on your CI',
170-
},
171-
{ value: null, label: 'None', hint: 'Local builds only' },
172-
],
173-
}) as Promise<SupportedRemoteCacheProviders | null>;
168+
initialValue: providers[0],
169+
options: providers.map((provider) => ({
170+
value: provider,
171+
label: provider.displayName,
172+
})),
173+
});
174+
}
175+
176+
export function promptRemoteCacheProvidersConfig(
177+
provider: SupportedRemoteCacheProviders,
178+
) {
179+
switch (provider) {
180+
case 'github-actions':
181+
return promptGroup({
182+
owner: () => promptText({ message: 'GitHub owner' }),
183+
repo: () => promptText({ message: 'GitHub repo' }),
184+
token: () =>
185+
promptPassword({ message: 'GitHub Personal Access Token (PAT)' }),
186+
});
187+
case 's3':
188+
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' }),
194+
});
195+
}
174196
}
175197

176198
export function confirmOverrideFiles(targetDir: string) {

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

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

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

13-
export type SupportedRemoteCacheProviders = 'github-actions';
13+
export type SupportedRemoteCacheProviders = 'github-actions' | 's3';
1414

1515
export type RemoteArtifact = {
1616
name: string;

0 commit comments

Comments
 (0)