-
Notifications
You must be signed in to change notification settings - Fork 2
/
_backend.gs
155 lines (82 loc) · 3.38 KB
/
_backend.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// https://github.com/marcomow/babel-apps-script/
const NAME_FILE_POLYFILLS = 'polyfill.min';
const doGet = function () {
return HtmlService.createHtmlOutputFromFile('index')
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setTitle('babel-apps-script')
}
const get_array_objects_scripts = function () {
const array_objects_scripts = [];
const iterator = DriveApp.getFilesByType(MimeType.GOOGLE_APPS_SCRIPT)
while (iterator.hasNext()) {
const file = iterator.next();
const object_script = {
id: file.getId(),
name: file.getName()
}
array_objects_scripts.push(object_script)
}
return JSON.stringify(array_objects_scripts);
}
const get_project = function (id_script) {
const url_base = 'https://script.googleapis.com/v1/projects/' + id_script + '/content';
const object_payload = {}
const object_headers = {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
const object_options = {
method: 'get',
headers: object_headers,
muteHttpExceptions: true
}
const fetch = UrlFetchApp.fetch(url_base, object_options).getContentText();
const content = JSON.parse(fetch);
return content;
}
const update_project = function (id_script, object_project) {
const url_base = 'https://script.googleapis.com/v1/projects/' + id_script + '/content';
const object_payload = JSON.stringify(object_project)
const object_headers = {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
'Content-Type': 'application/json'
}
const object_options = {
method: 'put',
headers: object_headers,
payload: object_payload,
muteHttpExceptions: true
}
const fetch = UrlFetchApp.fetch(url_base, object_options).getContentText();
const content = JSON.parse(fetch);
return content;
}
const compile = function (id_script_origin, id_script_destination) {
const object_project_origin = get_project(id_script_origin);
const array_files_origin = object_project_origin.files;
const array_files_destination = array_files_origin.map(function (object_file) {
if (object_file.type === 'SERVER_JS') {
object_file.source = Babel.transform(object_file.source, {
presets: ['env']
}).code;
}
if(object_file.name === 'appsscript' && object_file.type === 'JSON'){
const object_source = JSON.parse(object_file.source) ;
object_source.runtimeVersion = 'DEPRECATED_ES5';
object_file.source = JSON.stringify(object_source) ;
}
return object_file;
});
object_project_origin.files = array_files_destination;
const condition_file_named_polyfills = object_project_origin.files.find(a=>a.name===NAME_FILE_POLYFILLS);
if(condition_file_named_polyfills){
return JSON.stringify({error:{message:'compiling failed. You should NOT have a file named "' + NAME_FILE_POLYFILLS + '" as it is reserved'}}) ;
}
const object_file_polyfills = {
name:NAME_FILE_POLYFILLS,
type:'SERVER_JS',
source: HtmlService.createHtmlOutputFromFile('polyfill').getContent().replace('<script>','').replace('</script>','')
}
object_project_origin.files.push(object_file_polyfills);
const result = update_project(id_script_destination, object_project_origin);
return JSON.stringify(result) ;
}