create-nexaui-electron
Version:
Create Nexa App - Tool untuk membuat aplikasi Nexa Electron
197 lines (171 loc) ⢠6.02 kB
JavaScript
const fs = require("fs-extra");
const path = require("path");
const { execSync } = require("child_process");
const chalk = require("chalk");
const ora = require("ora");
process.on("unhandledRejection", (err) => {
console.error(chalk.red("Failed to execute nexa-create"));
console.error(err);
process.exit(1);
});
async function createApp() {
console.log(chalk.blue("\nš Welcome to Create Nexa App!\n"));
// Get project name
const projectName = process.argv[2];
if (!projectName) {
console.error(chalk.red("Please specify project name:"));
console.log(chalk.cyan(" npx nexa-create my-app"));
process.exit(1);
}
const projectPath = path.join(process.cwd(), projectName);
const templatesPath = path.join(__dirname, "../scripts");
// Tambahkan logging
console.log("Template path:", templatesPath);
console.log("Project path:", projectPath);
// Check if directory exists
if (fs.existsSync(projectPath)) {
console.error(chalk.red(`\nā Directory ${projectName} already exists.`));
process.exit(1);
}
console.log(chalk.green(`\nš Creating a new Nexa app in ${projectPath}\n`));
try {
// Buat struktur folder dasar
await fs.ensureDir(projectPath);
// Periksa isi folder template
const templateContents = await fs.readdir(templatesPath);
console.log("\nTemplate contents:", templateContents);
// Salin main.js, preload.js, dan nodemon.json
const filesToCopy = [
{ src: "main.js", dest: "main.js" },
{ src: "preload.js", dest: "preload.js" },
];
for (const file of filesToCopy) {
const srcPath = path.join(templatesPath, file.src);
const destPath = path.join(projectPath, file.dest);
if (await fs.pathExists(srcPath)) {
await fs.copy(srcPath, destPath);
console.log(`Copied ${file.src}`);
} else {
console.log(`${file.src} not found in template`);
}
}
// Salin folder assets dengan struktur yang benar
const assetsPath = path.join(templatesPath, "assets");
if (await fs.pathExists(assetsPath)) {
await fs.copy(assetsPath, path.join(projectPath, "assets"));
console.log("Copied assets folder");
} else {
console.log("assets folder not found in template");
}
// Salin folder public
const publicPath = path.join(templatesPath, "public");
if (await fs.pathExists(publicPath)) {
await fs.copy(publicPath, path.join(projectPath, "public"));
console.log("Copied public folder");
} else {
console.log("public folder not found in template");
}
// Create package.json
const packageJson = {
name: projectName,
version: "1.0.0",
description: "Aplikasi nexaui electron",
main: "main.js",
scripts: {
start: "electron .",
dev: "electron . --dev",
pack: "electron-builder --dir",
dist: "electron-builder",
build: "electron-builder build --win --publish never",
deploy: "electron-builder build --win --publish always",
},
author: "",
license: "ISC",
devDependencies: {
electron: "^28.0.0",
"electron-builder": "^24.13.3",
"electron-reloader": "^1.2.3",
},
dependencies: {
express: "^4.21.2",
jsdom: "^22.1.0",
portfinder: "^1.0.32",
},
build: {
appId: `com.${projectName}.app`,
productName: projectName,
directories: {
output: "dist",
},
win: {
target: ["nsis", "portable"],
icon: "assets/icon.ico",
},
nsis: {
oneClick: false,
allowToChangeInstallationDirectory: true,
},
extraResources: [
{
from: "assets",
to: "assets",
},
{
from: "public",
to: "public",
},
],
},
};
await fs.writeJson(path.join(projectPath, "package.json"), packageJson, {
spaces: 2,
EOL: "\n",
});
// Install dependencies
const spinner = ora("Installing packages...").start();
try {
console.log("\nInstalling dependencies in:", projectPath);
// Install dependencies
execSync("npm install", {
cwd: projectPath,
stdio: "inherit",
});
spinner.succeed("Packages installed successfully!");
} catch (error) {
spinner.fail("Failed to install packages");
console.error("\nDetailed error:");
console.error(error.message);
// Cek apakah package.json ada
const pkgExists = await fs.pathExists(
path.join(projectPath, "package.json")
);
console.log("\npackage.json exists:", pkgExists);
// Tampilkan isi package.json
if (pkgExists) {
const pkg = await fs.readJson(path.join(projectPath, "package.json"));
console.log("\npackage.json contents:", JSON.stringify(pkg, null, 2));
}
throw error;
}
console.log(chalk.green("\nā
Success! Created"), chalk.cyan(projectName));
console.log(chalk.yellow("\nInside that directory, you can run:\n"));
console.log(chalk.cyan(" npm start"));
console.log(" Starts the development server.\n");
console.log(chalk.cyan(" npm run dev"));
console.log(" Starts the development server with hot reload.\n");
console.log(chalk.yellow("We suggest that you begin by typing:\n"));
console.log(chalk.cyan(` cd ${projectName}`));
console.log(chalk.cyan(" npm start\n"));
} catch (error) {
console.error(chalk.red("\nā Error:"), error);
// Cleanup
await fs.remove(projectPath);
process.exit(1);
}
}
createApp().catch((err) => {
console.error(chalk.red("Failed to create app"));
console.error(err);
process.exit(1);
});