Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-storybook-loader

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-storybook-loader - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

src/paths/cliResolver.js

7

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc