mega-pkg
Version:
> This readme is written by the author of this package [amanuel](https://github.com/amanuel15)
165 lines (140 loc) • 3.8 kB
JavaScript
const shell = require("shelljs");
const chalk = require("chalk");
const { spawn } = require("child_process");
const logcat = require("adbkit-logcat");
module.exports.postProcessNode = (path) => {
shell.cd(path);
let cmd = "";
if (shell.which("yarn")) {
cmd = "yarn install";
} else if (shell.which("npm")) {
cmd = "npm install";
}
if (cmd) {
const result = shell.exec(cmd);
if (result.code !== 0) {
return false;
} else {
console.log(chalk.green("Your app is ready"));
}
} else {
console.log(chalk.red("No yarn or npm found. Cannot run installation."));
}
return true;
};
module.exports.installPkg = (package) => {
let cmd = "";
if (shell.which("yarn")) {
cmd = `yarn add ${package}`;
} else if (shell.which("npm")) {
cmd = `npm install ${package}`;
}
if (cmd) {
const result = shell.exec(cmd);
if (result.code !== 0) {
return false;
} else {
console.log(
chalk.green(`${package} is successfully added to your project`)
);
}
} else {
console.log(chalk.red("No yarn or npm found. Cannot run installation."));
}
return true;
};
module.exports.removePkg = (package) => {
let cmd = "";
if (shell.which("yarn")) {
cmd = `yarn remove ${package}`;
} else if (shell.which("npm")) {
cmd = `npm uninstall ${package}`;
}
if (cmd) {
const result = shell.exec(cmd);
if (result.code !== 0) {
return false;
} else {
console.log(
chalk.green(`${package} is successfully removed from your project`)
);
}
} else {
console.log(chalk.red("No yarn or npm found. Cannot run installation."));
}
return true;
};
module.exports.startApp = () => {
let cmd = "npx react-native start";
// if (shell.which("yarn")) {
// cmd = `yarn start`;
// } else if (shell.which("npm")) {
// cmd = `npm start`;
// }
if (cmd) {
const result = shell.exec(cmd);
if (result.code !== 0) {
return false;
} else {
console.log(chalk.green(`Your app is running`));
}
} else {
console.log(chalk.red("Something went wrong."));
}
return true;
};
module.exports.runBundle = (enteryFile) => {
let cmd = `npx react-native bundle --platform android --dev false --entry-file ${enteryFile}.js --bundle-output mdp/index.android.bundle --assets-dest ./mdp`;
if (cmd) {
const result = shell.exec(cmd);
if (result.code !== 0) {
return false;
} else {
console.log(
chalk.green(
"You can find your bundle in the mdp folder in the root folder of your project."
)
);
}
} else {
console.log(chalk.red("Something went wrong!"));
}
return true;
};
module.exports.debug = () => {
const proc = spawn("adb", ["logcat", "-B"]);
// Connect logcat to the stream
reader = logcat.readStream(proc.stdout);
const date = new Date();
reader.on("entry", (entry) => {
const entryDate = new Date(entry.date);
// console.log(entryDate, date, entryDate > date);
if (entry.tag == "ReactNativeJS" && entryDate > date) {
switch (entry.priority) {
case 3:
console.log(chalk.blue("Debug: "), chalk.green(entry.message));
break;
case 4:
console.log(chalk.cyan("Info: "), chalk.green(entry.message));
break;
case 5:
console.log(chalk.yellow("Warning: "), entry.message);
break;
case 6:
console.log(chalk.red("Error: "), entry.message);
break;
default:
console.log(chalk.bgGray("Unknown:"), " ", entry.message);
}
// console.log(entry.message);
}
// if (entry.tag == "ReactContextBaseJavaModule") {
// console.log(entry);
// }
});
// Make sure we don't leave anything hanging
process.on("exit", () => {
proc.kill();
});
return true;
};