@nexical/cli 0.11.4 → 0.11.6

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.
Files changed (43) hide show
  1. package/dist/chunk-2FKDEDDE.js +39 -0
  2. package/dist/chunk-2FKDEDDE.js.map +1 -0
  3. package/dist/chunk-2JW5BYZW.js +24 -0
  4. package/dist/chunk-2JW5BYZW.js.map +1 -0
  5. package/dist/chunk-EKCOW7FM.js +118 -0
  6. package/dist/chunk-EKCOW7FM.js.map +1 -0
  7. package/dist/index.js +13 -11
  8. package/dist/index.js.map +1 -1
  9. package/dist/src/commands/deploy.d.ts +2 -18
  10. package/dist/src/commands/deploy.js +100 -150
  11. package/dist/src/commands/deploy.js.map +1 -1
  12. package/dist/src/commands/init.js +3 -3
  13. package/dist/src/commands/module/add.js +3 -3
  14. package/dist/src/deploy/config-manager.d.ts +11 -0
  15. package/dist/src/deploy/config-manager.js +9 -0
  16. package/dist/src/deploy/config-manager.js.map +1 -0
  17. package/dist/src/deploy/providers/cloudflare.d.ts +12 -0
  18. package/dist/src/deploy/providers/cloudflare.js +113 -0
  19. package/dist/src/deploy/providers/cloudflare.js.map +1 -0
  20. package/dist/src/deploy/providers/github.d.ts +10 -0
  21. package/dist/src/deploy/providers/github.js +121 -0
  22. package/dist/src/deploy/providers/github.js.map +1 -0
  23. package/dist/src/deploy/providers/railway.d.ts +13 -0
  24. package/dist/src/deploy/providers/railway.js +114 -0
  25. package/dist/src/deploy/providers/railway.js.map +1 -0
  26. package/dist/src/deploy/registry.d.ts +15 -0
  27. package/dist/src/deploy/registry.js +9 -0
  28. package/dist/src/deploy/registry.js.map +1 -0
  29. package/dist/src/deploy/types.d.ts +47 -0
  30. package/dist/src/deploy/types.js +8 -0
  31. package/dist/src/deploy/types.js.map +1 -0
  32. package/dist/src/deploy/utils.d.ts +6 -0
  33. package/dist/src/deploy/utils.js +11 -0
  34. package/dist/src/deploy/utils.js.map +1 -0
  35. package/package.json +13 -11
  36. package/src/commands/deploy.ts +125 -193
  37. package/src/deploy/config-manager.ts +41 -0
  38. package/src/deploy/providers/cloudflare.ts +143 -0
  39. package/src/deploy/providers/github.ts +135 -0
  40. package/src/deploy/providers/railway.ts +143 -0
  41. package/src/deploy/registry.ts +136 -0
  42. package/src/deploy/types.ts +63 -0
  43. package/src/deploy/utils.ts +13 -0
