ecopages 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +114 -49
- package/bin/brand.js +42 -0
- package/bin/cli.js +372 -194
- package/bin/git-template-source-providers.js +56 -0
- package/bin/git-template-source-shared.js +7 -0
- package/bin/git-template-source.js +62 -0
- package/bin/init.js +392 -0
- package/bin/launch-plan.js +156 -60
- package/bin/node-require-preload.js +4 -0
- package/css/view-transitions.css +3 -6
- package/package.json +13 -12
- package/templates.json +115 -0
- package/bin/cli.test.ts +0 -136
- package/bin/launch-plan.test.ts +0 -160
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizeBitbucketParts,
|
|
3
|
+
normalizeGithubParts,
|
|
4
|
+
normalizeGitlabParts,
|
|
5
|
+
normalizeSourcehutParts,
|
|
6
|
+
} from './git-template-source-providers.js';
|
|
7
|
+
|
|
8
|
+
const GIT_PROVIDER_PREFIX = /^(github|gitlab|bitbucket|sourcehut):/u;
|
|
9
|
+
|
|
10
|
+
const HOST_PROVIDER = {
|
|
11
|
+
'github.com': 'github',
|
|
12
|
+
'gitlab.com': 'gitlab',
|
|
13
|
+
'bitbucket.org': 'bitbucket',
|
|
14
|
+
'git.sr.ht': 'sourcehut',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const PROVIDER_NORMALIZERS = {
|
|
18
|
+
github: normalizeGithubParts,
|
|
19
|
+
gitlab: normalizeGitlabParts,
|
|
20
|
+
bitbucket: normalizeBitbucketParts,
|
|
21
|
+
sourcehut: normalizeSourcehutParts,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function normalizeProviderUrl(hostProvider, parts, source) {
|
|
25
|
+
const normalize = PROVIDER_NORMALIZERS[hostProvider];
|
|
26
|
+
const normalized = normalize?.(parts, source);
|
|
27
|
+
if (normalized) {
|
|
28
|
+
return normalized;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
throw new Error(`Unsupported template URL '${source}'. Include a repository and optional Git ref.`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Normalizes a Git provider URL or giget source into a giget template source.
|
|
36
|
+
*/
|
|
37
|
+
export function normalizeGitSource(source) {
|
|
38
|
+
if (GIT_PROVIDER_PREFIX.test(source)) {
|
|
39
|
+
return source;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let url;
|
|
43
|
+
try {
|
|
44
|
+
url = new URL(source);
|
|
45
|
+
} catch {
|
|
46
|
+
throw new Error(`Unsupported template source '${source}'. Use a Git provider URL or giget source.`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const hostProvider = HOST_PROVIDER[url.hostname];
|
|
50
|
+
if (!hostProvider) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`Unsupported template host '${url.hostname}'. Supported hosts are GitHub, GitLab, Bitbucket, and SourceHut.`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const parts = url.pathname
|
|
57
|
+
.replace(/^\/+|\/+$/gu, '')
|
|
58
|
+
.split('/')
|
|
59
|
+
.filter(Boolean);
|
|
60
|
+
|
|
61
|
+
return normalizeProviderUrl(hostProvider, parts, source);
|
|
62
|
+
}
|
package/bin/init.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { downloadTemplate } from 'giget';
|
|
2
|
+
import * as prompts from '@clack/prompts';
|
|
3
|
+
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
import { basename, join } from 'node:path';
|
|
6
|
+
import { parseArgs } from 'node:util';
|
|
7
|
+
import { printBrandBanner, withBrandBanner } from './brand.js';
|
|
8
|
+
import { normalizeGitSource } from './git-template-source.js';
|
|
9
|
+
|
|
10
|
+
const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
|
|
11
|
+
const templateManifest = JSON.parse(readFileSync(new URL('../templates.json', import.meta.url), 'utf-8'));
|
|
12
|
+
const officialTemplates = [...templateManifest.templates].sort((left, right) => left.order - right.order);
|
|
13
|
+
const defaultOfficialTemplate = officialTemplates.find((template) => template.default) ?? officialTemplates[0];
|
|
14
|
+
|
|
15
|
+
const initOptionDefinitions = {
|
|
16
|
+
template: {
|
|
17
|
+
type: 'string',
|
|
18
|
+
},
|
|
19
|
+
repo: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
},
|
|
22
|
+
from: {
|
|
23
|
+
type: 'string',
|
|
24
|
+
},
|
|
25
|
+
interactive: {
|
|
26
|
+
type: 'boolean',
|
|
27
|
+
},
|
|
28
|
+
'no-interactive': {
|
|
29
|
+
type: 'boolean',
|
|
30
|
+
},
|
|
31
|
+
install: {
|
|
32
|
+
type: 'boolean',
|
|
33
|
+
},
|
|
34
|
+
'no-install': {
|
|
35
|
+
type: 'boolean',
|
|
36
|
+
},
|
|
37
|
+
start: {
|
|
38
|
+
type: 'boolean',
|
|
39
|
+
},
|
|
40
|
+
'no-start': {
|
|
41
|
+
type: 'boolean',
|
|
42
|
+
},
|
|
43
|
+
help: {
|
|
44
|
+
type: 'boolean',
|
|
45
|
+
short: 'h',
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function getInitCommandHelpText() {
|
|
50
|
+
return [
|
|
51
|
+
'Usage: ecopages init [dir] [options]',
|
|
52
|
+
'',
|
|
53
|
+
'Initialize a new project from a template.',
|
|
54
|
+
'',
|
|
55
|
+
'With no directory in a TTY, starts the interactive project creator.',
|
|
56
|
+
'',
|
|
57
|
+
'Options:',
|
|
58
|
+
' --template <id> Official template ID',
|
|
59
|
+
' --from <source> Community Git template source',
|
|
60
|
+
' --interactive Force interactive mode',
|
|
61
|
+
' --no-interactive Disable interactive mode',
|
|
62
|
+
' --install Install dependencies',
|
|
63
|
+
' --no-install Skip dependency installation',
|
|
64
|
+
' --start Install and start the dev server',
|
|
65
|
+
' --no-start Do not start the dev server',
|
|
66
|
+
' -h, --help Show help',
|
|
67
|
+
].join('\n');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function parseExclusiveBoolean(enable, disable, enableFlag, disableFlag) {
|
|
71
|
+
if (enable && disable) {
|
|
72
|
+
throw new Error(`The \`${enableFlag}\` and \`${disableFlag}\` options cannot be used together.`);
|
|
73
|
+
}
|
|
74
|
+
if (enable) return true;
|
|
75
|
+
if (disable) return false;
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseInitCommandArgs(rawArgs) {
|
|
80
|
+
const { values, positionals } = parseArgs({
|
|
81
|
+
args: rawArgs,
|
|
82
|
+
options: initOptionDefinitions,
|
|
83
|
+
allowPositionals: true,
|
|
84
|
+
strict: true,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
if (values.help) {
|
|
88
|
+
console.log(withBrandBanner(pkg.version, getInitCommandHelpText()));
|
|
89
|
+
return { help: true };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (positionals.length > 1) {
|
|
93
|
+
throw new Error('The `init` command accepts at most one target directory argument.');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (values.template && values.from) {
|
|
97
|
+
throw new Error('The `--template` and `--from` options cannot be used together.');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (values.repo) {
|
|
101
|
+
throw new Error('The `--repo` option was removed. Use `--from <source>` instead.');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const interactive = parseExclusiveBoolean(
|
|
105
|
+
values.interactive,
|
|
106
|
+
values['no-interactive'],
|
|
107
|
+
'--interactive',
|
|
108
|
+
'--no-interactive',
|
|
109
|
+
);
|
|
110
|
+
const install = parseExclusiveBoolean(values.install, values['no-install'], '--install', '--no-install');
|
|
111
|
+
const start = parseExclusiveBoolean(values.start, values['no-start'], '--start', '--no-start');
|
|
112
|
+
|
|
113
|
+
if (start && install === false) {
|
|
114
|
+
throw new Error('The `--start` option requires dependency installation.');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const dir = positionals[0];
|
|
118
|
+
const useInteractive = interactive ?? (!dir && Boolean(process.stdin.isTTY));
|
|
119
|
+
|
|
120
|
+
if (!dir && !useInteractive) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
'A target directory is required outside a TTY. Run `ecopages init --interactive` to use prompts.',
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
dir,
|
|
128
|
+
template: values.template,
|
|
129
|
+
from: values.from,
|
|
130
|
+
interactive: useInteractive,
|
|
131
|
+
install,
|
|
132
|
+
start,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function findOfficialTemplate(templateId) {
|
|
137
|
+
const template = officialTemplates.find((candidate) => candidate.id === templateId);
|
|
138
|
+
if (!template) {
|
|
139
|
+
const available = officialTemplates.map((candidate) => candidate.id).join(', ');
|
|
140
|
+
throw new Error(`Unknown official template '${templateId}'. Available templates: ${available}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return template;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function detectPackageManager() {
|
|
147
|
+
const userAgent = process.env.npm_config_user_agent || '';
|
|
148
|
+
if (userAgent.startsWith('bun/')) return 'bun';
|
|
149
|
+
if (userAgent.startsWith('pnpm/')) return 'pnpm';
|
|
150
|
+
return 'npm';
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function getTemplateSource(templateId, source) {
|
|
154
|
+
if (source) return normalizeGitSource(source);
|
|
155
|
+
const officialTemplate = findOfficialTemplate(templateId);
|
|
156
|
+
return `github:ecopages/ecopages/templates/${officialTemplate.source}#v${pkg.version}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function runChildCommand(command, args, cwd) {
|
|
160
|
+
return new Promise((resolve, reject) => {
|
|
161
|
+
const child = spawn(command, args, { cwd, stdio: 'inherit' });
|
|
162
|
+
child.on('error', reject);
|
|
163
|
+
child.on('exit', (code, signal) => {
|
|
164
|
+
if (signal) {
|
|
165
|
+
reject(new Error(`${command} was terminated by ${signal}.`));
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (code !== 0) {
|
|
170
|
+
reject(new Error(`${command} exited with code ${code ?? 1}.`));
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
resolve();
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function rewriteWorkspaceDependencies(projectPkg) {
|
|
180
|
+
for (const blockName of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
|
|
181
|
+
const block = projectPkg[blockName];
|
|
182
|
+
if (!block || typeof block !== 'object') continue;
|
|
183
|
+
for (const [name, version] of Object.entries(block)) {
|
|
184
|
+
if (version === 'workspace:*') block[name] = pkg.version;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function updateProjectManifest(targetDir, packageDirectory, isOfficial) {
|
|
190
|
+
const packagePath = join(targetDir, packageDirectory, 'package.json');
|
|
191
|
+
if (!existsSync(packagePath)) {
|
|
192
|
+
throw new Error(`Template package directory '${packageDirectory}' does not contain a package.json.`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const projectPkg = JSON.parse(readFileSync(packagePath, 'utf-8'));
|
|
196
|
+
projectPkg.name = basename(targetDir);
|
|
197
|
+
if (isOfficial) rewriteWorkspaceDependencies(projectPkg);
|
|
198
|
+
|
|
199
|
+
if (isOfficial) {
|
|
200
|
+
for (const blockName of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
|
|
201
|
+
const block = projectPkg[blockName];
|
|
202
|
+
if (!block || typeof block !== 'object') continue;
|
|
203
|
+
if (
|
|
204
|
+
Object.values(block).some((version) => typeof version === 'string' && version.startsWith('workspace:'))
|
|
205
|
+
) {
|
|
206
|
+
throw new Error(`Template package still contains workspace dependencies in ${packagePath}.`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
writeFileSync(packagePath, `${JSON.stringify(projectPkg, null, 2)}\n`);
|
|
212
|
+
return packagePath;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function isCancelled(value) {
|
|
216
|
+
if (prompts.isCancel(value)) {
|
|
217
|
+
prompts.cancel('Project creation cancelled.');
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function promptProjectDirectory() {
|
|
224
|
+
const answer = await prompts.text({
|
|
225
|
+
message: 'Project directory:',
|
|
226
|
+
placeholder: 'my-app',
|
|
227
|
+
validate: (value) => (value.trim().length === 0 ? 'Enter a project directory.' : undefined),
|
|
228
|
+
});
|
|
229
|
+
if (isCancelled(answer)) return null;
|
|
230
|
+
return answer.trim();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async function promptTemplateSelection() {
|
|
234
|
+
const answer = await prompts.select({
|
|
235
|
+
message: 'Select a template:',
|
|
236
|
+
options: [
|
|
237
|
+
...officialTemplates.map((template) => ({
|
|
238
|
+
label: template.displayName,
|
|
239
|
+
value: template.id,
|
|
240
|
+
hint: template.description,
|
|
241
|
+
})),
|
|
242
|
+
{
|
|
243
|
+
label: 'Community template',
|
|
244
|
+
value: '__community__',
|
|
245
|
+
hint: 'Use a Git repository or provider source.',
|
|
246
|
+
},
|
|
247
|
+
],
|
|
248
|
+
});
|
|
249
|
+
if (isCancelled(answer)) return null;
|
|
250
|
+
return answer;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async function promptCommunityTemplateSource() {
|
|
254
|
+
const sourceAnswer = await prompts.text({
|
|
255
|
+
message: 'Template source:',
|
|
256
|
+
placeholder: 'github:owner/repository#v1.0.0',
|
|
257
|
+
validate: (value) => (value.trim().length === 0 ? 'Enter a Git template source.' : undefined),
|
|
258
|
+
});
|
|
259
|
+
if (isCancelled(sourceAnswer)) return null;
|
|
260
|
+
return sourceAnswer.trim();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function resolveInteractiveTemplateChoice(templateId, source) {
|
|
264
|
+
if (templateId || source) {
|
|
265
|
+
return { templateId, source };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const answer = await promptTemplateSelection();
|
|
269
|
+
if (answer === null) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
if (answer === '__community__') {
|
|
273
|
+
const communitySource = await promptCommunityTemplateSource();
|
|
274
|
+
if (communitySource === null) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
return { templateId, source: communitySource };
|
|
278
|
+
}
|
|
279
|
+
return { templateId: answer, source };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function promptInstallChoice(packageManager, parsed) {
|
|
283
|
+
if (parsed.start) {
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
if (parsed.install !== undefined) {
|
|
287
|
+
return parsed.install;
|
|
288
|
+
}
|
|
289
|
+
if (!parsed.interactive) {
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const answer = await prompts.confirm({
|
|
294
|
+
message: `Install dependencies with ${packageManager}?`,
|
|
295
|
+
initialValue: true,
|
|
296
|
+
});
|
|
297
|
+
if (isCancelled(answer)) return null;
|
|
298
|
+
return answer;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async function promptStartChoice(parsed, install) {
|
|
302
|
+
if (parsed.start !== undefined) {
|
|
303
|
+
return parsed.start;
|
|
304
|
+
}
|
|
305
|
+
if (!parsed.interactive || !install) {
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const answer = await prompts.confirm({ message: 'Start the development server now?', initialValue: false });
|
|
310
|
+
if (isCancelled(answer)) return null;
|
|
311
|
+
return answer;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async function collectInitAnswers(parsed) {
|
|
315
|
+
const packageManager = detectPackageManager();
|
|
316
|
+
let dir = parsed.dir;
|
|
317
|
+
let templateId = parsed.template;
|
|
318
|
+
let source = parsed.from;
|
|
319
|
+
|
|
320
|
+
if (parsed.interactive && !dir) {
|
|
321
|
+
const promptedDir = await promptProjectDirectory();
|
|
322
|
+
if (promptedDir === null) return null;
|
|
323
|
+
dir = promptedDir;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (parsed.interactive) {
|
|
327
|
+
const templateChoice = await resolveInteractiveTemplateChoice(templateId, source);
|
|
328
|
+
if (templateChoice === null) return null;
|
|
329
|
+
templateId = templateChoice.templateId;
|
|
330
|
+
source = templateChoice.source;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (!templateId && !source) templateId = defaultOfficialTemplate.id;
|
|
334
|
+
if (parsed.interactive) {
|
|
335
|
+
prompts.log.step(`Resolved template source: ${getTemplateSource(templateId, source)}`);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const installAnswer = await promptInstallChoice(packageManager, parsed);
|
|
339
|
+
if (installAnswer === null) return null;
|
|
340
|
+
const install = installAnswer ?? false;
|
|
341
|
+
|
|
342
|
+
const startAnswer = await promptStartChoice(parsed, install);
|
|
343
|
+
if (startAnswer === null) return null;
|
|
344
|
+
const start = startAnswer ?? false;
|
|
345
|
+
|
|
346
|
+
return { dir, templateId, source, packageManager, install, start };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export async function runInitCommand(rawArgs, logger) {
|
|
350
|
+
const parsed = parseInitCommandArgs(rawArgs);
|
|
351
|
+
if (parsed.help) return;
|
|
352
|
+
|
|
353
|
+
printBrandBanner(pkg.version);
|
|
354
|
+
const answers = await collectInitAnswers(parsed);
|
|
355
|
+
if (!answers) return;
|
|
356
|
+
|
|
357
|
+
const { dir, templateId, source, packageManager, install, start } = answers;
|
|
358
|
+
if (existsSync(dir)) throw new Error(`Target directory already exists: ${dir}`);
|
|
359
|
+
|
|
360
|
+
const officialTemplate = source ? null : findOfficialTemplate(templateId);
|
|
361
|
+
const templateSource = getTemplateSource(templateId, source);
|
|
362
|
+
const packageDirectory = officialTemplate?.packageDirectory ?? '.';
|
|
363
|
+
let targetCreated = false;
|
|
364
|
+
|
|
365
|
+
try {
|
|
366
|
+
if (!parsed.interactive) prompts.log.step(`Resolved template source: ${templateSource}`);
|
|
367
|
+
prompts.log.step(`Downloading ${officialTemplate?.displayName ?? 'community template'}...`);
|
|
368
|
+
targetCreated = true;
|
|
369
|
+
await downloadTemplate(templateSource, { dir, force: true });
|
|
370
|
+
prompts.log.step('Preparing project manifest...');
|
|
371
|
+
updateProjectManifest(dir, packageDirectory, Boolean(officialTemplate));
|
|
372
|
+
prompts.log.step(`Project created in ${dir}.`);
|
|
373
|
+
|
|
374
|
+
const packageCwd = join(dir, packageDirectory);
|
|
375
|
+
if (install) {
|
|
376
|
+
prompts.log.step(`Installing dependencies with ${packageManager}...`);
|
|
377
|
+
await runChildCommand(packageManager, ['install'], packageCwd);
|
|
378
|
+
}
|
|
379
|
+
if (start) {
|
|
380
|
+
prompts.log.step('Starting the development server...');
|
|
381
|
+
await runChildCommand(packageManager, ['run', 'dev'], packageCwd);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const relativeCwd = packageDirectory === '.' ? dir : join(dir, packageDirectory);
|
|
385
|
+
logger.info(
|
|
386
|
+
`Project initialized. Run 'cd "${relativeCwd}" && ${packageManager} install && ${packageManager} run dev'.`,
|
|
387
|
+
);
|
|
388
|
+
} catch (error) {
|
|
389
|
+
if (targetCreated) rmSync(dir, { recursive: true, force: true });
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
392
|
+
}
|