-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathcreate-utils.ts
More file actions
194 lines (162 loc) · 5.5 KB
/
Copy pathcreate-utils.ts
File metadata and controls
194 lines (162 loc) · 5.5 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
185
186
187
188
189
190
191
192
193
194
import { createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { Separator } from '@inquirer/core';
import type { Manifest, Template } from '@apify/actor-templates';
import type { ChoicesType } from './hooks/user-confirmations/useSelectFromList.js';
import { useSelectFromList } from './hooks/user-confirmations/useSelectFromList.js';
import { useUserInput } from './hooks/user-confirmations/useUserInput.js';
import { warning } from './outputs.js';
import { httpsGet, validateActorName } from './utils.js';
const PROGRAMMING_LANGUAGES = ['JavaScript', 'TypeScript', 'Python'];
export async function ensureValidActorName(maybeActorName?: string) {
if (maybeActorName) {
validateActorName(maybeActorName);
return maybeActorName;
}
return promptActorName();
}
// TODO: this isn't even used anymore
export async function getTemplateDefinition(
maybeTemplateName: string | undefined,
manifestPromise: Promise<Manifest | Error>,
) {
const manifest = await manifestPromise;
// If the fetch failed earlier, the resolve value of
// the promise will be the error from fetching the manifest.
if (manifest instanceof Error) throw manifest;
if (maybeTemplateName) {
const templateDefinition = manifest.templates.find(
(t) => t.name === maybeTemplateName || t.aliases?.includes(maybeTemplateName),
);
if (!templateDefinition) {
throw new Error(`Could not find the selected template: ${maybeTemplateName} in the list of templates.`);
}
return templateDefinition;
}
return executePrompts(manifest);
}
// TODO: this isn't even used anymore
/**
* Fetch local readme suffix from the manifest and append it to the readme.
*/
export async function enhanceReadmeWithLocalSuffix(readmePath: string, manifestPromise: Promise<Manifest | Error>) {
const manifest = await manifestPromise;
// If the fetch failed earlier, the resolve value of
// the promise will be the error from fetching the manifest.
if (manifest instanceof Error) throw manifest;
try {
const suffixStream = await httpsGet(manifest.localReadmeSuffixUrl!);
const readmeStream = createWriteStream(readmePath, { flags: 'a' });
readmeStream.write('\n\n');
await pipeline(suffixStream, readmeStream);
} catch (err) {
warning({
message: `Could not append local development instructions to README.md. Cause: ${(err as Error).message}`,
});
}
}
export function formatCreateSuccessMessage(params: {
actorName: string;
dependenciesInstalled: boolean;
postCreate?: string | null;
gitRepositoryInitialized?: boolean;
installCommandSuggestion?: string | null;
}) {
const { actorName, dependenciesInstalled, postCreate, gitRepositoryInitialized, installCommandSuggestion } = params;
let message = `✅ Actor '${actorName}' created successfully!`;
if (dependenciesInstalled) {
message += `\n\nNext steps:\n\ncd "${actorName}"\napify run`;
} else {
const installLine = installCommandSuggestion || 'install dependencies with your package manager';
message += `\n\nNext steps:\n\ncd "${actorName}"\n${installLine}\napify run`;
}
message += `\n\n💡 Tip: Use 'apify push' to deploy your Actor to the Apify platform\n📖 Docs: https://docs.apify.com/platform/actors/development`;
if (gitRepositoryInitialized) {
message += `\n🌱 Git repository initialized in '${actorName}'. You can now commit and push your Actor to Git.`;
}
if (postCreate) {
message += `\n\n${postCreate}`;
}
return message;
}
/**
* Inquirer does not have a native way to "go back" between prompts.
*/
async function executePrompts(manifest: Manifest) {
const programmingLanguage = await promptProgrammingLanguage();
while (true) {
const templateDefinition = await promptTemplateDefinition(manifest, programmingLanguage);
if (templateDefinition) {
const shouldInstall = await promptTemplateInstallation();
if (shouldInstall) {
return templateDefinition;
}
} else {
return executePrompts(manifest);
}
}
}
async function promptActorName() {
const answer = await useUserInput({
message: 'Name of your new Actor:',
validate: (promptText) => {
try {
validateActorName(promptText);
} catch (err) {
return (err as Error).message;
}
return true;
},
});
return answer;
}
async function promptProgrammingLanguage() {
const language = await useSelectFromList<string>({
message: 'Choose the programming language of your new Actor:',
choices: PROGRAMMING_LANGUAGES,
loop: false,
default: PROGRAMMING_LANGUAGES[0],
});
return language;
}
async function promptTemplateDefinition(manifest: Manifest, programmingLanguage: string): Promise<Template | false> {
const choices: ChoicesType<Template | false> = [
...manifest.templates
.filter((t) => {
return t.category.toLowerCase() === programmingLanguage.toLowerCase();
})
.map((t) => {
return {
name: t.label,
value: t,
};
}),
new Separator(),
{
name: 'Go back',
value: false,
},
];
const templateDefinition = await useSelectFromList({
message: 'Choose a template for your new Actor. You can check more information at https://apify.com/templates.',
default: choices[0],
choices,
loop: false,
pageSize: 8,
});
return templateDefinition;
}
async function promptTemplateInstallation() {
const choices: ChoicesType<boolean> = [
{ name: `Install dependencies`, value: true },
new Separator(),
{ name: 'Go back', value: false },
];
const answer = await useSelectFromList<boolean>({
message: 'Almost done! Last step is to install dependencies.',
default: choices[0],
choices,
loop: false,
});
return answer;
}