Skip to content

Commit

Permalink
feat: config package to read configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 30, 2020
1 parent ac28e3f commit bbd06f7
Show file tree
Hide file tree
Showing 21 changed files with 565 additions and 38 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [@component-controls/loader](#component-controlsloader)
- [@component-controls/webpack-configs](#component-controlswebpack-configs)
- [@component-controls/store](#component-controlsstore)
- [@component-controls/config](#component-controlsconfig)
- [UI packages](#ui-packages)
- [@component-controls/pages](#component-controlspages)
- [@component-controls/blocks](#component-controlsblocks)
Expand Down Expand Up @@ -202,6 +203,18 @@ Utility class to abstract loading the stories store from the webpack loader and

<!-- END-PACKAGE-SECTION -->

<package-section file="./core/config/README.md" section="overview" />

<!-- START-PACKAGE-SECTION -->

## [@component-controls/config](https://github.com/ccontrols/component-controls/blob/master/core/config)

Component controls configuration reading utilities

Configration file utilities. Uses the [glob](https://www.npmjs.com/package/glob) package to locate story files from the configuration

<!-- END-PACKAGE-SECTION -->

# UI packages

The UI libraries are built around [theme-ui](https://theme-ui.com) and are designed to abstract the user interface level of components.
Expand Down
13 changes: 13 additions & 0 deletions core/config/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"presets": [
"@babel/preset-typescript",
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
21 changes: 21 additions & 0 deletions core/config/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Atanas Stoyanov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
99 changes: 99 additions & 0 deletions core/config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Table of contents

- [Overview](#overview)
- [Installation](#installation)
- [API](#api)
- [ConfigrationResult](#configrationresult)
- [configFileNames](#configfilenames)
- [extractStories](#extractstories)
- [getConfiguration](#getconfiguration)
- [Configuration](#configuration)

# Overview

Configration file utilities. Uses the [glob](https://www.npmjs.com/package/glob) package to locate story files from the configuration

# Installation

This package is usually installed as part of the @component-controls package, but you can also install it standalone:

```bash
$ npm install @component-controls/config --save-dev
```

# API

<tsdoc-typescript entry="./src/index.ts" files="../specification/src/configuration.ts"/>

<!-- START-TSDOC-TYPESCRIPT -->

## ConfigrationResult

_defined in [@component-controls/config/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/config/src/index.ts#L9)_



### properties

| Name | Type | Description |
| ------------- | ------------------------------- | ----------- |
| `config*` | [Configuration](#configuration) | |
| `configPath*` | string | |

## configFileNames

_defined in [@component-controls/config/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/config/src/index.ts#L7)_



## extractStories

find the story files out of a configuration file
using glob for the regex file search

_defined in [@component-controls/config/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/config/src/index.ts#L56)_

**function** extractStories(`__namedParameters`\*: **config**: [Configuration](#configuration)**configPath**: string): string\[] | undefined;

### parameters

| Name | Type | Description |
| -------------------- | ----------------------------------------------------------------- | ----------- |
| `__namedParameters*` | **config**: [Configuration](#configuration)**configPath**: string | |
| `returns` | string\[] \| undefined | |

## getConfiguration

return the configration object
command line accepts -c/ -config parameter for config path
the config file is assumed named main.js/main.ts

_defined in [@component-controls/config/src/index.ts](https://github.com/ccontrols/component-controls/tree/master/core/config/src/index.ts#L19)_

**function** getConfiguration(`baseFolder`\*: string, `args`\*: string\[]): [ConfigrationResult](#configrationresult) | undefined;

### parameters

| Name | Type | Description |
| ------------- | ------------------------------------------------------ | --------------------------------------- |
| `baseFolder*` | string | project folder to start the searh with |
| `args*` | string\[] | |
| `returns` | [ConfigrationResult](#configrationresult) \| undefined | |

## Configuration

global configuration
stored in a file named main.js/main.ts

_defined in [@component-controls/specification/src/configuration.ts](https://github.com/ccontrols/component-controls/tree/master/core/specification/src/configuration.ts#L7)_



### properties

| Name | Type | Description |
| ------------ | ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `decorators` | [StoryRenderFn](#storyrenderfn)\[] | story decorator functions - used to wrap stories example: \[story => &lt;ThemeProvider>{story()}&lt;/ThemeProvider>] |
| `stories*` | string\[] | wild card search string for the stories internally using \`glob\` for the search: https&#x3A;//www.npmjs.com/package/glob example: "./stories/ |

<!-- END-TSDOC-TYPESCRIPT -->
61 changes: 61 additions & 0 deletions core/config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "@component-controls/config",
"version": "1.0.1",
"description": "Component controls configuration reading utilities",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"typings": "dist/index.d.ts",
"files": [
"dist/",
"package.json",
"README.md"
],
"scripts": {
"build": "yarn cross-env NODE_ENV=production rollup -c",
"dev": "yarn cross-env NODE_ENV=development rollup -cw",
"docs": "ts-node -O '{\"module\": \"commonjs\" }' ../../scripts/docs.ts",
"fix": "yarn lint --fix",
"lint": "yarn eslint . --ext mdx,ts,tsx",
"prepare": "yarn build",
"test": "yarn jest"
},
"homepage": "https://github.com/ccontrols/component-controls",
"bugs": {
"url": "https://github.com/ccontrols/component-controls/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/ccontrols/component-controls.git",
"directory": "core/config"
},
"license": "MIT",
"dependencies": {
"@component-controls/specification": "^1.0.1",
"glob": "^7.1.6",
"typescript": "^3.8.3",
"yargs": "^15.3.1"
},
"devDependencies": {
"@types/faker": "^4.1.9",
"@types/jest": "^25.1.2",
"cross-env": "^5.2.1",
"eslint": "^6.5.1",
"jest": "^24.9.0"
},
"browser": {
"fs": false
},
"jest": {
"testEnvironment": "node",
"roots": [
"."
]
},
"publishConfig": {
"access": "public"
},
"authors": [
"Atanas Stoyanov"
],
"gitHead": "c5145d66c6b8a355839e53c3bca97fd361ce9377"
}
5 changes: 5 additions & 0 deletions core/config/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { config } from '../../rollup-config';

export default config({
input: './src/index.ts',
});
65 changes: 65 additions & 0 deletions core/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as path from 'path';
import * as fs from 'fs';
import { sync as globSync } from 'glob';
import yargs from 'yargs';
import { Configuration } from '@component-controls/specification';

export const configFileNames = ['main.js', 'main.json', 'main.ts'];

export interface ConfigrationResult {
config: Configuration;
configPath: string;
}
/**
* return the configration object
* command line accepts -c/ -config parameter for config path
* the config file is assumed named main.js/main.ts
* @param baseFolder project folder to start the searh with
*/
export const getConfiguration = (
baseFolder: string,
args: string[] = process.argv,
): ConfigrationResult | undefined => {
const argv = yargs(args)
.option('config', {
alias: 'c',
description: 'configuration folder',
type: 'string',
})
.help()
.alias('help', 'h').argv;

const configPath = argv.config
? path.resolve(baseFolder, argv.config)
: baseFolder;
const configFiles = fs.readdirSync(configPath);
const configFile = configFiles.find(file =>
configFileNames.includes(file.toLowerCase()),
);

return configFile
? {
config: require(path.resolve(configPath, configFile)),
configPath,
}
: undefined;
};

/**
* find the story files out of a configuration file
* using glob for the regex file search
* @param param0 configuration info return by getConfiguration
*/
export const extractStories = ({
config,
configPath,
}: ConfigrationResult): string[] | undefined => {
const stories =
config && config.stories
? config.stories.reduce((acc: string[], storyRg: string) => {
const regex = storyRg.replace(/\((.*?)\)/g, '+($1)');
return [...acc, ...globSync(path.resolve(configPath, regex))];
}, [])
: undefined;
return stories;
};
51 changes: 51 additions & 0 deletions core/config/test/__snapshots__/config-folder.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`config-folder config file long option 1`] = `
Object {
"config": Object {
"addons": Array [
Object {
"name": "@component-controls/storybook",
"options": Object {
"webpack": Array [
"instrument",
"react-docgen-typescript",
],
},
},
],
"stories": Array [
"../../../../ui/editors/src/**/*.stories.(js|tsx|mdx)",
"../../../../ui/components/src/**/*.stories.(js|tsx|mdx)",
"../../../../ui/blocks/src/**/*.stories.(js|tsx|mdx)",
"../../../../examples/stories/src/**/*.stories.(js|tsx|mdx)",
],
},
"configPath": "/Users/atanasster/component-controls/core/config/test/fixtures",
}
`;

exports[`config-folder config file short option 1`] = `
Object {
"config": Object {
"addons": Array [
Object {
"name": "@component-controls/storybook",
"options": Object {
"webpack": Array [
"instrument",
"react-docgen-typescript",
],
},
},
],
"stories": Array [
"../../../../ui/editors/src/**/*.stories.(js|tsx|mdx)",
"../../../../ui/components/src/**/*.stories.(js|tsx|mdx)",
"../../../../ui/blocks/src/**/*.stories.(js|tsx|mdx)",
"../../../../examples/stories/src/**/*.stories.(js|tsx|mdx)",
],
},
"configPath": "/Users/atanasster/component-controls/core/config/test/fixtures",
}
`;
Loading

0 comments on commit bbd06f7

Please sign in to comment.