Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package manager agnostic build and dev scripts for Tauri #44

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
executeWithPackageManager,
getPackageManagerExecutable,
} from "./common.js";

(async () => {
const packageManager = await getPackageManagerExecutable();
await executeWithPackageManager(packageManager, "build");
})();
77 changes: 77 additions & 0 deletions scripts/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { exec as _exec, spawn, spawnSync } from "child_process";
import { promisify } from "node:util";
const exec = promisify(_exec);

/**
* @type {Record<'npm' | 'yarn', { args?: string[] }>}}
* @constant
*/
const PACKAGE_MANAGERS_OPTIONS = {
npm: {
args: ["run"],
},
yarn: {},
};

/**
*
* @param {keyof typeof PACKAGE_MANAGERS_OPTIONS} executable
* @returns {Promise<boolean>}
*/
const hasPackageManager = async (executable) => {
try {
await exec(`${executable} --version`, { stdio: "ignore" });
return true;
} catch {
return false;
}
};

/**
*
* @returns {Promise<keyof typeof PACKAGE_MANAGERS_OPTIONS>}
*/
export const getPackageManagerExecutable = async () => {
const results = await Promise.allSettled(
Object.keys(PACKAGE_MANAGERS_OPTIONS).map((exec) =>
hasPackageManager(exec).then((isInstalled) => isInstalled && exec)
)
);

const packageManager = results.find(
({ status, value }) => status === "fulfilled" && value
);

if (!packageManager) {
console.error(
`No package manager found. Supported: ${Object.keys(
PACKAGE_MANAGERS_OPTIONS
).join(", ")}`
);
process.exit(1);
}

return packageManager.value;
};

/**
*
* @param {keyof typeof PACKAGE_MANAGERS_OPTIONS} packageManager
* @param {'build' | 'dev'} command
*/
export const executeWithPackageManager = async (packageManager, command) => {
const commandToRun = [
packageManager,
...(PACKAGE_MANAGERS_OPTIONS[packageManager].args ?? []),
command,
].join(" ");
const executable = spawn(commandToRun, {
shell: true,
cwd: process.cwd(),
timeout: 10000,
});

executable.stdout.on("data", (data) => console.log(data.toString()));
executable.stderr.on("data", (data) => console.error(data.toString()));
executable.on("exit", (code) => process.exit(code));
};
9 changes: 9 additions & 0 deletions scripts/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
executeWithPackageManager,
getPackageManagerExecutable,
} from "./common.js";

(async () => {
const packageManager = await getPackageManagerExecutable();
await executeWithPackageManager(packageManager, "dev");
})();
4 changes: 2 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"build": {
"beforeDevCommand": "yarn dev",
"beforeBuildCommand": "yarn build",
"beforeDevCommand": "node ./scripts/dev.js",
"beforeBuildCommand": "node ./scripts/build.js",
"devPath": "http://localhost:1420",
"distDir": "../dist",
"withGlobalTauri": false
Expand Down