refactor: simplify project file lookup

This commit is contained in:
Igor Randjelovic
2021-03-07 19:33:42 +01:00
parent 39d90d5c32
commit 02d2befc8a
7 changed files with 26 additions and 49 deletions

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env node
#!/bin/env node
import { redBright, green, greenBright } from 'chalk';
import { program } from 'commander';

View File

@@ -1,6 +1,6 @@
import Config from 'webpack-chain';
import { getProjectRootPath } from '../helpers/project';
import { getProjectFilePath, getProjectRootPath } from '../helpers/project';
import { getPlatformName } from '../helpers/platform';
import { env as _env, IWebpackEnv } from '../index';
import { error } from '../helpers/log';
@@ -52,15 +52,16 @@ function getSvelteConfigPreprocessor(): any {
return config?.preprocess;
}
function getSvelteConfig(): { preprocess: any } | undefined {
interface ISvelteConfig {
preprocess: any
}
function getSvelteConfig(): ISvelteConfig | undefined {
try {
const resolvedPath = require.resolve(`./svelte.config.js`, {
paths: [getProjectRootPath()],
});
return require(resolvedPath);
return require(
getProjectFilePath('svelte.config.js')
) as ISvelteConfig;
} catch (err) {
// todo: remove when jest supports mocking require.resolve
if (__TEST__) return;
error('Could not find svelte.config.js.', err);
}
}

View File

@@ -1 +1 @@
declare var __TEST__: boolean;
// define globals here

View File

@@ -19,7 +19,15 @@ interface IPackageJson {
* Utility function to get the contents of the project package.json
*/
export function getPackageJson() {
const packageJsonPath = resolve(getProjectRootPath(), 'package.json');
return require(packageJsonPath) as IPackageJson;
return require(
getProjectFilePath('package.json')
) as IPackageJson;
}
/**
* Utility to get project files relative to the project root.
* @param filePath path to get
*/
export function getProjectFilePath(filePath: string): string {
return resolve(getProjectRootPath(), filePath);
}