Skip to content

Commit

Permalink
feat: Add support for tauri v2-beta config structure (#702)
Browse files Browse the repository at this point in the history
* feat: Add support for tauri v2-beta config structure

Co-authored-by: l1xnan <l1xnan@qq.com>

* init-project.ts

---------

Co-authored-by: l1xnan <l1xnan@qq.com>
  • Loading branch information
FabianLars and l1xnan committed Feb 8, 2024
1 parent 4bb1df9 commit 81921ba
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changes/tauri-v2-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
action: 'patch'
---

Add support for Tauri's new config structure introduced in `2.0.0-beta.0`.
6 changes: 3 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

64 changes: 52 additions & 12 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { parse as parseToml } from '@iarna/toml';
import JSON5 from 'json5';
import merge from 'lodash.merge';

import { TauriConfig } from './types';
import { TauriConfigV1, TauriConfigV2 } from './types';

function _tryParseJsonConfig(contents: string): TauriConfig | null {
function _tryParseJsonConfig(
contents: string,
): TauriConfigV1 | TauriConfigV2 | null {
try {
const config = JSON.parse(contents) as TauriConfig;
const config = JSON.parse(contents) as TauriConfigV1 | TauriConfigV2;
return config;
} catch (e) {
// @ts-ignore
Expand All @@ -18,9 +20,11 @@ function _tryParseJsonConfig(contents: string): TauriConfig | null {
}
}

function _tryParseJson5Config(contents: string): TauriConfig | null {
function _tryParseJson5Config(
contents: string,
): TauriConfigV1 | TauriConfigV2 | null {
try {
const config = JSON5.parse(contents) as TauriConfig;
const config = JSON5.parse(contents) as TauriConfigV1 | TauriConfigV2;
return config;
} catch (e) {
// @ts-ignore
Expand All @@ -29,9 +33,11 @@ function _tryParseJson5Config(contents: string): TauriConfig | null {
}
}

function _tryParseTomlConfig(contents: string): TauriConfig | null {
function _tryParseTomlConfig(
contents: string,
): TauriConfigV1 | TauriConfigV2 | null {
try {
const config = parseToml(contents) as TauriConfig;
const config = parseToml(contents) as TauriConfigV1 | TauriConfigV2;
return config;
} catch (e) {
// @ts-ignore
Expand All @@ -43,7 +49,7 @@ function _tryParseTomlConfig(contents: string): TauriConfig | null {
function readPlatformConfig(
tauriDir: string,
platform: string,
): TauriConfig | null {
): TauriConfigV1 | TauriConfigV2 | null {
let path = join(tauriDir, `tauri.${platform}.conf.json`);
if (existsSync(path)) {
const contents = readFileSync(path).toString();
Expand All @@ -70,7 +76,7 @@ function readPlatformConfig(

/// This modifies baseConfig in-place!
export function mergePlatformConfig(
baseConfig: TauriConfig,
baseConfig: TauriConfigV1 | TauriConfigV2,
tauriDir: string,
target: string,
) {
Expand All @@ -84,7 +90,7 @@ export function mergePlatformConfig(
/// This modifies baseConfig in-place!
export function mergeUserConfig(
root: string,
baseConfig: TauriConfig,
baseConfig: TauriConfigV1 | TauriConfigV2,
mergeConfig: string,
) {
let config = _tryParseJsonConfig(mergeConfig);
Expand All @@ -106,7 +112,7 @@ export function mergeUserConfig(

export function getConfig(
tauriDir: string /* customPath?: string */,
): TauriConfig {
): TauriConfigV1 | TauriConfigV2 {
/* if (customPath) {
return readCustomConfig(customPath);
} */
Expand Down Expand Up @@ -139,7 +145,7 @@ export function getConfig(
throw "Couldn't locate or parse tauri config.";
}

function readCustomConfig(customPath: string) {
function readCustomConfig(customPath: string): TauriConfigV1 | TauriConfigV2 {
if (!existsSync(customPath)) {
throw `Provided config path \`${customPath}\` does not exist.`;
}
Expand All @@ -164,3 +170,37 @@ function readCustomConfig(customPath: string) {

throw `Couldn't parse \`${customPath}\` as ${ext.substring(1)}.`;
}

export function isV2Config(
config: TauriConfigV1 | TauriConfigV2,
): config is TauriConfigV2 {
return 'identifier' in config;
}

export function convertToV2Config(config: TauriConfigV1): TauriConfigV2 {
if (!config.tauri?.bundle?.identifier) {
throw Error('v1 config has no bundle identifier.');
}

return {
identifier: config.tauri?.bundle?.identifier,
productName: config.package?.productName,
version: config.package?.version,
build: {
frontendDist: config.build?.distDir,
beforeBuildCommand: config.build?.beforeBuildCommand,
},
bundle: {
linux: {
rpm: {
release: config.tauri?.bundle?.rpm?.release,
},
},
windows: {
wix: {
language: config.tauri?.bundle?.windows?.wix,
},
},
},
};
}
101 changes: 86 additions & 15 deletions src/init-project.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { writeFileSync } from 'fs';
import { join } from 'path';

import { getConfig } from './config';
import { getConfig, isV2Config } from './config';
import { Runner } from './runner';
import { getTauriDir } from './utils';

import type { InitOptions } from './types';
import type { InitOptions, TauriConfigV1, TauriConfigV2 } from './types';

export async function initProject(
root: string,
runner: Runner,
{ iconPath, bundleIdentifier, distPath, appName, appVersion }: InitOptions,
options: InitOptions,
) {
await runner.execTauriCommand(['init'], ['--ci'], root);

Expand All @@ -23,36 +23,60 @@ export async function initProject(

const config = getConfig(tauriPath);

if (isV2Config(config)) {
initV2(config, options, tauriPath);
} else {
initV1(config, options, tauriPath);
}

if (options.iconPath) {
await runner.execTauriCommand(
['icon', join(root, options.iconPath)],
[],
root,
);
}
}

function initV1(
config: TauriConfigV1,
options: InitOptions,
tauriPath: string,
) {
console.log(
`Replacing tauri.conf.json config - package.version=${appVersion}`,
`Replacing tauri.conf.json config - package.version=${options.appVersion}`,
);
const pkgConfig = {
...config.package,
version: appVersion ?? undefined,
version: options.appVersion ?? undefined,
};

console.log(
`Replacing tauri.conf.json config - package.productName=${appName}`,
`Replacing tauri.conf.json config - package.productName=${options.appName}`,
);
pkgConfig.productName = appName ?? undefined;
pkgConfig.productName = options.appName ?? undefined;
config.package = pkgConfig;

if (bundleIdentifier) {
if (options.bundleIdentifier) {
console.log(
`Replacing tauri.conf.json config - tauri.bundle.identifier=${bundleIdentifier}`,
`Replacing tauri.conf.json config - tauri.bundle.identifier=${options.bundleIdentifier}`,
);
config.tauri = {
...config.tauri,
bundle: {
...config.tauri?.bundle,
identifier: bundleIdentifier,
identifier: options.bundleIdentifier,
},
};
}
if (distPath) {
console.log(`Replacing tauri.conf.json config - build.distDir=${distPath}`);

if (options.distPath) {
console.log(
`Replacing tauri.conf.json config - build.distDir=${options.distPath}`,
);
config.build = {
...config.build,
distDir: distPath,
distDir: options.distPath,
};
}

Expand All @@ -64,8 +88,55 @@ export async function initProject(

const configPath = join(tauriPath, 'tauri.conf.json');
writeFileSync(configPath, JSON.stringify(config, null, 2));
}

function initV2(
config: TauriConfigV2,
options: InitOptions,
tauriPath: string,
) {
console.log(
`Replacing tauri.conf.json config - version=${options.appVersion}`,
);
config = {
...config,
version: options.appVersion ?? undefined,
};

console.log(
`Replacing tauri.conf.json config - productName=${options.appName}`,
);
config = {
...config,
productName: options.appName ?? undefined,
};

if (options.bundleIdentifier) {
console.log(
`Replacing tauri.conf.json config - identifier=${options.bundleIdentifier}`,
);
config = {
...config,
identifier: options.bundleIdentifier ?? undefined,
};
}

if (iconPath) {
await runner.execTauriCommand(['icon', join(root, iconPath)], [], root);
if (options.distPath) {
console.log(
`Replacing tauri.conf.json config - build.frontendDist=${options.distPath}`,
);
config.build = {
...config.build,
frontendDist: options.distPath,
};
}

// `tauri init` defaults to npm run dev/build but we can't assume that here.
config.build = {
...config.build,
beforeBuildCommand: '',
};

const configPath = join(tauriPath, 'tauri.conf.json');
writeFileSync(configPath, JSON.stringify(config, null, 2));
}
24 changes: 23 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface TargetInfo {
platform: TargetPlatform;
}

export interface TauriConfig {
export interface TauriConfigV1 {
package?: {
productName?: string;
version?: string;
Expand All @@ -75,6 +75,28 @@ export interface TauriConfig {
};
}

export interface TauriConfigV2 {
identifier: string;
productName?: string;
version?: string;
build?: {
frontendDist?: string;
beforeBuildCommand?: string;
};
bundle?: {
linux?: {
rpm?: {
release?: string;
};
};
windows?: {
wix?: {
language?: string | string[] | { [language: string]: unknown };
};
};
};
}

export interface CargoConfig {
build?: {
target?: string;
Expand Down
Loading

0 comments on commit 81921ba

Please sign in to comment.