Skip to content

Commit

Permalink
feat: Read --config arg, closes #346 (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianLars authored Mar 29, 2023
1 parent d3b4f60 commit 2a4a05a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changes/read-cli-config-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'action': patch
---

Automatically read configs provided via the `-c`/`--config` argument.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ export async function buildProject(
? ['--debug', ...(buildOpts.args ?? [])]
: buildOpts.args ?? [];

const found = [...tauriArgs].findIndex((e) => e === '-t' || e === '--target');
const targetPath = found >= 0 ? [...tauriArgs][found + 1] : undefined;
const targetArgIdx = [...tauriArgs].findIndex(
(e) => e === '-t' || e === '--target'
);
const targetPath =
targetArgIdx >= 0 ? [...tauriArgs][targetArgIdx + 1] : undefined;

const configArgIdx = [...tauriArgs].findIndex(
(e) => e === '-c' || e === '--config'
);
const configArgPath =
configArgIdx >= 0 ? [...tauriArgs][configArgIdx + 1] : undefined;

const targetInfo = getTargetInfo(targetPath);

const info = getInfo(root, buildOpts.configPath, targetInfo);
const info = getInfo(root, buildOpts.configPath, targetInfo, configArgPath);

const app = info.tauriPath
? {
Expand Down
59 changes: 36 additions & 23 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,18 @@ export function mergePlatformConfig(
}
}

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

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;
}
/// This modifies baseConfig in-place!
export function mergeUserConfig(baseConfig: TauriConfig, configPath: string) {
const config = readCustomConfig(configPath);

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

throw `Couldn't parse \`${customPath}\` as ${ext.substring(1)}.`;
export function getConfig(tauriDir: string, customPath?: string): TauriConfig {
if (customPath) {
return readCustomConfig(customPath);
}

if (existsSync(join(tauriDir, 'tauri.conf.json'))) {
Expand Down Expand Up @@ -135,3 +122,29 @@ export function getConfig(tauriDir: string, customPath?: string): TauriConfig {

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

function readCustomConfig(customPath: string) {
if (!existsSync(customPath)) {
throw `Provided config path \`${customPath}\` does not exist.`;
}

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;
}

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

throw `Couldn't parse \`${customPath}\` as ${ext.substring(1)}.`;
}
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,20 @@ async function run(): Promise<void> {
bundleIdentifier,
};

const _found = args.findIndex((e) => e === '-t' || e === '--target');
const targetPath = _found >= 0 ? args[_found + 1] : undefined;
const targetArgIdx = [...args].findIndex(
(e) => e === '-t' || e === '--target'
);
const targetPath =
targetArgIdx >= 0 ? [...args][targetArgIdx + 1] : undefined;

const configArgIdx = [...args].findIndex(
(e) => e === '-c' || e === '--config'
);
const configArgPath =
configArgIdx >= 0 ? [...args][configArgIdx + 1] : undefined;

const targetInfo = getTargetInfo(targetPath);
const info = getInfo(projectPath, undefined, targetInfo);
const info = getInfo(projectPath, undefined, targetInfo, configArgPath);

const artifacts: Artifact[] = [];
if (includeRelease) {
Expand Down
8 changes: 6 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { parse as parseToml } from '@iarna/toml';
import { sync as globSync } from 'glob-gitignore';
import ignore from 'ignore';

import { getConfig, mergePlatformConfig } from './config';
import { getConfig, mergePlatformConfig, mergeUserConfig } from './config';

import type { CargoManifest, Info, TargetInfo, TargetPlatform } from './types';

Expand Down Expand Up @@ -154,7 +154,8 @@ export function execCommand(
export function getInfo(
root: string,
inConfigPath?: string,
targetInfo?: TargetInfo
targetInfo?: TargetInfo,
configFlag?: string
): Info {
const tauriDir = getTauriDir(root);
if (tauriDir !== null) {
Expand All @@ -167,6 +168,9 @@ export function getInfo(
if (targetInfo) {
mergePlatformConfig(config, tauriDir, targetInfo.platform);
}
if (configFlag) {
mergeUserConfig(config, configFlag);
}

if (config.package) {
name = config.package.productName;
Expand Down

0 comments on commit 2a4a05a

Please sign in to comment.