diff --git a/examples/gatsby/package.json b/examples/gatsby/package.json index 88e967134..cd400095e 100644 --- a/examples/gatsby/package.json +++ b/examples/gatsby/package.json @@ -1,6 +1,6 @@ { "private": true, - "name": "gatsby-starter-default", + "name": "gatsby", "version": "1.0.0", "dependencies": { "gatsby": "^2.18.14", diff --git a/examples/gridsome/package.json b/examples/gridsome/package.json index dfb636089..9d56cc041 100644 --- a/examples/gridsome/package.json +++ b/examples/gridsome/package.json @@ -1,5 +1,5 @@ { - "name": "gridsomee", + "name": "gridsome", "private": true, "scripts": { "build": "gridsome build", diff --git a/packages/now-build-utils/test/fixtures/05-zero-config-gatsby/gatsby-config.js b/packages/now-build-utils/test/fixtures/05-zero-config-gatsby/gatsby-config.js index 999bd3d34..157a0011f 100644 --- a/packages/now-build-utils/test/fixtures/05-zero-config-gatsby/gatsby-config.js +++ b/packages/now-build-utils/test/fixtures/05-zero-config-gatsby/gatsby-config.js @@ -18,7 +18,7 @@ module.exports = { { resolve: `gatsby-plugin-manifest`, options: { - name: `gatsby-starter-default`, + name: `05-zero-config-gatsby`, short_name: `starter`, start_url: `/`, background_color: `#663399`, diff --git a/packages/now-build-utils/test/fixtures/05-zero-config-gatsby/package.json b/packages/now-build-utils/test/fixtures/05-zero-config-gatsby/package.json index 7a92eca73..860baea7b 100644 --- a/packages/now-build-utils/test/fixtures/05-zero-config-gatsby/package.json +++ b/packages/now-build-utils/test/fixtures/05-zero-config-gatsby/package.json @@ -1,5 +1,5 @@ { - "name": "gatsby-starter-default", + "name": "05-zero-config-gatsby", "private": true, "description": "A simple starter to get up and developing quickly with Gatsby", "version": "0.1.0", diff --git a/packages/now-cli/test/dev/fixtures/05-gatsby/gatsby-config.js b/packages/now-cli/test/dev/fixtures/05-gatsby/gatsby-config.js index 999bd3d34..d26af35a7 100644 --- a/packages/now-cli/test/dev/fixtures/05-gatsby/gatsby-config.js +++ b/packages/now-cli/test/dev/fixtures/05-gatsby/gatsby-config.js @@ -18,7 +18,7 @@ module.exports = { { resolve: `gatsby-plugin-manifest`, options: { - name: `gatsby-starter-default`, + name: `05-gatsby`, short_name: `starter`, start_url: `/`, background_color: `#663399`, diff --git a/packages/now-cli/test/dev/fixtures/05-gatsby/package.json b/packages/now-cli/test/dev/fixtures/05-gatsby/package.json index c5113c7b5..6dfd73820 100644 --- a/packages/now-cli/test/dev/fixtures/05-gatsby/package.json +++ b/packages/now-cli/test/dev/fixtures/05-gatsby/package.json @@ -1,5 +1,5 @@ { - "name": "gatsby-starter-default", + "name": "05-gatsby", "private": true, "description": "A simple starter to get up and developing quickly with Gatsby", "version": "0.1.0", diff --git a/packages/now-cli/test/dev/fixtures/08-hugo/themes/ananke/package.json b/packages/now-cli/test/dev/fixtures/08-hugo/themes/ananke/package.json index 3a5d48818..12600ae01 100644 --- a/packages/now-cli/test/dev/fixtures/08-hugo/themes/ananke/package.json +++ b/packages/now-cli/test/dev/fixtures/08-hugo/themes/ananke/package.json @@ -1,8 +1,8 @@ { - "name": "gohugo-default-theme", + "name": "08-gohugo-default-theme", "version": "1.0.0", "description": "Base Theme to start Hugo Sites", - "main": "index.js", + "main": "index.js", "repository": { "type": "git", "url": "git+https://github.com/theNewDynamic/thenewdynamic.com.git" diff --git a/packages/now-cli/test/integration.js b/packages/now-cli/test/integration.js index aef7400dc..de12afa37 100644 --- a/packages/now-cli/test/integration.js +++ b/packages/now-cli/test/integration.js @@ -20,6 +20,7 @@ import logo from '../src/util/output/logo'; import sleep from '../src/util/sleep'; import pkg from '../package'; import prepareFixtures from './helpers/prepare'; +import { fetchTokenWithRetry } from '../../../test/lib/deployment/now-deploy'; // log command when running `execa` function execa(file, args, options) { @@ -70,25 +71,6 @@ const waitForDeployment = async href => { } }; -function fetchTokenWithRetry(url, retries = 3) { - return retry( - async () => { - const res = await fetch(url); - - if (!res.ok) { - throw new Error( - `Failed to fetch ${url}, received status ${res.status}` - ); - } - - const data = await res.json(); - - return data.token; - }, - { retries, factor: 1 } - ); -} - function fetchTokenInformation(token, retries = 3) { const url = `https://api.zeit.co/www/user`; const headers = { Authorization: `Bearer ${token}` }; diff --git a/packages/now-static-build/test/fixtures/10-gatsby-without-build-script/gatsby-config.js b/packages/now-static-build/test/fixtures/10-gatsby-without-build-script/gatsby-config.js index 999bd3d34..0fd0cbfdc 100644 --- a/packages/now-static-build/test/fixtures/10-gatsby-without-build-script/gatsby-config.js +++ b/packages/now-static-build/test/fixtures/10-gatsby-without-build-script/gatsby-config.js @@ -18,7 +18,7 @@ module.exports = { { resolve: `gatsby-plugin-manifest`, options: { - name: `gatsby-starter-default`, + name: `10-gatsby-without-build-script`, short_name: `starter`, start_url: `/`, background_color: `#663399`, diff --git a/packages/now-static-build/test/fixtures/10-gatsby-without-build-script/package.json b/packages/now-static-build/test/fixtures/10-gatsby-without-build-script/package.json index b605e5fbf..8caa32086 100644 --- a/packages/now-static-build/test/fixtures/10-gatsby-without-build-script/package.json +++ b/packages/now-static-build/test/fixtures/10-gatsby-without-build-script/package.json @@ -1,5 +1,5 @@ { - "name": "gatsby-starter-default", + "name": "10-gatsby-without-build-script", "private": true, "description": "A simple starter to get up and developing quickly with Gatsby", "version": "0.1.0", diff --git a/packages/now-static-build/test/fixtures/10-gatsby/gatsby-config.js b/packages/now-static-build/test/fixtures/10-gatsby/gatsby-config.js index 999bd3d34..9444ecc52 100644 --- a/packages/now-static-build/test/fixtures/10-gatsby/gatsby-config.js +++ b/packages/now-static-build/test/fixtures/10-gatsby/gatsby-config.js @@ -18,7 +18,7 @@ module.exports = { { resolve: `gatsby-plugin-manifest`, options: { - name: `gatsby-starter-default`, + name: `10-gatsby`, short_name: `starter`, start_url: `/`, background_color: `#663399`, diff --git a/packages/now-static-build/test/fixtures/10-gatsby/package.json b/packages/now-static-build/test/fixtures/10-gatsby/package.json index dc4fb60e8..8e1277a4a 100644 --- a/packages/now-static-build/test/fixtures/10-gatsby/package.json +++ b/packages/now-static-build/test/fixtures/10-gatsby/package.json @@ -1,5 +1,5 @@ { - "name": "gatsby-starter-default", + "name": "10-gatsby", "private": true, "description": "A simple starter to get up and developing quickly with Gatsby", "version": "0.1.0", diff --git a/packages/now-static-build/test/fixtures/26-ejected-cra/package.json b/packages/now-static-build/test/fixtures/26-ejected-cra/package.json index 6c26f2207..3b8472b7c 100644 --- a/packages/now-static-build/test/fixtures/26-ejected-cra/package.json +++ b/packages/now-static-build/test/fixtures/26-ejected-cra/package.json @@ -1,5 +1,5 @@ { - "name": "12-create-react-app", + "name": "26-ejected-cra", "version": "0.1.0", "private": true, "dependencies": { diff --git a/packages/now-static-build/test/fixtures/31-hugo/themes/ananke/package.json b/packages/now-static-build/test/fixtures/31-hugo/themes/ananke/package.json index 3a5d48818..9ea71c1d9 100755 --- a/packages/now-static-build/test/fixtures/31-hugo/themes/ananke/package.json +++ b/packages/now-static-build/test/fixtures/31-hugo/themes/ananke/package.json @@ -1,8 +1,8 @@ { - "name": "gohugo-default-theme", + "name": "31-gohugo-default-theme", "version": "1.0.0", "description": "Base Theme to start Hugo Sites", - "main": "index.js", + "main": "index.js", "repository": { "type": "git", "url": "git+https://github.com/theNewDynamic/thenewdynamic.com.git" diff --git a/packages/now-static-build/test/fixtures/46-hugo-with-framework/themes/ananke/package.json b/packages/now-static-build/test/fixtures/46-hugo-with-framework/themes/ananke/package.json index 19f5b0f07..d2240cdc8 100755 --- a/packages/now-static-build/test/fixtures/46-hugo-with-framework/themes/ananke/package.json +++ b/packages/now-static-build/test/fixtures/46-hugo-with-framework/themes/ananke/package.json @@ -1,5 +1,5 @@ { - "name": "gohugo-default-theme", + "name": "46-gohugo-default-theme", "version": "2.5.5", "description": "Base Theme to start Hugo Sites", "main": "index.js", diff --git a/packages/now-static-build/test/fixtures/48-nuxt-without-framework/package.json b/packages/now-static-build/test/fixtures/48-nuxt-without-framework/package.json index 8a6addc2f..d38d75b38 100644 --- a/packages/now-static-build/test/fixtures/48-nuxt-without-framework/package.json +++ b/packages/now-static-build/test/fixtures/48-nuxt-without-framework/package.json @@ -1,5 +1,5 @@ { - "name": "47-nuxt-with-custom-output", + "name": "48-nuxt-without-framework", "version": "1.0.0", "main": "pages/index.js", "scripts": { diff --git a/test/lib/deployment/now-deploy.js b/test/lib/deployment/now-deploy.js index a2d611e33..f34a382ee 100644 --- a/test/lib/deployment/now-deploy.js +++ b/test/lib/deployment/now-deploy.js @@ -1,14 +1,16 @@ const assert = require('assert'); const { createHash } = require('crypto'); const path = require('path'); +const _fetch = require('node-fetch'); const fetch = require('./fetch-retry.js'); +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); const str = 'aHR0cHM6Ly9hcGktdG9rZW4tZmFjdG9yeS56ZWl0LnNo'; -async function nowDeploy (bodies, randomness) { +async function nowDeploy(bodies, randomness) { const files = Object.keys(bodies) - .filter((n) => n !== 'now.json') - .map((n) => ({ + .filter(n => n !== 'now.json') + .map(n => ({ sha: digestOfFile(bodies[n]), size: bodies[n].length, file: n, @@ -35,7 +37,7 @@ async function nowDeploy (bodies, randomness) { }; if (process.env.FORCE_BUILD_IN_REGION) { - const { builds=[] } = nowDeployPayload; + const { builds = [] } = nowDeployPayload; builds.forEach(b => { if (!b.config) { b.config = {}; @@ -55,7 +57,8 @@ async function nowDeploy (bodies, randomness) { { const json = await deploymentPost(nowDeployPayload); - if (json.error && json.error.code === 'missing_files') throw new Error('Missing files'); + if (json.error && json.error.code === 'missing_files') + throw new Error('Missing files'); deploymentId = json.id; deploymentUrl = json.url; } @@ -65,21 +68,22 @@ async function nowDeploy (bodies, randomness) { for (let i = 0; i < 750; i += 1) { const { state } = await deploymentGet(deploymentId); - if (state === 'ERROR') throw new Error(`State of ${deploymentUrl} is ${state}`); + if (state === 'ERROR') + throw new Error(`State of ${deploymentUrl} is ${state}`); if (state === 'READY') break; - await new Promise((r) => setTimeout(r, 1000)); + await new Promise(r => setTimeout(r, 1000)); } return { deploymentId, deploymentUrl }; } -function digestOfFile (body) { +function digestOfFile(body) { return createHash('sha1') .update(body) .digest('hex'); } -async function filePost (body, digest) { +async function filePost(body, digest) { assert(Buffer.isBuffer(body)); const headers = { @@ -102,13 +106,13 @@ async function filePost (body, digest) { if (json.error) { const { status, statusText, headers } = resp; const { message } = json.error; - console.log('Fetch Error', { url , status, statusText, headers, digest }); + console.log('Fetch Error', { url, status, statusText, headers, digest }); throw new Error(message); } return json; } -async function deploymentPost (payload) { +async function deploymentPost(payload) { const url = '/v6/now/deployments?forceNew=1'; const resp = await fetchWithAuth(url, { method: 'POST', @@ -120,30 +124,30 @@ async function deploymentPost (payload) { if (json.error) { const { status, statusText, headers } = resp; const { message } = json.error; - console.log('Fetch Error', { url , status, statusText, headers }); + console.log('Fetch Error', { url, status, statusText, headers }); throw new Error(message); } return json; } -async function deploymentGet (deploymentId) { +async function deploymentGet(deploymentId) { const url = `/v3/now/deployments/${deploymentId}`; const resp = await fetchWithAuth(url); const json = await resp.json(); if (json.error) { const { status, statusText, headers } = resp; const { message } = json.error; - console.log('Fetch Error', { url , status, statusText, headers }); + console.log('Fetch Error', { url, status, statusText, headers }); throw new Error(message); } - return json + return json; } let token; let currentCount = 0; const MAX_COUNT = 10; -async function fetchWithAuth (url, opts = {}) { +async function fetchWithAuth(url, opts = {}) { if (!opts.headers) opts.headers = {}; if (!opts.headers.Authorization) { @@ -166,28 +170,33 @@ async function fetchWithAuth (url, opts = {}) { return await fetchApi(url, opts); } -function fetchTokenWithRetry (url, retries = 3) { - return new Promise(async (resolve, reject) => { - try { - const res = await fetch(url); - const data = await res.json(); - resolve(data.token); - } catch (error) { - console.log(`Failed to fetch token. Retries remaining: ${retries}`); - if (retries === 0) { - reject(error); - return; - } - setTimeout(() => { - fetchTokenWithRetry(url, retries - 1) - .then(resolve) - .catch(reject); - }, 500); +async function fetchTokenWithRetry(url, retries = 5) { + try { + const res = await _fetch(url); + if (!res.ok) { + throw new Error(`Unexpected status from token factory: ${res.status}`); } - }); + const data = await res.json(); + if (!data) { + throw new Error(`Unexpected response from token factory: no body`); + } + if (!data.token) { + const text = JSON.stringify(data); + throw new Error(`Unexpected response from token factory: ${text}`); + } + return data.token; + } catch (error) { + console.log(`Failed to fetch token. Retries remaining: ${retries}`); + if (retries === 0) { + console.log(error); + throw error; + } + await sleep(500); + return fetchTokenWithRetry(url, retries - 1); + } } -async function fetchApi (url, opts = {}) { +async function fetchApi(url, opts = {}) { const apiHost = process.env.API_HOST || 'api.zeit.co'; const urlWithHost = `https://${apiHost}${url}`; const { method = 'GET', body } = opts; @@ -212,4 +221,5 @@ module.exports = { fetchApi, fetchWithAuth, nowDeploy, + fetchTokenWithRetry, };