Skip to content

Commit

Permalink
feat: Add support for Tauri.toml config (#375)
Browse files Browse the repository at this point in the history
* feat: Add support for `Tauri.toml` config

* fix error when conf.json doesn't exist

* log parsing error

* rebuild dist

* remove logging again

* try some nicer logging

* check the file ext of custom config

* change path import

* fix type
  • Loading branch information
FabianLars committed Feb 22, 2023
1 parent e1348b7 commit 06b006d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changes/read-tauri-toml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'action': patch
---

Add support for Tauri's toml-based config (`Tauri.toml`).
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function buildProject(
: await initProject(root, runner, info, buildOpts);

const tauriConfPath = join(app.tauriPath, 'tauri.conf.json');
if (buildOpts.configPath !== null) {
if (buildOpts.configPath) {
copyFileSync(buildOpts.configPath, tauriConfPath);
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function run(): Promise<void> {
}

const options: BuildOptions = {
configPath: existsSync(configPath) ? configPath : null,
configPath: existsSync(configPath) ? configPath : undefined,
distPath,
iconPath,
tauriScript,
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface Artifact {
}

export interface BuildOptions {
configPath: string | null;
configPath?: string;
distPath: string | null;
iconPath: string | null;
tauriScript: string | null;
Expand Down
105 changes: 82 additions & 23 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ export function getTauriDir(root: string): string | null {
} else {
ignoreRules.add('node_modules').add('target');
}
const paths = globSync('**/tauri.conf.json', {
cwd: root,
ignore: ignoreRules,
});
const paths = globSync(
['**/tauri.conf.json', '**/tauri.conf.json5', '**/Tauri.toml'],
{
cwd: root,
ignore: ignoreRules,
}
);
const tauriConfPath = paths[0];
return tauriConfPath ? resolve(root, tauriConfPath, '..') : null;
}
Expand Down Expand Up @@ -147,46 +150,102 @@ export function execCommand(
}).then();
}

function _getJson5Config(contents: string): TauriConfig | null {
function _tryParseJsonConfig(contents: string): TauriConfig | null {
try {
const config = JSON.parse(contents) as TauriConfig;
return config;
} catch (e) {
// @ts-ignore
console.error(e.message);
return null;
}
}

function _tryParseJson5Config(contents: string): TauriConfig | null {
try {
const config = JSON5.parse(contents) as TauriConfig;
return config;
} catch (e) {
// @ts-ignore
console.error(e.message);
return null;
}
}

export function getConfig(path: string): TauriConfig {
const contents = readFileSync(path).toString();
function _tryParseTomlConfig(contents: string): TauriConfig | null {
try {
const config = JSON.parse(contents) as TauriConfig;
const config = parseToml(contents) as TauriConfig;
return config;
} catch (e) {
let json5Conf = _getJson5Config(contents);
if (json5Conf === null) {
json5Conf = _getJson5Config(
readFileSync(join(path, '..', 'tauri.conf.json5')).toString()
);
// @ts-ignore
console.error(e.message);
return null;
}
}

export function getConfig(tauriDir: string, customPath?: string): TauriConfig {
if (customPath) {
if (!existsSync(customPath)) {
throw `Provided config path \`${customPath}\` does not exist.`;
}
if (json5Conf) {
return json5Conf;

const contents = readFileSync(customPath).toString();
const ext = path.extname(customPath);

if (ext === '.json') {
const config = _tryParseJsonConfig(contents);
if (config) return config;
}

if (ext === '.json5') {
const config = _tryParseJson5Config(contents);
if (config) return config;
}
throw e;

if (ext === '.toml') {
const config = _tryParseTomlConfig(contents);
if (config) return config;
}

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

if (existsSync(join(tauriDir, 'tauri.conf.json'))) {
const contents = readFileSync(join(tauriDir, 'tauri.conf.json')).toString();
const config = _tryParseJsonConfig(contents);
if (config) return config;
console.error("Found tauri.conf.json file but couldn't parse it as JSON.");
}

if (existsSync(join(tauriDir, 'tauri.conf.json5'))) {
const contents = readFileSync(
join(tauriDir, 'tauri.conf.json5')
).toString();
const config = _tryParseJson5Config(contents);
if (config) return config;
console.error(
"Found tauri.conf.json5 file but couldn't parse it as JSON5."
);
}

if (existsSync(join(tauriDir, 'Tauri.toml'))) {
const contents = readFileSync(join(tauriDir, 'Tauri.toml')).toString();
const config = _tryParseTomlConfig(contents);
if (config) return config;
console.error("Found Tauri.toml file but couldn't parse it as TOML.");
}

throw "Couldn't locate or parse tauri config.";
}

export function getInfo(
root: string,
inConfigPath: string | null = null
): Info {
export function getInfo(root: string, inConfigPath?: string): Info {
const tauriDir = getTauriDir(root);
if (tauriDir !== null) {
const configPath = inConfigPath ?? join(tauriDir, 'tauri.conf.json');
let name;
let version;
let wixLanguage: string | string[] | { [language: string]: unknown } =
'en-US';
const config = getConfig(configPath);
const config = getConfig(tauriDir, inConfigPath);
if (config.package) {
name = config.package.productName;
version = config.package.version;
Expand All @@ -209,7 +268,7 @@ export function getInfo(
}

if (!(name && version)) {
console.error('Could not determine package name and version');
console.error('Could not determine package name and version.');
process.exit(1);
}

Expand Down

0 comments on commit 06b006d

Please sign in to comment.