'use strict';

require('dotenv').config();

const PRODUCTION = process.env.NODE_ENV === 'production';

const FASTLY_API_KEY = process.env.FASTLY_API_KEY;
const SURROGATE_KEY = process.env.SURROGATE_KEY;

if (!SURROGATE_KEY) {
	console.error('In order to purge assets from Fastly, you need to have set the environment variable "SURROGATE_KEY". This can be done by creating a file named ".env" in the root of this repository with the contents "SURROGATE_KEY=XXXXXX", where XXXXXX is the surrogate-key used in your polyfill-service application.');
	process.exit(1);
}

let FASTLY_SERVICE_ID;

if (!FASTLY_API_KEY) {
	console.error('In order to purge assets from Fastly, you need to have set the environment variable "FASTLY_API_KEY". This can be done by creating a file named ".env" in the root of this repository with the contents "FASTLY_API_KEY=XXXXXX", where XXXXXX is your Fastly API key.');
	process.exit(1);
}

if (PRODUCTION && !process.env.FASTLY_SERVICE_ID) {
	console.error('In order to purge assets from the production Fastly service, you need to have set the environment variable "FASTLY_SERVICE_ID". This can be done by creating a file named ".env" in the root of this repository with the contents "FASTLY_SERVICE_ID=XXXXXX", where XXXXXX is your Fastly service ID.');
	process.exit(1);
} else {
	FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID;
}

if (!PRODUCTION && !process.env.FASTLY_SERVICE_ID_QA) {
	console.error('In order to purge assets from the QA Fastly service, you need to have set the environment variable "FASTLY_SERVICE_ID_QA". This can be done by creating a file named ".env" in the root of this repository with the contents "FASTLY_SERVICE_ID_QA=XXXXXX", where XXXXXX is your Fastly service ID.');
	process.exit(1);
} else {
	FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID_QA;
}

const FastlyPurge = require('fastly-purge');

const fastly = new FastlyPurge(FASTLY_API_KEY, {
	softPurge: true
});


fastly.key(FASTLY_SERVICE_ID, SURROGATE_KEY, {
	apiKey: FASTLY_API_KEY
}, error => {
	if (error) {
		console.error(`Failed to purge endpoints. ${error}`);
		process.exit(1);
	} else {
		console.log(`Purged key: ${SURROGATE_KEY} from ${PRODUCTION ? 'production' : 'qa'} service - https://manage.fastly.com/dashboard/services/${FASTLY_SERVICE_ID}/datacenters/all`);
	}
});

@@ -914,37 +914,41 @@
 %0A%0Aif (PRODUCTION
- && !
+) %7B%0A%09if (
 process.env.FAST
@@ -960,24 +960,88 @@
 RVICE_ID) %7B%0A
+%09%09FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID;%0A%09%7D else %7B%0A%09
 %09console.err
@@ -1333,32 +1333,33 @@
  service ID.');%0A
+%09
 %09process.exit(1)
@@ -1352,32 +1352,35 @@
 rocess.exit(1);%0A
+%09%7D%0A
 %7D else %7B%0A%09FASTLY
@@ -1365,32 +1365,48 @@
 );%0A%09%7D%0A%7D else %7B%0A%09
+if (process.env.
 FASTLY_SERVICE_I
@@ -1402,39 +1402,33 @@
 Y_SERVICE_ID
- = process.env.
+_QA) %7B%0A%09%09
 FASTLY_SERVI
@@ -1436,33 +1436,11 @@
 E_ID
-;%0A%7D%0A%0Aif (!PRODUCTION && !
+ = 
 proc
@@ -1467,20 +1467,29 @@
 CE_ID_QA
-)
+;%0A%09%7D else
  %7B%0A
+%09
 %09console
@@ -1787,24 +1787,25 @@
 vice ID.');%0A
+%09
 %09process.exi
@@ -1814,71 +1814,10 @@
 1);%0A
+%09
 %7D
- else %7B%0A%09FASTLY_SERVICE_ID = process.env.FASTLY_SERVICE_ID_QA;
 %0A%7D%0A%0A

Fix logic around purging assets