netlify-cli 9.10.0 → 9.11.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,12 +1,12 @@
1
1
  {
2
2
  "name": "netlify-cli",
3
- "version": "9.10.0",
3
+ "version": "9.11.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "netlify-cli",
9
- "version": "9.10.0",
9
+ "version": "9.11.0",
10
10
  "hasInstallScript": true,
11
11
  "license": "MIT",
12
12
  "dependencies": {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "netlify-cli",
3
3
  "description": "Netlify command line tool",
4
- "version": "9.10.0",
4
+ "version": "9.11.0",
5
5
  "author": "Netlify Inc.",
6
6
  "contributors": [
7
7
  "Abraham Schilling <AbrahamSchilling@gmail.com> (https://gitlab.com/n4bb12)",
@@ -2,12 +2,14 @@
2
2
 
3
3
  const inquirer = require('inquirer')
4
4
  const pick = require('lodash/pick')
5
+ const parseGitHubUrl = require('parse-github-url')
5
6
  const prettyjson = require('prettyjson')
7
+ const terminalLink = require('terminal-link')
6
8
 
7
9
  const { chalk, error, getRepoData, log, logJson, track, warn } = require('../../utils')
8
10
  const { configureRepo } = require('../../utils/init/config')
9
11
  const { getGitHubToken } = require('../../utils/init/config-github')
10
- const { createRepo, getTemplatesFromGitHub } = require('../../utils/sites/utils')
12
+ const { createRepo, getTemplatesFromGitHub, validateTemplate } = require('../../utils/sites/utils')
11
13
 
12
14
  const { getSiteNameInput } = require('./sites-create')
13
15
 
@@ -23,12 +25,45 @@ const fetchTemplates = async (token) => {
23
25
  }))
24
26
  }
25
27
 
28
+ const getTemplateName = async ({ ghToken, options, repository }) => {
29
+ if (repository) {
30
+ const { repo } = parseGitHubUrl(repository)
31
+ return repo || `netlify-templates/${repository}`
32
+ }
33
+
34
+ if (options.url) {
35
+ const urlFromOptions = new URL(options.url)
36
+ return urlFromOptions.pathname.slice(1)
37
+ }
38
+
39
+ const templates = await fetchTemplates(ghToken)
40
+
41
+ log(`Choose one of our starter templates. Netlify will create a new repo for this template in your GitHub account.`)
42
+
43
+ const { templateName } = await inquirer.prompt([
44
+ {
45
+ type: 'list',
46
+ name: 'templateName',
47
+ message: 'Template:',
48
+ choices: templates.map((template) => ({
49
+ value: template.slug,
50
+ name: template.name,
51
+ })),
52
+ },
53
+ ])
54
+
55
+ return templateName
56
+ }
57
+
58
+ const getGitHubLink = ({ options, templateName }) => options.url || `https://github.com/${templateName}`
59
+
26
60
  /**
27
61
  * The sites:create-template command
62
+ * @param repository {string}
28
63
  * @param {import('commander').OptionValues} options
29
64
  * @param {import('../base-command').BaseCommand} command
30
65
  */
