-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
236 lines (185 loc) · 6.33 KB
/
index.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env node
const fs = require("fs");
const os = require("os");
const opn = require("opn");
const path = require("path");
const underscore = require("underscore");
const folders = require("platform-folders");
const settings = require("./settings");
const processor = require("./scripts/processor");
const config_utils = require("./scripts/config_utils");
const AUTH_DIR = path.join(folders.getDataHome(), settings.app_name);
const TOKEN_PATH = path.join(AUTH_DIR, settings.token_name);
const CREDENTIALS_PATH = path.join(AUTH_DIR, settings.credentials_name);
const DEFAULT_CONFIG_PATH = path.join(process.cwd(), settings.default_config_path, settings.default_config_name);
// List all files in a directory in Node.js recursively in a synchronous fashion
function walk_sync(dir, filelist) {
let files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
let filepath = path.join(dir, file);
if (fs.statSync(filepath).isDirectory()) {
filelist = walk_sync(filepath, filelist);
} else {
filelist.push(file);
}
});
return filelist;
};
function move_file(from, to) {
fs.mkdirSync(path.dirname(to), { recursive: true });
fs.copyFileSync(from, to);
fs.unlinkSync(from);
};
function setup_token() {
let setup_config_path = path.join(__dirname, settings.setup_config_path);
console.log("Need to auth to create the " + settings.token_name);
console.log("Please restart the export to process all config");
console.log("Process setup config for check token is correct...");
return setup_config_path;
};
async function load_config(config_path, sheet_name, rule_name) {
let config = JSON.parse(fs.readFileSync(config_path));
settings.runtime.config_dir = path.dirname(config_path);
if (sheet_name) {
config.sheets = config.sheets.filter((sheet) => {
return sheet.name == sheet_name;
})
}
// Post-process config
for (let i in config.sheets) {
let sheet = config.sheets[i];
let rule_path = path.join(settings.runtime.config_dir, sheet.rule);
sheet.rule = JSON.parse(fs.readFileSync(rule_path));
if (rule_name) {
let single_rule = {};
single_rule[rule_name] = sheet.rule.rules[rule_name];
sheet.rule.rules = single_rule;
}
for (let j in sheet.save) {
sheet.save[j].temp_dist = sheet.save[j].dist.replace(/\.\./g, ".");
sheet.save[j].temp_dir = fs.mkdtempSync(os.tmpdir());
}
}
console.log("Start export data");
console.log("Config path:", config_path);
console.log("Sheet:", sheet_name || "All sheets");
console.log("Rule:", rule_name || "All rules");
console.log("");
return config
}
async function download_export(config) {
let promise = new Promise((resolve) => {
let part_resolve = underscore.after(config.sheets.length, resolve);
for (let i in config.sheets) {
processor.process_sheet(config.sheets[i], part_resolve);
}
})
await promise;
return config;
}
async function validate_export(config) {
return config;
}
async function apply_data(config) {
for (let i in config.sheets) {
for (let j in config.sheets[i].save) {
let save_param = config.sheets[i].save[j];
let files = walk_sync(save_param.temp_dir);
for (let k in files) {
let from_path = path.join(save_param.temp_dir, save_param.temp_dist, files[k]);
let to_path = path.join(settings.runtime.config_dir, save_param.dist, files[k]);
move_file(from_path, to_path);
console.log("Upload file", to_path);
}
}
}
}
function start_pipeline(config_path, sheet, rule_name) {
load_config(config_path, sheet, rule_name)
.then(download_export)
.then(validate_export)
.then(apply_data)
};
function setup_credentials() {
console.log("Check setup instructions here: https://github.com/Insality/sheets-exporter/blob/master/docs/01_installation.md");
console.log("Place credentials in " + CREDENTIALS_PATH);
};
function credentials_folder() {
console.log("Credentials path:", CREDENTIALS_PATH);
}
function help() {
console.log("");
console.log("This information")
console.log("\tsheets-exporter help");
console.log()
console.log("Init default sheets-exporter configs")
console.log("\tsheets-exporter init");
console.log()
console.log("Go to the setup credentials instruction site")
console.log("\tsheets-exporter setup_credentials");
console.log()
console.log("Show the credentials folder")
console.log("\tsheets-exporter credentials_folder");
console.log()
console.log("Add default sheet config to current config")
console.log("\tsheets-exporter add_sheet sheet_name sheet_id {config_path}");
console.log()
console.log("Add default rule config for specific Google list")
console.log("\tsheets-exporter add_rule sheet_name list_name {config_path}");
console.log()
console.log("Start export pipeline. Use default config_path by default and export all sheets and rules")
console.log("\tsheets-exporter export {config_path} {sheet_name} {rule_name}");
};
function init_configs() {
if (fs.existsSync(DEFAULT_CONFIG_PATH)) {
console.log("Found config file:", DEFAULT_CONFIG_PATH);
console.log("The config file is already exists, abort initing...");
return;
}
fs.mkdirSync(DEFAULT_CONFIG_PATH, { recursive: true });
};
function export_configs() {
let config_path = process.argv[3] || DEFAULT_CONFIG_PATH;
if (!fs.existsSync(TOKEN_PATH)) {
config_path = setup_token()
}
if (!fs.existsSync(config_path)) {
console.log("ERROR: No sheets exporter config found");
console.log("Use `sheets-exporter init` for default config init");
console.log("Use `sheets-exporter add_sheet` to add Google document configs");
console.log("Use `sheets-exporter add_rule` to add specific Google list configs");
return;
}
let sheet = process.argv[4];
let rule_name = process.argv[5];
start_pipeline(config_path, sheet, rule_name);
};
let COMMANDS = {
// ({config_path})
"init": config_utils.init_configs,
"setup_credentials": setup_credentials,
// (sheet_name, sheet_id, {config_path})
"add_sheet": config_utils.add_sheet,
// ()
"credentials_folder": credentials_folder,
// (sheet_name, list_name, {config_path})
"add_rule": config_utils.add_rule,
"help": help,
// ({config_path}, {sheet_name}, {list_name})
"export": export_configs,
}
function start() {
fs.mkdirSync(AUTH_DIR, {recursive: true})
if (!fs.existsSync(CREDENTIALS_PATH)) {
setup_credentials()
return
}
let command = process.argv[2];
if (command && COMMANDS[command]) {
COMMANDS[command]();
} else {
help()
}
}
start()