1- import getTemplate from 'common/templates' ;
21import { omit } from 'lodash-es' ;
32
43export 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}
0 commit comments