-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·153 lines (144 loc) · 4.86 KB
/
cli.js
File metadata and controls
executable file
·153 lines (144 loc) · 4.86 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
#!/usr/bin/env node
import path from 'path'
import dotenv from 'dotenv'
import fs from 'fs'
import sade from 'sade'
import { fileURLToPath } from 'url'
import { build } from 'esbuild'
import Sentry from '@sentry/cli'
import { createRequire } from 'module'
// @ts-ignore
import git from 'git-rev-sync'
import { servicesExecCmd, servicesPullCmd } from './cmds/services.js'
import { dbSqlCmd } from './cmds/db-sql.js'
import { dbTypesCmd } from './cmds/db-types.js'
import { minioBucketCreateCmd, minioBucketRemoveCmd } from './cmds/minio.js'
import { runTestSuiteCmd } from './cmds/run-test.js'
import { runDevServerCmd } from './cmds/run-dev.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const require = createRequire(__dirname)
const prog = sade('api')
dotenv.config({
path: path.join(__dirname, '..', '..', '..', '.env'),
})
const pkg = JSON.parse(
fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8')
)
/** @type {import('esbuild').Plugin} */
const PluginAlias = {
name: 'alias',
setup(build) {
build.onResolve({ filter: /^stream$/ }, () => {
return { path: require.resolve('readable-stream') }
})
build.onResolve({ filter: /^node-fetch$/ }, () => {
return { path: path.resolve(__dirname, 'fetch.js') }
})
build.onResolve({ filter: /^cross-fetch$/ }, () => {
return { path: path.resolve(__dirname, 'fetch.js') }
})
},
}
prog
.command('build')
.describe('Build the worker.')
.option('--env', 'Environment', 'dev')
.action(async (opts) => {
try {
const version = `${pkg.name}@${pkg.version}-${opts.env}+${git.short(
__dirname
)}`
const commit = git.long(__dirname)
const branch = git.branch(__dirname)
await build({
entryPoints: [path.join(__dirname, '../src/index.js')],
bundle: true,
outfile: 'dist/worker.js',
legalComments: 'external',
inject: [path.join(__dirname, 'node-globals.js')],
plugins: [PluginAlias],
define: {
NFT_STORAGE_VERSION: JSON.stringify(version),
NFT_STORAGE_COMMITHASH: JSON.stringify(commit),
NFT_STORAGE_BRANCH: JSON.stringify(branch),
global: 'globalThis',
},
minify: opts.env === 'dev' || opts.env === 'test' ? false : true,
sourcemap: true,
})
// Sentry release and sourcemap upload
if (process.env.SENTRY_UPLOAD === 'true') {
const cli = new Sentry(undefined, {
authToken: process.env.SENTRY_TOKEN,
org: 'protocol-labs-it',
project: 'api',
dist: git.short(__dirname),
})
await cli.releases.new(version)
await cli.releases.setCommits(version, {
auto: true,
ignoreEmpty: true,
ignoreMissing: true,
})
await cli.releases.uploadSourceMaps(version, {
include: ['./dist'],
urlPrefix: '/',
})
await cli.releases.finalize(version)
await cli.releases.newDeploy(version, {
env: opts.env,
})
}
} catch (err) {
console.error(err)
process.exit(1)
}
})
.command('services exec [command]')
.describe(
'Run docker compose to setup Cluster, PostgreSQL, PostgREST and Minio. Executes your command while services are running, then tears down the docker env.'
)
.option('--persistent', 'Whether to enable persistent data volumes', false)
.action(servicesExecCmd)
.command('services pull')
.describe('pull and build all docker images used for dev/test')
.action(servicesPullCmd)
.command('db-sql')
.describe('Database scripts')
.option('--reset', 'Reset db before running SQL.', false)
.option('--cargo', 'Import cargo data.', false)
.option('--testing', 'Tweak schema for testing.', false)
.action(dbSqlCmd)
.command('db-types')
.describe('Database openapi types')
.action(dbTypesCmd)
.command('minio bucket create <name>')
.describe('Create a new bucket')
.action(minioBucketCreateCmd)
.command('minio bucket remove <name>')
.describe('Remove a bucket, automatically removing all contents')
.action(minioBucketRemoveCmd)
.command('run test')
.describe(
'Run the test suite. Any unrecognized positional args will be passed on to the test runner.'
)
.option(
'--services',
'Run service dependencies using docker compose and cleanup after the dev server exits. Set to false if running in a custom environment',
true
)
.action(runTestSuiteCmd)
.command('run dev')
.describe('Run the development API server using miniflare')
.option(
'--services',
'Run service dependencies using docker compose and cleanup after the dev server exits. Set to false if running in a custom environment',
true
)
.option(
'--persistent',
'Whether to enable persistent data volumes. Only has an effect when --services=true',
false
)
.action(runDevServerCmd)
prog.parse(process.argv)