@astralkit/cli 3.1.0 → 4.1.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.
@@ -1,86 +1,54 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const { execSync } = require('child_process');
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { execa } from 'execa';
4
4
 
5
- /**
6
- * Detect the package manager from lockfiles in the project directory.
7
- * Falls back to npm if nothing detected.
8
- */
9
- function detectPackageManager(dir = process.cwd()) {
5
+ export function detectPackageManager(dir = process.cwd()) {
10
6
  if (fs.existsSync(path.join(dir, 'bun.lockb')) || fs.existsSync(path.join(dir, 'bun.lock'))) return 'bun';
11
7
  if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) return 'pnpm';
12
8
  if (fs.existsSync(path.join(dir, 'yarn.lock'))) return 'yarn';
13
9
  return 'npm';
14
10
  }
15
11
 
16
- /**
17
- * Get the install command for a package manager.
18
- */
19
- function getInstallCommand(pm, packages = []) {
12
+ export function getInstallCommand(pm, packages = []) {
20
13
  if (packages.length === 0) {
21
- // Install all deps from package.json
22
- switch (pm) {
23
- case 'bun': return 'bun install';
24
- case 'pnpm': return 'pnpm install';
25
- case 'yarn': return 'yarn';
26
- default: return 'npm install';
27
- }
14
+ const cmds = { bun: 'bun install', pnpm: 'pnpm install', yarn: 'yarn', npm: 'npm install' };
15
+ return cmds[pm] ?? 'npm install';
28
16
  }
29
-
30
- // Install specific packages
31
17
  const pkgList = packages.join(' ');
32
- switch (pm) {
33
- case 'bun': return `bun add ${pkgList}`;
34
- case 'pnpm': return `pnpm add ${pkgList}`;
35
- case 'yarn': return `yarn add ${pkgList}`;
36
- default: return `npm install ${pkgList}`;
37
- }
18
+ const cmds = { bun: `bun add ${pkgList}`, pnpm: `pnpm add ${pkgList}`, yarn: `yarn add ${pkgList}`, npm: `npm install ${pkgList}` };
19
+ return cmds[pm] ?? `npm install ${pkgList}`;
38
20
  }
39
21
 
40
- /**
41
- * Get the run command for a package manager.
42
- */
43
- function getRunCommand(pm, script) {
44
- switch (pm) {
45
- case 'bun': return `bun run ${script}`;
46
- case 'pnpm': return `pnpm ${script}`;
47
- case 'yarn': return `yarn ${script}`;
48
- default: return `npm run ${script}`;
49
- }
22
+ export function getRunCommand(pm, script) {
23
+ const cmds = { bun: `bun run ${script}`, pnpm: `pnpm ${script}`, yarn: `yarn ${script}`, npm: `npm run ${script}` };
24
+ return cmds[pm] ?? `npm run ${script}`;
50
25
  }
51
26
 
52
- /**
53
- * Get the exec command (npx equivalent) for a package manager.
54
- */
55
- function getExecCommand(pm) {
56
- switch (pm) {
57
- case 'bun': return 'bunx';
58
- case 'pnpm': return 'pnpx';
59
- case 'yarn': return 'yarn dlx';
60
- default: return 'npx';
61
- }
27
+ export function getExecCommand(pm) {
28
+ const cmds = { bun: 'bunx', pnpm: 'pnpx', yarn: 'yarn dlx', npm: 'npx' };
29
+ return cmds[pm] ?? 'npx';
62
30
  }
63
31
 
64
32
  /**
65
- * Run an install command silently, capturing output.
66
- * Returns { success, error? }
33
+ * Run an install command using execa.
34
+ * Returns { success: true } or { success: false, error, hint, raw }
67
35
  */
68
- function runInstall(pm, cwd, packages = []) {
69
- const cmd = getInstallCommand(pm, packages);
36
+ export async function runInstall(pm, cwd, packages = []) {
37
+ const [bin, ...args] = getInstallCommand(pm, packages).split(' ');
38
+
70
39
  try {
71
- execSync(cmd, { cwd, stdio: 'pipe', timeout: 120000 });
40
+ await execa(bin, args, { cwd, timeout: 120_000 });
72
41
  return { success: true };
73
42
  } catch (err) {
74
- const stderr = err.stderr?.toString() || '';
43
+ const stderr = err.stderr || '';
75
44
 
76
- // Detect common errors and provide friendly messages
77
45
  if (stderr.includes('ERESOLVE') || stderr.includes('peer dep')) {
78
46
  return {
79
47
  success: false,
80
48
  error: 'Dependency conflict detected.',
81
49
  hint: pm === 'npm'
82
50
  ? `Try: npm install ${packages.join(' ')} --legacy-peer-deps`
83
- : `Try: ${cmd} --force`,
51
+ : `Try: ${getInstallCommand(pm, packages)} --force`,
84
52
  raw: stderr,
85
53
  };
86
54
  }
@@ -88,7 +56,7 @@ function runInstall(pm, cwd, packages = []) {
88
56
  if (stderr.includes('ENOENT') || stderr.includes('not found')) {
89
57
  return {
90
58
  success: false,
91
- error: `${pm} is not installed or not in PATH.`,
59
+ error: `${pm} not found in PATH.`,
92
60
  hint: `Install ${pm} or use a different package manager.`,
93
61
  raw: stderr,
94
62
  };
@@ -97,16 +65,8 @@ function runInstall(pm, cwd, packages = []) {
97
65
  return {
98
66
  success: false,
99
67
  error: 'Install failed.',
100
- hint: `Run manually: ${cmd}`,
68
+ hint: `Run manually: ${getInstallCommand(pm, packages)}`,
101
69
  raw: stderr.slice(0, 500),
102
70
  };
103
71
  }
104
72
  }
105
-
106
- module.exports = {
107
- detectPackageManager,
108
- getInstallCommand,
109
- getRunCommand,
110
- getExecCommand,
111
- runInstall,
112
- };
@@ -1,87 +1,50 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const chalk = require('chalk');
4
-
5
- const VALID_FRAMEWORKS = ['html', 'react', 'nextjs'];
6
- const SLUG_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;
7
-
8
- /**
9
- * Validate a framework name against the allowed list.
10
- * Returns true if valid, false if invalid (and logs an error).
11
- */
12
- function validateFramework(framework) {
13
- if (!framework) return true; // null/undefined means "auto-detect"
14
- if (VALID_FRAMEWORKS.includes(framework)) return true;
15
-
16
- console.error(
17
- chalk.red(`Invalid framework "${framework}".`),
18
- chalk.gray(`Valid options: ${VALID_FRAMEWORKS.join(', ')}`)
19
- );
20
- return false;
21
- }
22
-
23
- /**
24
- * Validate a slug format.
25
- * @param {string} slug
26
- * @param {string} [label='name'] - Content type label for error messages.
27
- * Returns true if valid, false if invalid (and logs an error).
28
- */
29
- function validateSlug(slug, label = 'name') {
30
- if (!slug || typeof slug !== 'string') {
31
- console.error(chalk.red(`${label.charAt(0).toUpperCase() + label.slice(1)} is required.`));
32
- return false;
33
- }
34
-
35
- if (slug.length > 128) {
36
- console.error(chalk.red(`${label.charAt(0).toUpperCase() + label.slice(1)} is too long (max 128 characters).`));
37
- return false;
38
- }
39
-
40
- if (!SLUG_PATTERN.test(slug)) {
41
- console.error(
42
- chalk.red(`Invalid ${label} "${slug}".`),
43
- chalk.gray('Use lowercase letters, numbers, and hyphens only (e.g., "button-sizes").')
44
- );
45
- return false;
46
- }
47
-
48
- return true;
49
- }
50
-
51
- /**
52
- * Check if a file path is safely contained within a base directory.
53
- * Prevents path traversal attacks (e.g., ../../etc/passwd).
54
- */
55
- function isSafePath(basedir, filepath) {
56
- const resolved = path.resolve(basedir, filepath);
57
- const relative = path.relative(basedir, resolved);
58
- return !relative.startsWith('..') && !path.isAbsolute(relative);
59
- }
60
-
61
- /**
62
- * Write files to a base directory with path-traversal protection.
63
- * Returns the number of files successfully written.
64
- */
65
- function writeFiles(baseDir, files) {
66
- let count = 0;
67
- for (const file of files) {
68
- if (!isSafePath(baseDir, file.path)) {
69
- console.log(chalk.red(` Skipping suspicious file path: ${file.path}`));
70
- continue;
71
- }
72
- const filePath = path.resolve(baseDir, file.path);
73
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
74
- fs.writeFileSync(filePath, file.content);
75
- count++;
76
- }
77
- return count;
78
- }
79
-
80
- module.exports = {
81
- VALID_FRAMEWORKS,
82
- SLUG_PATTERN,
83
- validateFramework,
84
- validateSlug,
85
- isSafePath,
86
- writeFiles,
87
- };
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import pc from 'picocolors';
4
+
5
+ export const VALID_FRAMEWORKS = ['html', 'react', 'nextjs'];
6
+ export const SLUG_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;
7
+
8
+ export function validateFramework(framework) {
9
+ if (!framework) return true;
10
+ if (VALID_FRAMEWORKS.includes(framework)) return true;
11
+ console.error(pc.red(`Invalid framework "${framework}".`), pc.gray(`Valid options: ${VALID_FRAMEWORKS.join(', ')}`));
12
+ return false;
13
+ }
14
+
15
+ export function validateSlug(slug, label = 'name') {
16
+ if (!slug || typeof slug !== 'string') {
17
+ console.error(pc.red(`${label.charAt(0).toUpperCase() + label.slice(1)} is required.`));
18
+ return false;
19
+ }
20
+ if (slug.length > 128) {
21
+ console.error(pc.red(`${label.charAt(0).toUpperCase() + label.slice(1)} is too long (max 128 chars).`));
22
+ return false;
23
+ }
24
+ if (!SLUG_PATTERN.test(slug)) {
25
+ console.error(pc.red(`Invalid ${label} "${slug}".`), pc.gray('Use lowercase letters, numbers, and hyphens only.'));
26
+ return false;
27
+ }
28
+ return true;
29
+ }
30
+
31
+ export function isSafePath(basedir, filepath) {
32
+ const resolved = path.resolve(basedir, filepath);
33
+ const relative = path.relative(basedir, resolved);
34
+ return !relative.startsWith('..') && !path.isAbsolute(relative);
35
+ }
36
+
37
+ export function writeFiles(baseDir, files) {
38
+ let count = 0;
39
+ for (const file of files) {
40
+ if (!isSafePath(baseDir, file.path)) {
41
+ console.log(pc.red(` Skipping suspicious path: ${file.path}`));
42
+ continue;
43
+ }
44
+ const filePath = path.resolve(baseDir, file.path);
45
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
46
+ fs.writeFileSync(filePath, file.content);
47
+ count++;
48
+ }
49
+ return count;
50
+ }