UNPKG

3.06 kBPlain TextView Raw
1#!/usr/bin/env node
2
3console.error("The ns-verify-bundle script is deprecated!")
4
5const path = require("path");
6const fs = require("fs");
7
8const { getProjectDir } = require("../projectHelpers");
9
10const PROJECT_DIR = getProjectDir();
11const APP_ID = require(path.resolve(PROJECT_DIR, "./package.json")).nativescript.id;
12const APP_NAME = APP_ID.substring(APP_ID.lastIndexOf(".") + 1);
13const 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
18const npmArgs = JSON.parse(process.env.npm_config_argv).original;
19const flags = npmArgs.filter(a => a.startsWith("--")).map(a => a.substring(2));
20const file = getTargetFile(flags);
21const platform = getPlatform(flags);
22
23const filePath = path.resolve(PROJECT_PATHS[platform], file);
24
25console.log(`Checking ${filePath} exists`);
26if (!fs.existsSync(filePath)) {
27 throwError({message: `${filePath} doesn not exist!`});
28}
29
30const maxSize = getMaxSize(flags);
31if (maxSize) {
32 checkFileSizeIsUnder(filePath, maxSize).then().catch(throwError);
33}
34
35function 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
46function 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
59function 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
73function 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
88function 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
100function throwError(error) {
101 console.error(error.message);
102 process.exit(error.code || 1);
103}
104