Skip to content

Commit

Permalink
Revert "WIP: create snowpack configuration programmatically (#449)"
Browse files Browse the repository at this point in the history
This reverts commit 1c99ad8.
  • Loading branch information
FredKSchott committed Jun 12, 2020
1 parent 913a05c commit 47e0dd2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 38 deletions.
45 changes: 10 additions & 35 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export type Proxy = [string, ProxyOptions];

// interface this library uses internally
export interface SnowpackConfig {
install: string[];
extends?: string;
exclude: string[];
knownEntrypoints: string[];
Expand Down Expand Up @@ -237,24 +236,6 @@ const configSchema = {
},
};

function validateConfigOnce() {
let validated = false;
return function (config: object, errorHandler?: (errors) => void) {
if (!validated) {
const validation = validate(config, configSchema, {
allowUnknownAttributes: false,
propertyName: CONFIG_NAME,
});

validated = true;

errorHandler && errorHandler(validation.errors);
}
};
}

const validateOnce = validateConfigOnce();

/**
* Convert CLI flags to an incomplete Snowpack config representation.
* We need to be careful about setting properties here if the flag value
Expand Down Expand Up @@ -557,7 +538,7 @@ function handleConfigError(msg: string) {
process.exit(1);
}

function handleValidationErrors(filepath: string | null, errors: {toString: () => string}[]) {
function handleValidationErrors(filepath: string, errors: {toString: () => string}[]) {
console.error(chalk.red(`! ${filepath || 'Configuration error'}`));
console.error(errors.map((err) => ` - ${err.toString()}`).join('\n'));
console.error(` See https://www.snowpack.dev/#configuration for more info.`);
Expand Down Expand Up @@ -688,13 +669,6 @@ function validateConfigAgainstV1(rawConfig: any, cliFlags: any) {
}
}

export function createConfiguration(config: Partial<SnowpackConfig>): SnowpackConfig {
validateOnce(config);

const mergedConfig = merge<SnowpackConfig>([DEFAULT_CONFIG, config]);
return normalizeConfig(mergedConfig);
}

export function loadAndValidateConfig(flags: CLIFlags, pkgManifest: any): SnowpackConfig {
const explorerSync = cosmiconfigSync(CONFIG_NAME, {
// only support these 3 types of config for now
Expand Down Expand Up @@ -725,15 +699,15 @@ export function loadAndValidateConfig(flags: CLIFlags, pkgManifest: any): Snowpa
validateConfigAgainstV1(config, flags);
const cliConfig = expandCliFlags(flags);

function errorHandler(errors) {
if (errors && errors.length > 0) {
handleValidationErrors(result.filepath, errors);
process.exit(1);
}
const validation = validate(config, configSchema, {
allowUnknownAttributes: false,
propertyName: CONFIG_NAME,
});
if (validation.errors && validation.errors.length > 0) {
handleValidationErrors(result.filepath, validation.errors);
process.exit(1);
}

validateOnce(config, errorHandler);

let extendConfig: SnowpackConfig | {} = {};
if (config.extends) {
const extendConfigLoc = config.extends.startsWith('.')
Expand All @@ -756,6 +730,7 @@ export function loadAndValidateConfig(flags: CLIFlags, pkgManifest: any): Snowpa
}
// if valid, apply config over defaults
const mergedConfig = merge<SnowpackConfig>([
DEFAULT_CONFIG,
extendConfig,
{
webDependencies: pkgManifest.webDependencies,
Expand All @@ -777,5 +752,5 @@ export function loadAndValidateConfig(flags: CLIFlags, pkgManifest: any): Snowpa
}
}

return createConfiguration(mergedConfig);
return normalizeConfig(mergedConfig);
}
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {command as buildCommand} from './commands/build';
import {command as devCommand} from './commands/dev';
import {addCommand, rmCommand} from './commands/add-rm';
import {command as installCommand} from './commands/install';
import {CLIFlags, loadAndValidateConfig, createConfiguration} from './config.js';
import {CLIFlags, loadAndValidateConfig} from './config.js';
import {readLockfile, clearCache} from './util.js';

const cwd = process.cwd();
Expand Down Expand Up @@ -33,8 +33,6 @@ ${chalk.bold('Flags:')}
);
}

export {createConfiguration};

export async function cli(args: string[]) {
// parse CLI flags
const cliFlags = yargs(args, {
Expand Down

0 comments on commit 47e0dd2

Please sign in to comment.