31
- const sitesCreateTemplate = async (options, command) => {
66
+ const sitesCreateTemplate = async (repository, options, command) => {
32
67
  const { api } = command.netlify
33
68
 
34
69
  await command.authenticate()
@@ -36,27 +71,22 @@ const sitesCreateTemplate = async (options, command) => {
36
71
  const { globalConfig } = command.netlify
37
72
  const ghToken = await getGitHubToken({ globalConfig })
38
73
 
39
- let { url: templateUrl } = options
40
-
41
- if (templateUrl) {
42
- const urlFromOptions = new URL(templateUrl)
43
- templateUrl = { templateName: urlFromOptions.pathname.slice(1) }
44
- } else {
45
- const templates = await fetchTemplates(ghToken)
46
-
47
- log(`Choose one of our starter templates. Netlify will create a new repo for this template in your GitHub account.`)
48
-
49
- templateUrl = await inquirer.prompt([
50
- {
51
- type: 'list',
52
- name: 'templateName',
53
- message: 'Template:',
54
- choices: templates.map((template) => ({
55
- value: template.slug,
56
- name: template.name,
57
- })),
58
- },
59
- ])
74
+ const templateName = await getTemplateName({ ghToken, options, repository })
75
+ const { exists, isTemplate } = await validateTemplate({ templateName, ghToken })
76
+ if (!exists) {
77
+ const githubLink = getGitHubLink({ options, templateName })
78
+ error(
79
+ `Could not find template ${chalk.bold(templateName)}. Please verify it exists and you can ${terminalLink(
80
+ 'access to it on GitHub',
81
+ githubLink,
82
+ )}`,
83
+ )
84
+ return
85
+ }
86
+ if (!isTemplate) {
87
+ const githubLink = getGitHubLink({ options, templateName })
88
+ error(`${terminalLink(chalk.bold(templateName), githubLink)} is not a valid GitHub template`)
89
+ return
60
90
  }
61
91
 
62
92
  const accounts = await api.listAccountsForUser()
@@ -90,12 +120,12 @@ const sitesCreateTemplate = async (options, command) => {
90
120
  const siteName = inputName ? inputName.trim() : siteSuggestion
91
121
 
92
122
  // Create new repo from template
93
- const repoResp = await createRepo(templateUrl, ghToken, siteName)
123
+ const repoResp = await createRepo(templateName, ghToken, siteName)
94
124
 
95
125
  if (repoResp.errors) {
96
126
  if (repoResp.errors[0].includes('Name already exists on this account')) {
97
127
  warn(
98
- `Oh no! We found already a repository with this name. It seems you have already created a template with the name ${templateUrl.templateName}. Please try to run the command again and provide a different name.`,
128
+ `Oh no! We found already a repository with this name. It seems you have already created a template with the name ${templateName}. Please try to run the command again and provide a different name.`,
99
129
  )
100
130
  await inputSiteName()
101
131
  } else {
@@ -206,7 +236,13 @@ Create a site from a starter template.`,
206
236
  .option('-u, --url [url]', 'template url')
207
237
  .option('-a, --account-slug [slug]', 'account slug to create the site under')
208
238
  .option('-c, --with-ci', 'initialize CI hooks during site creation')
239
+ .argument('[repository]', 'repository to use as starter template')
209
240
  .addHelpText('after', `(Beta) Create a site from starter template.`)
241
+ .addExamples([
242
+ 'netlify sites:create-template',
243
+ 'netlify sites:create-template nextjs-blog-theme',
244
+ 'netlify sites:create-template my-github-profile/my-template',
245
+ ])
210
246
  .action(sitesCreateTemplate)
211
247
 
212
248
  module.exports = { createSitesFromTemplateCommand, fetchTemplates }
@@ -120,7 +120,8 @@ const createHandler = function (options) {
120
120
  if (func.isBackground) {
121
121
  handleBackgroundFunction(functionName, response)
122
122
 
123
- const { error } = await func.invoke(event, clientContext)
123
+ // background functions do not receive a clientContext
124
+ const { error } = await func.invoke(event)
124
125
 
125
126
  handleBackgroundFunctionResult(functionName, error)
126
127
  } else if (await func.isScheduled()) {
@@ -12,8 +12,27 @@ const getTemplatesFromGitHub = async (token) => {
12
12
  return allTemplates
13
13
  }
14
14
 
15
- const createRepo = async (templateUrl, ghToken, siteName) => {
16
- const resp = await fetch(`https://api.github.com/repos/${templateUrl.templateName}/generate`, {
15
+ const validateTemplate = async ({ ghToken, templateName }) => {
16
+ const response = await fetch(`https://api.github.com/repos/${templateName}`, {
17
+ headers: {
18
+ Authorization: `token ${ghToken}`,
19
+ },
20
+ })
21
+
22
+ if (response.status === 404) {
23
+ return { exists: false }
24
+ }
25
+
26
+ if (!response.ok) {
27
+ throw new Error(`Error fetching template ${templateName}: ${await response.text()}`)
28
+ }
29
+
30
+ const data = await response.json()
31
+ return { exists: true, isTemplate: data.is_template }
32
+ }
33
+
34
+ const createRepo = async (templateName, ghToken, siteName) => {
35
+ const resp = await fetch(`https://api.github.com/repos/${templateName}/generate`, {
17
36
  method: 'POST',
18
37
  headers: {
19
38
  Authorization: `token ${ghToken}`,
@@ -27,4 +46,4 @@ const createRepo = async (templateUrl, ghToken, siteName) => {
27
46
  return data
28
47
  }
29
48
 
30
- module.exports = { getTemplatesFromGitHub, createRepo }
49
+ module.exports = { getTemplatesFromGitHub, createRepo, validateTemplate }