1+ // TODO
2+ // README
3+
4+ // Netlify doesn't expose live vars in the CLI, so we need to
5+ // confgure SITE_ID and BUILD_SITREP_TOKEN locally for development
6+ require ( 'dotenv' ) . config ( )
7+ let ejs = require ( 'ejs' ) ;
8+
9+ const fetch = require ( 'node-fetch' ) ;
10+ const {
11+ env : {
12+ // Must be configured by the user as a Netlify environment var
13+ BUILD_SITREP_TOKEN ,
14+ // These are provided by Netlify and are read-only
15+ CONTEXT ,
16+ SITE_ID ,
17+ COMMIT_REF ,
18+ DEPLOY_ID ,
19+ BUILD_ID
20+ }
21+ } = require ( 'process' )
22+
23+ // Configure the required API vars
24+ let authToken = BUILD_SITREP_TOKEN ;
25+ let authString = 'Bearer ' + authToken
26+ let baseURL = `https://api.netlify.com/api/v1/sites/${ SITE_ID } /snippets`
27+
28+ module . exports = {
29+ onPreBuild : ( { inputs, utils : { build : { failPlugin } } } ) => {
30+ // Make sure the token has been set
31+ if ( typeof authToken == 'undefined' ) {
32+ return failPlugin ( 'The BUILD_SITREP_TOKEN environment var has not been set.' ) ;
33+ }
34+
35+ // Make sure we don't run in disallowed contexts
36+ console . log ( 'Verfying allowable contexts...' ) ;
37+
38+ let allow_prod = inputs . allow_prod ;
39+ switch ( allow_prod ) {
40+ case 'false' :
41+ if ( CONTEXT == 'production' ) {
42+ return failPlugin ( 'Production debug is disabled. This can be changed in netlify.toml' ) ;
43+ break ;
44+ }
45+ case 'true' :
46+ console . log ( 'Production debug is enabled. This can be changed in netlify.toml' )
47+ break ;
48+ }
49+ } ,
50+
51+ onBuild ( { utils : { build : { failPlugin } } } ) {
52+ console . log ( 'Building the tag UI...' )
53+
54+ let data = { build_id : BUILD_ID , context : CONTEXT , commit_ref : COMMIT_REF , deploy_id : DEPLOY_ID } ;
55+ let output = ejs . renderFile ( __dirname + './templates/template.ejs' , data , function ( err , data ) {
56+ if ( err ) {
57+ return failPlugin ( 'Something went wrong when processing the display template.' ) ;
58+ }
59+
60+ let renderToBase64 = new Buffer . from ( data ) ;
61+ let encodedRender = renderToBase64 . toString ( 'base64' ) ;
62+
63+ var tagOpen = '<script>' +
64+ 'window.onload = function() {' +
65+ 'var ifrm = document.createElement(\'iframe\');' +
66+ 'ifrm.setAttribute(\'id\', \'ifrm\');' +
67+ 'ifrm.setAttribute(\'style\', \'position: fixed; bottom: 0; right: 0; border: 0;\');' +
68+ 'document.body.appendChild(ifrm);'
69+
70+ var tagData = 'ifrm.setAttribute(\'src\', \'data:text/html;base64,' + encodedRender + '\');'
71+ var tagEnd = '}' +
72+ '</script>'
73+
74+ global . tagComplete = tagOpen + tagData + tagEnd
75+ console . log ( 'Successfully built the tag UI.' )
76+
77+ } ) ;
78+ } ,
79+
80+ async onSuccess ( { utils : { build : { failPlugin, failBuild } } } ) {
81+
82+ console . log ( 'Here we go...' ) ;
83+
84+ try {
85+
86+ console . log ( 'Checking to see if a snippet already exists...' ) ;
87+
88+ await fetch ( baseURL , { method : 'GET' , headers : { 'Authorization' : authString } } )
89+ . then ( ( res ) => {
90+ return res . json ( )
91+ } )
92+
93+ . then ( ( json ) => {
94+ var findSnippet = json . filter ( function ( item ) {
95+ return item . title == "netlify-build-sitrep" ;
96+ } ) ;
97+
98+ // TODO: this is super sketchy, needs to be addressed
99+ if ( findSnippet === undefined || findSnippet . length == 0 ) {
100+ console . log ( 'Couldn\'t find an existing snippet.' ) ;
101+ extendedURL = baseURL ;
102+ fetchMethod = 'POST'
103+ } else {
104+ var snippetID = findSnippet [ 0 ] [ 'id' ] ;
105+ extendedURL = baseURL + '/' + snippetID ;
106+ console . log ( 'Found snippet with id: ' + snippetID ) ;
107+ fetchMethod = 'PUT'
108+ }
109+ } )
110+
111+ const { status, statusText } = await fetch ( extendedURL , {
112+ method : fetchMethod ,
113+ headers : {
114+ 'Authorization' : authString ,
115+ "Content-type" : "application/json" ,
116+ "Accept" : "application/json" ,
117+ "Accept-Charset" : "utf-8"
118+ } ,
119+ body : JSON . stringify ( {
120+ title : "netlify-build-sitrep" ,
121+ general : tagComplete ,
122+ general_position : "body" ,
123+ } )
124+ } )
125+
126+ if ( status == 201 ) {
127+ console . log ( 'Snippet was added.' ) ;
128+ } else if ( status == 200 ) {
129+ console . log ( 'Snippet was updated.' ) ;
130+ } else {
131+ return failPlugin ( 'Something went wrong. Netlify said: ' + status + statusText ) ;
132+ }
133+ }
134+ catch ( error ) {
135+ return failBuild ( 'Something catastrophic happened.' , { error } )
136+ }
137+ }
138+ }
0 commit comments