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

fix: switch from npx to npm run, closes #367 #387

Merged
merged 6 commits into from
Mar 8, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changes/fix-npx-workspace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'action': patch
---

The action will now use `npm run tauri` instead of `npx tauri` to prevent issues in npm workspaces.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ These inputs are _typically_ only used if your GitHub repo does not contain an e

These inputs allow you to change how your Tauri project will be build.

| Name | Required | Description | Type | Default |
| ---------------- | :------: | ------------------------------------------------------------------------------------------ | ------ | ----------------- |
| `includeDebug` | false | whether to include a debug build or not | bool | false |
| `includeRelease` | false | whether to include a release build or not | bool | true |
| `tauriScript` | false | the script to execute the Tauri CLI. It must not include any args or commands like `build` | string | `yarn\|npx tauri` |
| `args` | false | Additional arguments to the current build command | string | |
| Name | Required | Description | Type | Default |
| ---------------- | :------: | ------------------------------------------------------------------------------------------ | ------ | --------------------------- |
| `includeDebug` | false | whether to include a debug build or not | bool | false |
| `includeRelease` | false | whether to include a release build or not | bool | true |
| `tauriScript` | false | the script to execute the Tauri CLI. It must not include any args or commands like `build` | string | `npm run\|pnpm\|yarn tauri` |
| `args` | false | Additional arguments to the current build command | string | |

### Release Configuration

Expand Down Expand Up @@ -236,8 +236,10 @@ These inputs allow you to modify the GitHub release.

- You can use this Action on a repo that doesn't have Tauri configured. We automatically initialize Tauri before building, and configure it to use your Web artifacts.
- You can configure Tauri with the `configPath`, `distPath` and `iconPath` options.
- You can run custom Tauri CLI scripts with the `tauriScript` option. So instead of running `yarn tauri <COMMAND> <ARGS>` or `npx tauri <COMMAND> <ARGS>`, we'll execute `${tauriScript} <COMMAND> <ARGS>`.
- You can run custom Tauri CLI scripts with the `tauriScript` option. So instead of running `yarn tauri <COMMAND> <ARGS>` or `npm run tauri <COMMAND> <ARGS>`, we'll execute `${tauriScript} <COMMAND> <ARGS>`.
- Useful when you need custom build functionality when creating Tauri apps e.g. a `desktop:build` script.
- `tauriScript` can also be an absolute file path pointing to a `tauri-cli` binary. The path currently cannot contain spaces.
- If you want to add additional arguments to the build command, you can use the `args` option. For example, if you're setting a specific target for your build, you can specify `args: --target your-target-arch`.
- When your app isn't on the root of the repo, use the `projectPath` input.
- When your Tauri app is not in the root of the repo, use the `projectPath` input.
- Usually it will work without it, but the action will install and use a global `@tauri-apps/cli` installation instead of your project's CLI which can cause issues if you also configured `tauriScript` or if you have multiple `tauri.conf.json` files in your repo.
- If you create the release yourself and provide a `releaseId` but do not set `tagName`, the download url for updater bundles in `latest.json` will point to `releases/latest/download/<bundle>` which can cause issues if your repo contains releases that do not include updater bundles.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

61 changes: 7 additions & 54 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,10 @@ import { readFileSync, existsSync, copyFileSync, writeFileSync } from 'fs';
import { join } from 'path';

import { initProject } from './init-project';
import {
execCommand,
getInfo,
getTargetDir,
getWorkspaceDir,
hasDependency,
usesPnpm,
usesYarn,
} from './utils';

import type { Artifact, BuildOptions, Runner } from './types';

async function getRunner(
root: string,
tauriScript: string | null
): Promise<Runner> {
if (tauriScript) {
const [runnerCommand, ...runnerArgs] = tauriScript.split(' ');
return { runnerCommand, runnerArgs };
}

if (
hasDependency('@tauri-apps/cli', root) ||
hasDependency('vue-cli-plugin-tauri', root)
) {
if (usesYarn(root)) return { runnerCommand: 'yarn', runnerArgs: ['tauri'] };
if (usesPnpm(root)) return { runnerCommand: 'pnpm', runnerArgs: ['tauri'] };
// FIXME: This can trigger a download of the tauri alpha package. Likely when the tauri frontend is part of a workspace and projectPath is undefined.
return { runnerCommand: 'npx', runnerArgs: ['tauri'] };
}

await execCommand('npm', ['install', '-g', '@tauri-apps/cli'], {
cwd: undefined,
});
import { getRunner } from './runner';
import { getInfo, getTargetDir, getWorkspaceDir, hasDependency } from './utils';

return { runnerCommand: 'tauri', runnerArgs: [] };
}
import type { Artifact, BuildOptions } from './types';

