@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.
package/src/lib/auth.js CHANGED
@@ -1,142 +1,98 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const os = require('os');
4
-
5
- const CONFIG_DIR = path.join(os.homedir(), '.astralkit');
6
- const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
7
-
8
- function ensureConfigDir() {
9
- if (!fs.existsSync(CONFIG_DIR)) {
10
- fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
11
- }
12
- }
13
-
14
- /**
15
- * Decode a JWT payload without verifying the signature.
16
- * Used only to check the `exp` claim for local expiry detection.
17
- */
18
- function decodeJwtPayload(token) {
19
- try {
20
- const parts = token.split('.');
21
- if (parts.length !== 3) return null;
22
- const payload = parts[1].replace(/-/g, '+').replace(/_/g, '/');
23
- return JSON.parse(Buffer.from(payload, 'base64').toString('utf8'));
24
- } catch {
25
- return null;
26
- }
27
- }
28
-
29
- /**
30
- * Check if a JWT token is expired based on its `exp` claim.
31
- * Returns true if expired, false if still valid or no exp claim.
32
- */
33
- function isTokenExpired(token) {
34
- if (!token) return true;
35
- const payload = decodeJwtPayload(token);
36
- if (!payload || !payload.exp) return false; // No exp = assume valid
37
- return Date.now() >= payload.exp * 1000;
38
- }
39
-
40
- /**
41
- * Read the stored auth config.
42
- * Checks ASTRALKIT_TOKEN env var first (for CI/CD), then falls back to config file.
43
- */
44
- function getAuth() {
45
- // CI/CD: environment variable takes priority
46
- const envToken = process.env.ASTRALKIT_TOKEN;
47
- if (envToken) {
48
- if (isTokenExpired(envToken)) {
49
- return { token: null, email: null, plan: null, expired: true, source: 'env' };
50
- }
51
- return { token: envToken, email: null, plan: null, source: 'env' };
52
- }
53
-
54
- // File-based config
55
- try {
56
- if (fs.existsSync(CONFIG_FILE)) {
57
- const config = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
58
- const token = config.token || null;
59
-
60
- if (token && isTokenExpired(token)) {
61
- return {
62
- token: null,
63
- email: config.email || null,
64
- plan: config.plan || null,
65
- expired: true,
66
- source: 'config',
67
- };
68
- }
69
-
70
- return {
71
- token,
72
- email: config.email || null,
73
- plan: config.plan || null,
74
- source: 'config',
75
- };
76
- }
77
- } catch {
78
- // Ignore corrupt config
79
- }
80
- return { token: null, email: null, plan: null };
81
- }
82
-
83
- /**
84
- * Save auth token and user info.
85
- */
86
- function saveAuth({ token, email, plan }) {
87
- ensureConfigDir();
88
- const existing = getFullConfig();
89
- const config = { ...existing, token, email, plan };
90
- fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
91
- }
92
-
93
- /**
94
- * Save an API key (replaces Bearer token auth — simpler, permanent).
95
- */
96
- function saveApiKey(apiKey) {
97
- ensureConfigDir();
98
- const existing = getFullConfig();
99
- const config = { ...existing, apiKey };
100
- // Clear legacy JWT fields when switching to API key
101
- delete config.token;
102
- fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
103
- }
104
-
105
- /**
106
- * Get the stored API key (if any).
107
- */
108
- function getApiKey() {
109
- const envKey = process.env.ASTRALKIT_API_KEY;
110
- if (envKey) return envKey;
111
-
112
- const config = getFullConfig();
113
- return config.apiKey || null;
114
- }
115
-
116
- /**
117
- * Remove stored auth.
118
- */
119
- function clearToken() {
120
- ensureConfigDir();
121
- const config = getFullConfig();
122
- delete config.token;
123
- delete config.email;
124
- delete config.plan;
125
- fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
126
- }
127
-
128
- /**
129
- * Read the full config file (preserving non-auth fields).
130
- */
131
- function getFullConfig() {
132
- try {
133
- if (fs.existsSync(CONFIG_FILE)) {
134
- return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
135
- }
136
- } catch {
137
- // Ignore
138
- }
139
- return {};
140
- }
141
-
142
- module.exports = { getAuth, saveAuth, saveApiKey, getApiKey, clearToken, isTokenExpired, CONFIG_DIR, CONFIG_FILE };
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import os from 'os';
4
+
5
+ const CONFIG_DIR = path.join(os.homedir(), '.astralkit');
6
+ const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
7
+
8
+ function ensureConfigDir() {
9
+ if (!fs.existsSync(CONFIG_DIR)) {
10
+ fs.mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
11
+ }
12
+ }
13
+
14
+ function decodeJwtPayload(token) {
15
+ try {
16
+ const parts = token.split('.');
17
+ if (parts.length !== 3) return null;
18
+ const payload = parts[1].replace(/-/g, '+').replace(/_/g, '/');
19
+ return JSON.parse(Buffer.from(payload, 'base64').toString('utf8'));
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
24
+
25
+ function isTokenExpired(token) {
26
+ if (!token) return true;
27
+ const payload = decodeJwtPayload(token);
28
+ if (!payload || !payload.exp) return false;
29
+ return Date.now() >= payload.exp * 1000;
30
+ }
31
+
32
+ export function getAuth() {
33
+ const envToken = process.env.ASTRALKIT_TOKEN;
34
+ if (envToken) {
35
+ if (isTokenExpired(envToken)) {
36
+ return { token: null, email: null, plan: null, expired: true, source: 'env' };
37
+ }
38
+ return { token: envToken, email: null, plan: null, source: 'env' };
39
+ }
40
+
41
+ try {
42
+ if (fs.existsSync(CONFIG_FILE)) {
43
+ const config = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
44
+ const token = config.token || null;
45
+
46
+ if (token && isTokenExpired(token)) {
47
+ return { token: null, email: config.email || null, plan: config.plan || null, expired: true, source: 'config' };
48
+ }
49
+
50
+ return { token, email: config.email || null, plan: config.plan || null, source: 'config' };
51
+ }
52
+ } catch {
53
+ // Ignore corrupt config
54
+ }
55
+ return { token: null, email: null, plan: null };
56
+ }
57
+
58
+ export function saveAuth({ token, email, plan }) {
59
+ ensureConfigDir();
60
+ const existing = getFullConfig();
61
+ const config = { ...existing, token, email, plan };
62
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
63
+ }
64
+
65
+ export function saveApiKey(apiKey) {
66
+ ensureConfigDir();
67
+ const existing = getFullConfig();
68
+ const config = { ...existing, apiKey };
69
+ delete config.token;
70
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
71
+ }
72
+
73
+ export function getApiKey() {
74
+ const envKey = process.env.ASTRALKIT_API_KEY;
75
+ if (envKey) return envKey;
76
+ const config = getFullConfig();
77
+ return config.apiKey || null;
78
+ }
79
+
80
+ export function clearToken() {
81
+ ensureConfigDir();
82
+ const config = getFullConfig();
83
+ delete config.token;
84
+ delete config.email;
85
+ delete config.plan;
86
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
87
+ }
88
+
89
+ function getFullConfig() {
90
+ try {
91
+ if (fs.existsSync(CONFIG_FILE)) {
92
+ return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
93
+ }
94
+ } catch { /* ignore */ }
95
+ return {};
96
+ }
97
+
98
+ export { CONFIG_DIR, CONFIG_FILE };
package/src/lib/config.js CHANGED
@@ -1,163 +1,127 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
-
4
- const CONFIG_FILENAME = 'astralkit.config.json';
5
-
6
- /**
7
- * Default project config.
8
- */
9
- function defaultConfig(framework = 'html') {
10
- return {
11
- $schema: 'https://astralkit.com/schema/config.json',
12
- framework,
13
- componentsDir: 'src/components/ui',
14
- };
15
- }
16
-
17
- /**
18
- * Find the project config file by walking up from cwd.
19
- */
20
- function findConfigFile(startDir = process.cwd()) {
21
- let dir = startDir;
22
- while (true) {
23
- const configPath = path.join(dir, CONFIG_FILENAME);
24
- if (fs.existsSync(configPath)) {
25
- return configPath;
26
- }
27
- const parent = path.dirname(dir);
28
- if (parent === dir) break; // reached root
29
- dir = parent;
30
- }
31
- return null;
32
- }
33
-
34
- /**
35
- * Read the project config.
36
- */
37
- function getProjectConfig() {
38
- const configPath = findConfigFile();
39
- if (!configPath) return null;
40
-
41
- try {
42
- const raw = fs.readFileSync(configPath, 'utf8');
43
- return { ...JSON.parse(raw), _path: configPath };
44
- } catch {
45
- return null;
46
- }
47
- }
48
-
49
- /**
50
- * Write the project config.
51
- */
52
- function writeProjectConfig(config, dir = process.cwd()) {
53
- const configPath = path.join(dir, CONFIG_FILENAME);
54
- const { _path, ...rest } = config;
55
- fs.writeFileSync(configPath, JSON.stringify(rest, null, 2) + '\n');
56
- return configPath;
57
- }
58
-
59
- /**
60
- * Auto-detect framework from project files.
61
- */
62
- function detectFramework(dir = process.cwd()) {
63
- // Check for Next.js
64
- if (
65
- fs.existsSync(path.join(dir, 'next.config.js')) ||
66
- fs.existsSync(path.join(dir, 'next.config.mjs')) ||
67
- fs.existsSync(path.join(dir, 'next.config.ts'))
68
- ) {
69
- return 'nextjs';
70
- }
71
-
72
- // Check package.json for React
73
- const pkgPath = path.join(dir, 'package.json');
74
- if (fs.existsSync(pkgPath)) {
75
- try {
76
- const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
77
- const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
78
- if (allDeps['react']) return 'react';
79
- } catch {
80
- // Ignore
81
- }
82
- }
83
-
84
- return 'html';
85
- }
86
-
87
- /**
88
- * Check if the astralkit npm package is installed in the project.
89
- */
90
- function isAstralkitInstalled(dir = process.cwd()) {
91
- return fs.existsSync(path.join(dir, 'node_modules', 'astralkit', 'package.json'));
92
- }
93
-
94
- /**
95
- * Common CSS file locations to search for when adding imports.
96
- */
97
- const CSS_ENTRY_CANDIDATES = [
98
- 'src/index.css',
99
- 'src/app/globals.css',
100
- 'src/styles/globals.css',
101
- 'src/styles/index.css',
102
- 'app/globals.css',
103
- 'styles/globals.css',
104
- 'index.css',
105
- ];
106
-
107
- const REQUIRED_CSS_IMPORTS = [
108
- '@import "tailwindcss";',
109
- '@import "astralkit/theme";',
110
- '@import "astralkit/utilities";',
111
- ];
112
-
113
- /**
114
- * Find the project's CSS entry file.
115
- */
116
- function findCssEntryFile(dir = process.cwd()) {
117
- for (const candidate of CSS_ENTRY_CANDIDATES) {
118
- const filePath = path.join(dir, candidate);
119
- if (fs.existsSync(filePath)) {
120
- return { path: filePath, relative: candidate };
121
- }
122
- }
123
- return null;
124
- }
125
-
126
- /**
127
- * Check which of the 3 required CSS imports are missing from a file.
128
- * Returns array of missing import lines.
129
- */
130
- function getMissingCssImports(cssFilePath) {
131
- try {
132
- const content = fs.readFileSync(cssFilePath, 'utf8');
133
- return REQUIRED_CSS_IMPORTS.filter(imp => !content.includes(imp));
134
- } catch {
135
- return REQUIRED_CSS_IMPORTS;
136
- }
137
- }
138
-
139
- /**
140
- * Add missing CSS imports to the top of a file.
141
- */
142
- function addCssImports(cssFilePath, imports) {
143
- const existing = fs.existsSync(cssFilePath)
144
- ? fs.readFileSync(cssFilePath, 'utf8')
145
- : '';
146
- const newContent = imports.join('\n') + '\n\n' + existing;
147
- fs.writeFileSync(cssFilePath, newContent);
148
- }
149
-
150
- module.exports = {
151
- CONFIG_FILENAME,
152
- defaultConfig,
153
- findConfigFile,
154
- getProjectConfig,
155
- writeProjectConfig,
156
- detectFramework,
157
- isAstralkitInstalled,
158
- CSS_ENTRY_CANDIDATES,
159
- REQUIRED_CSS_IMPORTS,
160
- findCssEntryFile,
161
- getMissingCssImports,
162
- addCssImports,
163
- };
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ // ─── CSS entry file candidates (searched in order) ────────
5
+ export const CSS_ENTRY_CANDIDATES = [
6
+ 'src/app/globals.css',
7
+ 'src/index.css',
8
+ 'src/styles/globals.css',
9
+ 'src/styles/index.css',
10
+ 'app/globals.css',
11
+ 'styles/globals.css',
12
+ 'index.css',
13
+ ];
14
+
15
+ // ─── Framework detection ──────────────────────────────────
16
+
17
+ export function detectFramework(dir = process.cwd()) {
18
+ // Next.js
19
+ if (
20
+ fs.existsSync(path.join(dir, 'next.config.js')) ||
21
+ fs.existsSync(path.join(dir, 'next.config.mjs')) ||
22
+ fs.existsSync(path.join(dir, 'next.config.ts'))
23
+ ) return 'nextjs';
24
+
25
+ // React (Vite, CRA, etc.)
26
+ const pkgPath = path.join(dir, 'package.json');
27
+ if (fs.existsSync(pkgPath)) {
28
+ try {
29
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
30
+ const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
31
+ if (allDeps['react']) return 'react';
32
+ } catch { /* ignore */ }
33
+ }
34
+
35
+ return 'html';
36
+ }
37
+
38
+ // ─── Tailwind version detection ───────────────────────────
39
+
40
+ export function detectTailwindVersion(dir = process.cwd()) {
41
+ const pkgPath = path.join(dir, 'package.json');
42
+ if (!fs.existsSync(pkgPath)) return null;
43
+ try {
44
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
45
+ const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
46
+ const ver = allDeps['tailwindcss'];
47
+ if (!ver) return null;
48
+ const major = parseInt(ver.replace(/[^0-9]/, ''), 10);
49
+ if (major >= 4) return 4;
50
+ if (major >= 3) return 3;
51
+ return null;
52
+ } catch { return null; }
53
+ }
54
+
55
+ // ─── TypeScript detection ─────────────────────────────────
56
+
57
+ export function detectTypeScript(dir = process.cwd()) {
58
+ return fs.existsSync(path.join(dir, 'tsconfig.json'));
59
+ }
60
+
61
+ // ─── astralkit installed check ────────────────────────────
62
+
63
+ export function isAstralkitInstalled(dir = process.cwd()) {
64
+ return fs.existsSync(path.join(dir, 'node_modules', 'astralkit', 'package.json'));
65
+ }
66
+
67
+ // ─── CSS entry file search ────────────────────────────────
68
+
69
+ export function findCssEntryFile(dir = process.cwd()) {
70
+ for (const candidate of CSS_ENTRY_CANDIDATES) {
71
+ const filePath = path.join(dir, candidate);
72
+ if (fs.existsSync(filePath)) return { path: filePath, relative: candidate };
73
+ }
74
+ return null;
75
+ }
76
+
77
+ // ─── CSS imports (Tailwind v3 vs v4 aware) ────────────────
78
+
79
+ export function getRequiredCssImports(tailwindVersion) {
80
+ if (tailwindVersion === 3) {
81
+ return [
82
+ '@tailwind base;',
83
+ '@tailwind components;',
84
+ '@tailwind utilities;',
85
+ '@import "astralkit/theme";',
86
+ '@import "astralkit/utilities";',
87
+ ];
88
+ }
89
+ // v4 or unknown — default to v4 syntax
90
+ return [
91
+ '@import "tailwindcss";',
92
+ '@import "astralkit/theme";',
93
+ '@import "astralkit/utilities";',
94
+ ];
95
+ }
96
+
97
+ export function getMissingCssImports(cssFilePath, tailwindVersion) {
98
+ try {
99
+ const content = fs.readFileSync(cssFilePath, 'utf8');
100
+ const required = getRequiredCssImports(tailwindVersion);
101
+
102
+ return required.filter(imp => {
103
+ // For tailwind directives, check if either v3 or v4 syntax is present
104
+ if (imp === '@import "tailwindcss";') {
105
+ return !content.includes('@import "tailwindcss"') && !content.includes('@tailwind base');
106
+ }
107
+ if (imp === '@tailwind base;') {
108
+ return !content.includes('@tailwind base') && !content.includes('@import "tailwindcss"');
109
+ }
110
+ if (imp === '@tailwind components;') {
111
+ return !content.includes('@tailwind components');
112
+ }
113
+ if (imp === '@tailwind utilities;') {
114
+ return !content.includes('@tailwind utilities');
115
+ }
116
+ // AstralKit imports — check directly
117
+ return !content.includes(imp);
118
+ });
119
+ } catch {
120
+ return getRequiredCssImports(tailwindVersion);
121
+ }
122
+ }
123
+
124
+ export function addCssImports(cssFilePath, imports) {
125
+ const existing = fs.existsSync(cssFilePath) ? fs.readFileSync(cssFilePath, 'utf8') : '';
126
+ fs.writeFileSync(cssFilePath, imports.join('\n') + '\n\n' + existing);
127
+ }