-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
196 lines (174 loc) · 5.11 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
const fs = require("fs");
const path = require("path");
const globby = require("globby");
const oracledb = require("oracledb");
const mime = require("mime-types");
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
module.exports = {
// Validates a JSON object
publish({
libDir,
user,
password,
connectString,
directory,
appID,
destination,
pluginName,
}) {
// Validate arguments
if (typeof connectString === "undefined") {
throw new TypeError("connectString is required.");
}
if (typeof directory === "undefined") {
throw new TypeError("directory is a required argument.");
}
if (typeof appID === "undefined") {
throw new TypeError("appID is a required argument.");
}
if (
destination.toLowerCase() === "plugin" &&
typeof pluginName === "undefined"
) {
throw new Error("pluginName is a required argument.");
}
if (!fs.existsSync(directory)) {
throw new Error(`Directory ${directory} is not a valid path.`);
}
async function run() {
let connection;
let result;
let createFileApi;
const files = await globby([directory]);
if (files.length === 0) {
console.log("Directory is empty.");
} else {
switch (destination.toLowerCase()) {
case "theme":
console.log(`Uploading to ${appID} - Theme Files...`);
createFileApi = `
wwv_flow_api.create_theme_file (
p_flow_id => :application_id,
p_theme_id => :theme_number,
p_file_name => :file_name,
p_mime_type => :mime_type,
p_file_charset => 'utf-8',
p_file_content => :b
);`;
break;
case "workspace":
console.log(`Uploading to ${appID} - Workspace Files...`);
createFileApi = `
wwv_flow_api.create_workspace_static_file (
p_file_name => :file_name,
p_mime_type => :mime_type,
p_file_charset => 'utf-8',
p_file_content => :b
);`;
break;
case "plugin":
console.log(
`Uploading to ${appID} - ${pluginName} - Plugin Files...`
);
createFileApi = `
declare
l_plugin_id apex_appl_plugins%plugin_id%type;
begin
select plugin_id
into l_plugin_id
from apex_appl_plugins
where application_id = l_app_id
and name = l_plugin_name
;
wwv_flow_api.create_plugin_file (
p_flow_id => :application_id,
p_plugin_id => l_plugin_id,
p_file_name => :file_name,
p_mime_type => :mime_type,
p_file_charset => 'utf-8',
p_file_content => :b
);
exception when no_data_found then
raise_application_error(-20001, 'Plugin ' || l_plugin_name || ' is not valid.');
end;`;
break;
default:
console.log(`Uploading to ${appID} - Application Static Files...`);
createFileApi = `
wwv_flow_api.create_app_static_file (
p_flow_id => :application_id,
p_file_name => :file_name,
p_mime_type => :mime_type,
p_file_charset => 'utf-8',
p_file_content => :b
);`;
}
try {
oracledb.initOracleClient({ libDir });
connection = await oracledb.getConnection({
user,
password,
connectString,
});
result = await connection.execute(
`
declare
l_app_id varchar2(100) := :appID;
l_workspace_id apex_applications.workspace_id%type;
begin
select application_id, theme_number, workspace_id
into :application_id, :theme_number, l_workspace_id
from apex_applications
where to_char(application_id) = l_app_id
or upper(alias) = upper(l_app_id);
apex_util.set_security_group_id (p_security_group_id => l_workspace_id);
execute immediate 'alter session set current_schema='||apex_application.g_flow_schema_owner;
exception when no_data_found then
raise_application_error(-20001, 'Application ' || l_app_id || ' is not valid.');
end;`,
{
appID,
application_id: { type: oracledb.NUMBER, dir: oracledb.BIND_OUT },
theme_number: { type: oracledb.NUMBER, dir: oracledb.BIND_OUT },
}
);
const application_id = result.outBinds.application_id;
const theme_number = result.outBinds.theme_number;
await asyncForEach(files, async (file) => {
const b = fs.readFileSync(file);
const file_name = path.relative(directory, file);
const mime_type = mime.lookup(file);
let opts = {
b,
file_name,
mime_type,
application_id
};
if (destination.toLowerCase() === 'theme') {
opts.theme_number = theme_number;
}
const result2 = await connection.execute(`begin ${createFileApi} end;`, opts);
console.log(`Uploaded ${file_name}`);
});
console.log(`Uploading Complete`);
} catch (err) {
console.error(err);
} finally {
if (connection) {
try {
await connection.commit();
await connection.close();
} catch (err) {
console.error(err);
}
}
}
}
}
run();
},
};