export async function buildProject(
root: string,
Expand Down Expand Up @@ -74,29 +41,15 @@ export async function buildProject(
const tauriArgs = debug
? ['--debug', ...(buildOpts.args ?? [])]
: buildOpts.args ?? [];
let buildCommand;
let buildArgs: string[] = [];

let buildCommand;
if (hasDependency('vue-cli-plugin-tauri', root)) {
if (usesYarn(root)) {
buildCommand = 'yarn';
buildArgs = ['tauri:build'];
}
if (usesPnpm(root)) {
buildCommand = 'pnpm';
buildArgs = ['tauri:build'];
} else {
buildCommand = 'npm';
buildArgs = ['run', 'tauri:build'];
}
buildCommand = 'tauri:build';
} else {
buildCommand = app.runner.runnerCommand;
buildArgs = [...app.runner.runnerArgs, 'build'];
buildCommand = 'build';
}

await execCommand(buildCommand, [...buildArgs, ...tauriArgs], {
cwd: root,
});
await runner.execTauriCommand([buildCommand], [...tauriArgs], root);

let fileAppName = app.name;
// on Linux, the app product name is converted to kebab-case
Expand Down
23 changes: 8 additions & 15 deletions src/init-project.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { writeFileSync } from 'fs';
import { join } from 'path';

import { execCommand, getConfig, getPackageJson, getTauriDir } from './utils';
import { Runner } from './runner';
import { getConfig, getPackageJson, getTauriDir } from './utils';

import type { Application, BuildOptions, Info, Runner } from './types';
import type { Application, BuildOptions, Info } from './types';

export async function initProject(
root: string,
Expand All @@ -14,12 +15,10 @@ export async function initProject(
const packageJson = getPackageJson(root);
const tauriPath = getTauriDir(root);

await execCommand(
runner.runnerCommand,
[...runner.runnerArgs, 'init', '--ci', '--app-name', info.name],
{
cwd: root,
}
await runner.execTauriCommand(
['init'],
['--ci', '--app-name', info.name],
root
);

if (tauriPath === null) {
Expand Down Expand Up @@ -68,13 +67,7 @@ export async function initProject(
};

if (iconPath) {
await execCommand(
runner.runnerCommand,
[...runner.runnerArgs, 'icon', join(root, iconPath)],
{
cwd: root,
}
);
await runner.execTauriCommand(['icon', join(root, iconPath)], [], root);
}

return app;
Expand Down
68 changes: 68 additions & 0 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { execCommand, hasDependency, usesPnpm, usesYarn } from './utils';

class Runner {
// Could be "npm", "yarn", "pnpm", "cargo", "path/to/tauri-cli/binary" or "tauri"
bin: string;
// could be ["tauri"], ["run", "tauri"], ["some package.json script"], ["run", "some package.json script"] or []
tauriScript: string[];
// vue-cli-plugin-tauri uses `tauri:build` instead of `tauri build`
vueCli: boolean;

constructor(bin: string, tauriScript?: string[], vueCli?: boolean) {
this.bin = bin;
this.tauriScript = tauriScript || [];
this.vueCli = !!vueCli;
}

async execTauriCommand(
command: string[],
commandOptions: string[],
cwd?: string
): Promise<void> {
const args: string[] = [];

if (this.bin === 'npm' && this.tauriScript[0] !== 'run') {
args.push('run');
}

if (!(this.vueCli && command[0] === 'tauri:build')) {
args.push(...this.tauriScript);
}

args.push(...command);

if (this.bin === 'npm' && commandOptions.length) {
args.push('--');
}

args.push(...commandOptions);

return execCommand(this.bin, args, { cwd });
}
}

async function getRunner(
root: string,
tauriScript: string | null
): Promise<Runner> {
if (tauriScript) {
// FIXME: This will also split file paths with spaces.
const [runnerCommand, ...runnerArgs] = tauriScript.split(' ');
return new Runner(runnerCommand, runnerArgs);
}

const vueCli = hasDependency('vue-cli-plugin-tauri', root);
if (hasDependency('@tauri-apps/cli', root) || vueCli) {
if (usesYarn(root)) return new Runner('yarn', ['tauri'], vueCli);
if (usesPnpm(root)) return new Runner('pnpm', ['tauri'], vueCli);
return new Runner('npm', ['run', 'tauri'], vueCli);
}

await execCommand('npm', ['install', '-g', '@tauri-apps/cli'], {
cwd: undefined,
});

return new Runner('tauri');
}

export { Runner, getRunner };
7 changes: 2 additions & 5 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Runner } from './runner';

export interface Application {
tauriPath: string;
runner: Runner;
Expand Down Expand Up @@ -36,11 +38,6 @@ export interface Info {
wixLanguage: string | string[] | { [language: string]: unknown };
}

export interface Runner {
runnerCommand: string;
runnerArgs: string[];
}

export interface TauriConfig {
package?: {
productName?: string;
Expand Down