ghostcn 1.0.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/dist/commands/add.d.ts +4 -0
- package/dist/commands/add.js +117 -0
- package/dist/commands/init.d.ts +4 -0
- package/dist/commands/init.js +70 -0
- package/dist/commands/list.d.ts +1 -0
- package/dist/commands/list.js +33 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +34 -0
- package/dist/test/registry.test.d.ts +1 -0
- package/dist/test/registry.test.js +62 -0
- package/dist/utils/registry.d.ts +31 -0
- package/dist/utils/registry.js +112 -0
- package/package.json +54 -0
|
@@ -0,0 +1,117 @@
|
|
|
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.add = add;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const prompts_1 = __importDefault(require("prompts"));
|
|
10
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
11
|
+
const ora_1 = __importDefault(require("ora"));
|
|
12
|
+
const registry_1 = require("../utils/registry");
|
|
13
|
+
async function installSingleComponent(componentName, config, options) {
|
|
14
|
+
let sanitizedName;
|
|
15
|
+
try {
|
|
16
|
+
sanitizedName = (0, registry_1.sanitizeComponentName)(componentName);
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
console.error(chalk_1.default.red(err.message));
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const cwd = process.cwd();
|
|
23
|
+
const spinner = (0, ora_1.default)(`Fetching component "${sanitizedName}"...`).start();
|
|
24
|
+
const manifest = await (0, registry_1.getComponent)(sanitizedName);
|
|
25
|
+
if (!manifest) {
|
|
26
|
+
spinner.fail(`Component "${sanitizedName}" not found in the registry.`);
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
spinner.succeed(`Found "${sanitizedName}"!`);
|
|
30
|
+
for (const file of manifest.files) {
|
|
31
|
+
let targetDirAlias = '';
|
|
32
|
+
if (file.type === 'partial')
|
|
33
|
+
targetDirAlias = config.aliases?.partials || 'partials/components';
|
|
34
|
+
else if (file.type === 'style')
|
|
35
|
+
targetDirAlias = config.aliases?.styles || 'assets/css/components';
|
|
36
|
+
else if (file.type === 'js')
|
|
37
|
+
targetDirAlias = config.aliases?.js || 'assets/js/components';
|
|
38
|
+
if (!targetDirAlias)
|
|
39
|
+
continue;
|
|
40
|
+
const targetDir = path_1.default.join(cwd, targetDirAlias);
|
|
41
|
+
const targetPath = path_1.default.join(targetDir, file.name);
|
|
42
|
+
if (!fs_1.default.existsSync(targetDir)) {
|
|
43
|
+
fs_1.default.mkdirSync(targetDir, { recursive: true });
|
|
44
|
+
}
|
|
45
|
+
if (fs_1.default.existsSync(targetPath) && !options.yes) {
|
|
46
|
+
const response = await (0, prompts_1.default)({
|
|
47
|
+
type: 'confirm',
|
|
48
|
+
name: 'overwrite',
|
|
49
|
+
message: `File ${targetDirAlias}/${file.name} already exists. Overwrite?`,
|
|
50
|
+
initial: false
|
|
51
|
+
});
|
|
52
|
+
if (!response.overwrite) {
|
|
53
|
+
console.log(chalk_1.default.yellow(`Skipped ${file.name}.`));
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
fs_1.default.writeFileSync(targetPath, file.content, 'utf8');
|
|
58
|
+
console.log(chalk_1.default.green(`Installed ${file.name} to ${targetDirAlias}/${file.name}`));
|
|
59
|
+
}
|
|
60
|
+
console.log(chalk_1.default.blue(`Component "${sanitizedName}" successfully installed!`));
|
|
61
|
+
if (manifest.files.some(f => f.type === 'style')) {
|
|
62
|
+
console.log(chalk_1.default.yellow(`Note: Import ${config.aliases?.styles || 'assets/css/components'}/<style>.css into your main stylesheet if using Vanilla CSS.`));
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
async function add(componentName, options = {}) {
|
|
67
|
+
const cwd = process.cwd();
|
|
68
|
+
const configPath = path_1.default.join(cwd, 'components.json');
|
|
69
|
+
if (!fs_1.default.existsSync(configPath)) {
|
|
70
|
+
console.error(chalk_1.default.red('components.json not found. Please run "ghostcn init" first.'));
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
let config;
|
|
74
|
+
try {
|
|
75
|
+
const raw = fs_1.default.readFileSync(configPath, 'utf8').replace(/^\uFEFF/, '');
|
|
76
|
+
config = JSON.parse(raw);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
console.error(chalk_1.default.red('Could not parse components.json. Ensure it contains valid JSON.'));
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
// If component name is provided directly, install it
|
|
83
|
+
if (componentName) {
|
|
84
|
+
const ok = await installSingleComponent(componentName, config, options);
|
|
85
|
+
if (!ok)
|
|
86
|
+
process.exit(1);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
// If no component specified, display interactive selection
|
|
90
|
+
const spinner = (0, ora_1.default)('Loading component catalog...').start();
|
|
91
|
+
const availableItems = await (0, registry_1.getRegistryIndex)();
|
|
92
|
+
spinner.stop();
|
|
93
|
+
if (!availableItems || availableItems.length === 0) {
|
|
94
|
+
console.error(chalk_1.default.red('No components found in the registry.'));
|
|
95
|
+
process.exit(1);
|
|
96
|
+
}
|
|
97
|
+
const response = await (0, prompts_1.default)({
|
|
98
|
+
type: 'multiselect',
|
|
99
|
+
name: 'selected',
|
|
100
|
+
message: 'Which components would you like to install?',
|
|
101
|
+
choices: availableItems.map(item => ({
|
|
102
|
+
title: `${item.name.padEnd(18)} ${chalk_1.default.dim(item.description)}`,
|
|
103
|
+
value: item.name
|
|
104
|
+
})),
|
|
105
|
+
hint: '- Space to select. Return to submit'
|
|
106
|
+
});
|
|
107
|
+
if (!response.selected || response.selected.length === 0) {
|
|
108
|
+
console.log(chalk_1.default.yellow('No components selected. Exiting.'));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
console.log(chalk_1.default.blue(`\nInstalling ${response.selected.length} component(s)...\n`));
|
|
112
|
+
for (const item of response.selected) {
|
|
113
|
+
await installSingleComponent(item, config, options);
|
|
114
|
+
console.log('');
|
|
115
|
+
}
|
|
116
|
+
console.log(chalk_1.default.green('All selected components have been installed!'));
|
|
117
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
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.init = init;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const prompts_1 = __importDefault(require("prompts"));
|
|
10
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
11
|
+
async function init(options = {}) {
|
|
12
|
+
console.log(chalk_1.default.blue('Initializing Ghost UI component system...'));
|
|
13
|
+
const cwd = process.cwd();
|
|
14
|
+
// Verify it's a ghost theme
|
|
15
|
+
const packageJsonPath = path_1.default.join(cwd, 'package.json');
|
|
16
|
+
if (!fs_1.default.existsSync(packageJsonPath)) {
|
|
17
|
+
console.error(chalk_1.default.red('No package.json found. Please run this command from the root of a Ghost theme.'));
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf8').replace(/^\uFEFF/, ''));
|
|
21
|
+
if (!packageJson.engines || (!packageJson.engines.ghost && !packageJson.engines['ghost-api'])) {
|
|
22
|
+
console.warn(chalk_1.default.yellow('Warning: No "ghost" engine found in package.json. Continuing anyway, but ensure this is a Ghost theme.'));
|
|
23
|
+
}
|
|
24
|
+
let style = 'tailwind';
|
|
25
|
+
let partialsAlias = 'partials/components';
|
|
26
|
+
let stylesAlias = 'assets/css/components';
|
|
27
|
+
if (!options.yes) {
|
|
28
|
+
const response = await (0, prompts_1.default)([
|
|
29
|
+
{
|
|
30
|
+
type: 'select',
|
|
31
|
+
name: 'style',
|
|
32
|
+
message: 'Which styling system are you using?',
|
|
33
|
+
choices: [
|
|
34
|
+
{ title: 'Tailwind CSS', value: 'tailwind' },
|
|
35
|
+
{ title: 'Vanilla CSS', value: 'css' }
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
type: 'text',
|
|
40
|
+
name: 'partialsAlias',
|
|
41
|
+
message: 'Where should partials be installed?',
|
|
42
|
+
initial: 'partials/components'
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: 'text',
|
|
46
|
+
name: 'stylesAlias',
|
|
47
|
+
message: 'Where should styles be installed?',
|
|
48
|
+
initial: 'assets/css/components'
|
|
49
|
+
}
|
|
50
|
+
]);
|
|
51
|
+
if (!response.partialsAlias || !response.stylesAlias) {
|
|
52
|
+
console.log(chalk_1.default.red('Initialization aborted.'));
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
style = response.style || style;
|
|
56
|
+
partialsAlias = response.partialsAlias;
|
|
57
|
+
stylesAlias = response.stylesAlias;
|
|
58
|
+
}
|
|
59
|
+
const config = {
|
|
60
|
+
style,
|
|
61
|
+
aliases: {
|
|
62
|
+
partials: partialsAlias,
|
|
63
|
+
styles: stylesAlias
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const configPath = path_1.default.join(cwd, 'components.json');
|
|
67
|
+
fs_1.default.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
|
68
|
+
console.log(chalk_1.default.green('\nSuccess! Configuration written to components.json.'));
|
|
69
|
+
console.log(`You can now add components using ${chalk_1.default.cyan('npx ghostcn add <component>')}`);
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function list(): Promise<void>;
|
|
@@ -0,0 +1,33 @@
|
|
|
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.list = list;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const ora_1 = __importDefault(require("ora"));
|
|
9
|
+
const registry_1 = require("../utils/registry");
|
|
10
|
+
async function list() {
|
|
11
|
+
const spinner = (0, ora_1.default)('Fetching available components from Ghost UI registry...').start();
|
|
12
|
+
try {
|
|
13
|
+
const items = await (0, registry_1.getRegistryIndex)();
|
|
14
|
+
if (!items || items.length === 0) {
|
|
15
|
+
spinner.warn('No components found in the registry.');
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
spinner.succeed(`Found ${items.length} components in Ghost UI registry:\n`);
|
|
19
|
+
for (const item of items) {
|
|
20
|
+
console.log(` ${chalk_1.default.bold.cyan(item.name)}`);
|
|
21
|
+
console.log(` ${chalk_1.default.gray(item.description)}`);
|
|
22
|
+
if (item.files && item.files.length > 0) {
|
|
23
|
+
console.log(` ${chalk_1.default.dim('Files:')} ${chalk_1.default.dim(item.files.join(', '))}`);
|
|
24
|
+
}
|
|
25
|
+
console.log('');
|
|
26
|
+
}
|
|
27
|
+
console.log(`Install any component using: ${chalk_1.default.green('ghostcn add <component>')}`);
|
|
28
|
+
console.log(`Or run ${chalk_1.default.green('ghostcn add')} to select interactively.\n`);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
spinner.fail(`Failed to fetch component catalog: ${err.message || err}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const commander_1 = require("commander");
|
|
8
|
+
const init_1 = require("./commands/init");
|
|
9
|
+
const add_1 = require("./commands/add");
|
|
10
|
+
const list_1 = require("./commands/list");
|
|
11
|
+
const package_json_1 = __importDefault(require("../package.json"));
|
|
12
|
+
function main() {
|
|
13
|
+
const program = new commander_1.Command()
|
|
14
|
+
.name('ghostcn')
|
|
15
|
+
.description('The shadcn/ui for Ghost CMS themes')
|
|
16
|
+
.version(package_json_1.default.version || '1.0.0');
|
|
17
|
+
program
|
|
18
|
+
.command('init')
|
|
19
|
+
.description('Initialize your Ghost theme and choose a component structure')
|
|
20
|
+
.option('-y, --yes', 'Skip prompts and use default configuration')
|
|
21
|
+
.action(init_1.init);
|
|
22
|
+
program
|
|
23
|
+
.command('list')
|
|
24
|
+
.description('List all available components in the Ghost UI registry')
|
|
25
|
+
.action(list_1.list);
|
|
26
|
+
program
|
|
27
|
+
.command('add')
|
|
28
|
+
.description('Add one or more components to your Ghost theme')
|
|
29
|
+
.argument('[component]', 'The component to add (leave blank for interactive selection)')
|
|
30
|
+
.option('-y, --yes', 'Overwrite existing files without prompting')
|
|
31
|
+
.action(add_1.add);
|
|
32
|
+
program.parse();
|
|
33
|
+
}
|
|
34
|
+
main();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
const node_test_1 = require("node:test");
|
|
7
|
+
const node_assert_1 = __importDefault(require("node:assert"));
|
|
8
|
+
const registry_1 = require("../utils/registry");
|
|
9
|
+
(0, node_test_1.describe)('Ghost UI Registry Utils', () => {
|
|
10
|
+
(0, node_test_1.describe)('sanitizeComponentName', () => {
|
|
11
|
+
(0, node_test_1.test)('accepts valid kebab-case names', () => {
|
|
12
|
+
node_assert_1.default.strictEqual((0, registry_1.sanitizeComponentName)('newsletter-form'), 'newsletter-form');
|
|
13
|
+
node_assert_1.default.strictEqual((0, registry_1.sanitizeComponentName)('pricing-table'), 'pricing-table');
|
|
14
|
+
node_assert_1.default.strictEqual((0, registry_1.sanitizeComponentName)('post-card-2'), 'post-card-2');
|
|
15
|
+
});
|
|
16
|
+
(0, node_test_1.test)('trims whitespace and converts uppercase to lowercase', () => {
|
|
17
|
+
node_assert_1.default.strictEqual((0, registry_1.sanitizeComponentName)(' Newsletter-Form '), 'newsletter-form');
|
|
18
|
+
});
|
|
19
|
+
(0, node_test_1.test)('rejects path traversal and invalid characters', () => {
|
|
20
|
+
node_assert_1.default.throws(() => (0, registry_1.sanitizeComponentName)('../secret'), /Invalid component name/);
|
|
21
|
+
node_assert_1.default.throws(() => (0, registry_1.sanitizeComponentName)('component/nested'), /Invalid component name/);
|
|
22
|
+
node_assert_1.default.throws(() => (0, registry_1.sanitizeComponentName)('name with spaces'), /Invalid component name/);
|
|
23
|
+
node_assert_1.default.throws(() => (0, registry_1.sanitizeComponentName)('hello$world'), /Invalid component name/);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
(0, node_test_1.describe)('getRegistryIndex', () => {
|
|
27
|
+
(0, node_test_1.test)('returns list of available components', async () => {
|
|
28
|
+
const items = await (0, registry_1.getRegistryIndex)();
|
|
29
|
+
node_assert_1.default.ok(Array.isArray(items));
|
|
30
|
+
node_assert_1.default.ok(items.length >= 4, `Expected at least 4 components, got ${items.length}`);
|
|
31
|
+
const names = items.map(i => i.name);
|
|
32
|
+
node_assert_1.default.ok(names.includes('newsletter-form'));
|
|
33
|
+
node_assert_1.default.ok(names.includes('pricing-table'));
|
|
34
|
+
node_assert_1.default.ok(names.includes('author-card'));
|
|
35
|
+
node_assert_1.default.ok(names.includes('post-card'));
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
(0, node_test_1.describe)('getComponent', () => {
|
|
39
|
+
(0, node_test_1.test)('retrieves newsletter-form manifest correctly', async () => {
|
|
40
|
+
const manifest = await (0, registry_1.getComponent)('newsletter-form');
|
|
41
|
+
node_assert_1.default.ok(manifest !== null);
|
|
42
|
+
node_assert_1.default.strictEqual(manifest?.name, 'newsletter-form');
|
|
43
|
+
node_assert_1.default.ok(Array.isArray(manifest?.files));
|
|
44
|
+
node_assert_1.default.ok(manifest?.files.length >= 2);
|
|
45
|
+
const hbs = manifest?.files.find(f => f.type === 'partial');
|
|
46
|
+
node_assert_1.default.ok(hbs !== null);
|
|
47
|
+
node_assert_1.default.ok(hbs?.content.includes('gh-newsletter-form'));
|
|
48
|
+
const css = manifest?.files.find(f => f.type === 'style');
|
|
49
|
+
node_assert_1.default.ok(css !== null);
|
|
50
|
+
});
|
|
51
|
+
(0, node_test_1.test)('retrieves pricing-table manifest correctly', async () => {
|
|
52
|
+
const manifest = await (0, registry_1.getComponent)('pricing-table');
|
|
53
|
+
node_assert_1.default.ok(manifest !== null);
|
|
54
|
+
node_assert_1.default.strictEqual(manifest?.name, 'pricing-table');
|
|
55
|
+
node_assert_1.default.ok(manifest?.files.some(f => f.name === 'pricing-table.hbs'));
|
|
56
|
+
});
|
|
57
|
+
(0, node_test_1.test)('returns null for non-existent component', async () => {
|
|
58
|
+
const manifest = await (0, registry_1.getComponent)('non-existent-xyz-123');
|
|
59
|
+
node_assert_1.default.strictEqual(manifest, null);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const DEFAULT_REGISTRY_URL = "https://raw.githubusercontent.com/Geekbits-Org/ghost-ui/main/registry";
|
|
2
|
+
export interface ComponentFile {
|
|
3
|
+
name: string;
|
|
4
|
+
type: 'partial' | 'style' | 'js';
|
|
5
|
+
target: string;
|
|
6
|
+
content: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ComponentManifest {
|
|
9
|
+
name: string;
|
|
10
|
+
type: string;
|
|
11
|
+
description: string;
|
|
12
|
+
dependencies?: string[];
|
|
13
|
+
ghost_version?: string;
|
|
14
|
+
files: ComponentFile[];
|
|
15
|
+
gscan?: {
|
|
16
|
+
rules_satisfied: string[];
|
|
17
|
+
notes: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export interface RegistryItem {
|
|
21
|
+
name: string;
|
|
22
|
+
description: string;
|
|
23
|
+
category: string;
|
|
24
|
+
ghost_version?: string;
|
|
25
|
+
files: string[];
|
|
26
|
+
}
|
|
27
|
+
export declare function sanitizeComponentName(name: string): string;
|
|
28
|
+
export declare function findLocalIndex(): RegistryItem[] | null;
|
|
29
|
+
export declare function findLocalManifest(componentName: string): ComponentManifest | null;
|
|
30
|
+
export declare function getRegistryIndex(): Promise<RegistryItem[]>;
|
|
31
|
+
export declare function getComponent(componentName: string): Promise<ComponentManifest | null>;
|
|
@@ -0,0 +1,112 @@
|
|
|
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.DEFAULT_REGISTRY_URL = void 0;
|
|
7
|
+
exports.sanitizeComponentName = sanitizeComponentName;
|
|
8
|
+
exports.findLocalIndex = findLocalIndex;
|
|
9
|
+
exports.findLocalManifest = findLocalManifest;
|
|
10
|
+
exports.getRegistryIndex = getRegistryIndex;
|
|
11
|
+
exports.getComponent = getComponent;
|
|
12
|
+
const fs_1 = __importDefault(require("fs"));
|
|
13
|
+
const path_1 = __importDefault(require("path"));
|
|
14
|
+
exports.DEFAULT_REGISTRY_URL = 'https://raw.githubusercontent.com/Geekbits-Org/ghost-ui/main/registry';
|
|
15
|
+
function sanitizeComponentName(name) {
|
|
16
|
+
const sanitized = name.trim().toLowerCase();
|
|
17
|
+
if (!/^[a-z0-9-]+$/.test(sanitized)) {
|
|
18
|
+
throw new Error(`Invalid component name "${name}". Component names must contain only lowercase letters, numbers, and hyphens.`);
|
|
19
|
+
}
|
|
20
|
+
return sanitized;
|
|
21
|
+
}
|
|
22
|
+
async function fetchWithTimeout(url, timeoutMs = 5000) {
|
|
23
|
+
const controller = new AbortController();
|
|
24
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
25
|
+
try {
|
|
26
|
+
return await fetch(url, { signal: controller.signal });
|
|
27
|
+
}
|
|
28
|
+
finally {
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function findLocalRegistryDir() {
|
|
33
|
+
const searchStarts = [process.cwd(), __dirname];
|
|
34
|
+
for (const start of searchStarts) {
|
|
35
|
+
let currentDir = start;
|
|
36
|
+
while (currentDir !== path_1.default.parse(currentDir).root) {
|
|
37
|
+
const potentialPath = path_1.default.join(currentDir, 'registry');
|
|
38
|
+
if (fs_1.default.existsSync(potentialPath) && fs_1.default.statSync(potentialPath).isDirectory()) {
|
|
39
|
+
return potentialPath;
|
|
40
|
+
}
|
|
41
|
+
currentDir = path_1.default.dirname(currentDir);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
function findLocalIndex() {
|
|
47
|
+
const regDir = findLocalRegistryDir();
|
|
48
|
+
if (!regDir)
|
|
49
|
+
return null;
|
|
50
|
+
const indexPath = path_1.default.join(regDir, 'index.json');
|
|
51
|
+
if (!fs_1.default.existsSync(indexPath))
|
|
52
|
+
return null;
|
|
53
|
+
try {
|
|
54
|
+
const raw = fs_1.default.readFileSync(indexPath, 'utf8').replace(/^\uFEFF/, '');
|
|
55
|
+
return JSON.parse(raw);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function findLocalManifest(componentName) {
|
|
62
|
+
const regDir = findLocalRegistryDir();
|
|
63
|
+
if (!regDir)
|
|
64
|
+
return null;
|
|
65
|
+
const manifestPath = path_1.default.join(regDir, 'components', `${componentName}.json`);
|
|
66
|
+
if (!fs_1.default.existsSync(manifestPath))
|
|
67
|
+
return null;
|
|
68
|
+
try {
|
|
69
|
+
const raw = fs_1.default.readFileSync(manifestPath, 'utf8').replace(/^\uFEFF/, '');
|
|
70
|
+
return JSON.parse(raw);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async function getRegistryIndex() {
|
|
77
|
+
const registryBaseUrl = process.env.GHOST_UI_REGISTRY_URL || exports.DEFAULT_REGISTRY_URL;
|
|
78
|
+
// 1. Try remote fetch
|
|
79
|
+
try {
|
|
80
|
+
const res = await fetchWithTimeout(`${registryBaseUrl}/index.json`);
|
|
81
|
+
if (res.ok) {
|
|
82
|
+
const data = await res.json();
|
|
83
|
+
return data;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// Network or abort error, continue to fallback
|
|
88
|
+
}
|
|
89
|
+
// 2. Fallback to local
|
|
90
|
+
const localIndex = findLocalIndex();
|
|
91
|
+
if (localIndex) {
|
|
92
|
+
return localIndex;
|
|
93
|
+
}
|
|
94
|
+
return [];
|
|
95
|
+
}
|
|
96
|
+
async function getComponent(componentName) {
|
|
97
|
+
const sanitized = sanitizeComponentName(componentName);
|
|
98
|
+
const registryBaseUrl = process.env.GHOST_UI_REGISTRY_URL || exports.DEFAULT_REGISTRY_URL;
|
|
99
|
+
// 1. Try remote fetch
|
|
100
|
+
try {
|
|
101
|
+
const res = await fetchWithTimeout(`${registryBaseUrl}/components/${sanitized}.json`);
|
|
102
|
+
if (res.ok) {
|
|
103
|
+
const data = await res.json();
|
|
104
|
+
return data;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// Network or abort error, continue to fallback
|
|
109
|
+
}
|
|
110
|
+
// 2. Fallback to local
|
|
111
|
+
return findLocalManifest(sanitized);
|
|
112
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ghostcn",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The shadcn/ui for Ghost CMS themes - component distribution system",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"ghostcn": "dist/index.js",
|
|
9
|
+
"ghost-ui": "dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"prepublishOnly": "npm run build",
|
|
17
|
+
"start": "ts-node src/index.ts",
|
|
18
|
+
"dev": "ts-node src/index.ts",
|
|
19
|
+
"test": "node --test dist/test/*.test.js"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/Geekbits-Org/ghost-ui.git",
|
|
24
|
+
"directory": "packages/cli"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"ghost",
|
|
28
|
+
"ghost-cms",
|
|
29
|
+
"ghost-theme",
|
|
30
|
+
"theme",
|
|
31
|
+
"components",
|
|
32
|
+
"shadcn",
|
|
33
|
+
"cli",
|
|
34
|
+
"handlebars"
|
|
35
|
+
],
|
|
36
|
+
"author": "Geekbits Org",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/Geekbits-Org/ghost-ui/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/Geekbits-Org/ghost-ui#readme",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"chalk": "^4.1.2",
|
|
44
|
+
"commander": "^12.0.0",
|
|
45
|
+
"prompts": "^2.4.2",
|
|
46
|
+
"ora": "^5.4.1"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^20.11.24",
|
|
50
|
+
"@types/prompts": "^2.4.9",
|
|
51
|
+
"ts-node": "^10.9.2",
|
|
52
|
+
"typescript": "^5.3.3"
|
|
53
|
+
}
|
|
54
|
+
}
|