@lemonadecms/create 0.0.0-stage → 0.1.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/CHANGELOG.md +23 -0
- package/LICENSE +21 -0
- package/README.md +42 -2
- package/bin/create-lemonade.mjs +124 -0
- package/dist/catalog.json +772 -0
- package/dist/templates/angular/angular.json +54 -0
- package/dist/templates/angular/locks/npm-lock.json.template +7079 -0
- package/dist/templates/angular/locks/pnpm-lock.yaml.template +4237 -0
- package/dist/templates/angular/locks/yarn.lock.template +4899 -0
- package/dist/templates/angular/package.json +68 -0
- package/dist/templates/angular/src/app/app.config.server.ts +23 -0
- package/dist/templates/angular/src/app/app.config.ts +34 -0
- package/dist/templates/angular/src/app/app.ts +9 -0
- package/dist/templates/angular/src/app/blog.service.ts +74 -0
- package/dist/templates/angular/src/app/journal.html +71 -0
- package/dist/templates/angular/src/app/journal.ts +47 -0
- package/dist/templates/angular/src/index.html +14 -0
- package/dist/templates/angular/src/main.server.ts +5 -0
- package/dist/templates/angular/src/main.ts +6 -0
- package/dist/templates/angular/src/server.ts +79 -0
- package/dist/templates/angular/tsconfig.app.json +8 -0
- package/dist/templates/angular/tsconfig.json +21 -0
- package/dist/templates/angular/vendor/README.md +32 -0
- package/dist/templates/angular/vendor/picomatch-4.0.7-lemonade-sec51.1.tgz +0 -0
- package/dist/templates/angular/vendor/picomatch-4.0.7.patch +59 -0
- package/dist/templates/angular/vendor/provenance.json +29 -0
- package/dist/templates/astro/astro.config.mjs +7 -0
- package/dist/templates/astro/locks/npm-lock.json.template +5478 -0
- package/dist/templates/astro/locks/pnpm-lock.yaml.template +3537 -0
- package/dist/templates/astro/locks/yarn.lock.template +4085 -0
- package/dist/templates/astro/package.json +51 -0
- package/dist/templates/astro/src/components/BlogView.astro +92 -0
- package/dist/templates/astro/src/layouts/Site.astro +23 -0
- package/dist/templates/astro/src/pages/articles/[id].astro +13 -0
- package/dist/templates/astro/src/pages/index.astro +13 -0
- package/dist/templates/astro/src/pages/lemonade/preview/[...path].ts +4 -0
- package/dist/templates/astro/src/pages/lemonade/preview.ts +4 -0
- package/dist/templates/astro/tsconfig.json +9 -0
- package/dist/templates/astro/vendor/README.md +32 -0
- package/dist/templates/astro/vendor/picomatch-2.3.2-lemonade-sec51.1.tgz +0 -0
- package/dist/templates/astro/vendor/picomatch-2.3.2.patch +59 -0
- package/dist/templates/astro/vendor/picomatch-4.0.7-lemonade-sec51.1.tgz +0 -0
- package/dist/templates/astro/vendor/picomatch-4.0.7.patch +59 -0
- package/dist/templates/astro/vendor/provenance.json +43 -0
- package/dist/templates/next/app/articles/[id]/page.tsx +29 -0
- package/dist/templates/next/app/data.ts +12 -0
- package/dist/templates/next/app/error.tsx +15 -0
- package/dist/templates/next/app/layout.tsx +13 -0
- package/dist/templates/next/app/lemonade/preview/[[...path]]/route.ts +7 -0
- package/dist/templates/next/app/loading.tsx +7 -0
- package/dist/templates/next/app/not-found.tsx +8 -0
- package/dist/templates/next/app/page.tsx +17 -0
- package/dist/templates/next/app/view.tsx +111 -0
- package/dist/templates/next/locks/npm-lock.json.template +1158 -0
- package/dist/templates/next/locks/pnpm-lock.yaml.template +715 -0
- package/dist/templates/next/locks/yarn.lock.template +775 -0
- package/dist/templates/next/next-env.d.ts +2 -0
- package/dist/templates/next/next.config.mjs +8 -0
- package/dist/templates/next/package.json +44 -0
- package/dist/templates/next/proxy.ts +19 -0
- package/dist/templates/next/tsconfig.json +25 -0
- package/dist/templates/nuxt/app/app.vue +1 -0
- package/dist/templates/nuxt/app/components/JournalView.vue +75 -0
- package/dist/templates/nuxt/app/pages/articles/[id].vue +8 -0
- package/dist/templates/nuxt/app/pages/index.vue +8 -0
- package/dist/templates/nuxt/locks/npm-lock.json.template +9113 -0
- package/dist/templates/nuxt/locks/pnpm-lock.yaml.template +6471 -0
- package/dist/templates/nuxt/locks/yarn.lock.template +6915 -0
- package/dist/templates/nuxt/nuxt.config.ts +27 -0
- package/dist/templates/nuxt/package.json +66 -0
- package/dist/templates/nuxt/server/api/blog/[id].get.ts +10 -0
- package/dist/templates/nuxt/server/api/blog.get.ts +9 -0
- package/dist/templates/nuxt/server/middleware/preview.ts +9 -0
- package/dist/templates/nuxt/server/utils/cms-environment.ts +15 -0
- package/dist/templates/nuxt/tsconfig.json +7 -0
- package/dist/templates/nuxt/vendor/README.md +32 -0
- package/dist/templates/nuxt/vendor/picomatch-2.3.2-lemonade-sec51.1.tgz +0 -0
- package/dist/templates/nuxt/vendor/picomatch-2.3.2.patch +59 -0
- package/dist/templates/nuxt/vendor/picomatch-4.0.7-lemonade-sec51.1.tgz +0 -0
- package/dist/templates/nuxt/vendor/picomatch-4.0.7.patch +59 -0
- package/dist/templates/nuxt/vendor/provenance.json +43 -0
- package/dist/templates/shared/GETTING_STARTED.md +29 -0
- package/dist/templates/shared/README.md +8 -0
- package/dist/templates/shared/env.example.template +11 -0
- package/dist/templates/shared/gitignore.template +16 -0
- package/dist/templates/shared/model-template.json +75 -0
- package/dist/templates/shared/public/blog.css +255 -0
- package/dist/templates/shared/public/lemonade.svg +5 -0
- package/dist/templates/shared/schema-contract.json +37 -0
- package/dist/templates/shared/scripts/cms-tools.mjs +171 -0
- package/dist/templates/shared/scripts/environment.mjs +68 -0
- package/dist/templates/shared/scripts/run.mjs +122 -0
- package/dist/templates/shared/server/blog.d.mts +35 -0
- package/dist/templates/shared/server/blog.mjs +159 -0
- package/dist/templates/shared/server/config.d.mts +11 -0
- package/dist/templates/shared/server/config.mjs +42 -0
- package/dist/templates/shared/server/preview.d.mts +4 -0
- package/dist/templates/shared/server/preview.mjs +276 -0
- package/dist/templates/shared/src/types/cms-models.ts +37 -0
- package/dist/templates/shared/yarnrc.yml.template +2 -0
- package/package.json +24 -4
- package/src/arguments.mjs +75 -0
- package/src/catalog.mjs +166 -0
- package/src/errors.mjs +10 -0
- package/src/generation.mjs +103 -0
- package/src/paths.mjs +90 -0
- package/src/prompts.mjs +68 -0
package/src/catalog.mjs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { open, lstat } from 'node:fs/promises';
|
|
2
|
+
import { resolve, relative, isAbsolute } from 'node:path';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { safeRelative } from './paths.mjs';
|
|
5
|
+
import { invariant } from './errors.mjs';
|
|
6
|
+
import { packageManagers } from './arguments.mjs';
|
|
7
|
+
export const hash = (bytes) => createHash('sha256').update(bytes).digest('hex');
|
|
8
|
+
|
|
9
|
+
function renameYarnWorkspace(body, templateName, name) {
|
|
10
|
+
if (templateName === name) return body;
|
|
11
|
+
const current = `${templateName}@workspace:.`;
|
|
12
|
+
const next = `${name}@workspace:.`;
|
|
13
|
+
const blocks = body.toString('utf8').trimEnd().split('\n\n');
|
|
14
|
+
const currentKey = `${JSON.stringify(current)}:`;
|
|
15
|
+
const workspace = blocks.findIndex((entry) => entry.startsWith(currentKey + '\n'));
|
|
16
|
+
invariant(workspace >= 2, 'Yarn starter lockfile is missing its root workspace.');
|
|
17
|
+
const [entry] = blocks.splice(workspace, 1);
|
|
18
|
+
const nextKey = `${JSON.stringify(next)}:`;
|
|
19
|
+
const position = blocks.findIndex(
|
|
20
|
+
(block, index) => index >= 2 && block.split('\n', 1)[0] > nextKey,
|
|
21
|
+
);
|
|
22
|
+
// Yarn serializes entries in key order, and file archives bind to the root locator.
|
|
23
|
+
blocks.splice(
|
|
24
|
+
position < 0 ? blocks.length : position,
|
|
25
|
+
0,
|
|
26
|
+
entry.replaceAll(current, next),
|
|
27
|
+
);
|
|
28
|
+
return Buffer.from(
|
|
29
|
+
blocks
|
|
30
|
+
.join('\n\n')
|
|
31
|
+
.replaceAll(encodeURIComponent(current), encodeURIComponent(next)) + '\n',
|
|
32
|
+
'utf8',
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
async function bundledDirectory(path) {
|
|
36
|
+
const stat = await lstat(path);
|
|
37
|
+
invariant(
|
|
38
|
+
stat.isDirectory() && !stat.isSymbolicLink(),
|
|
39
|
+
'Bundled asset directories cannot be links.',
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
export async function boundedFile(path, limit = 8 * 1024 * 1024) {
|
|
43
|
+
const stat = await lstat(path);
|
|
44
|
+
invariant(
|
|
45
|
+
stat.isFile() && !stat.isSymbolicLink() && stat.size <= limit,
|
|
46
|
+
'Invalid or oversized bundled file.',
|
|
47
|
+
);
|
|
48
|
+
const handle = await open(path, 'r');
|
|
49
|
+
try {
|
|
50
|
+
const actual = await handle.stat();
|
|
51
|
+
invariant(
|
|
52
|
+
actual.ino === stat.ino && actual.dev === stat.dev && actual.size <= limit,
|
|
53
|
+
'Bundled file changed.',
|
|
54
|
+
);
|
|
55
|
+
const bytes = Buffer.alloc(actual.size + 1);
|
|
56
|
+
let count = 0;
|
|
57
|
+
while (count < bytes.length) {
|
|
58
|
+
const { bytesRead } = await handle.read(
|
|
59
|
+
bytes,
|
|
60
|
+
count,
|
|
61
|
+
bytes.length - count,
|
|
62
|
+
count,
|
|
63
|
+
);
|
|
64
|
+
if (!bytesRead) break;
|
|
65
|
+
count += bytesRead;
|
|
66
|
+
}
|
|
67
|
+
const after = await handle.stat();
|
|
68
|
+
invariant(
|
|
69
|
+
count === actual.size &&
|
|
70
|
+
after.size === actual.size &&
|
|
71
|
+
after.mtimeMs === actual.mtimeMs,
|
|
72
|
+
'Bundled file changed.',
|
|
73
|
+
);
|
|
74
|
+
return bytes.subarray(0, count);
|
|
75
|
+
} finally {
|
|
76
|
+
await handle.close();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export async function loadCatalog(root) {
|
|
80
|
+
await bundledDirectory(resolve(root, 'dist'));
|
|
81
|
+
const bytes = await boundedFile(resolve(root, 'dist/catalog.json'), 512 * 1024);
|
|
82
|
+
const value = JSON.parse(bytes.toString('utf8'));
|
|
83
|
+
invariant(
|
|
84
|
+
value.version === 1 &&
|
|
85
|
+
typeof value.revision === 'string' &&
|
|
86
|
+
Array.isArray(value.files) &&
|
|
87
|
+
value.files.length <= 2000,
|
|
88
|
+
'Invalid starter catalog.',
|
|
89
|
+
);
|
|
90
|
+
const keys = new Set();
|
|
91
|
+
let total = 0;
|
|
92
|
+
for (const file of value.files) {
|
|
93
|
+
safeRelative(file.source);
|
|
94
|
+
safeRelative(file.target);
|
|
95
|
+
invariant(
|
|
96
|
+
file.target !== 'lemonade.starter.json',
|
|
97
|
+
'The provenance path is reserved.',
|
|
98
|
+
);
|
|
99
|
+
invariant(
|
|
100
|
+
['shared', 'astro', 'nuxt', 'next', 'angular'].includes(file.framework) &&
|
|
101
|
+
(file.manager === 'both' || Object.hasOwn(packageManagers, file.manager)),
|
|
102
|
+
'Invalid catalog scope.',
|
|
103
|
+
);
|
|
104
|
+
invariant(
|
|
105
|
+
/^[a-f0-9]{64}$/.test(file.sha256) &&
|
|
106
|
+
Number.isSafeInteger(file.bytes) &&
|
|
107
|
+
file.bytes >= 0 &&
|
|
108
|
+
file.bytes <= 8 * 1024 * 1024,
|
|
109
|
+
'Invalid catalog file identity.',
|
|
110
|
+
);
|
|
111
|
+
const key = `${file.framework}:${file.manager}:${file.target.normalize('NFC').toLowerCase()}`;
|
|
112
|
+
invariant(!keys.has(key), 'Duplicate catalog output.');
|
|
113
|
+
keys.add(key);
|
|
114
|
+
total += file.bytes;
|
|
115
|
+
const path = resolve(root, 'dist/templates', file.source);
|
|
116
|
+
const rel = relative(resolve(root, 'dist/templates'), path);
|
|
117
|
+
invariant(!rel.startsWith('..') && !isAbsolute(rel), 'Unsafe asset source.');
|
|
118
|
+
}
|
|
119
|
+
invariant(total <= 64 * 1024 * 1024, 'Starter payload exceeds its bound.');
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
export async function selectedFiles(root, catalog, framework, manager, name) {
|
|
123
|
+
await bundledDirectory(resolve(root, 'dist'));
|
|
124
|
+
await bundledDirectory(resolve(root, 'dist/templates'));
|
|
125
|
+
const result = [];
|
|
126
|
+
const seen = new Set();
|
|
127
|
+
for (const item of catalog.files.filter(
|
|
128
|
+
(f) =>
|
|
129
|
+
['shared', framework].includes(f.framework) &&
|
|
130
|
+
['both', manager].includes(f.manager),
|
|
131
|
+
)) {
|
|
132
|
+
const key = item.target.normalize('NFC').toLowerCase();
|
|
133
|
+
invariant(!seen.has(key), 'Conflicting starter files.');
|
|
134
|
+
seen.add(key);
|
|
135
|
+
let parent = resolve(root, 'dist/templates');
|
|
136
|
+
for (const part of item.source.split('/').slice(0, -1)) {
|
|
137
|
+
parent = resolve(parent, part);
|
|
138
|
+
await bundledDirectory(parent);
|
|
139
|
+
}
|
|
140
|
+
let body = await boundedFile(resolve(root, 'dist/templates', item.source));
|
|
141
|
+
invariant(
|
|
142
|
+
body.length === item.bytes && hash(body) === item.sha256,
|
|
143
|
+
'Starter assets failed their integrity check.',
|
|
144
|
+
);
|
|
145
|
+
if (['package.json', 'package-lock.json'].includes(item.target)) {
|
|
146
|
+
const json = JSON.parse(body.toString('utf8'));
|
|
147
|
+
json.name = name;
|
|
148
|
+
if (item.target === 'package.json') {
|
|
149
|
+
json.packageManager = `${manager}@${packageManagers[manager].version}`;
|
|
150
|
+
if (manager !== 'yarn') delete json.resolutions;
|
|
151
|
+
}
|
|
152
|
+
if (json.packages?.['']) json.packages[''].name = name;
|
|
153
|
+
body = Buffer.from(JSON.stringify(json, null, 2) + '\n');
|
|
154
|
+
}
|
|
155
|
+
if (item.target === 'yarn.lock')
|
|
156
|
+
body = renameYarnWorkspace(body, 'lemonade-blog', name);
|
|
157
|
+
result.push({ path: item.target, body });
|
|
158
|
+
}
|
|
159
|
+
invariant(
|
|
160
|
+
result.some((f) => f.path === 'package.json') &&
|
|
161
|
+
result.some((f) => f.path === '.gitignore') &&
|
|
162
|
+
result.some((f) => f.path === '.env.example'),
|
|
163
|
+
'Starter is incomplete.',
|
|
164
|
+
);
|
|
165
|
+
return result;
|
|
166
|
+
}
|
package/src/errors.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export class CreateError extends Error {
|
|
2
|
+
constructor(code, message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'CreateError';
|
|
5
|
+
this.code = code;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function invariant(condition, message, code = 5) {
|
|
9
|
+
if (!condition) throw new CreateError(code, message);
|
|
10
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { mkdir, open, lstat, unlink, rmdir } from 'node:fs/promises';
|
|
2
|
+
import { resolve, dirname, relative } from 'node:path';
|
|
3
|
+
import { hash, boundedFile } from './catalog.mjs';
|
|
4
|
+
import { CreateError, invariant } from './errors.mjs';
|
|
5
|
+
export async function generate(target, files, provenance, { signal, onWrite } = {}) {
|
|
6
|
+
const owned = [],
|
|
7
|
+
directories = [],
|
|
8
|
+
paths = new Set();
|
|
9
|
+
let claimed = false;
|
|
10
|
+
const check = () => {
|
|
11
|
+
if (signal?.aborted) throw new CreateError(130, 'Creation cancelled.');
|
|
12
|
+
};
|
|
13
|
+
async function directory(path) {
|
|
14
|
+
if (path === target || paths.has(path)) return;
|
|
15
|
+
await directory(dirname(path));
|
|
16
|
+
await mkdir(path);
|
|
17
|
+
const stat = await lstat(path);
|
|
18
|
+
directories.push({ path, ino: stat.ino, dev: stat.dev });
|
|
19
|
+
paths.add(path);
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
check();
|
|
23
|
+
await mkdir(target);
|
|
24
|
+
claimed = true;
|
|
25
|
+
const stat = await lstat(target);
|
|
26
|
+
directories.push({ path: target, ino: stat.ino, dev: stat.dev });
|
|
27
|
+
paths.add(target);
|
|
28
|
+
for (const item of [
|
|
29
|
+
...files,
|
|
30
|
+
{
|
|
31
|
+
path: 'lemonade.starter.json',
|
|
32
|
+
body: Buffer.from(JSON.stringify(provenance, null, 2) + '\n'),
|
|
33
|
+
},
|
|
34
|
+
]) {
|
|
35
|
+
check();
|
|
36
|
+
const path = resolve(target, item.path);
|
|
37
|
+
invariant(
|
|
38
|
+
relative(target, path) && !relative(target, path).startsWith('..'),
|
|
39
|
+
'Unsafe output.',
|
|
40
|
+
);
|
|
41
|
+
await directory(dirname(path));
|
|
42
|
+
check();
|
|
43
|
+
const handle = await open(path, 'wx', 0o644);
|
|
44
|
+
let row;
|
|
45
|
+
try {
|
|
46
|
+
const stat = await handle.stat();
|
|
47
|
+
row = { path, ino: stat.ino, dev: stat.dev, digest: hash(item.body) };
|
|
48
|
+
owned.push(row);
|
|
49
|
+
await handle.writeFile(item.body);
|
|
50
|
+
} finally {
|
|
51
|
+
await handle.close();
|
|
52
|
+
}
|
|
53
|
+
await onWrite?.(item.path);
|
|
54
|
+
check();
|
|
55
|
+
}
|
|
56
|
+
return { files: owned.length };
|
|
57
|
+
} catch (error) {
|
|
58
|
+
let retained = false;
|
|
59
|
+
if (claimed) {
|
|
60
|
+
for (const file of owned.reverse()) {
|
|
61
|
+
try {
|
|
62
|
+
const stat = await lstat(file.path);
|
|
63
|
+
if (
|
|
64
|
+
stat.isFile() &&
|
|
65
|
+
!stat.isSymbolicLink() &&
|
|
66
|
+
stat.ino === file.ino &&
|
|
67
|
+
stat.dev === file.dev &&
|
|
68
|
+
hash(await boundedFile(file.path)) === file.digest
|
|
69
|
+
)
|
|
70
|
+
await unlink(file.path);
|
|
71
|
+
else retained = true;
|
|
72
|
+
} catch (e) {
|
|
73
|
+
if (e.code !== 'ENOENT') retained = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (const row of directories.reverse()) {
|
|
77
|
+
try {
|
|
78
|
+
const stat = await lstat(row.path);
|
|
79
|
+
if (
|
|
80
|
+
stat.isDirectory() &&
|
|
81
|
+
!stat.isSymbolicLink() &&
|
|
82
|
+
stat.ino === row.ino &&
|
|
83
|
+
stat.dev === row.dev
|
|
84
|
+
)
|
|
85
|
+
await rmdir(row.path);
|
|
86
|
+
else retained = true;
|
|
87
|
+
} catch (e) {
|
|
88
|
+
if (e.code !== 'ENOENT') retained = true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (retained)
|
|
93
|
+
throw new CreateError(
|
|
94
|
+
error.code === 130 ? 130 : 6,
|
|
95
|
+
'Creation stopped. Changed or incomplete files were retained; inspect the destination before removing it.',
|
|
96
|
+
);
|
|
97
|
+
if (error instanceof CreateError) throw error;
|
|
98
|
+
throw new CreateError(
|
|
99
|
+
6,
|
|
100
|
+
'Could not create the project. Choose an available new directory.',
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/paths.mjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { dirname, basename, resolve, parse } from 'node:path';
|
|
2
|
+
import { realpath, lstat } from 'node:fs/promises';
|
|
3
|
+
import { CreateError, invariant } from './errors.mjs';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
// Only the exact public placeholder is allowed, including on case-insensitive disks.
|
|
6
|
+
export function privateConfigurationName(value) {
|
|
7
|
+
const folded = value.normalize('NFC').toLowerCase();
|
|
8
|
+
return folded === '.npmrc' || (folded.startsWith('.env') && value !== '.env.example');
|
|
9
|
+
}
|
|
10
|
+
export function safeRelative(value) {
|
|
11
|
+
invariant(
|
|
12
|
+
typeof value === 'string' &&
|
|
13
|
+
value.length <= 240 &&
|
|
14
|
+
!/[\\\x00-\x1f\x7f:<>"|?*]/.test(value),
|
|
15
|
+
'Unsafe template path.',
|
|
16
|
+
);
|
|
17
|
+
const parts = value.split('/');
|
|
18
|
+
invariant(
|
|
19
|
+
parts.every(
|
|
20
|
+
(p) =>
|
|
21
|
+
p &&
|
|
22
|
+
p !== '.' &&
|
|
23
|
+
p !== '..' &&
|
|
24
|
+
!/[. ]$/.test(p) &&
|
|
25
|
+
!/^(con|prn|aux|nul|com[0-9¹²³]|lpt[0-9¹²³])(?:\.|$)/i.test(p),
|
|
26
|
+
),
|
|
27
|
+
'Unsafe template path.',
|
|
28
|
+
);
|
|
29
|
+
invariant(
|
|
30
|
+
!parts.some(
|
|
31
|
+
(p) =>
|
|
32
|
+
['.git', 'node_modules'].includes(p.normalize('NFC').toLowerCase()) ||
|
|
33
|
+
privateConfigurationName(p),
|
|
34
|
+
),
|
|
35
|
+
'Private or generated paths cannot be bundled.',
|
|
36
|
+
);
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
export async function destination(value, cwd = process.cwd()) {
|
|
40
|
+
invariant(
|
|
41
|
+
typeof value === 'string' && value.length <= 4096 && !/[\x00-\x1f\x7f]/.test(value),
|
|
42
|
+
'Choose a safe new directory.',
|
|
43
|
+
2,
|
|
44
|
+
);
|
|
45
|
+
const path = resolve(cwd, value);
|
|
46
|
+
invariant(
|
|
47
|
+
![parse(path).root, resolve(cwd), resolve(homedir())].includes(path),
|
|
48
|
+
'Choose a new child directory.',
|
|
49
|
+
6,
|
|
50
|
+
);
|
|
51
|
+
try {
|
|
52
|
+
for (const part of value
|
|
53
|
+
.slice(parse(value).root.length)
|
|
54
|
+
.split(/[\\/]/)
|
|
55
|
+
.filter(Boolean))
|
|
56
|
+
safeRelative(part);
|
|
57
|
+
safeRelative(basename(path));
|
|
58
|
+
} catch {
|
|
59
|
+
throw new CreateError(
|
|
60
|
+
2,
|
|
61
|
+
'Choose a portable directory name without reserved or traversal segments.',
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
let present;
|
|
65
|
+
try {
|
|
66
|
+
present = await lstat(path);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (error.code !== 'ENOENT')
|
|
69
|
+
throw new CreateError(
|
|
70
|
+
6,
|
|
71
|
+
'The destination is unavailable. Choose a new directory.',
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (present)
|
|
75
|
+
throw new CreateError(6, 'The destination already exists. Choose a new directory.');
|
|
76
|
+
let parent;
|
|
77
|
+
try {
|
|
78
|
+
parent = await realpath(dirname(path));
|
|
79
|
+
} catch {
|
|
80
|
+
throw new CreateError(6, 'Choose an existing, accessible parent directory.');
|
|
81
|
+
}
|
|
82
|
+
const name =
|
|
83
|
+
basename(path)
|
|
84
|
+
.normalize('NFKD')
|
|
85
|
+
.toLowerCase()
|
|
86
|
+
.replace(/[^a-z0-9._-]+/g, '-')
|
|
87
|
+
.replace(/^[._-]+|[._-]+$/g, '')
|
|
88
|
+
.slice(0, 100) || 'lemonade-blog';
|
|
89
|
+
return { path: resolve(parent, basename(path)), name };
|
|
90
|
+
}
|
package/src/prompts.mjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline';
|
|
2
|
+
import { CreateError } from './errors.mjs';
|
|
3
|
+
import { destination } from './paths.mjs';
|
|
4
|
+
import { packageManagers } from './arguments.mjs';
|
|
5
|
+
export async function choose(options, { input, output, signal }) {
|
|
6
|
+
if (signal?.aborted) throw new CreateError(130, 'Creation cancelled.');
|
|
7
|
+
const rl = createInterface({ input, output, terminal: true });
|
|
8
|
+
let pending,
|
|
9
|
+
ended = false;
|
|
10
|
+
const check = () => {
|
|
11
|
+
if (ended || signal?.aborted) throw new CreateError(130, 'Creation cancelled.');
|
|
12
|
+
};
|
|
13
|
+
const cancel = () => {
|
|
14
|
+
ended = true;
|
|
15
|
+
pending?.(new CreateError(130, 'Creation cancelled.'));
|
|
16
|
+
rl.close();
|
|
17
|
+
};
|
|
18
|
+
rl.on('SIGINT', cancel);
|
|
19
|
+
signal?.addEventListener('abort', cancel, { once: true });
|
|
20
|
+
const question = (text) =>
|
|
21
|
+
new Promise((resolve, reject) => {
|
|
22
|
+
check();
|
|
23
|
+
pending = reject;
|
|
24
|
+
rl.question(text, (answer) => {
|
|
25
|
+
pending = null;
|
|
26
|
+
resolve(answer.trim());
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
const closed = () => {
|
|
30
|
+
ended = true;
|
|
31
|
+
pending?.(new CreateError(130, 'Selection ended. No project was created.'));
|
|
32
|
+
};
|
|
33
|
+
rl.on('close', closed);
|
|
34
|
+
rl.on('error', closed);
|
|
35
|
+
try {
|
|
36
|
+
if (!options.directory)
|
|
37
|
+
options.directory = await question('New project directory: ');
|
|
38
|
+
if (!options.framework) {
|
|
39
|
+
const answer = await question(
|
|
40
|
+
'Framework: 1 Astro, 2 Nuxt, 3 Next.js, 4 Angular: ',
|
|
41
|
+
);
|
|
42
|
+
options.framework = ['astro', 'nuxt', 'next', 'angular'][Number(answer) - 1];
|
|
43
|
+
if (!options.framework)
|
|
44
|
+
throw new CreateError(2, 'Choose a framework from 1 to 4.');
|
|
45
|
+
}
|
|
46
|
+
if (!options.managerProvided) {
|
|
47
|
+
const answer = await question('Package manager: 1 npm, 2 pnpm, 3 Yarn [1]: ');
|
|
48
|
+
options.packageManager = /^[1-3]$/.test(answer || '1')
|
|
49
|
+
? Object.keys(packageManagers)[Number(answer || '1') - 1]
|
|
50
|
+
: null;
|
|
51
|
+
if (!options.packageManager)
|
|
52
|
+
throw new CreateError(2, 'Choose npm, pnpm, or Yarn.');
|
|
53
|
+
}
|
|
54
|
+
const target = await destination(options.directory, options.cwd);
|
|
55
|
+
check();
|
|
56
|
+
output.write(
|
|
57
|
+
`Create ${options.framework} blog in ${JSON.stringify(target.path)} as package ${JSON.stringify(target.name)} using ${options.packageManager}.\n`,
|
|
58
|
+
);
|
|
59
|
+
if ((await question('Create files? [y/N] ')).toLowerCase() !== 'y')
|
|
60
|
+
throw new CreateError(130, 'Creation cancelled. No project was created.');
|
|
61
|
+
return options;
|
|
62
|
+
} finally {
|
|
63
|
+
pending = null;
|
|
64
|
+
signal?.removeEventListener('abort', cancel);
|
|
65
|
+
rl.removeListener('SIGINT', cancel);
|
|
66
|
+
rl.close();
|
|
67
|
+
}
|
|
68
|
+
}
|