Skip to content

Commit aa263f0

Browse files
SaraVieiraCompuIves
authored andcommitted
Support Custom Deployment with Now.json (codesandbox#1210)
* add support for now.json * add support for package.json now
1 parent 0e55f62 commit aa263f0

File tree

23 files changed

+109
-149
lines changed

23 files changed

+109
-149
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/Files/DirectoryEntry/Entry/EntryIcons/GetIconURL.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import faviconSvg from 'common/components/icons/favicon.svg';
44
import fileSvg from 'common/components/icons/file.svg';
55
import imageSvg from 'common/components/icons/image.svg';
66
import codesandboxSvg from 'common/components/icons/codesandbox.svg';
7+
import nowSvg from 'common/components/icons/now.svg';
78

89
function imageExists(url) {
910
return new Promise((resolve, reject) => {
@@ -20,6 +21,7 @@ const icons = {
2021
favicon: faviconSvg,
2122
image: imageSvg,
2223
codesandbox: codesandboxSvg,
24+
now: nowSvg,
2325
};
2426

2527
async function getIconURL(type) {
Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import getTemplate from 'common/templates';
21
import { omit } from 'lodash-es';
32

43
export function createZip({ utils, state }) {
@@ -17,13 +16,14 @@ export async function createApiData({ props, state }) {
1716
const { contents } = props;
1817
const sandboxId = state.get('editor.currentId');
1918
const sandbox = state.get(`editor.sandboxes.${sandboxId}`);
20-
let apiData = {
19+
const apiData = {
2120
files: [],
2221
};
23-
const filePaths = Object.keys(contents.files);
2422

2523
let packageJSON = {};
24+
let nowJSON = {};
2625
const projectPackage = contents.files['package.json'];
26+
const nowFile = contents.files['now.json'];
2727

2828
if (projectPackage) {
2929
const data = await projectPackage.async('text'); // eslint-disable-line no-await-in-loop
@@ -32,21 +32,43 @@ export async function createApiData({ props, state }) {
3232
packageJSON = parsed;
3333
}
3434

35+
if (nowFile) {
36+
const data = await nowFile.async('text'); // eslint-disable-line no-await-in-loop
37+
38+
const parsed = JSON.parse(data);
39+
nowJSON = parsed;
40+
} else if (packageJSON.now) {
41+
// Also support package.json if imported like that
42+
nowJSON = packageJSON.now;
43+
}
44+
45+
const nowDefaults = {
46+
name: `csb-${sandbox.id}`,
47+
type: 'NPM',
48+
public: true,
49+
};
50+
51+
const filePaths = nowJSON.files || Object.keys(contents.files);
52+
3553
// We'll omit the homepage-value from package.json as it creates wrong assumptions over the now deployment evironment.
3654
packageJSON = omit(packageJSON, 'homepage');
3755

3856
// We force the sandbox id, so ZEIT will always group the deployments to a
3957
// single sandbox
40-
packageJSON.name = `csb-${sandbox.id}`;
41-
42-
apiData.name = `csb-${sandbox.id}`;
43-
apiData.deploymentType = 'NPM';
44-
apiData.public = true;
45-
46-
apiData.files.push({
47-
file: 'package.json',
48-
data: JSON.stringify(packageJSON, null, 2),
49-
});
58+
packageJSON.name = nowJSON.name || nowDefaults.name;
59+
60+
apiData.name = nowJSON.name || nowDefaults.name;
61+
apiData.deploymentType = nowJSON.type || nowDefaults.type;
62+
apiData.public = nowJSON.public || nowDefaults.public;
63+
apiData.config = omit(nowJSON, ['public', 'type', 'name', 'files']);
64+
apiData.forceNew = true;
65+
66+
if (!nowJSON.files) {
67+
apiData.files.push({
68+
file: 'package.json',
69+
data: JSON.stringify(packageJSON, null, 2),
70+
});
71+
}
5072

5173
for (let i = 0; i < filePaths.length; i += 1) {
5274
const filePath = filePaths[i];
@@ -59,29 +81,45 @@ export async function createApiData({ props, state }) {
5981
}
6082
}
6183

62-
const template = getTemplate(sandbox.template);
84+
return { apiData };
85+
}
6386

64-
if (template.alterDeploymentData) {
65-
apiData = template.alterDeploymentData(apiData);
87+
// Will be used later to create alias but I wanna get this PR merged first
88+
export async function addAlias({ http, path, props, state }) {
89+
const { apiData, deploymentId } = props;
90+
const token = state.get('user.integrations.zeit.token');
91+
try {
92+
const alias = await http.request({
93+
url: `https://api.zeit.co/v2/now/deployments/${deploymentId}/aliases`,
94+
body: { alias: apiData.config.alias },
95+
method: 'POST',
96+
headers: { Authorization: `bearer ${token}` },
97+
});
98+
const url = `https://${alias.result.alias}`;
99+
return path.success({ url });
100+
} catch (error) {
101+
console.error(error);
102+
return path.error({ error });
66103
}
67-
68-
return { apiData };
69104
}
70105

71-
export function postToZeit({ http, path, props, state }) {
106+
export async function postToZeit({ http, path, props, state }) {
72107
const { apiData } = props;
73108
const token = state.get('user.integrations.zeit.token');
74109

75-
return http
76-
.request({
77-
method: 'POST',
110+
try {
111+
const deployment = await http.request({
78112
url: 'https://api.zeit.co/v3/now/deployments',
79113
body: apiData,
114+
method: 'POST',
80115
headers: { Authorization: `bearer ${token}` },
81-
})
82-
.then(response => {
83-
const url = `https://${response.result.url}`;
84-
return path.success({ url });
85-
})
86-
.catch(error => path.error({ error }));
116+
});
117+
118+
const url = `https://${deployment.result.url}`;
119+
120+
return path.success({ url });
121+
} catch (error) {
122+
console.error(error);
123+
return path.error({ error });
124+
}
87125
}

packages/app/src/app/utils/get-type.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const specialCasesMap = {
88
'yarn.lock': 'yarn',
99
'package.json': 'npm',
1010
'sandbox.config.json': 'codesandbox',
11+
'now.json': 'now',
1112
'readme.md': 'readme',
1213
'contributing.md': 'contributing',
1314
'tsconfig.json': 'typescript',
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 17 deletions
Loading
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)