netlify-cli 14.4.0 → 15.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,170 +0,0 @@
1
- // @ts-check
2
- import events from 'events'
3
- import process from 'process'
4
-
5
- import {
6
- OneGraphCliClient,
7
- loadCLISession,
8
- markCliSessionInactive,
9
- persistNewOperationsDocForSession,
10
- startOneGraphCLISession,
11
- } from '../lib/one-graph/cli-client.mjs'
12
- import {
13
- defaultExampleOperationsDoc,
14
- getGraphEditUrlBySiteId,
15
- getNetlifyGraphConfig,
16
- readGraphQLOperationsSourceFile,
17
- } from '../lib/one-graph/cli-netlify-graph.mjs'
18
-
19
- import { chalk, error, getToken, log, normalizeConfig, warn, watchDebounced } from './command-helpers.mjs'
20
- import { generateNetlifyGraphJWT, processOnExit } from './dev.mjs'
21
- import { addCleanupJob } from './shell.mjs'
22
-
23
- export const startPollingForAPIAuthentication = async function (options) {
24
- const { api, command, config, site, siteInfo } = options
25
- const frequency = 5000
26
-
27
- const helper = async (maybeSiteData) => {
28
- const siteData = await (maybeSiteData || api.getSite({ siteId: site.id }))
29
- const authlifyTokenId = siteData && siteData.authlify_token_id
30
-
31
- const existingAuthlifyTokenId = config && config.netlifyGraphConfig && config.netlifyGraphConfig.authlifyTokenId
32
- if (authlifyTokenId && authlifyTokenId !== existingAuthlifyTokenId) {
33
- const netlifyToken = await command.authenticate()
34
- // Only inject the authlify config if a token ID exists. This prevents
35
- // calling command.authenticate() (which opens a browser window) if the
36
- // user hasn't enabled API Authentication
37
- const netlifyGraphConfig = {
38
- netlifyToken,
39
- authlifyTokenId: siteData.authlify_token_id,
40
- siteId: site.id,
41
- }
42
- config.netlifyGraphConfig = netlifyGraphConfig
43
-
44
- const netlifyGraphJWT = generateNetlifyGraphJWT(netlifyGraphConfig)
45
-
46
- if (netlifyGraphJWT != null) {
47
- // XXX(anmonteiro): this name is deprecated. Delete after 3/31/2022
48
- process.env.ONEGRAPH_AUTHLIFY_TOKEN = netlifyGraphJWT
49
- process.env.NETLIFY_GRAPH_TOKEN = netlifyGraphJWT
50
- }
51
- } else if (!authlifyTokenId) {
52
- // If there's no `authlifyTokenId`, it's because the user disabled API
53
- // Auth. Delete the config in this case.
54
- delete config.netlifyGraphConfig
55
- }
56
-
57
- setTimeout(helper, frequency)
58
- }
59
-
60
- await helper(siteInfo)
61
- }
62
-
63
- export const startNetlifyGraph = async ({
64
- command,
65
- config,
66
- options,
67
- settings,
68
- site,
69
- startNetlifyGraphWatcher,
70
- state,
71
- }) => {
72
- if (startNetlifyGraphWatcher && options.offline) {
73
- warn(`Unable to start Netlify Graph in offline mode`)
74
- } else if (startNetlifyGraphWatcher && !site.id) {
75
- error(
76
- `No siteId defined, unable to start Netlify Graph. To enable, run ${chalk.yellow(
77
- 'netlify init',
78
- )} or ${chalk.yellow('netlify link')}.`,
79
- )
80
- } else if (startNetlifyGraphWatcher) {
81
- const netlifyToken = await command.authenticate()
82
- await OneGraphCliClient.ensureAppForSite(netlifyToken, site.id)
83
-
84
- let stopWatchingCLISessions
85
-
86
- let liveConfig = { ...config }
87
- let isRestartingSession = false
88
-
89
- const createOrResumeSession = async function () {
90
- const netlifyGraphConfig = await getNetlifyGraphConfig({ command, options, settings })
91
-
92
- let graphqlDocument = readGraphQLOperationsSourceFile(netlifyGraphConfig)
93
-
94
- if (!graphqlDocument || graphqlDocument.trim().length === 0) {
95
- graphqlDocument = defaultExampleOperationsDoc
96
- }
97
-
98
- stopWatchingCLISessions = await startOneGraphCLISession({
99
- config: liveConfig,
100
- netlifyGraphConfig,
101
- netlifyToken,
102
- site,
103
- state,
104
- oneGraphSessionId: options.sessionId,
105
- })
106
-
107
- // Should be created by startOneGraphCLISession
108
- const oneGraphSessionId = loadCLISession(state)
109
-
110
- await persistNewOperationsDocForSession({
111
- config: liveConfig,
112
- netlifyGraphConfig,
113
- netlifyToken,
114
- oneGraphSessionId,
115
- operationsDoc: graphqlDocument,
116
- siteId: site.id,
117
- siteRoot: site.root,
118
- })
119
-
120
- return oneGraphSessionId
121
- }
122
-
123
- const configWatcher = new events.EventEmitter()
124
-
125
- // Only set up a watcher if we know the config path.
126
- const { configPath } = command.netlify.site
127
- if (configPath) {
128
- // chokidar handle
129
- command.configWatcherHandle = await watchDebounced(configPath, {
130
- depth: 1,
131
- onChange: async () => {
132
- const cwd = options.cwd || process.cwd()
133
- const [token] = await getToken(options.auth)
134
- const { config: newConfig } = await command.getConfig({ cwd, state, token, ...command.netlify.apiUrlOpts })
135
-
136
- const normalizedNewConfig = normalizeConfig(newConfig)
137
- configWatcher.emit('change', normalizedNewConfig)
138
- },
139
- })
140
-
141
- processOnExit(async () => {
142
- await command.configWatcherHandle.close()
143
- })
144
- }
145
-
146
- // Set up a handler for config changes.
147
- configWatcher.on('change', async (newConfig) => {
148
- command.netlify.config = newConfig
149
- liveConfig = newConfig
150
- if (isRestartingSession) {
151
- return
152
- }
153
- stopWatchingCLISessions && stopWatchingCLISessions()
154
- isRestartingSession = true
155
- await createOrResumeSession()
156
- isRestartingSession = false
157
- })
158
-
159
- const oneGraphSessionId = await createOrResumeSession()
160
- const cleanupSession = () => markCliSessionInactive({ netlifyToken, sessionId: oneGraphSessionId, siteId: site.id })
161
-
162
- addCleanupJob(cleanupSession)
163
-
164
- const graphEditUrl = getGraphEditUrlBySiteId({ siteId: site.id, oneGraphSessionId })
165
-
166
- log(
167
- `Starting Netlify Graph session, to edit your library visit ${graphEditUrl} or run \`netlify graph:edit\` in another tab`,
168
- )
169
- }
170
- }