Skip to content

Commit d86e42d

Browse files
SaraVieiraCompuIves
authored andcommitted
Add zeit 2 support (codesandbox#1417)
<!-- Is it a Bug fix, feature, docs update, ... --> **What kind of change does this PR introduce?** Adds support for zeit now version 2 <!-- You can also link to an open issue here --> **What is the current behavior?** Currently we have a counter and it had like 140 people so I guess it's time <!-- if this is a feature change --> **What is the new behavior?** If the now.json has `version:2` specified the new now platform will be used Here is an example: https://codesandbox-v2-test.now.sh/graphql Also all the other endpoints remained the same so it was easier then I thought o.o cc @timothyis @rauchg
1 parent 6fd9b24 commit d86e42d

File tree

7 files changed

+19
-151
lines changed

7 files changed

+19
-151
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/items/Deployment/index.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
class Deployment extends Component {
2525
componentDidMount = () => {
2626
this.props.signals.deployment.getDeploys();
27-
this.props.signals.deployment.getPeople();
2827
};
2928

3029
render() {
@@ -33,9 +32,6 @@ class Deployment extends Component {
3332
store: { user, deployment },
3433
} = this.props;
3534

36-
const hasVoted = deployment.peopleWant2.find(
37-
a => a.username === user.username
38-
);
3935
return (
4036
<div>
4137
<Description>
@@ -50,50 +46,6 @@ class Deployment extends Component {
5046
{!user.integrations.zeit &&
5147
' You need to add ZEIT to your integrations to deploy.'}
5248
</Description>
53-
<Description
54-
css={`
55-
margin: 0;
56-
padding: 0.5rem 1rem;
57-
58-
background: #122d42;
59-
`}
60-
>
61-
We currently only support Zeit v1.
62-
{!hasVoted && deployment.peopleWant2.length ? (
63-
<Fragment>
64-
<br />
65-
<br />
66-
If you would like us to support version 2.0 too please add your
67-
thumbs up
68-
</Fragment>
69-
) : null}
70-
<Button
71-
css={`
72-
padding: 5px 8px;
73-
margin-top: 1rem;
74-
`}
75-
block
76-
disabled={hasVoted || !deployment.peopleWant2.length}
77-
onClick={() =>
78-
signals.deployment.addPersonFor2({
79-
username: user.username,
80-
})
81-
}
82-
>
83-
{deployment.peopleWant2.length > 0
84-
? `${deployment.peopleWant2.length} people want v2`
85-
: null}{' '}
86-
<span
87-
css={`
88-
margin-left: 5px;
89-
`}
90-
role="img"
91-
aria-label="I want this"
92-
>
93-
👍
94-
</span>
95-
</Button>
96-
</Description>
9749

9850
{user.integrations.zeit ? (
9951
<Fragment>

packages/app/src/app/store/modules/deployment/actions.js

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,6 @@ export function loadZip({ props, jsZip }) {
1313
return jsZip.loadAsync(file).then(result => ({ contents: result }));
1414
}
1515

16-
export async function getPeopleWhoWant2() {
17-
const Airtable = await import(/* webpackChunkName: 'airtable' */ '../../utils/setAirtable');
18-
19-
const base = await Airtable.default.base('apppgSmcJWwuXac6t');
20-
const params = base('zeit2').select({
21-
view: 'Grid view',
22-
maxRecords: 100000,
23-
});
24-
const people = [];
25-
26-
const getPeople = () =>
27-
new Promise(res => {
28-
params.eachPage(
29-
(records, fetchNextPage) => {
30-
records.forEach(record => {
31-
people.push(record);
32-
});
33-
fetchNextPage();
34-
},
35-
() => res()
36-
);
37-
});
38-
39-
await getPeople();
40-
41-
return {
42-
people: people.map(a => a.fields),
43-
};
44-
}
45-
46-
export async function addPersonWhoWant2({ path, props }) {
47-
const Airtable = await import(/* webpackChunkName: 'airtable' */ '../../utils/setAirtable');
48-
49-
const base = await Airtable.default.base('apppgSmcJWwuXac6t');
50-
51-
const AddPerson = () =>
52-
new Promise((res, rej) => {
53-
base('zeit2').create({ username: props.username }, err => {
54-
if (err) {
55-
console.error(err);
56-
rej();
57-
}
58-
res();
59-
});
60-
});
61-
62-
try {
63-
await AddPerson();
64-
65-
return path.success();
66-
} catch (e) {
67-
return path.error();
68-
}
69-
}
70-
7116
export async function createApiData({ props, state }) {
7217
const { contents } = props;
7318
const sandboxId = state.get('editor.currentId');
@@ -101,13 +46,12 @@ export async function createApiData({ props, state }) {
10146

10247
const nowDefaults = {
10348
name: `csb-${sandbox.id}`,
104-
type: 'NPM',
10549
public: true,
10650
};
10751

10852
const filePaths = nowJSON.files || Object.keys(contents.files);
10953

110-
// We'll omit the homepage-value from package.json as it creates wrong assumptions over the now deployment evironment.
54+
// We'll omit the homepage-value from package.json as it creates wrong assumptions over the now deployment environment.
11155
packageJSON = omit(packageJSON, 'homepage');
11256

11357
// We force the sandbox id, so ZEIT will always group the deployments to a
@@ -117,8 +61,16 @@ export async function createApiData({ props, state }) {
11761
apiData.name = nowJSON.name || nowDefaults.name;
11862
apiData.deploymentType = nowJSON.type || nowDefaults.type;
11963
apiData.public = nowJSON.public || nowDefaults.public;
120-
apiData.config = omit(nowJSON, ['public', 'type', 'name', 'files']);
121-
apiData.forceNew = true;
64+
65+
// if now v2 we need to tell now the version, builds and routes
66+
if (nowJSON.version === 2) {
67+
apiData.version = 2;
68+
apiData.builds = nowJSON.builds;
69+
apiData.routes = nowJSON.routes;
70+
} else {
71+
apiData.config = omit(nowJSON, ['public', 'type', 'name', 'files']);
72+
apiData.forceNew = true;
73+
}
12274

12375
if (!nowJSON.files) {
12476
apiData.files.push({
@@ -138,7 +90,9 @@ export async function createApiData({ props, state }) {
13890
}
13991
}
14092

141-
if (template.alterDeploymentData) {
93+
// this adds unnecessary code for version 2
94+
// packages/common/templates/template.js
95+
if (template.alterDeploymentData && nowJSON.version !== 2) {
14296
apiData = template.alterDeploymentData(apiData);
14397
}
14498

@@ -167,10 +121,11 @@ export async function aliasDeployment({ http, path, props, state }) {
167121
export async function postToZeit({ http, path, props, state }) {
168122
const { apiData } = props;
169123
const token = state.get('user.integrations.zeit.token');
124+
const deploymentVersion = apiData.version === 2 ? 'v6' : 'v3';
170125

171126
try {
172127
const deployment = await http.request({
173-
url: 'https://api.zeit.co/v3/now/deployments',
128+
url: `https://api.zeit.co/${deploymentVersion}/now/deployments?forceNew=1`,
174129
body: apiData,
175130
method: 'POST',
176131
headers: { Authorization: `bearer ${token}` },

packages/app/src/app/store/modules/deployment/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as sequences from './sequences';
55
export default Module({
66
model,
77
state: {
8-
peopleWant2: [],
98
hasAlias: false,
109
deployToDelete: null,
1110
deploying: false,
@@ -15,12 +14,10 @@ export default Module({
1514
},
1615
signals: {
1716
getDeploys: sequences.getDeploys,
18-
addPersonFor2: sequences.addPersonFor2,
1917
deployClicked: sequences.deploy,
2018
deploySandboxClicked: sequences.openDeployModal,
2119
setDeploymentToDelete: sequences.deploymentToDelete,
2220
deleteDeployment: sequences.deleteDeployment,
2321
aliasDeployment: sequences.aliasDeployment,
24-
getPeople: sequences.getPeopleWhoWant2,
2522
},
2623
});

packages/app/src/app/store/modules/deployment/model.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,10 @@ const Deploy = types.model('Deploy', {
3636
alias: types.maybeNull(types.array(Alias)),
3737
scale: types.maybeNull(Scale),
3838
creator: Creator,
39-
type: types.enumeration('types', ['NPM', 'DOCKER', 'STATIC']),
40-
});
41-
42-
const People = types.model('Person', {
43-
username: types.maybeNull(types.string),
39+
type: types.enumeration('types', ['NPM', 'DOCKER', 'STATIC', 'LAMBDAS']),
4440
});
4541

4642
export default {
47-
peopleWant2: types.array(People),
4843
hasAlias: types.boolean,
4944
deployToDelete: types.maybeNull(types.string),
5045
deploying: types.boolean,

packages/app/src/app/store/modules/deployment/sequences.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,6 @@ export const deploymentToDelete = [
1414
set(state`deployment.deployToDelete`, props`id`),
1515
];
1616

17-
export const getPeopleWhoWant2 = [
18-
actions.getPeopleWhoWant2,
19-
set(state`deployment.peopleWant2`, props`people`),
20-
];
21-
22-
export const addPersonFor2 = [
23-
actions.addPersonWhoWant2,
24-
{
25-
success: [
26-
actions.getPeopleWhoWant2,
27-
set(state`deployment.peopleWant2`, props`people`),
28-
addNotification('Your feedback has been saved', 'success'),
29-
],
30-
error: [
31-
addNotification(
32-
'An unknown error occurred when adding your vote',
33-
'error'
34-
),
35-
],
36-
},
37-
actions.getPeopleWhoWant2,
38-
set(state`deployment.peopleWant2`, props`people`),
39-
];
40-
4117
export const getDeploys = [
4218
actions.getDeploymentData,
4319
actions.getDeploymentData,

packages/common/templates/configuration/now/index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@ const config: ConfigurationFile = {
77
description: 'Configuration for your deployments on now.',
88
moreInfoUrl: 'https://zeit.co/docs/features/configuration',
99

10-
getDefaultCode: () =>
11-
JSON.stringify(
12-
{
13-
type: 'NPM',
14-
},
15-
null,
16-
2
17-
),
10+
getDefaultCode: () => JSON.stringify({}, null, 2),
1811
};
1912

2013
export default config;

packages/common/templates/template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ export default class Template {
126126
serve: '^10.1.1',
127127
},
128128
scripts: {
129-
...parsedFile.scripts,
130129
'now-start': `cd ${this.distDir} && serve -s ./`,
130+
...parsedFile.scripts,
131131
},
132132
};
133133

0 commit comments

Comments
 (0)