1 | #!/usr/bin/env node
|
2 |
|
3 | console.error("The ns-verify-bundle script is deprecated!")
|
4 |
|
5 | const path = require("path");
|
6 | const fs = require("fs");
|
7 |
|
8 | const { getProjectDir } = require("../projectHelpers");
|
9 |
|
10 | const PROJECT_DIR = getProjectDir();
|
11 | const APP_ID = require(path.resolve(PROJECT_DIR, "./package.json")).nativescript.id;
|
12 | const APP_NAME = APP_ID.substring(APP_ID.lastIndexOf(".") + 1);
|
13 | const PROJECT_PATHS = {
|
14 | android: path.resolve(PROJECT_DIR, "platforms/android/src/main/assets/app"),
|
15 | ios: path.resolve(PROJECT_DIR, `platforms/ios/build/emulator/${APP_NAME}.app/app`),
|
16 | };
|
17 |
|
18 | const npmArgs = JSON.parse(process.env.npm_config_argv).original;
|
19 | const flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2));
|
20 | const file = getTargetFile(flags);
|
21 | const platform = getPlatform(flags);
|
22 |
|
23 | const filePath = path.resolve(PROJECT_PATHS[platform], file);
|
24 |
|
25 | console.log(`Checking ${filePath} exists`);
|
26 | if (!fs.existsSync(filePath)) {
|
27 | throwError({message: `${filePath} doesn not exist!`});
|
28 | }
|
29 |
|
30 | const maxSize = getMaxSize(flags);
|
31 | if (maxSize) {
|
32 | checkFileSizeIsUnder(filePath, maxSize).then().catch(throwError);
|
33 | }
|
34 |
|
35 | function getTargetFile(flags) {
|
36 | let fileFlags = flags.filter(f => f.startsWith("file="));
|
37 |
|
38 | if (fileFlags.length != 1) {
|
39 | throwError({message: "You must provide a target file!"});
|
40 | }
|
41 |
|
42 | fileFlags = fileFlags[0];
|
43 | return fileFlags.substring(fileFlags.indexOf("=") + 1);
|
44 | }
|
45 |
|
46 | function getMaxSize(flags) {
|
47 | let sizeFlags = flags.filter(f => f.startsWith("maxSize="));
|
48 |
|
49 | if (sizeFlags.length == 0) {
|
50 | return;
|
51 | } else if (sizeFlags.length > 1) {
|
52 | throwError({message: "You must provide 0 or 1 maxSize flags!"});
|
53 | }
|
54 |
|
55 | sizeFlags = sizeFlags[0];
|
56 | return sizeFlags.substring(sizeFlags.indexOf("=") + 1);
|
57 | }
|
58 |
|
59 | function getPlatform(flags) {
|
60 | if (flags.includes("android") && flags.includes("ios")) {
|
61 | throwError({message: "You cannot use both --android and --ios flags!"});
|
62 | }
|
63 |
|
64 | if (flags.includes("android")) {
|
65 | return "android";
|
66 | } else if (flags.includes("ios")) {
|
67 | return "ios";
|
68 | } else {
|
69 | throwError({message: "You must provide a target platform! Use either --android, or --ios flag."});
|
70 | }
|
71 | }
|
72 |
|
73 | function checkFileSizeIsUnder(fileName, sizeInBytes) {
|
74 | console.log(`Checking ${fileName} size is under ${sizeInBytes}`);
|
75 |
|
76 | return new Promise((resolve, reject) => {
|
77 | readFile(fileName)
|
78 | .then(content => {
|
79 | if (content.length <= sizeInBytes) {
|
80 | resolve();
|
81 | } else {
|
82 | reject({message: `File "${fileName}" exceeded file size of "${sizeInBytes}".`});
|
83 | }
|
84 | });
|
85 | });
|
86 | }
|
87 |
|
88 | function readFile(fileName) {
|
89 | return new Promise((resolve, reject) => {
|
90 | fs.readFile(fileName, "utf-8", (err, data) => {
|
91 | if (err) {
|
92 | reject(err);
|
93 | } else {
|
94 | resolve(data);
|
95 | }
|
96 | });
|
97 | });
|
98 | }
|
99 |
|
100 | function throwError(error) {
|
101 | console.error(error.message);
|
102 | process.exit(error.code || 1);
|
103 | }
|
104 |
|