Skip to content

Commit a44de09

Browse files
committed
Update ZEIT Deployment API
1 parent 045ce1a commit a44de09

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,46 @@ export async function createApiData({ props, state }) {
2020
const { contents } = props;
2121
const sandboxId = state.get('editor.currentId');
2222
const sandbox = state.get(`editor.sandboxes.${sandboxId}`);
23-
let apiData = {};
23+
let apiData = {
24+
files: [],
25+
};
2426
const filePaths = Object.keys(contents.files);
27+
28+
let packageJSON = {};
29+
const projectPackage = contents.files['package.json'];
30+
31+
if (projectPackage) {
32+
const data = await projectPackage.async('text'); // eslint-disable-line no-await-in-loop
33+
34+
const parsed = JSON.parse(data);
35+
packageJSON = parsed;
36+
}
37+
packageJSON = omitHomepage(packageJSON);
38+
39+
// We force the sandbox id, so ZEIT will always group the deployments to a
40+
// single sandbox
41+
packageJSON.name = `csb-${sandbox.id}`;
42+
43+
apiData.name = `csb-${sandbox.id}`;
44+
apiData.deploymentType = 'NPM';
45+
apiData.public = true;
46+
47+
apiData.files.push({
48+
file: 'package.json',
49+
data: JSON.stringify(packageJSON, null, 2),
50+
});
51+
2552
for (let i = 0; i < filePaths.length; i += 1) {
2653
const filePath = filePaths[i];
2754
const file = contents.files[filePath];
2855

29-
if (!file.dir) {
30-
apiData[filePath] = await file.async('text'); // eslint-disable-line no-await-in-loop
56+
if (!file.dir && filePath !== 'package.json') {
57+
const data = await file.async('text'); // eslint-disable-line no-await-in-loop
58+
59+
apiData.files.push({ file: filePath, data });
3160
}
3261
}
3362

34-
apiData.package = omitHomepage(JSON.parse(apiData['package.json']));
35-
// We force the sandbox id, so ZEIT will always group the deployments to a
36-
// single sandbox
37-
apiData.package.name = `csb-${sandbox.id}`;
38-
delete apiData['package.json'];
39-
4063
const template = getTemplate(sandbox.template);
4164

4265
if (template.alterDeploymentData) {
@@ -53,7 +76,7 @@ export function postToZeit({ http, path, props, state }) {
5376
return http
5477
.request({
5578
method: 'POST',
56-
url: 'https://api.zeit.co/now/deployments',
79+
url: 'https://api.zeit.co/v3/now/deployments',
5780
body: apiData,
5881
headers: { Authorization: `bearer ${token}` },
5982
})

packages/common/templates/template.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,31 @@ export default class Template {
8989
/**
9090
* Alter the apiData to ZEIT for making deployment work
9191
*/
92-
alterDeploymentData = (apiData: any) => ({
93-
...apiData,
94-
package: {
95-
...apiData.package,
92+
alterDeploymentData = (apiData: any) => {
93+
const packageJSONFile = apiData.files.find(x => x.file === 'package.json');
94+
const parsedFile = JSON.parse(packageJSONFile.data);
95+
96+
const newParsedFile = {
97+
...parsedFile,
9698
devDependencies: {
97-
...apiData.package.devDependencies,
99+
...parsedFile.devDependencies,
98100
serve: '^5.0.1',
99101
},
100102
scripts: {
101-
...apiData.package.scripts,
103+
...parsedFile.scripts,
102104
'now-start': `cd ${this.distDir} && serve -s ./`,
103105
},
104-
},
105-
});
106+
};
107+
108+
return {
109+
...apiData,
110+
files: [
111+
...apiData.files.filter(x => x.file !== 'package.json'),
112+
{
113+
file: 'package.json',
114+
data: JSON.stringify(newParsedFile, null, 2),
115+
},
116+
],
117+
};
118+
};
106119
}

0 commit comments

Comments
 (0)