-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathcli.js
executable file
·146 lines (136 loc) · 4.47 KB
/
cli.js
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
#!/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 {
servicesStartCmd,
servicesStopCmd,
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 { requestW3upConsoleUcan } from './cmds/w3up-console-ucan-request.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 start')
.describe(
'Run docker compose to setup Cluster, PostgreSQL, PostgREST and Minio'
)
.option('--project', 'Project name', 'nft-storage-dev')
.action(servicesStartCmd)
.command('services stop')
.describe(
'Run docker compose to setup Cluster, PostgreSQL, PostgREST and Minio'
)
.option('--project', 'Project name', 'nft-storage-dev')
.option('--clean', 'Clean all dockers artifacts', false)
.action(servicesStopCmd)
.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('w3up console ucan generate')
.describe(
'request info to build and output a UCAN that can be imported into console.web3.storage to browse data nft.storage stores with w3up'
)
.action(requestW3upConsoleUcan)
prog.parse(process.argv)