UNPKG

3.46 kBJavaScriptView Raw
1/* Copyright (c) 2019, 2023, Oracle and/or its affiliates. */
2
3/******************************************************************************
4 *
5 * This software is dual-licensed to you under the Universal Permissive License
6 * (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
7 * 2.0 as shown at https://www.apache.org/licenses/LICENSE-2.0. You may choose
8 * either license.
9 *
10 * If you elect to accept the software under the Apache License, Version 2.0,
11 * the following applies:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * https://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * NAME
26 * prunebinaries.js
27 *
28 * DESCRIPTION
29 * Removes pre-built binaries for all other architectures.
30 * This can be used to reduce the footprint of a node-oracledb install.
31 *
32 * USAGE
33 * Invoke this from the top level directory.
34 * After an 'npm install oracledb' installs pre-built binaries, this file
35 * can be run with 'npm run prune [all]'.
36 * 'npm run prune' will keep only the binary files for the platform where
37 * the application using node-oracledb is being run.
38 * Passing the optional 'all' parameter deletes all binary files, so that
39 * only Thin mode will be available.
40 *
41 *****************************************************************************/
42
43'use strict';
44
45const fs = require('fs');
46const nodbUtil = require('../lib/util.js');
47
48const dir = nodbUtil.RELEASE_DIR; // contains the binaries
49
50let re = new RegExp(nodbUtil.BINARY_FILE);
51
52try {
53 let f = fs.readdirSync(dir);
54 let opt = process.argv[2];
55
56 if (opt) {
57 // 'npm run prune <option>' is called
58 if (opt.toLowerCase() === 'all') {
59 // Remove all the binaries in nodbUtil.RELEASE_DIR
60 for (let i = 0; i < f.length; i++) {
61 removeFileorDir(dir + '/' + f[i]);
62 }
63 } else {
64 // Invalid option
65 throw new Error('Invalid Command option: ' + `'${opt}'. Usage 'npm run prune [all]'`);
66 }
67 } else {
68 // 'npm run prune' is called
69 for (let i = 0; i < f.length; i++) {
70 if (!f[i].match(re) && (f[i].match(/oracledb.*\.node(-buildinfo.txt)*/))) {
71 fs.unlinkSync(dir + '/' + f[i]);
72 }
73 }
74 }
75} catch (err) {
76 console.error(err.message);
77}
78
79async function removeFileorDir(fileOrDirPath) {
80 try {
81 const isDir = fs.statSync(fileOrDirPath).isDirectory();
82 if (isDir) {
83 // Remove the directory and its files recursively
84 // Use fs.promises for Node.js versions 14.6-14.13,
85 // which do not support fs.rmSync()
86 const vs = process.version.substring(1).split(".").map(Number);
87 if (vs[0] > 14 || (vs[0] === 14 && vs[1] >= 14))
88 fs.rmSync(fileOrDirPath, { recursive: true, force: true });
89 else
90 await fs.promises.rmdir(fileOrDirPath, { recursive: true, force: true });
91 } else {
92 // Remove the file or the symlink synchronously
93 fs.unlinkSync(fileOrDirPath);
94 }
95 } catch (err) {
96 throw new Error('Invalid File or Directory: ' + `'${fileOrDirPath}'`);
97 }
98}