Skip to content

Commit 071bb2a

Browse files
committed
poc
1 parent f01472d commit 071bb2a

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

getSpreadsheetDocs.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
const fs = require('fs');
2+
const axios = require('axios');
3+
4+
const API_JSON_DIR = 'api-json'
5+
const CLASSES_DIR = 'classes'
6+
7+
8+
const processObj = (obj, treeName) => {
9+
if (!obj) {
10+
return;
11+
}
12+
const name = obj['1'];
13+
const dirToSave = CLASSES_DIR + '/' + name;
14+
const filePath = dirToSave + '/' + (!treeName ? 'index.js' : name + '.js' )
15+
let metaClass = 'class ' + name + '\n';
16+
17+
if (!treeName) {
18+
setupDir(dirToSave);
19+
}
20+
21+
const props = obj['2']
22+
const funcs = obj['3']
23+
if (!treeName && props && Array.isArray(props)) {
24+
props.forEach(item => {
25+
processObj(item, name)
26+
});
27+
}
28+
29+
if (funcs && Array.isArray(funcs)) {
30+
funcs.forEach(func => {
31+
const params = func['3']
32+
let strParams = ''
33+
if (params) {
34+
params.forEach(param => {
35+
strParams += param['1'] + ': ' + param['2'] + ', '
36+
})
37+
strParams = strParams.substring(0, strParams.length - 2)
38+
}
39+
console.info(' +------> ' + func['1'] + '(' + strParams + '): ' + func['2'])
40+
})
41+
}
42+
43+
metaClass += 'export default ' + name;
44+
fs.writeFile(name, JSON.stringify(a.data), function(err) {
45+
})
46+
}
47+
48+
49+
const loadObject = (objectVersion) => {
50+
return new Promise((resolve, reject) => {
51+
const objectName = objectVersion.split('-')[1]
52+
const fileName = 'API_JSON_DIR/' + objectName + '.json'
53+
if (fs.existsSync(fileName)) {
54+
fs.readFile(fileName, 'utf8', (err, data) => {
55+
resolve(objectName, data, false);
56+
});
57+
} else {
58+
axios({
59+
method: 'get',
60+
url: 'https://script.google.com/macros/autocomplete/static/' + objectVersion
61+
}).then(a => {
62+
fs.writeFile(fileName, JSON.stringify(a.data), function(err) {
63+
if (err) {
64+
reject(objectName, err);
65+
}
66+
resolve(objectName, a.data, true);
67+
});
68+
});
69+
}
70+
});
71+
}
72+
73+
const setupDir = (dir) => {
74+
if (!fs.existsSync(dir)){
75+
fs.mkdirSync(dir);
76+
}
77+
}
78+
79+
fs.readFile('versions.json', 'utf8', function (err, data) {
80+
setupDir(API_JSON_DIR)
81+
setupDir(CLASSES_DIR)
82+
83+
var versions = JSON.parse(data);
84+
const creatingFiles = [];
85+
const processingFiles = [];
86+
console.log('+> Process started');
87+
versions.forEach(version => {
88+
const loadFilePromise = loadObject(version)
89+
creatingFiles.push(loadFilePromise)
90+
91+
loadFilePromise.then((objectName, data, newFile) => {
92+
processingFiles.push(processObj(data));
93+
console.log('+-> File ' + objectName + ' ' + (newFile ? 'saved' : 'loaded'));
94+
}).catch((objectName, err) => {
95+
console.error('+-----------> File ' + objectName + ' with error', err)
96+
});
97+
})
98+
99+
Promise.all(creatingFiles).then(() => {
100+
console.log('+--> All files generated and loaded!');
101+
console.log('+>Generating classes')
102+
Promise.all(processingFiles).then(() => {
103+
console.log('All processed saved!');
104+
}).catch((err) => {
105+
console.error(err)
106+
})
107+
}).catch((err) => {
108+
console.error(err)
109+
})
110+
});

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "app-script-api",
3+
"version": "1.0.0",
4+
"description": "Create ES6 classes that reproduces exactly Google App Script API (without implementation)",
5+
"main": "getSpreadsheetDocs.js",
6+
"scripts": {
7+
"test": "test"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/matheusmr13/app-script-api.git"
12+
},
13+
"keywords": [
14+
"google-app-script",
15+
"app-script"
16+
],
17+
"author": "Matheus Martins do Rego",
18+
"license": "ISC",
19+
"bugs": {
20+
"url": "https://github.com/matheusmr13/app-script-api/issues"
21+
},
22+
"homepage": "https://github.com/matheusmr13/app-script-api#readme",
23+
"devDependencies": {
24+
"axios": "^0.16.2"
25+
}
26+
}

versions.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
["1960516349-Browser",
2+
"1423661430-CacheService",
3+
"3859772957-CalendarApp",
4+
"2773744902-Charts",
5+
"2474883271-ContactsApp",
6+
"759020288-ContentService",
7+
"1218806250-DocumentApp",
8+
"3330338646-DriveApp",
9+
"3920621183-FormApp",
10+
"3805022219-GmailApp",
11+
"2027746137-GroupsApp",
12+
"891004504-HtmlService",
13+
"294500145-Jdbc",
14+
"177788814-LanguageApp",
15+
"2123910714-LinearOptimizationService",
16+
"1138724038-LockService",
17+
"1792381341-Logger",
18+
"2039619557-MailApp",
19+
"265514893-Maps",
20+
"3517480757-MimeType",
21+
"3097423787-PropertiesService",
22+
"1368517142-ScriptApp",
23+
"264765698-ScriptProperties",
24+
"1634442656-Session",
25+
"2266443718-SitesApp",
26+
"1164645908-SpreadsheetApp",
27+
"2088800959-UiApp",
28+
"4128815824-UrlFetchApp",
29+
"1643443305-UserProperties",
30+
"1096254363-Utilities",
31+
"3395778541-XmlService",
32+
"3436841392-console",
33+
"3203702295-BigNumber",
34+
"452298494-JSON",
35+
"2179421567-Math",
36+
"1418869423-Object",
37+
"3811995935-Blob",
38+
"3105583603-BlobSource",
39+
"2798440586-Button",
40+
"4257272373-ButtonSet",
41+
"1607816240-ColumnType",
42+
"3750693408-DataTable",
43+
"1229068627-DataTableBuilder",
44+
"3891345350-DataTableSource",
45+
"4239288315-DigestAlgorithm",
46+
"577024753-Menu",
47+
"289920513-Month",
48+
"1580762917-PromptResponse",
49+
"3147477283-Ui",
50+
"3857245807-User",
51+
"1741780935-Weekday",
52+
"2313421668-Array"]

0 commit comments

Comments
 (0)