-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.js
112 lines (95 loc) · 3.57 KB
/
build.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
const yargs = require('yargs');
const Fs = require('fs');
const Path = require('path');
const { exit } = require('yargs');
const { execSync } = require('child_process');
const { Base64 } = require('js-base64');
const options = yargs
.usage('Usage: -webhookname <name>')
.option('app-authentication', {
alias: 'app_authentication',
describe: 'Application Authentication',
type: 'string',
demandOption: false,
}).argv;
const webHookName = options.name;
const { app_authentication } = options;
// Check if file exists
const configPath = Path.join(__dirname, './realm-app/realm_config.json');
if (!Fs.existsSync(configPath)) {
console.log('Please pull application configuration first');
exit();
}
const httpEndpointConfig = `
[
{
"route": "/http_endpoint_reactivesearch",
"http_method": "POST",
"function_name": "http_endpoint_reactivesearch",
"validation_method": "NO_VALIDATION",
"respond_result": true,
"fetch_custom_user_data": false,
"create_user_on_auth": false,
"disabled": false,
"return_type": "JSON"
}
]
`;
const httpEndpointDirectory = Path.join(
__dirname,
`./realm-app/http_endpoints`,
);
Fs.mkdirSync(httpEndpointDirectory, { recursive: true });
Fs.writeFileSync(`${httpEndpointDirectory}/config.json`, httpEndpointConfig);
// Create webhook directory (Updated:2022)
const webhookDirectory = Path.join(
__dirname,
`./realm-app/functions/`,
);
Fs.mkdirSync(webhookDirectory, { recursive: true });
const webHookConfigFileContent = `
[
{
"name": "http_endpoint_reactivesearch",
"private": false,
"run_as_system": true,
"disable_arg_logs": true
}
]
`;
const webHookConfigFilePath = Path.join(webhookDirectory, 'config.json');
Fs.writeFileSync(webHookConfigFilePath, webHookConfigFileContent);
const webHookSourceFilePath = Path.join(webhookDirectory, 'http_endpoint_reactivesearch.js');
if (!Fs.existsSync('dist')) {
Fs.mkdirSync('dist');
}
execSync(
`./node_modules/concat/bin/concat -o dist/source.ts ./src/searchFunction/source.ts ./src/constants.ts ./src/utils.ts ./src/types/* ./src/validators/* ./src/targets/* ./src/searchFunction/schema.ts ./src/searchFunction/index.ts`,
);
execSync(`cp ./src/searchFunction/realm.d.ts ./dist/realm.d.ts`);
execSync(`cp ./src/validate/schema.js ./dist/schema.js`);
const regex =
/import(?:["'\s]*([\w*{}\n\r\t, ]+)from\s*)?["'\s].*([@\w_-]+)["'\s].*;$/gm;
const data = Fs.readFileSync('./dist/source.ts', { encoding: 'utf-8' });
var result = data.replace(regex, '');
result = result.replace(new RegExp('export const', 'g'), 'const');
result = result.replace(new RegExp('export type', 'g'), 'type');
if (app_authentication) {
result = result.replace(
new RegExp('AUTHORIZATION_CREDENTIALS = null', 'g'),
`AUTHORIZATION_CREDENTIALS = "Basic ${Base64.encode(app_authentication)}"`,
);
}
Fs.writeFileSync('./dist/source.ts', result, { encoding: 'utf-8' });
execSync(`./node_modules/typescript/bin/tsc ./dist/source.ts`);
execSync(
`./node_modules/concat/bin/concat -o dist/source.js dist/source.js ./src/validate/*`,
);
let dataJS = Fs.readFileSync('./dist/source.js', { encoding: 'utf-8' });
dataJS = dataJS.replace(new RegExp('exports.__esModule = true;', 'g'), '');
dataJS = dataJS.replace(new RegExp('exports.ReactiveSearch.*;', 'g'), '');
dataJS = dataJS.replace(new RegExp('export default.*', 'g'), '');
dataJS = dataJS.replace(new RegExp('export function', 'g'), 'function');
dataJS = dataJS.replace(regex, '');
Fs.writeFileSync('./dist/source.js', dataJS, { encoding: 'utf-8' });
execSync(`mv ./dist/source.js ${webHookSourceFilePath}`);