ghostcn 1.0.3 → 1.2.0
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 +96 -5
- package/dist/commands/add.js +41 -36
- package/dist/commands/create.d.ts +5 -0
- package/dist/commands/create.js +115 -0
- package/dist/commands/doctor.d.ts +1 -0
- package/dist/commands/doctor.js +7 -0
- package/dist/commands/init.js +134 -26
- package/dist/commands/pack.d.ts +1 -0
- package/dist/commands/pack.js +8 -0
- package/dist/index.js +12 -0
- package/dist/registry/components/author-card.json +2 -2
- package/dist/registry/components/featured-posts.json +27 -0
- package/dist/registry/components/member-cta.json +27 -0
- package/dist/registry/components/newsletter-form.json +9 -6
- package/dist/registry/components/pagination.json +28 -0
- package/dist/registry/components/post-card.json +2 -2
- package/dist/registry/components/post-header.json +27 -0
- package/dist/registry/components/pricing-table.json +5 -5
- package/dist/registry/components/site-footer.json +27 -0
- package/dist/registry/components/site-header.json +27 -0
- package/dist/registry/index.json +61 -1
- package/dist/templates/theme-tools.mjs +178 -0
- package/dist/utils/component-installer.d.ts +16 -0
- package/dist/utils/component-installer.js +60 -0
- package/dist/utils/registry.d.ts +1 -0
- package/dist/utils/registry.js +42 -19
- package/dist/utils/styles.d.ts +3 -0
- package/dist/utils/styles.js +20 -0
- package/dist/utils/theme-files.d.ts +13 -0
- package/dist/utils/theme-files.js +108 -0
- package/dist/utils/theme-project.d.ts +29 -0
- package/dist/utils/theme-project.js +46 -0
- package/dist/utils/theme-scaffold.d.ts +21 -0
- package/dist/utils/theme-scaffold.js +313 -0
- package/dist/utils/theme-tooling.d.ts +2 -0
- package/dist/utils/theme-tooling.js +20 -0
- package/dist/utils/theme.d.ts +46 -0
- package/dist/utils/theme.js +429 -0
- package/package.json +7 -4
- package/dist/test/registry.test.d.ts +0 -1
- package/dist/test/registry.test.js +0 -62
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
function inside(root, relative) {
|
|
6
|
+
const target = path.resolve(root, relative);
|
|
7
|
+
if (target === root || !target.startsWith(root + path.sep)) throw new Error(`Path escapes theme: ${relative}`);
|
|
8
|
+
// Never follow links when inspecting or packaging a theme.
|
|
9
|
+
let current = root;
|
|
10
|
+
for (const part of path.relative(root, target).split(path.sep)) {
|
|
11
|
+
current = path.join(current, part);
|
|
12
|
+
if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink()) throw new Error(`Symlink is not supported: ${relative}`);
|
|
13
|
+
}
|
|
14
|
+
return target;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function inspectTheme(themeRoot) {
|
|
18
|
+
const root = path.resolve(themeRoot);
|
|
19
|
+
const errors = [], warnings = [];
|
|
20
|
+
const exists = relative => fs.existsSync(inside(root, relative));
|
|
21
|
+
const read = relative => fs.readFileSync(inside(root, relative), 'utf8').replace(/^\uFEFF/, '');
|
|
22
|
+
let pkg, config;
|
|
23
|
+
try {
|
|
24
|
+
for (const required of ['package.json', 'default.hbs', 'index.hbs']) {
|
|
25
|
+
if (!exists(required)) errors.push(`Missing ${required}`);
|
|
26
|
+
}
|
|
27
|
+
if (exists('package.json')) {
|
|
28
|
+
pkg = JSON.parse(read('package.json'));
|
|
29
|
+
if (!pkg.engines?.ghost) errors.push('package.json needs engines.ghost');
|
|
30
|
+
if (!pkg.author?.email) errors.push('package.json needs author.email');
|
|
31
|
+
}
|
|
32
|
+
if (exists('components.json')) {
|
|
33
|
+
config = JSON.parse(read('components.json'));
|
|
34
|
+
if (!['tailwind', 'css'].includes(config.style)) errors.push('components.json style must be tailwind or css');
|
|
35
|
+
for (const relative of [config.cssFile, config.aliases?.styles, config.aliases?.partials]) {
|
|
36
|
+
if (typeof relative !== 'string' || !relative.trim()) errors.push('components.json needs cssFile and partials/styles aliases');
|
|
37
|
+
else if (!exists(relative)) errors.push(`Missing configured path: ${relative}`);
|
|
38
|
+
}
|
|
39
|
+
if (config.style === 'tailwind' && !pkg?.devDependencies?.tailwindcss && !pkg?.dependencies?.tailwindcss) {
|
|
40
|
+
warnings.push('Tailwind selected but no local tailwindcss dependency. Configure a build pipeline before using utilities.');
|
|
41
|
+
}
|
|
42
|
+
if (config.style === 'tailwind' && !pkg?.scripts?.build) warnings.push('No build script is configured. Tailwind classes need compiled CSS loaded by your layout.');
|
|
43
|
+
} else warnings.push('No components.json. Run ghostcn init if you want to install components.');
|
|
44
|
+
const scan = (dir, visit) => {
|
|
45
|
+
for (const entry of fs.readdirSync(inside(root, dir), { withFileTypes: true })) {
|
|
46
|
+
const relative = path.posix.join(dir, entry.name);
|
|
47
|
+
inside(root, relative);
|
|
48
|
+
if (entry.isDirectory()) scan(relative, visit);
|
|
49
|
+
else if (entry.isFile()) visit(relative);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const templates = fs.readdirSync(root).filter(name => name.endsWith('.hbs'));
|
|
53
|
+
if (exists('partials')) scan('partials', file => { if (file.endsWith('.hbs')) templates.push(file); });
|
|
54
|
+
for (const file of templates) {
|
|
55
|
+
const source = read(file).replace(/\{\{!--[\s\S]*?--\}\}/g, '');
|
|
56
|
+
for (const match of source.matchAll(/\{\{asset\s+["']([^"']+)["']/g)) {
|
|
57
|
+
if (!exists(`assets/${match[1]}`)) errors.push(`${file}: missing asset ${match[1]} (run your build)`);
|
|
58
|
+
}
|
|
59
|
+
for (const match of source.matchAll(/\{\{>\s*["']([^"']+)["']/g)) {
|
|
60
|
+
if (!exists(`partials/${match[1]}.hbs`)) errors.push(`${file}: missing partial ${match[1]}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (exists('default.hbs')) {
|
|
64
|
+
const layout = read('default.hbs');
|
|
65
|
+
const linked = new Set();
|
|
66
|
+
for (const tag of layout.replace(/\{\{!--[\s\S]*?--\}\}/g, '').matchAll(/<link\b[^>]*>/gi)) {
|
|
67
|
+
if (!/\brel\s*=\s*["']stylesheet["']/i.test(tag[0])) continue;
|
|
68
|
+
const asset = tag[0].match(/\{\{asset\s+["']([^"']+)["']/);
|
|
69
|
+
if (asset) linked.add(asset[1]);
|
|
70
|
+
}
|
|
71
|
+
for (const helper of ['ghost_head', 'ghost_foot']) {
|
|
72
|
+
if (!new RegExp(`\\{\\{\\s*${helper}\\s*\\}\\}`).test(layout)) errors.push(`default.hbs needs {{${helper}}}`);
|
|
73
|
+
}
|
|
74
|
+
if (config && !linked.has(path.relative(path.join(root, 'assets'), inside(root, config.cssFile)).replaceAll('\\', '/'))) {
|
|
75
|
+
errors.push('default.hbs does not link the configured ghostcn stylesheet');
|
|
76
|
+
}
|
|
77
|
+
if (config?.aliases?.styles && exists(config.aliases.styles)) {
|
|
78
|
+
for (const name of fs.readdirSync(inside(root, config.aliases.styles)).filter(name => name.endsWith('.css'))) {
|
|
79
|
+
const asset = path.relative(path.join(root, 'assets'), inside(root, path.join(config.aliases.styles, name))).replaceAll('\\', '/');
|
|
80
|
+
if (!linked.has(asset)) errors.push(`default.hbs does not link component stylesheet ${asset}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} catch (error) { errors.push(error.message); }
|
|
85
|
+
return { errors: [...new Set(errors)], warnings: [...new Set(warnings)] };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function crc32(buffer) {
|
|
89
|
+
let crc = 0xffffffff;
|
|
90
|
+
for (const byte of buffer) {
|
|
91
|
+
crc ^= byte;
|
|
92
|
+
for (let bit = 0; bit < 8; bit++) crc = (crc >>> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
|
|
93
|
+
}
|
|
94
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function packageTheme(themeRoot) {
|
|
98
|
+
const root = path.resolve(themeRoot);
|
|
99
|
+
const result = inspectTheme(root);
|
|
100
|
+
if (result.errors.length) throw new Error(result.errors.join('\n'));
|
|
101
|
+
const pkg = JSON.parse(fs.readFileSync(inside(root, 'package.json'), 'utf8'));
|
|
102
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(pkg.name) || !/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?$/.test(pkg.version)) {
|
|
103
|
+
throw new Error('Packaging needs a safe lowercase theme name and a semantic version in package.json');
|
|
104
|
+
}
|
|
105
|
+
const files = fs.readdirSync(root).filter(name => name.endsWith('.hbs') || ['package.json', 'LICENSE', 'LICENSE.md'].includes(name));
|
|
106
|
+
// Keep a source stylesheet if an existing theme actually links it at runtime.
|
|
107
|
+
const sourceStyles = new Set(['assets/css/source.css', 'assets/css/ghostcn-tailwind.css']);
|
|
108
|
+
const linkedStyles = new Set();
|
|
109
|
+
const findLinks = dir => {
|
|
110
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
111
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules' || entry.name === 'dist' || entry.name === 'assets') continue;
|
|
112
|
+
const target = path.join(dir, entry.name);
|
|
113
|
+
if (entry.isDirectory() && entry.name === 'partials') {
|
|
114
|
+
inside(root, path.relative(root, target));
|
|
115
|
+
findLinks(target);
|
|
116
|
+
}
|
|
117
|
+
else if (entry.isFile() && entry.name.endsWith('.hbs')) {
|
|
118
|
+
inside(root, path.relative(root, target));
|
|
119
|
+
for (const match of fs.readFileSync(target, 'utf8').matchAll(/\{\{asset\s+["']([^"']+)["']/g)) linkedStyles.add(`assets/${match[1]}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
findLinks(root);
|
|
124
|
+
const collect = dir => {
|
|
125
|
+
for (const entry of fs.readdirSync(inside(root, dir), { withFileTypes: true })) {
|
|
126
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules') continue;
|
|
127
|
+
const relative = path.posix.join(dir, entry.name);
|
|
128
|
+
inside(root, relative);
|
|
129
|
+
if (entry.isDirectory()) collect(relative);
|
|
130
|
+
else if (entry.isFile() && !relative.endsWith('.map') && (!sourceStyles.has(relative) || linkedStyles.has(relative))) files.push(relative);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
for (const dir of ['partials', 'assets']) if (fs.existsSync(inside(root, dir))) collect(dir);
|
|
134
|
+
const chunks = [], directory = [];
|
|
135
|
+
let offset = 0;
|
|
136
|
+
for (const file of files.sort()) {
|
|
137
|
+
const name = Buffer.from(file.replaceAll('\\', '/'));
|
|
138
|
+
const data = fs.readFileSync(inside(root, file));
|
|
139
|
+
const crc = crc32(data);
|
|
140
|
+
const local = Buffer.alloc(30);
|
|
141
|
+
local.writeUInt32LE(0x04034b50); local.writeUInt16LE(20, 4); local.writeUInt16LE(0x800, 6);
|
|
142
|
+
local.writeUInt16LE(33, 12); // 1980-01-01: deterministic archive timestamps
|
|
143
|
+
local.writeUInt32LE(crc, 14); local.writeUInt32LE(data.length, 18); local.writeUInt32LE(data.length, 22); local.writeUInt16LE(name.length, 26);
|
|
144
|
+
chunks.push(local, name, data);
|
|
145
|
+
const central = Buffer.alloc(46);
|
|
146
|
+
central.writeUInt32LE(0x02014b50); central.writeUInt16LE(20, 4); central.writeUInt16LE(20, 6); central.writeUInt16LE(0x800, 8);
|
|
147
|
+
central.writeUInt16LE(33, 14); central.writeUInt32LE(crc, 16); central.writeUInt32LE(data.length, 20); central.writeUInt32LE(data.length, 24);
|
|
148
|
+
central.writeUInt16LE(name.length, 28); central.writeUInt32LE(offset, 42);
|
|
149
|
+
directory.push(central, name);
|
|
150
|
+
offset += local.length + name.length + data.length;
|
|
151
|
+
}
|
|
152
|
+
if (files.length > 65535 || offset > 0xffffffff) throw new Error('Theme exceeds ZIP32 limits');
|
|
153
|
+
const central = Buffer.concat(directory), end = Buffer.alloc(22);
|
|
154
|
+
end.writeUInt32LE(0x06054b50); end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10);
|
|
155
|
+
end.writeUInt32LE(central.length, 12); end.writeUInt32LE(offset, 16);
|
|
156
|
+
const dist = inside(root, 'dist');
|
|
157
|
+
fs.mkdirSync(dist, { recursive: true });
|
|
158
|
+
const output = inside(root, `dist/${pkg.name}-${pkg.version}.zip`);
|
|
159
|
+
fs.writeFileSync(output, Buffer.concat([...chunks, central, end]));
|
|
160
|
+
return { output, files };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function reportTheme(themeRoot) {
|
|
164
|
+
const result = inspectTheme(themeRoot);
|
|
165
|
+
for (const warning of result.warnings) console.warn(`WARN: ${warning}`);
|
|
166
|
+
for (const error of result.errors) console.error(`ERROR: ${error}`);
|
|
167
|
+
if (result.errors.length) return false;
|
|
168
|
+
console.log('Theme checks passed. Run the official gscan validator before publishing.');
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
173
|
+
const root = path.resolve(process.argv[3] || process.cwd());
|
|
174
|
+
try {
|
|
175
|
+
if (process.argv[2] === 'pack') console.log(`Created ${packageTheme(root).output}`);
|
|
176
|
+
else if (!reportTheme(root)) process.exitCode = 1;
|
|
177
|
+
} catch (error) { console.error(error.message); process.exitCode = 1; }
|
|
178
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { GhostcnConfig } from './theme-project';
|
|
2
|
+
import { StyleSyncResult } from './theme-files';
|
|
3
|
+
export interface InstallComponentOptions {
|
|
4
|
+
themeRoot: string;
|
|
5
|
+
componentName: string;
|
|
6
|
+
config: GhostcnConfig;
|
|
7
|
+
overwrite?: boolean;
|
|
8
|
+
shouldOverwrite?: (relativePath: string) => Promise<boolean>;
|
|
9
|
+
}
|
|
10
|
+
export interface InstallComponentResult {
|
|
11
|
+
name: string;
|
|
12
|
+
installed: string[];
|
|
13
|
+
skipped: string[];
|
|
14
|
+
styleSync?: StyleSyncResult;
|
|
15
|
+
}
|
|
16
|
+
export declare function installComponent(options: InstallComponentOptions): Promise<InstallComponentResult>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.installComponent = installComponent;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const registry_1 = require("./registry");
|
|
10
|
+
const theme_files_1 = require("./theme-files");
|
|
11
|
+
const styles_1 = require("./styles");
|
|
12
|
+
function aliasedTarget(config, file) {
|
|
13
|
+
if (file.placement === 'theme') {
|
|
14
|
+
if (!file.target)
|
|
15
|
+
throw new Error(`Registry file "${file.name}" is missing its theme target.`);
|
|
16
|
+
return file.target;
|
|
17
|
+
}
|
|
18
|
+
let alias = '';
|
|
19
|
+
if (file.type === 'partial')
|
|
20
|
+
alias = config.aliases?.partials || 'partials/components';
|
|
21
|
+
if (file.type === 'style')
|
|
22
|
+
alias = config.aliases?.styles || 'assets/css/components';
|
|
23
|
+
if (file.type === 'js')
|
|
24
|
+
alias = config.aliases?.js || 'assets/js/components';
|
|
25
|
+
if (!alias)
|
|
26
|
+
throw new Error(`Unsupported registry file type "${file.type}".`);
|
|
27
|
+
return path_1.default.join(alias, file.name);
|
|
28
|
+
}
|
|
29
|
+
async function installComponent(options) {
|
|
30
|
+
const name = (0, registry_1.sanitizeComponentName)(options.componentName);
|
|
31
|
+
const manifest = await (0, registry_1.getComponent)(name);
|
|
32
|
+
if (!manifest)
|
|
33
|
+
throw new Error(`Component "${name}" was not found in the registry.`);
|
|
34
|
+
const result = { name, installed: [], skipped: [] };
|
|
35
|
+
for (const file of manifest.files) {
|
|
36
|
+
if (path_1.default.basename(file.name) !== file.name) {
|
|
37
|
+
throw new Error(`Registry returned an unsafe file name for "${name}".`);
|
|
38
|
+
}
|
|
39
|
+
const relativeTarget = aliasedTarget(options.config, file);
|
|
40
|
+
const targetPath = (0, theme_files_1.resolveWithinTheme)(options.themeRoot, relativeTarget);
|
|
41
|
+
if (fs_1.default.existsSync(targetPath) && !options.overwrite) {
|
|
42
|
+
const replace = options.shouldOverwrite ? await options.shouldOverwrite(relativeTarget.replace(/\\/g, '/')) : false;
|
|
43
|
+
if (!replace) {
|
|
44
|
+
result.skipped.push(relativeTarget);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
fs_1.default.mkdirSync(path_1.default.dirname(targetPath), { recursive: true });
|
|
49
|
+
fs_1.default.writeFileSync(targetPath, file.type === 'style' ? (0, styles_1.componentCss)(file.content, options.config.style) : file.content, 'utf8');
|
|
50
|
+
result.installed.push(relativeTarget);
|
|
51
|
+
}
|
|
52
|
+
if (manifest.files.some(file => file.type === 'style')) {
|
|
53
|
+
result.styleSync = (0, theme_files_1.syncGhostcnStyles)({
|
|
54
|
+
themeRoot: options.themeRoot,
|
|
55
|
+
cssFile: options.config.cssFile || 'assets/css/ghostcn.css',
|
|
56
|
+
stylesDir: options.config.aliases?.styles || 'assets/css/components'
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
}
|
package/dist/utils/registry.d.ts
CHANGED
package/dist/utils/registry.js
CHANGED
|
@@ -83,39 +83,62 @@ function findLocalManifest(componentName) {
|
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
async function getRegistryIndex() {
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
// If custom registry URL explicitly configured, try remote first
|
|
87
|
+
if (process.env.GHOST_UI_REGISTRY_URL) {
|
|
88
|
+
try {
|
|
89
|
+
const res = await fetchWithTimeout(`${process.env.GHOST_UI_REGISTRY_URL}/index.json`);
|
|
90
|
+
if (res.ok) {
|
|
91
|
+
return (await res.json());
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
// Fallback to local
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// 1. Try local bundled registry first (zero-latency, offline-first)
|
|
99
|
+
const localIndex = findLocalIndex();
|
|
100
|
+
if (localIndex && localIndex.length > 0) {
|
|
101
|
+
return localIndex;
|
|
102
|
+
}
|
|
103
|
+
// 2. Fallback to default remote registry
|
|
88
104
|
try {
|
|
89
|
-
const res = await fetchWithTimeout(`${
|
|
105
|
+
const res = await fetchWithTimeout(`${exports.DEFAULT_REGISTRY_URL}/index.json`);
|
|
90
106
|
if (res.ok) {
|
|
91
|
-
|
|
92
|
-
return data;
|
|
107
|
+
return (await res.json());
|
|
93
108
|
}
|
|
94
109
|
}
|
|
95
110
|
catch {
|
|
96
|
-
// Network or abort error
|
|
97
|
-
}
|
|
98
|
-
// 2. Fallback to local
|
|
99
|
-
const localIndex = findLocalIndex();
|
|
100
|
-
if (localIndex) {
|
|
101
|
-
return localIndex;
|
|
111
|
+
// Network or abort error
|
|
102
112
|
}
|
|
103
113
|
return [];
|
|
104
114
|
}
|
|
105
115
|
async function getComponent(componentName) {
|
|
106
116
|
const sanitized = sanitizeComponentName(componentName);
|
|
107
|
-
|
|
108
|
-
|
|
117
|
+
// If custom registry URL explicitly configured, try remote first
|
|
118
|
+
if (process.env.GHOST_UI_REGISTRY_URL) {
|
|
119
|
+
try {
|
|
120
|
+
const res = await fetchWithTimeout(`${process.env.GHOST_UI_REGISTRY_URL}/components/${sanitized}.json`);
|
|
121
|
+
if (res.ok) {
|
|
122
|
+
return (await res.json());
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
// Fallback to local
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// 1. Try local bundled registry first (zero-latency, offline-first)
|
|
130
|
+
const local = findLocalManifest(sanitized);
|
|
131
|
+
if (local)
|
|
132
|
+
return local;
|
|
133
|
+
// 2. Fallback to default remote registry
|
|
109
134
|
try {
|
|
110
|
-
const res = await fetchWithTimeout(`${
|
|
135
|
+
const res = await fetchWithTimeout(`${exports.DEFAULT_REGISTRY_URL}/components/${sanitized}.json`);
|
|
111
136
|
if (res.ok) {
|
|
112
|
-
|
|
113
|
-
return data;
|
|
137
|
+
return (await res.json());
|
|
114
138
|
}
|
|
115
139
|
}
|
|
116
140
|
catch {
|
|
117
|
-
// Network or abort error
|
|
141
|
+
// Network or abort error
|
|
118
142
|
}
|
|
119
|
-
|
|
120
|
-
return findLocalManifest(sanitized);
|
|
143
|
+
return null;
|
|
121
144
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.componentCss = componentCss;
|
|
4
|
+
exports.tailwindTheme = tailwindTheme;
|
|
5
|
+
/** Tailwind utilities must outrank portable component defaults, regardless of link order. */
|
|
6
|
+
function componentCss(content, style) {
|
|
7
|
+
return style === 'tailwind' ? `@layer theme, base, components, utilities;\n@layer components {\n${content}\n}\n` : content;
|
|
8
|
+
}
|
|
9
|
+
function tailwindTheme() {
|
|
10
|
+
const colors = ['background', 'foreground', 'card', 'card-foreground', 'popover', 'popover-foreground',
|
|
11
|
+
'primary', 'primary-foreground', 'secondary', 'secondary-foreground', 'muted', 'muted-foreground',
|
|
12
|
+
'accent', 'accent-foreground', 'destructive', 'destructive-foreground', 'border', 'input', 'ring'];
|
|
13
|
+
return `@theme inline {\n${colors.map(color => ` --color-${color}: var(--${color});`).join('\n')}\n --radius-ghostcn: var(--radius);\n}\n
|
|
14
|
+
@custom-variant dark {
|
|
15
|
+
&:where(.dark, .dark *, .dark-mode, .dark-mode *, [data-theme="dark"], [data-theme="dark"] *) { @slot; }
|
|
16
|
+
@media (prefers-color-scheme: dark) {
|
|
17
|
+
&:where(.auto-color, .auto-color *) { @slot; }
|
|
18
|
+
}
|
|
19
|
+
}\n`;
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface StyleSyncOptions {
|
|
2
|
+
themeRoot: string;
|
|
3
|
+
cssFile: string;
|
|
4
|
+
stylesDir: string;
|
|
5
|
+
}
|
|
6
|
+
export interface StyleSyncResult {
|
|
7
|
+
status: 'updated' | 'unchanged' | 'missing-template';
|
|
8
|
+
templatePath: string;
|
|
9
|
+
assetPaths: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare function resolveWithinTheme(themeRoot: string, relativePath: string): string;
|
|
12
|
+
export declare function toGhostAssetPath(themeRelativePath: string): string;
|
|
13
|
+
export declare function syncGhostcnStyles(options: StyleSyncOptions): StyleSyncResult;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.resolveWithinTheme = resolveWithinTheme;
|
|
7
|
+
exports.toGhostAssetPath = toGhostAssetPath;
|
|
8
|
+
exports.syncGhostcnStyles = syncGhostcnStyles;
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
const STYLES_START = '{{!-- ghostcn:styles:start --}}';
|
|
12
|
+
const STYLES_END = '{{!-- ghostcn:styles:end --}}';
|
|
13
|
+
function normalizeRelativePath(relativePath) {
|
|
14
|
+
return relativePath.trim().replace(/\\/g, '/').replace(/^\.\//, '');
|
|
15
|
+
}
|
|
16
|
+
function resolveWithinTheme(themeRoot, relativePath) {
|
|
17
|
+
const normalized = normalizeRelativePath(relativePath);
|
|
18
|
+
if (!normalized || path_1.default.isAbsolute(normalized)) {
|
|
19
|
+
throw new Error(`Path must be relative to the Ghost theme: ${relativePath}`);
|
|
20
|
+
}
|
|
21
|
+
const root = path_1.default.resolve(themeRoot);
|
|
22
|
+
const target = path_1.default.resolve(root, normalized);
|
|
23
|
+
if (target === root || !target.startsWith(`${root}${path_1.default.sep}`)) {
|
|
24
|
+
throw new Error(`Path must stay inside the Ghost theme: ${relativePath}`);
|
|
25
|
+
}
|
|
26
|
+
return target;
|
|
27
|
+
}
|
|
28
|
+
function toGhostAssetPath(themeRelativePath) {
|
|
29
|
+
const normalized = normalizeRelativePath(themeRelativePath);
|
|
30
|
+
if (!normalized.startsWith('assets/')) {
|
|
31
|
+
throw new Error(`Stylesheets must be stored inside assets/: ${themeRelativePath}`);
|
|
32
|
+
}
|
|
33
|
+
const assetPath = normalized.slice('assets/'.length);
|
|
34
|
+
if (!assetPath) {
|
|
35
|
+
throw new Error(`Stylesheet path must include a file name: ${themeRelativePath}`);
|
|
36
|
+
}
|
|
37
|
+
return assetPath;
|
|
38
|
+
}
|
|
39
|
+
function stylesheetBlock(assetPaths, newline) {
|
|
40
|
+
return [
|
|
41
|
+
STYLES_START,
|
|
42
|
+
...assetPaths.map(assetPath => `<link rel="stylesheet" href="{{asset "${assetPath}"}}" />`),
|
|
43
|
+
STYLES_END
|
|
44
|
+
].join(newline);
|
|
45
|
+
}
|
|
46
|
+
function indentBlock(block, indent, newline) {
|
|
47
|
+
return block
|
|
48
|
+
.split(newline)
|
|
49
|
+
.map(line => `${indent}${line}`)
|
|
50
|
+
.join(newline);
|
|
51
|
+
}
|
|
52
|
+
function removeOwnedStylesheetLinks(template, assetPaths) {
|
|
53
|
+
const owned = new Set(assetPaths);
|
|
54
|
+
return template.replace(/^[ \t]*<link\b[^\r\n>]*>[ \t]*(?:\r?\n)?/gim, tag => {
|
|
55
|
+
if (!/rel=["']stylesheet["']/i.test(tag))
|
|
56
|
+
return tag;
|
|
57
|
+
const assetMatch = tag.match(/\{\{asset\s+["']([^"']+)["']\}\}/i);
|
|
58
|
+
return assetMatch && owned.has(assetMatch[1]) ? '' : tag;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function syncGhostcnStyles(options) {
|
|
62
|
+
const templatePath = resolveWithinTheme(options.themeRoot, 'default.hbs');
|
|
63
|
+
const cssPath = resolveWithinTheme(options.themeRoot, options.cssFile);
|
|
64
|
+
const stylesDirPath = resolveWithinTheme(options.themeRoot, options.stylesDir);
|
|
65
|
+
const assetPaths = [toGhostAssetPath(options.cssFile)];
|
|
66
|
+
if (fs_1.default.existsSync(stylesDirPath)) {
|
|
67
|
+
const componentStyles = fs_1.default
|
|
68
|
+
.readdirSync(stylesDirPath, { withFileTypes: true })
|
|
69
|
+
.filter(entry => entry.isFile() && entry.name.toLowerCase().endsWith('.css'))
|
|
70
|
+
.map(entry => toGhostAssetPath(path_1.default.relative(options.themeRoot, path_1.default.join(stylesDirPath, entry.name))))
|
|
71
|
+
.sort((a, b) => a.localeCompare(b));
|
|
72
|
+
assetPaths.push(...componentStyles);
|
|
73
|
+
}
|
|
74
|
+
if (!fs_1.default.existsSync(templatePath)) {
|
|
75
|
+
return { status: 'missing-template', templatePath, assetPaths };
|
|
76
|
+
}
|
|
77
|
+
if (!fs_1.default.existsSync(cssPath)) {
|
|
78
|
+
throw new Error(`Theme tokens file does not exist: ${options.cssFile}`);
|
|
79
|
+
}
|
|
80
|
+
const original = fs_1.default.readFileSync(templatePath, 'utf8');
|
|
81
|
+
const newline = original.includes('\r\n') ? '\r\n' : '\n';
|
|
82
|
+
const block = stylesheetBlock(assetPaths, newline);
|
|
83
|
+
const managedBlockPattern = new RegExp(`^[ \\t]*${STYLES_START.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[\\s\\S]*?${STYLES_END.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[ \\t]*(?:\\r?\\n)?`, 'm');
|
|
84
|
+
const withoutManagedBlock = original.replace(managedBlockPattern, '');
|
|
85
|
+
const withoutOwnedLinks = removeOwnedStylesheetLinks(withoutManagedBlock, assetPaths);
|
|
86
|
+
const ghostHeadPattern = /^([ \t]*)\{\{ghost_head\}\}/m;
|
|
87
|
+
const overrideAnchor = /^([ \t]*)\{\{!-- ghostcn:theme-styles --\}\}/m;
|
|
88
|
+
let updated;
|
|
89
|
+
if (overrideAnchor.test(withoutOwnedLinks)) {
|
|
90
|
+
updated = withoutOwnedLinks.replace(overrideAnchor, (match, indent) => `${indentBlock(block, indent, newline)}${newline}${match}`);
|
|
91
|
+
}
|
|
92
|
+
else if (ghostHeadPattern.test(withoutOwnedLinks)) {
|
|
93
|
+
updated = withoutOwnedLinks.replace(ghostHeadPattern, (_match, indent) => {
|
|
94
|
+
return `${indentBlock(block, indent, newline)}${newline}${indent}{{ghost_head}}`;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
else if (/<\/head>/i.test(withoutOwnedLinks)) {
|
|
98
|
+
updated = withoutOwnedLinks.replace(/<\/head>/i, `${block}${newline}</head>`);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
return { status: 'missing-template', templatePath, assetPaths };
|
|
102
|
+
}
|
|
103
|
+
if (updated === original) {
|
|
104
|
+
return { status: 'unchanged', templatePath, assetPaths };
|
|
105
|
+
}
|
|
106
|
+
fs_1.default.writeFileSync(templatePath, updated, 'utf8');
|
|
107
|
+
return { status: 'updated', templatePath, assetPaths };
|
|
108
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ThemeConfig } from './theme';
|
|
2
|
+
import { StyleSyncResult } from './theme-files';
|
|
3
|
+
export interface GhostcnConfig {
|
|
4
|
+
$schema: string;
|
|
5
|
+
style: ThemeConfig['style'];
|
|
6
|
+
theme: Pick<ThemeConfig, 'baseColor' | 'accentColor' | 'radius'>;
|
|
7
|
+
aliases: {
|
|
8
|
+
partials: string;
|
|
9
|
+
styles: string;
|
|
10
|
+
js?: string;
|
|
11
|
+
};
|
|
12
|
+
cssFile: string;
|
|
13
|
+
}
|
|
14
|
+
export interface InitializeThemeOptions {
|
|
15
|
+
themeRoot: string;
|
|
16
|
+
config: GhostcnConfig;
|
|
17
|
+
}
|
|
18
|
+
export interface InitializeThemeResult {
|
|
19
|
+
configPath: string;
|
|
20
|
+
cssPath: string;
|
|
21
|
+
styleSync: StyleSyncResult;
|
|
22
|
+
}
|
|
23
|
+
export declare function buildGhostcnConfig(theme: ThemeConfig, paths?: {
|
|
24
|
+
partials?: string;
|
|
25
|
+
styles?: string;
|
|
26
|
+
js?: string;
|
|
27
|
+
cssFile?: string;
|
|
28
|
+
}): GhostcnConfig;
|
|
29
|
+
export declare function initializeThemeProject(options: InitializeThemeOptions): InitializeThemeResult;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildGhostcnConfig = buildGhostcnConfig;
|
|
7
|
+
exports.initializeThemeProject = initializeThemeProject;
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const theme_1 = require("./theme");
|
|
11
|
+
const theme_files_1 = require("./theme-files");
|
|
12
|
+
const styles_1 = require("./styles");
|
|
13
|
+
function buildGhostcnConfig(theme, paths = {}) {
|
|
14
|
+
const normalized = (value, fallback) => value?.trim().replace(/\\/g, '/') || fallback;
|
|
15
|
+
return {
|
|
16
|
+
$schema: 'https://ghostcn.com/schema.json',
|
|
17
|
+
style: theme.style,
|
|
18
|
+
theme: {
|
|
19
|
+
baseColor: theme.baseColor,
|
|
20
|
+
accentColor: theme.accentColor,
|
|
21
|
+
radius: theme.radius
|
|
22
|
+
},
|
|
23
|
+
aliases: {
|
|
24
|
+
partials: normalized(paths.partials, 'partials/components'),
|
|
25
|
+
styles: normalized(paths.styles, 'assets/css/components'),
|
|
26
|
+
js: normalized(paths.js, 'assets/js/components')
|
|
27
|
+
},
|
|
28
|
+
cssFile: normalized(paths.cssFile, 'assets/css/ghostcn.css')
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function initializeThemeProject(options) {
|
|
32
|
+
const configPath = (0, theme_files_1.resolveWithinTheme)(options.themeRoot, 'components.json');
|
|
33
|
+
const cssPath = (0, theme_files_1.resolveWithinTheme)(options.themeRoot, options.config.cssFile);
|
|
34
|
+
fs_1.default.mkdirSync(path_1.default.dirname(cssPath), { recursive: true });
|
|
35
|
+
fs_1.default.writeFileSync(configPath, JSON.stringify(options.config, null, 2) + '\n', 'utf8');
|
|
36
|
+
fs_1.default.writeFileSync(cssPath, (0, theme_1.generateThemeCss)({ style: options.config.style, ...options.config.theme }), 'utf8');
|
|
37
|
+
if (options.config.style === 'tailwind') {
|
|
38
|
+
fs_1.default.writeFileSync(path_1.default.join(path_1.default.dirname(cssPath), 'ghostcn-tailwind.css'), (0, styles_1.tailwindTheme)(), 'utf8');
|
|
39
|
+
}
|
|
40
|
+
const styleSync = (0, theme_files_1.syncGhostcnStyles)({
|
|
41
|
+
themeRoot: options.themeRoot,
|
|
42
|
+
cssFile: options.config.cssFile,
|
|
43
|
+
stylesDir: options.config.aliases.styles
|
|
44
|
+
});
|
|
45
|
+
return { configPath, cssPath, styleSync };
|
|
46
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun';
|
|
2
|
+
export type ColorScheme = 'Light' | 'Dark' | 'Auto';
|
|
3
|
+
export interface ScaffoldThemeOptions {
|
|
4
|
+
parentDirectory: string;
|
|
5
|
+
themeName: string;
|
|
6
|
+
style: 'tailwind' | 'css';
|
|
7
|
+
colorScheme: ColorScheme;
|
|
8
|
+
packageManager: PackageManager;
|
|
9
|
+
authorName?: string;
|
|
10
|
+
authorEmail?: string;
|
|
11
|
+
includeNewsletter?: boolean;
|
|
12
|
+
includeAuthorCard?: boolean;
|
|
13
|
+
includePricing?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface ScaffoldThemeResult {
|
|
16
|
+
themeRoot: string;
|
|
17
|
+
files: string[];
|
|
18
|
+
}
|
|
19
|
+
export declare const REQUIRED_STARTER_COMPONENTS: readonly ["site-header", "site-footer", "featured-posts", "pagination", "post-header", "member-cta", "post-card"];
|
|
20
|
+
export declare function sanitizeThemeName(value: string): string;
|
|
21
|
+
export declare function scaffoldTheme(options: ScaffoldThemeOptions): ScaffoldThemeResult;
|