forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.js
More file actions
131 lines (108 loc) · 3.44 KB
/
Copy pathlink.js
File metadata and controls
131 lines (108 loc) · 3.44 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
const path = require('path')
const process = require('process')
const { flags: flagsLib } = require('@oclif/command')
const chalk = require('chalk')
const Command = require('../utils/command')
const { ensureNetlifyIgnore } = require('../utils/gitignore')
const linkPrompt = require('../utils/link/link-by-prompt')
const { track } = require('../utils/telemetry')
class LinkCommand extends Command {
async run() {
await this.authenticate()
const { flags } = this.parse(LinkCommand)
const { api, site, state } = this.netlify
const siteId = site.id
await this.config.runHook('analytics', {
eventName: 'command',
payload: {
command: 'link',
},
})
let siteData
try {
siteData = await api.getSite({ siteId })
} catch (error) {
// silent api error
}
// Add .netlify to .gitignore file
await ensureNetlifyIgnore(site.root)
// Site id is incorrect
if (siteId && !siteData) {
console.log(`"${siteId}" was not found in your Netlify account.`)
console.log(`Please double check your siteID and which account you are logged into via \`netlify status\`.`)
return this.exit()
}
// If already linked to site. exit and prompt for unlink
if (siteData) {
this.log(`Site already linked to "${siteData.name}"`)
this.log(`Admin url: ${siteData.admin_url}`)
this.log()
this.log(`To unlink this site, run: ${chalk.cyanBright('netlify unlink')}`)
return this.exit()
}
if (flags.id) {
try {
siteData = await api.getSite({ site_id: flags.id })
} catch (error) {
if (error.status === 404) {
this.error(new Error(`Site id ${flags.id} not found`))
} else {
this.error(error)
}
}
// Save site ID
state.set('siteId', siteData.id)
this.log(`Linked to ${siteData.name} in ${state.path}`)
await track('sites_linked', {
siteId: siteData.id,
linkType: 'manual',
kind: 'byId',
})
return this.exit()
}
if (flags.name) {
let results
try {
results = await api.listSites({
name: flags.name,
filter: 'all',
})
} catch (error) {
if (error.status === 404) {
this.error(new Error(`${flags.name} not found`))
} else {
this.error(error)
}
}
if (results.length === 0) {
this.error(new Error(`No sites found named ${flags.name}`))
}
const [firstSiteData] = results
state.set('siteId', firstSiteData.id)
this.log(`Linked to ${firstSiteData.name} in ${path.relative(path.join(process.cwd(), '..'), state.path)}`)
await track('sites_linked', {
siteId: (firstSiteData && firstSiteData.id) || siteId,
linkType: 'manual',
kind: 'byName',
})
return this.exit()
}
siteData = await linkPrompt(this, flags)
return siteData
}
}
LinkCommand.description = `Link a local repo or project folder to an existing site on Netlify`
LinkCommand.examples = ['netlify link', 'netlify link --id 123-123-123-123', 'netlify link --name my-site-name']
LinkCommand.flags = {
id: flagsLib.string({
description: 'ID of site to link to',
}),
name: flagsLib.string({
description: 'Name of site to link to',
}),
gitRemoteName: flagsLib.string({
description: 'Name of Git remote to use. e.g. "origin"',
}),
...LinkCommand.flags,
}
module.exports = LinkCommand