-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathcli.ts
More file actions
161 lines (150 loc) · 6.12 KB
/
cli.ts
File metadata and controls
161 lines (150 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /usr/bin/env node
/** Required to set max width of the help commands */
const oldWidth = process.stdout.columns;
process.stdout.columns = 100;
/** ---------------------------------------------- */
import { program } from 'commander';
import chalk from 'chalk';
import inquirer from 'inquirer';
import packageJson from './package.json' with { type: 'json' };
import { commandDescriptions, cliConfig } from './lib/parser.js';
import { getLatestVersion, compareVersions } from './lib/utils.js';
import inquirerSearchList from 'inquirer-search-list';
import { client } from './lib/commands/generic.js';
import { login, logout, whoami, migrate, register } from './lib/commands/generic.js';
import { init } from './lib/commands/init.js';
import { types } from './lib/commands/types.js';
import { pull } from './lib/commands/pull.js';
import { run } from './lib/commands/run.js';
import { push, deploy } from './lib/commands/push.js';
import { update } from './lib/commands/update.js';
import { generate } from './lib/commands/generate.js';
import { account } from './lib/commands/services/account.js';
import { activities } from './lib/commands/services/activities.js';
import { backups } from './lib/commands/services/backups.js';
import { databases } from './lib/commands/services/databases.js';
import { functions } from './lib/commands/services/functions.js';
import { graphql } from './lib/commands/services/graphql.js';
import { health } from './lib/commands/services/health.js';
import { locale } from './lib/commands/services/locale.js';
import { messaging } from './lib/commands/services/messaging.js';
import { migrations } from './lib/commands/services/migrations.js';
import { project } from './lib/commands/services/project.js';
import { projects } from './lib/commands/services/projects.js';
import { proxy } from './lib/commands/services/proxy.js';
import { sites } from './lib/commands/services/sites.js';
import { storage } from './lib/commands/services/storage.js';
import { tablesDB } from './lib/commands/services/tables-db.js';
import { teams } from './lib/commands/services/teams.js';
import { tokens } from './lib/commands/services/tokens.js';
import { users } from './lib/commands/services/users.js';
import { vcs } from './lib/commands/services/vcs.js';
const { version } = packageJson;
inquirer.registerPrompt('search-list', inquirerSearchList);
/**
* Check for updates and show version information
*/
async function checkVersion(): Promise<void> {
process.stdout.write(chalk.bold(`appwrite version ${version}`) + '\n');
try {
const latestVersion = await getLatestVersion();
const comparison = compareVersions(version, latestVersion);
if (comparison > 0) {
// Current version is older than latest
process.stdout.write(
chalk.yellow(`\n⚠️ A newer version is available: ${chalk.bold(latestVersion)}`) + '\n'
);
process.stdout.write(
chalk.cyan(
`💡 Run '${chalk.bold('appwrite update')}' to update to the latest version.`
) + '\n'
);
} else if (comparison === 0) {
process.stdout.write(chalk.green('\n✅ You are running the latest version!') + '\n');
} else {
// Current version is newer than latest (pre-release/dev)
process.stdout.write(chalk.blue('\n🚀 You are running a pre-release or development version.') + '\n');
}
} catch (_error) {
// Silently fail version check, just show current version
process.stdout.write(chalk.gray('\n(Unable to check for updates)') + '\n');
}
}
// Intercept version flag before Commander.js processes it
if (process.argv.includes('-v') || process.argv.includes('--version')) {
void (async () => {
await checkVersion();
process.exit(0);
})();
} else {
program
.description(commandDescriptions['main'])
.configureHelp({
helpWidth: process.stdout.columns || 80,
sortSubcommands: true,
})
.helpOption('-h, --help', 'Display help for command')
.version(version, '-v, --version', 'Output the version number')
.option('-V, --verbose', 'Show complete error log')
.option('-j, --json', 'Output in JSON format')
.hook('preAction', migrate)
.option('-f,--force', 'Flag to confirm all warnings')
.option('-a,--all', 'Flag to push all resources')
.option('--id [id...]', 'Flag to pass a list of ids for a given action')
.option('--report', 'Enable reporting in case of CLI errors')
.on('option:json', () => {
cliConfig.json = true;
})
.on('option:verbose', () => {
cliConfig.verbose = true;
})
.on('option:report', function () {
cliConfig.report = true;
cliConfig.reportData = { data: this };
})
.on('option:force', () => {
cliConfig.force = true;
})
.on('option:all', () => {
cliConfig.all = true;
})
.on('option:id', function () {
cliConfig.ids = (this.opts().id as string[]);
})
.showSuggestionAfterError()
.addCommand(whoami)
.addCommand(register)
.addCommand(login)
.addCommand(init)
.addCommand(pull)
.addCommand(push)
.addCommand(types)
.addCommand(deploy)
.addCommand(run)
.addCommand(update)
.addCommand(generate)
.addCommand(logout)
.addCommand(account)
.addCommand(activities)
.addCommand(backups)
.addCommand(databases)
.addCommand(functions)
.addCommand(graphql)
.addCommand(health)
.addCommand(locale)
.addCommand(messaging)
.addCommand(migrations)
.addCommand(project)
.addCommand(projects)
.addCommand(proxy)
.addCommand(sites)
.addCommand(storage)
.addCommand(tablesDB)
.addCommand(teams)
.addCommand(tokens)
.addCommand(users)
.addCommand(vcs)
.addCommand(client)
.parse(process.argv);
process.stdout.columns = oldWidth;
}