Skip to content

Commit

Permalink
Merge pull request #167 from pinefile/load-config-from-pkg
Browse files Browse the repository at this point in the history
Fix so config can be loaded from package.json
  • Loading branch information
frozzare authored Oct 4, 2022
2 parents fe8359c + 1b41e2d commit 7c3f3f9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-rabbits-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pinefile/pine": patch
---

Fix so config can be loaded from package.json
51 changes: 40 additions & 11 deletions packages/pine/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';
import { isObject } from '@pinefile/utils';
import { isObject, merge } from '@pinefile/utils';
import { Options } from './args';
import { findFile } from './file';
import { LogLevel, setLogger, Logger } from './logger';
import { Runner } from './runner';

const isDev = process.argv.some((a) => a.includes('packages/pine/src/bin.ts'));

export type Config = {
/**
* Dynamic config properties.
Expand Down Expand Up @@ -74,19 +77,45 @@ export type Config = {
*/
export type ConfigFunction = (cfg: Config) => Config;

const isDev = process.argv.some((a) => a.includes('packages/pine/src/bin.ts'));
let pkgConfig = {};

/**
* Load config from package.json.
*/
export const loadPkgConfig = () => {
if (Object.keys(pkgConfig).length) {
return pkgConfig;
}

let config: Config = {
dotenv: [],
env: {},
esbuild: !isDev,
logLevel: 'info',
options: {},
root: '',
require: [],
task: '',
const file = findFile('package.json');
if (!file) {
return pkgConfig;
}

try {
// eslint-disable-next-line
const pkg = require(file);
pkgConfig = (typeof pkg === 'object' ? pkg?.pine : null) || {};
// eslint-disable-next-line
} catch (err) {}

return pkgConfig;
};

let config: Config = merge<Config>(
{
dotenv: [],
env: {},
esbuild: !isDev,
logLevel: 'info',
options: {},
root: '',
require: [],
task: '',
},
loadPkgConfig()
);

const isError = (error: any): error is NodeJS.ErrnoException =>
error instanceof Error;

Expand Down

0 comments on commit 7c3f3f9

Please sign in to comment.