@astralkit/cli 3.0.5 → 4.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/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,156 @@
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
+ // Config file support — kept for backwards compatibility with existing projects
5
+ // New installs don't create this file; detection is automatic.
6
+ export const CONFIG_FILENAME = 'astralkit.config.json';
7
+
8
+ export function getProjectConfig(dir = process.cwd()) {
9
+ let searchDir = dir;
10
+ while (true) {
11
+ const configPath = path.join(searchDir, CONFIG_FILENAME);
12
+ if (fs.existsSync(configPath)) {
13
+ try {
14
+ const raw = fs.readFileSync(configPath, 'utf8');
15
+ return { ...JSON.parse(raw), _path: configPath };
16
+ } catch { return null; }
17
+ }
18
+ const parent = path.dirname(searchDir);
19
+ if (parent === searchDir) break;
20
+ searchDir = parent;
21
+ }
22
+ return null;
23
+ }
24
+
25
+ export const CSS_ENTRY_CANDIDATES = [
26
+ 'src/index.css',
27
+ 'src/app/globals.css',
28
+ 'src/styles/globals.css',
29
+ 'src/styles/index.css',
30
+ 'app/globals.css',
31
+ 'styles/globals.css',
32
+ 'styles/global.css',
33
+ 'index.css',
34
+ ];
35
+
36
+ /**
37
+ * CSS imports needed for AstralKit.
38
+ * Tailwind import is version-dependent — use getRequiredCssImports() instead.
39
+ */
40
+ export const REQUIRED_CSS_IMPORTS = [
41
+ '@import "tailwindcss";',
42
+ '@import "astralkit/theme";',
43
+ '@import "astralkit/utilities";',
44
+ ];
45
+
46
+ /**
47
+ * Get the correct CSS imports for the project's Tailwind version.
48
+ */
49
+ export function getRequiredCssImports(dir = process.cwd()) {
50
+ const twVersion = detectTailwindVersion(dir);
51
+ const tailwindImport = twVersion === 3
52
+ ? '@tailwind base;\n@tailwind components;\n@tailwind utilities;'
53
+ : '@import "tailwindcss";';
54
+ return [
55
+ tailwindImport,
56
+ '@import "astralkit/theme";',
57
+ '@import "astralkit/utilities";',
58
+ ];
59
+ }
60
+
61
+ export function detectFramework(dir = process.cwd()) {
62
+ if (
63
+ fs.existsSync(path.join(dir, 'next.config.js')) ||
64
+ fs.existsSync(path.join(dir, 'next.config.mjs')) ||
65
+ fs.existsSync(path.join(dir, 'next.config.ts'))
66
+ ) return 'nextjs';
67
+
68
+ const pkgPath = path.join(dir, 'package.json');
69
+ if (fs.existsSync(pkgPath)) {
70
+ try {
71
+ const pkgJson = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
72
+ const allDeps = { ...pkgJson.dependencies, ...pkgJson.devDependencies };
73
+ if (allDeps['react']) return 'react';
74
+ } catch { /* ignore */ }
75
+ }
76
+
77
+ return 'html';
78
+ }
79
+
80
+ /**
81
+ * Detect Tailwind CSS version from package.json.
82
+ * Returns 3, 4, or null.
83
+ */
84
+ export function detectTailwindVersion(dir = process.cwd()) {
85
+ try {
86
+ const pkgPath = path.join(dir, 'package.json');
87
+ if (!fs.existsSync(pkgPath)) return null;
88
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
89
+ const deps = { ...pkg.dependencies, ...pkg.devDependencies };
90
+ const ver = deps['tailwindcss'];
91
+ if (!ver) return null;
92
+ const major = parseInt(ver.replace(/[^0-9]/, ''), 10);
93
+ if (major >= 4) return 4;
94
+ if (major === 3) return 3;
95
+ return null;
96
+ } catch {
97
+ return null;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Detect if the project uses TypeScript (tsconfig.json exists).
103
+ */
104
+ export function isTypeScriptProject(dir = process.cwd()) {
105
+ return fs.existsSync(path.join(dir, 'tsconfig.json'));
106
+ }
107
+
108
+ export function isAstralkitInstalled(dir = process.cwd()) {
109
+ return fs.existsSync(path.join(dir, 'node_modules', 'astralkit', 'package.json'));
110
+ }
111
+
112
+ export function findCssEntryFile(dir = process.cwd()) {
113
+ for (const candidate of CSS_ENTRY_CANDIDATES) {
114
+ const filePath = path.join(dir, candidate);
115
+ if (fs.existsSync(filePath)) return { path: filePath, relative: candidate };
116
+ }
117
+ return null;
118
+ }
119
+
120
+ export function getMissingCssImports(cssFilePath) {
121
+ try {
122
+ const content = fs.readFileSync(cssFilePath, 'utf8');
123
+ // Check for both v3 and v4 Tailwind directives
124
+ const hasTailwind = content.includes('@import "tailwindcss"') || content.includes('@tailwind base');
125
+ const hasTheme = content.includes('astralkit/theme');
126
+ const hasUtilities = content.includes('astralkit/utilities');
127
+
128
+ const missing = [];
129
+ if (!hasTailwind) missing.push('tailwind');
130
+ if (!hasTheme) missing.push('@import "astralkit/theme";');
131
+ if (!hasUtilities) missing.push('@import "astralkit/utilities";');
132
+ return missing;
133
+ } catch {
134
+ return ['tailwind', '@import "astralkit/theme";', '@import "astralkit/utilities";'];
135
+ }
136
+ }
137
+
138
+ export function addCssImports(cssFilePath, missing, dir = process.cwd()) {
139
+ const existing = fs.existsSync(cssFilePath) ? fs.readFileSync(cssFilePath, 'utf8') : '';
140
+ const twVersion = detectTailwindVersion(dir);
141
+
142
+ const lines = [];
143
+ for (const imp of missing) {
144
+ if (imp === 'tailwind') {
145
+ if (twVersion === 3) {
146
+ lines.push('@tailwind base;', '@tailwind components;', '@tailwind utilities;');
147
+ } else {
148
+ lines.push('@import "tailwindcss";');
149
+ }
150
+ } else {
151
+ lines.push(imp);
152
+ }
153
+ }
154
+
155
+ fs.writeFileSync(cssFilePath, lines.join('\n') + '\n\n' + existing);
156
+ }