-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ts
More file actions
133 lines (107 loc) · 4.5 KB
/
deploy.ts
File metadata and controls
133 lines (107 loc) · 4.5 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
import * as cp from 'node:child_process';
import path from 'node:path';
import * as url from 'node:url';
import { createRequire } from 'node:module';
import 'dotenv/config'
import { createClient } from '@supabase/supabase-js';
const require = createRequire(import.meta.url);
// This should run the build
cp.spawnSync('source ~/.zshrc; npm run build', { shell: 'zsh', stdio: 'inherit' });
const PluginManifest: { minSupportedCliVersion: string | null } = require('../dist/plugin-manifest.json');
const version = process.env.npm_package_version;
if (!version) {
throw new Error('Unable to find version');
}
const isBeta = version.includes('beta');
if (isBeta) {
console.log('Deploying beta version!')
}
const name = process.env.npm_package_name;
if (!name) {
throw new Error('Unable to find package name');
}
console.log(`Uploading plugin ${name}, version ${version} to cloudflare!`)
const outputFilePath = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..', 'dist', 'index.js')
cp.spawnSync(`source ~/.zshrc; npx wrangler r2 object put plugins/${name}/${version}/index.js --file=${outputFilePath} --remote`, { shell: 'zsh', stdio: 'inherit' });
const client = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
);
console.log('Adding default plugin');
const defaultPlugin = await client.from('registry_plugins').upsert({
name: 'default',
display_name: 'Default Plugin',
homepage: 'https://codifycli.com',
repository_url: 'https://github.com/codifycli/default-plugin',
license: 'ISC',
}, { onConflict: 'name' })
.select()
.throwOnError();
const { id: pluginId, name: pluginName } = defaultPlugin.data![0];
const CodifySchema = require('../dist/schemas.json');
console.log('Upserting plugin version');
const versionRow = await client.from('registry_plugin_versions').upsert({
plugin_id: pluginId,
version,
bundle_url: `https://plugins.codifycli.com/${name}/${version}/index.js`,
min_cli_version: PluginManifest.minSupportedCliVersion,
published_at: new Date().toISOString(),
json_schema: CodifySchema,
}, { onConflict: 'plugin_id,version' })
.select()
.throwOnError();
await uploadResources(isBeta);
if (!isBeta) {
// Build and deploy completions as well.
console.log('Deploying completions...')
cp.spawnSync('source ~/.zshrc; npm run deploy:completions' , { shell: 'zsh', stdio: 'inherit' })
}
async function uploadResources(prerelease: boolean) {
const Metadata: Array<Record<string, any>> = require('../dist/metadata.json');
const metadataByType = new Map(Metadata.map((m) => [m.type, m]));
const { id: versionId } = versionRow.data![0];
if (!prerelease) {
console.log('Updating latest version pointer');
await client.from('registry_plugins')
.update({ latest_version: version, latest_version_id: versionId })
.eq('id', pluginId)
.throwOnError();
}
const resources = CodifySchema.items.oneOf;
for (const resource of resources) {
const type = resource.properties.type.const;
const metadata = metadataByType.get(type);
console.log(`Adding resource ${type} (prerelease=${prerelease})`)
const resourceRow = await client.from('registry_resources').upsert({
type,
plugin_id: pluginId,
plugin_name: pluginName,
prerelease,
schema: JSON.stringify(resource),
documentation_url: resource.$comment,
allow_multiple: metadata?.allowMultiple ?? false,
os: metadata?.operatingSystems ?? [],
default_config: metadata?.defaultConfig ? JSON.stringify(metadata.defaultConfig) : null,
example_config_1: metadata?.exampleConfigs?.example1 ? JSON.stringify(metadata.exampleConfigs.example1) : null,
example_config_2: metadata?.exampleConfigs?.example2 ? JSON.stringify(metadata.exampleConfigs.example2) : null,
}, { onConflict: 'type,plugin_id,prerelease' })
.select()
.throwOnError();
const { id: resourceId } = resourceRow.data![0];
const sensitiveParams: string[] = metadata?.sensitiveParameters ?? [];
const allSensitive = sensitiveParams.includes('*');
const parameters = Object.entries(resource.properties)
.filter(([k]) => k !== 'type')
.map(([key, property]) => ({
type: (property as any).type,
name: key,
resource_id: resourceId,
prerelease,
schema: property,
is_sensitive: allSensitive || sensitiveParams.includes(key),
}))
await client.from('registry_resource_parameters')
.upsert(parameters, { onConflict: 'name,resource_id,prerelease' })
.throwOnError();
}
}