/
lm-setup.mjs
120 lines (105 loc) · 3.17 KB
/
lm-setup.mjs
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
// @ts-check
import inquirer from 'inquirer'
import Listr from 'listr'
import { error, exit } from '../../utils/command-helpers.mjs'
import execa from '../../utils/execa.mjs'
import { installPlatform } from '../../utils/lm/install.mjs'
import { checkHelperVersion } from '../../utils/lm/requirements.mjs'
import { printBanner } from '../../utils/lm/ui.mjs'
const installHelperIfMissing = async function ({ force }) {
let installHelper = false
try {
const version = await checkHelperVersion()
if (!version) {
installHelper = true
}
} catch {
installHelper = true
}
if (installHelper) {
return installPlatform({ force })
}
return false
}
const provisionService = async function (siteId, api) {
const addonName = 'large-media'
if (!siteId) {
throw new Error('No site id found, please run inside a site folder or `netlify link`')
}
try {
await api.createServiceInstance({
siteId,
addon: addonName,
body: {},
})
} catch (error_) {
// error is JSONHTTPError
throw new Error(error_.json.error)
}
}
const configureLFSURL = async function (siteId, api) {
const siteInfo = await api.getSite({ siteId })
const url = `https://${siteInfo.id_domain}/.netlify/large-media`
return execa('git', ['config', '-f', '.lfsconfig', 'lfs.url', url])
}
/**
* The lm:setup command
* @param {import('commander').OptionValues} options
* @param {import('../base-command.mjs').default} command
*/
const lmSetup = async (options, command) => {
if (!options.force) {
const { wantsToProceed } = await inquirer.prompt({
type: 'confirm',
name: 'wantsToProceed',
message:
'Large media is a deprecated feature and will be removed in a future version. Are you sure you want to continue? (to skip this prompt, pass a --force flag)',
default: false,
})
if (!wantsToProceed) {
exit()
}
}
await command.authenticate()
const { api, site } = command.netlify
let helperInstalled = false
if (!options.skipInstall) {
try {
helperInstalled = await installHelperIfMissing({ force: options.forceInstall })
} catch (error_) {
error(error_)
}
}
const tasks = new Listr([
{
title: 'Provisioning Netlify Large Media',
async task() {
await provisionService(site.id, api)
},
},
{
title: 'Configuring Git LFS for this site',
async task() {
await configureLFSURL(site.id, api)
},
},
])
await tasks.run().catch(() => {})
if (helperInstalled) {
printBanner(options.forceInstall)
}
}
/**
* Creates the `netlify lm:setup` command
* @param {import('../base-command.mjs').default} program
* @returns
*/
export const createLmSetupCommand = (program) =>
program
.command('lm:setup', { hidden: true })
.description('Configures your site to use Netlify Large Media')
.option('-s, --skip-install', 'Skip the credentials helper installation check')
.option('-f, --force-install', 'Force the credentials helper installation')
.option('--force', 'Skip deprecation check')
.addHelpText('after', 'It runs the install command if you have not installed the dependencies yet.')
.action(lmSetup)