@@ -0,0 +1,135 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import YAML from 'yaml';
4
+ import { logger } from '@nexical/cli-core';
5
+ import { RepositoryProvider, DeploymentContext, DeploymentProvider } from '../types';
6
+ import { execAsync } from '../utils';
7
+
8
+ export class GitHubProvider implements RepositoryProvider {
9
+ name = 'github';
10
+
11
+ async configureSecrets(
12
+ context: DeploymentContext,
13
+ secrets: Record<string, string>,
14
+ ): Promise<void> {
15
+ for (const [key, value] of Object.entries(secrets)) {
16
+ if (!value) continue;
17
+ logger.info(`Setting secret ${key} in GitHub...`);
18
+ if (context.options.dryRun) {
19
+ logger.info(`[Dry Run] Would set secret ${key}`);
20
+ } else {
21
+ await execAsync(`gh secret set ${key} --body "${value}"`);
22
+ }
23
+ }
24
+ }
25
+
26
+ async configureVariables(
27
+ context: DeploymentContext,
28
+ variables: Record<string, string>,
29
+ ): Promise<void> {
30
+ for (const [key, value] of Object.entries(variables)) {
31
+ if (!value) continue;
32
+ logger.info(`Setting variable ${key} in GitHub...`);
33
+ if (context.options.dryRun) {
34
+ logger.info(`[Dry Run] Would set variable ${key}`);
35
+ } else {
36
+ await execAsync(`gh variable set ${key} --body "${value}"`);
37
+ }
38
+ }
39
+ }
40
+
41
+ async generateWorkflow(context: DeploymentContext, targets: DeploymentProvider[]): Promise<void> {
42
+ const workflowsDir = path.join(context.cwd, '.github/workflows');
43
+ await fs.mkdir(workflowsDir, { recursive: true });
44
+
45
+ for (const target of targets) {
46
+ const config = target.getCIConfig('github');
47
+ if (!config) continue;
48
+
49
+ const filename = `deploy-${target.type}.yml`;
50
+ const filepath = path.join(workflowsDir, filename);
51
+
52
+ const workflow: Record<string, unknown> = {
53
+ name: `Deploy ${target.type === 'backend' ? 'Backend' : 'Frontend'} to ${target.name}`,
54
+ on: {
55
+ push: { branches: ['main'] },
56
+ workflow_dispatch: {},
57
+ },
58
+ jobs: {
59
+ deploy: {
60
+ 'runs-on': 'ubuntu-latest',
61
+ permissions: {
62
+ contents: 'read',
63
+ deployments: 'write',
64
+ },
65
+ steps: [
66
+ {
67
+ name: 'Checkout',
68
+ uses: 'actions/checkout@v4',
69
+ with: { submodules: 'recursive' },
70
+ },
71
+ {
72
+ name: 'Setup Node',
73
+ uses: 'actions/setup-node@v4',
74
+ with: { 'node-version': 20, cache: 'npm' },
75
+ },
76
+ {
77
+ name: 'Install Dependencies',
78
+ run: 'npm ci',
79
+ },
80
+ ],
81
+ },
82
+ },
83
+ };
84
+
85
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
86
+ const steps = (workflow as any).jobs.deploy.steps;
87
+
88
+ // Build (if frontend)
89
+ if (target.type === 'frontend') {
90
+ steps.push({
91
+ name: 'Build Frontend',
92
+ run: 'npm run build --workspace=@app/frontend',
93
+ });
94
+ }
95
+
96
+ // Provider Install Steps
97
+ if (config.installSteps) {
98
+ for (const step of config.installSteps) {
99
+ steps.push({
100
+ name: `Install ${target.name} CLI`,
101
+ run: step,
102
+ });
103
+ }
104
+ }
105
+
106
+ // Provider Deploy Steps
107
+ if (config.deploySteps) {
108
+ for (const step of config.deploySteps) {
109
+ const deployStep: Record<string, unknown> = {
110
+ name: `Deploy to ${target.name}`,
111
+ run: step,
112
+ 'working-directory': target.type === 'backend' ? 'apps/backend' : 'apps/frontend',
113
+ };
114
+
115
+ if (config.secrets && config.secrets.length > 0) {
116
+ deployStep.env = config.secrets.reduce((acc: Record<string, string>, secret) => {
117
+ acc[secret] = `\${{ secrets.${secret} }}`;
118
+ return acc;
119
+ }, {});
120
+ }
121
+
122
+ steps.push(deployStep);
123
+ }
124
+ }
125
+
126
+ // Provider Action Step
127
+ if (config.githubActionStep) {
128
+ steps.push(config.githubActionStep);
129
+ }
130
+
131
+ await fs.writeFile(filepath, YAML.stringify(workflow), 'utf-8');
132
+ logger.info(`Generated workflow: ${filepath}`);
133
+ }
134
+ }
135
+ }
@@ -0,0 +1,143 @@
1
+ import path from 'node:path';
2
+ import { logger } from '@nexical/cli-core';
3
+ import { DeploymentProvider, DeploymentContext, CIConfig } from '../types';
4
+ import { execAsync } from '../utils';
5
+
6
+ export class RailwayProvider implements DeploymentProvider {
7
+ name = 'railway';
8
+ type = 'backend' as const;
9
+
10
+ async provision(context: DeploymentContext): Promise<void> {
11
+ const backendDir = path.join(context.cwd, 'apps/backend');
12
+ const railwayName = context.config.deploy?.backend?.projectName;
13
+
14
+ if (!railwayName) {
15
+ throw new Error(
16
+ "Railway project name not found in nexical.yaml. Please configure 'deploy.backend.projectName'.",
17
+ );
18
+ }
19
+
20
+ logger.info('Configuring Railway...');
21
+
22
+ if (context.options.dryRun) {
23
+ logger.info('[Dry Run] Would check Railway status and init project.');
24
+ return;
25
+ }
26
+
27
+ try {
28
+ // We consciously DO NOT pass any RAILWAY_API_TOKEN to the subprocess.
29
+ // The user may have an invalid token in their .env file (which process.env inherits).
30
+ // We want to force the Railway CLI to use the locally logged-in user's credentials.
31
+ const env = { ...process.env };
32
+ delete env.RAILWAY_API_TOKEN;
33
+ delete env.RAILWAY_TOKEN;
34
+
35
+ logger.info('Using local Railway CLI credentials (environment variables stripped).');
36
+
37
+ // Check status to see if we are linked to a project
38
+ try {
39
+ await execAsync('railway status', { cwd: backendDir, env });
40
+ } catch (error: unknown) {
41
+ const errMsg = error instanceof Error ? error.message : String(error);
42
+ const stderr = (error as { stderr?: string }).stderr || '';
43
+ const stdout = (error as { stdout?: string }).stdout || '';
44
+ const fullError = `${errMsg} ${stderr} ${stdout}`;
45
+
46
+ // If status fails, likely project doesn't exist locally or we aren't linked.
47
+ if (
48
+ fullError.includes('Project not found') ||
49
+ fullError.includes('No project') ||
50
+ fullError.includes('Project is deleted')
51
+ ) {
52
+ if (fullError.includes('Project is deleted')) {
53
+ logger.info('[Railway] Project is deleted. Unlinking...');
54
+ // If it's deleted, we might need to unlink first to clean up local config
55
+ await execAsync('railway unlink', { cwd: backendDir }).catch(() => {});
56
+ }
57
+ const initCmd = `railway init --name ${railwayName}`;
58
+ logger.info(`No active Railway project linked. Initializing with: ${initCmd}`);
59
+ await execAsync(initCmd, { cwd: backendDir, env });
60
+ } else if (
61
+ fullError.includes('Invalid RAILWAY_API_TOKEN') ||
62
+ fullError.includes('Unauthorized')
63
+ ) {
64
+ throw new Error('Railway authentication failed during status check.');
65
+ } else {
66
+ // Some other error (e.g. timeout), warn and try to proceed
67
+ logger.warn(`Railway status check failed: ${errMsg}. Proceeding.`);
68
+ }
69
+ }
70
+
71
+ logger.info(`Adding PostgreSQL service if missing for "${railwayName}"...`);
72
+ const { stdout: status } = await execAsync('railway status', { cwd: backendDir, env }).catch(
73
+ () => ({ stdout: '' }),
74
+ );
75
+ if (!status.includes('postgres')) {
76
+ try {
77
+ await execAsync('railway add --database postgres', { cwd: backendDir, env });
78
+ } catch {
79
+ logger.warn('Failed to auto-add PostgreSQL.');
80
+ }
81
+ }
82
+ } catch (e: unknown) {
83
+ // Rethrow explicit auth errors, otherwise warn
84
+ const errMsg = e instanceof Error ? e.message : String(e);
85
+
86
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
+ const stderr = (e as any).stderr || '';
88
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
89
+ const stdout = (e as any).stdout || '';
90
+
91
+ if (errMsg.includes('Railway authentication failed')) throw e;
92
+
93
+ logger.error(`Railway setup failed with error: ${errMsg}`);
94
+ if (stderr) logger.error(`[Railway stderr]: ${stderr}`);
95
+ if (stdout) logger.info(`[Railway stdout]: ${stdout}`);
96
+
97
+ logger.warn(
98
+ 'Railway setup encountered an issue. Ensure you are logged in or have a valid token.',
99
+ );
100
+ }
101
+ }
102
+
103
+ private resolveToken(context: DeploymentContext): string | undefined {
104
+ const options = context.config.deploy?.backend?.options || {};
105
+ const tokenEnvVar = typeof options.tokenEnvVar === 'string' ? options.tokenEnvVar : undefined;
106
+ return (
107
+ process.env.RAILWAY_API_TOKEN?.trim() ||
108
+ (tokenEnvVar ? process.env[tokenEnvVar]?.trim() : undefined) ||
109
+ process.env.RAILWAY_TOKEN?.trim()
110
+ );
111
+ }
112
+
113
+ async getSecrets(context: DeploymentContext): Promise<Record<string, string>> {
114
+ const token = this.resolveToken(context);
115
+ const secrets: Record<string, string> = {};
116
+
117
+ if (!token) {
118
+ // Strict check: Error if token is missing
119
+ throw new Error(
120
+ `Railway Token not found. Please provide it via:\n` +
121
+ `1. Setting RAILWAY_API_TOKEN in .env (Recommended)\n` +
122
+ `2. Configuring 'deploy.backend.options.tokenEnvVar' in nexical.yaml\n` +
123
+ `3. Setting RAILWAY_TOKEN in .env`,
124
+ );
125
+ }
126
+
127
+ secrets['RAILWAY_API_TOKEN'] = token;
128
+ return secrets;
129
+ }
130
+
131
+ async getVariables(context: DeploymentContext): Promise<Record<string, string>> {
132
+ return {};
133
+ }
134
+
135
+ getCIConfig(): CIConfig {
136
+ return {
137
+ secrets: ['RAILWAY_API_TOKEN'],
138
+ variables: [],
139
+ installSteps: ['npm install -g @railway/cli'],
140
+ deploySteps: ['railway up --detach'],
141
+ };
142
+ }
143
+ }
@@ -0,0 +1,136 @@
1
+ import path from 'node:path';
2
+ import fs from 'node:fs/promises';
3
+ import { logger } from '@nexical/cli-core';
4
+ import { DeploymentProvider, RepositoryProvider } from './types';
5
+
6
+ export class ProviderRegistry {
7
+ private deploymentProviders: Map<string, DeploymentProvider> = new Map();
8
+ private repositoryProviders: Map<string, RepositoryProvider> = new Map();
9
+
10
+ registerDeploymentProvider(provider: DeploymentProvider) {
11
+ this.deploymentProviders.set(provider.name, provider);
12
+ }
13
+
14
+ registerRepositoryProvider(provider: RepositoryProvider) {
15
+ this.repositoryProviders.set(provider.name, provider);
16
+ }
17
+
18
+ getDeploymentProvider(name: string): DeploymentProvider | undefined {
19
+ return this.deploymentProviders.get(name);
20
+ }
21
+
22
+ getRepositoryProvider(name: string): RepositoryProvider | undefined {
23
+ return this.repositoryProviders.get(name);
24
+ }
25
+
26
+ private registerProviderFromModule(module: unknown, source: string) {
27
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
+ const moduleAny = module as any;
29
+ let provider = moduleAny.default;
30
+
31
+ // Handle named exports if default is missing (fallback)
32
+ if (!provider && Object.keys(moduleAny).length > 0) {
33
+ // Try to find a class export that looks like a provider
34
+ for (const key of Object.keys(moduleAny)) {
35
+ if (typeof moduleAny[key] === 'function') {
36
+ provider = moduleAny[key];
37
+ break;
38
+ }
39
+ }
40
+ }
41
+
42
+ // If it's a class, instantiate it
43
+ if (typeof provider === 'function') {
44
+ try {
45
+ provider = new provider();
46
+ } catch {
47
+ // Not a constructor or failed
48
+ }
49
+ }
50
+
51
+ if (provider) {
52
+ if (typeof provider.provision === 'function' && typeof provider.getCIConfig === 'function') {
53
+ logger.info(`[Registry] Loaded ${source} deployment provider: ${provider.name}`);
54
+ this.registerDeploymentProvider(provider as DeploymentProvider);
55
+ } else if (
56
+ typeof provider.configureSecrets === 'function' &&
57
+ typeof provider.generateWorkflow === 'function'
58
+ ) {
59
+ logger.info(`[Registry] Loaded ${source} repository provider: ${provider.name}`);
60
+ this.registerRepositoryProvider(provider as RepositoryProvider);
61
+ }
62
+ }
63
+ }
64
+
65
+ async loadCoreProviders() {
66
+ const dirname = path.dirname(new URL(import.meta.url).pathname);
67
+
68
+ // Try multiple paths to find the providers directory
69
+ // 1. 'providers' - Standard source structure / flattened dist
70
+ // 2. 'src/deploy/providers' - tsup output (chunk in root, files in src/...)
71
+ const candidates = [
72
+ path.join(dirname, 'providers'),
73
+ path.join(dirname, 'src/deploy/providers'),
74
+ ];
75
+
76
+ let providersDir = '';
77
+ for (const candidate of candidates) {
78
+ try {
79
+ await fs.access(candidate);
80
+ providersDir = candidate;
81
+ break;
82
+ } catch {
83
+ // Ignore missing dir
84
+ }
85
+ }
86
+
87
+ if (!providersDir) {
88
+ logger.warn(
89
+ `[Registry] Could not locate core providers directory. Checked: ${candidates.join(', ')}`,
90
+ );
91
+ return;
92
+ }
93
+
94
+ try {
95
+ const files = await fs.readdir(providersDir);
96
+ for (const file of files) {
97
+ if (file.endsWith('.js') || (file.endsWith('.ts') && !file.endsWith('.d.ts'))) {
98
+ try {
99
+ const providerPath = path.join(providersDir, file);
100
+ const module = await import(providerPath);
101
+ this.registerProviderFromModule(module, 'core');
102
+ } catch (e: unknown) {
103
+ const message = e instanceof Error ? e.message : String(e);
104
+ logger.warn(`Failed to load core provider from ${file}: ${message}`);
105
+ }
106
+ }
107
+ }
108
+ } catch (e: unknown) {
109
+ const message = e instanceof Error ? e.message : String(e);
110
+ logger.warn(`Failed to scan core providers at ${providersDir}: ${message}`);
111
+ }
112
+ }
113
+
114
+ async loadLocalProviders(cwd: string) {
115
+ const deployDir = path.join(cwd, 'deploy');
116
+ try {
117
+ const files = await fs.readdir(deployDir);
118
+ for (const file of files) {
119
+ if (file.endsWith('.ts') || file.endsWith('.js')) {
120
+ try {
121
+ const providerPath = path.join(deployDir, file);
122
+ // Use jiti to load TS/JS files dynamically
123
+ const jiti = (await import('jiti')).createJiti(import.meta.url);
124
+ const module = (await jiti.import(providerPath)) as unknown;
125
+ this.registerProviderFromModule(module, 'local');
126
+ } catch (e: unknown) {
127
+ const message = e instanceof Error ? e.message : String(e);
128
+ logger.warn(`Failed to load local provider from ${file}: ${message}`);
129
+ }
130
+ }
131
+ }
132
+ } catch {
133
+ // Ignore if deploy dir doesn't exist
134
+ }
135
+ }
136
+ }
@@ -0,0 +1,63 @@
1
+ export interface CIConfig {
2
+ secrets: string[];
3
+ variables: string[];
4
+ installSteps?: string[];
5
+ buildSteps?: string[];
6
+ deploySteps?: string[];
7
+ // Platform specific overrides (e.g. uses: action/...)
8
+ githubActionStep?: Record<string, unknown>;
9
+ }
10
+
11
+ export interface DeploymentContext {
12
+ cwd: string;
13
+ config: NexicalConfig;
14
+ options: Record<string, unknown>;
15
+ }
16
+
17
+ export interface DeploymentProvider {
18
+ name: string;
19
+ type: 'frontend' | 'backend';
20
+
21
+ // Interactive or automatic setup of the provider resources
22
+ provision(context: DeploymentContext): Promise<void>;
23
+
24
+ // Returns the CI configuration for this provider
25
+ getCIConfig(repoType: 'github' | 'gitlab'): CIConfig;
26
+
27
+ // Returns a map of secrets to be set in the repository (e.g. tokens, account IDs)
28
+ // The provider is responsible for resolving these from config/env and throwing if missing.
29
+ getSecrets(context: DeploymentContext): Promise<Record<string, string>>;
30
+
31
+ // Returns a map of variables to be set in the repository (e.g. project names, service names)
32
+ getVariables(context: DeploymentContext): Promise<Record<string, string>>;
33
+ }
34
+
35
+ export interface RepositoryProvider {
36
+ name: string;
37
+
38
+ // Sets secrets/variables in the repo
39
+ configureSecrets(context: DeploymentContext, secrets: Record<string, string>): Promise<void>;
40
+ configureVariables(context: DeploymentContext, variables: Record<string, string>): Promise<void>;
41
+
42
+ // Generates and writes the CI workflow files
43
+ generateWorkflow(context: DeploymentContext, targets: DeploymentProvider[]): Promise<void>;
44
+ }
45
+
46
+ export interface NexicalConfig {
47
+ deploy?: {
48
+ backend?: {
49
+ provider: string;
50
+ projectName?: string;
51
+ options?: Record<string, unknown>;
52
+ };
53
+ frontend?: {
54
+ provider: string;
55
+ projectName?: string;
56
+ options?: Record<string, unknown>;
57
+ };
58
+ repository?: {
59
+ provider: string;
60
+ options?: Record<string, unknown>;
61
+ };
62
+ };
63
+ }
@@ -0,0 +1,13 @@
1
+ import { exec } from 'node:child_process';
2
+ import { promisify } from 'node:util';
3
+
4
+ export const execAsync = promisify(exec);
5
+
6
+ export async function checkCommand(command: string): Promise<boolean> {
7
+ try {
8
+ await execAsync(command);
9
+ return true;
10
+ } catch {
11
+ return false;
12
+ }
13
+ }