react-native-storybook-loader
Advanced tools
Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "react-native-storybook-loader", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"repository": "https://github.com/elderfo/react-native-storybook-loader.git", | ||
@@ -33,3 +33,4 @@ "bugs": "https://github.com/elderfo/react-native-storybook-loader/issues", | ||
"findup": "^0.1.5", | ||
"glob": "^7.1.1" | ||
"glob": "^7.1.1", | ||
"yargs": "^7.0.2" | ||
}, | ||
@@ -41,4 +42,4 @@ "scripts": { | ||
"lint:watch": "yarn run lint -- --watch", | ||
"prestart": "yarn run build", | ||
"start": "babel-node src/rnstl-cli.js", | ||
"start:help": "babel-node src/rnstl-cli.js --help", | ||
"prestart:dist": "yarn run build", | ||
@@ -45,0 +46,0 @@ "start:dist": "node dist/rnstl-cli.js", |
# react-native-storybook-loader | ||
[![Build Status](https://travis-ci.org/elderfo/react-native-storybook-loader.svg?branch=master)](https://travis-ci.org/elderfo/react-native-storybook-loader) | ||
[![Build Status](https://travis-ci.org/elderfo/react-native-storybook-loader.svg?branch=master)](https://travis-ci.org/elderfo/react-native-storybook-loader) [![Known Vulnerabilities](https://snyk.io/test/github/elderfo/react-native-storybook-loader/badge.svg)](https://snyk.io/test/github/elderfo/react-native-storybook-loader) | ||
A component for dynamically import stories into [react-native-storybook](https://github.com/storybooks/react-native-storybook). | ||
A CLI for dynamically import stories into [react-native-storybook](https://github.com/storybooks/react-native-storybook). | ||
## Purpose | ||
While using storybook for React Native, I repeatedly found myself manually creating a file with imports for all my stories. so I added an automated way to do it. This will locate files using configuration in the `package.json` file and create a story loader in the project directories. Search folder/patterns and the output file are all configurable. | ||
While using storybook for React Native, I repeatedly found myself manually creating a file with imports for all my stories. So I built an automated way to do it. `react-native-storybook-loader` can be run using configuration in your `package.json` or via the CLI interface. | ||
@@ -68,10 +69,14 @@ ## Installation | ||
| Setting | Type | Description | Default | | ||
|---|---|---|---| | ||
| **searchDir** | `string` or `string[]` | The directory or directories, relative to the project root, to search for files in. | Project root | | ||
| **outputFile** | `string` | The output file that will be written. It is relative to the project directory. | `./storybook/storyLoader.js` | | ||
| **pattern** | `string` | The pattern of files to look at. It can be a specific file, or any valid glob. | `./storybook/stories/index.js` (The default React Native storybook file) | | ||
| Setting | CLI Option | Type | Description | Default | | ||
|---|---|---|---|---| | ||
| **searchDir** | `--searchDir` | `string` or `string[]` | The directory or directories, relative to the project root, to search for files in. | Project root | | ||
| **outputFile** | `--outputFile` | `string` | The output file that will be written. It is relative to the project directory. | `./storybook/storyLoader.js` | | ||
| **pattern** | `--pattern` | `string` | The pattern of files to look at. It can be a specific file, or any valid glob. | `./storybook/stories/index.js` (The default React Native storybook file) | | ||
#### Example: | ||
> Note: When using the CLI, any of option passed will override the values in the `package.json` | ||
#### Examples: | ||
##### `package.json` | ||
```json | ||
@@ -95,4 +100,10 @@ { | ||
This configuration will search `src` and `packages` directories recursively for files that end with `.stories.js` and write the output to `./storybook/storyLoader.js` | ||
##### CLI | ||
```bash | ||
$ node ./node_modules/.bin/rnstl --searchDir ./src ./packages --pattern **/*.stories.js --outputFile ./storybook/storyLoader.js | ||
``` | ||
Both examples will search `src` and `packages` directories recursively for files that end with `.stories.js` and write the output to `./storybook/storyLoader.js` | ||
## Support | ||
@@ -99,0 +110,0 @@ Please log issues |
@@ -6,10 +6,16 @@ import fs from 'fs'; | ||
function getDefaultValue(baseDir, setting) { | ||
/** | ||
* Returns the default value for the specified | ||
* @param {string} setting Name of the setting | ||
*/ | ||
function getDefaultValue(setting) { | ||
switch (setting) { | ||
case 'pattern': | ||
return path.resolve(baseDir, './storybook/stories/index.js'); | ||
return './storybook/stories/index.js'; | ||
case 'outputFile': | ||
return path.resolve(baseDir, './storybook/storyLoader.js'); | ||
return './storybook/storyLoader.js'; | ||
case 'searchDir': | ||
return ['./']; | ||
default: | ||
return baseDir; | ||
return './'; | ||
} | ||
@@ -30,25 +36,31 @@ } | ||
* @param {object} pkg pkg the contents of the package.json in object form. | ||
* @param {*} setting setting Name of the setting to look for | ||
* @param {string} setting setting Name of the setting to look for | ||
* @param {bool} ensureArray flag denoting whether to ensure the setting is an array | ||
*/ | ||
function getConfigSetting(pkg, setting) { | ||
if (hasConfigSetting(pkg, setting)) { | ||
return pkg.config[appName][setting]; | ||
function getConfigSetting(pkg, setting, ensureArray) { | ||
if (!hasConfigSetting(pkg, setting)) { | ||
return null; | ||
} | ||
return null; | ||
} | ||
function getResolvedSetting(pkg, rootDir, setting) { | ||
const value = getConfigSetting(pkg, setting) || getDefaultValue(rootDir, setting); | ||
return path.resolve(rootDir, value); | ||
const value = pkg.config[appName][setting]; | ||
if (ensureArray && !Array.isArray(value)) { | ||
return [value]; | ||
} | ||
return value; | ||
} | ||
function getPatterns(pkg, baseDir) { | ||
let searchDirs = getConfigSetting(pkg, 'searchDir') || getDefaultValue(baseDir, 'searchDir'); | ||
const pattern = getConfigSetting(pkg, 'pattern') || getDefaultValue(baseDir, 'pattern'); | ||
/** | ||
* Parses the package.json file and returns a config object | ||
* @param {string} packageJsonFile Path to the package.json file | ||
*/ | ||
function getConfigSettings(packageJsonFile) { | ||
// Locate and read package.json | ||
const pkg = JSON.parse(fs.readFileSync(packageJsonFile)); | ||
if (!Array.isArray(searchDirs)) { | ||
searchDirs = [searchDirs]; | ||
} | ||
return searchDirs.map(dir => path.resolve(baseDir, dir, pattern)); | ||
return { | ||
searchDir: getConfigSetting(pkg, 'searchDir', true) || getDefaultValue('searchDir'), | ||
outputFile: getConfigSetting(pkg, 'outputFile') || getDefaultValue('outputFile'), | ||
pattern: getConfigSetting(pkg, 'pattern') || getDefaultValue('pattern'), | ||
}; | ||
} | ||
@@ -59,4 +71,2 @@ | ||
* { | ||
* "packageJsonFile": "", | ||
* "baseDir": "", | ||
* "outputFiles": [{ | ||
@@ -69,21 +79,20 @@ * "patterns":[], | ||
*/ | ||
export default function resolvePaths(processDirectory) { | ||
export default function resolvePaths(processDirectory, cliConfig) { | ||
const overrides = cliConfig || {}; | ||
// Locate and read package.json | ||
const packageJsonFile = path.resolve(findup.sync(processDirectory, 'package.json'), 'package.json'); | ||
const pkg = JSON.parse(fs.readFileSync(packageJsonFile)); | ||
// initialize single entry paths | ||
const baseDir = path.dirname(packageJsonFile); | ||
const outputFile = getResolvedSetting(pkg, baseDir, 'outputFile'); | ||
const config = Object.assign({}, getConfigSettings(packageJsonFile, baseDir), overrides); | ||
const outputFile = path.resolve(baseDir, config.outputFile); | ||
const outputFiles = [{ | ||
outputFile, | ||
patterns: getPatterns(pkg, baseDir), | ||
patterns: config.searchDir.map(dir => path.resolve(baseDir, dir, config.pattern)), | ||
}]; | ||
return { | ||
packageJsonFile, | ||
baseDir, | ||
outputFiles, | ||
}; | ||
} | ||
@@ -22,3 +22,2 @@ import path from 'path'; | ||
afterEach(() => { | ||
@@ -32,4 +31,2 @@ mock.restore(); | ||
const expected = { | ||
packageJsonFile: packageJsonFilePath, | ||
baseDir, | ||
outputFiles: [{ | ||
@@ -50,6 +47,4 @@ patterns: [path.resolve(baseDir, './storybook/stories/index.js')], | ||
const expected = { | ||
packageJsonFile: packageJsonFilePath, | ||
baseDir, | ||
outputFiles: [{ | ||
patterns: [path.resolve(baseDir, './src/storybook', '**/*.stories.js')], | ||
patterns: [path.resolve(baseDir, './src/storybook/**/*.stories.js')], | ||
outputFile: path.resolve(baseDir, './storybook/config.js'), | ||
@@ -73,8 +68,6 @@ }], | ||
const expected = { | ||
packageJsonFile: packageJsonFilePath, | ||
baseDir, | ||
outputFiles: [{ | ||
patterns: [ | ||
path.resolve(baseDir, './src/storybook', '**/*.stories.js'), | ||
path.resolve(baseDir, './packages', '**/*.stories.js'), | ||
path.resolve(baseDir, './src/storybook/**/*.stories.js'), | ||
path.resolve(baseDir, './packages/**/*.stories.js'), | ||
], | ||
@@ -89,1 +82,30 @@ outputFile: path.resolve(baseDir, './storybook/config.js'), | ||
}); | ||
test('should resolve expected paths with cli configs', () => { | ||
const packageJsonContents = generatePackageJson( | ||
['./src/storybook', './packages'], | ||
'**/*.stories.js', | ||
'./storybook/config.js', | ||
); | ||
const cliConfig = { | ||
searchDir: ['./src', './package/pkg1'], | ||
pattern: '*.js', | ||
outputFile: './storyLoader.js', | ||
}; | ||
mock({ [packageJsonFilePath]: JSON.stringify(packageJsonContents) }); | ||
const expected = { | ||
outputFiles: [{ | ||
patterns: [ | ||
path.resolve(baseDir, './src/*.js'), | ||
path.resolve(baseDir, './package/pkg1/*.js'), | ||
], | ||
outputFile: path.resolve(baseDir, './storyLoader.js'), | ||
}], | ||
}; | ||
const actual = resolvePaths(baseDir, cliConfig); | ||
expect(actual).toEqual(expected); | ||
}); |
#!/usr/bin/env node | ||
import yargs from 'yargs'; | ||
import cliResolver from './paths/cliResolver'; | ||
import { info } from './logger'; | ||
@@ -6,7 +9,25 @@ import { writeOutStoryLoader } from './storyWriterProcess'; | ||
const pathConfig = resolvePaths(process.cwd()); | ||
const args = yargs | ||
.usage('$0 [options]') | ||
.options({ | ||
searchDir: { | ||
array: true, | ||
desc: 'The directory or directories, relative to the project root, to search for files in.', | ||
}, | ||
pattern: { | ||
desc: 'The directory or directories, relative to the project root, to search for files in.', | ||
type: 'string', | ||
}, | ||
outputFile: { | ||
desc: 'The directory or directories, relative to the project root, to search for files in.', | ||
type: 'string', | ||
}, | ||
}) | ||
.help() | ||
.argv; | ||
const cliConfig = cliResolver(args); | ||
const pathConfig = resolvePaths(process.cwd(), cliConfig); | ||
info('\nGenerating Dynamic Storybook File List\n'); | ||
info('package.json: ', pathConfig.packageJsonFile); | ||
info('Base directory: ', pathConfig.baseDir); | ||
writeOutStoryLoader(pathConfig); |
@@ -14,4 +14,2 @@ import faker from 'faker'; | ||
const config = { | ||
baseDir: faker.system.fileName(), | ||
packageJsonFile: faker.system.fileName(), | ||
outputFiles: [{ | ||
@@ -18,0 +16,0 @@ outputFile: faker.system.fileName(), |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
644089
31
15126
112
5
23
6
+ Addedyargs@^7.0.2
+ Addedansi-regex@2.1.1(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcamelcase@3.0.0(transitive)
+ Addedcliui@3.2.0(transitive)
+ Addedcode-point-at@1.1.0(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfind-up@1.1.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-caller-file@1.0.3(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedinvert-kv@1.0.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedis-fullwidth-code-point@1.0.0(transitive)
+ Addedis-utf8@0.2.1(transitive)
+ Addedlcid@1.0.0(transitive)
+ Addedload-json-file@1.1.0(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedos-locale@1.4.0(transitive)
+ Addedparse-json@2.2.0(transitive)
+ Addedpath-exists@2.1.0(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpath-type@1.1.0(transitive)
+ Addedpify@2.3.0(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedread-pkg@1.1.0(transitive)
+ Addedread-pkg-up@1.0.1(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@1.0.1(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.20(transitive)
+ Addedstring-width@1.0.2(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedstrip-bom@2.0.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)
+ Addedwhich-module@1.0.0(transitive)
+ Addedwrap-ansi@2.1.0(transitive)
+ Addedy18n@3.2.2(transitive)
+ Addedyargs@7.1.2(transitive)
+ Addedyargs-parser@5.0.1(transitive)