-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: config package to read configuration files
- Loading branch information
1 parent
ac28e3f
commit bbd06f7
Showing
21 changed files
with
565 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-typescript", | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": "current" | ||
} | ||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => <ThemeProvider>{story()}</ThemeProvider>] | | ||
| `stories*` | string\[] | wild card search string for the stories internally using \`glob\` for the search: https://www.npmjs.com/package/glob example: "./stories/ | | ||
|
||
<!-- END-TSDOC-TYPESCRIPT --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
`; |
Oops, something went wrong.