react-native-storybook-loader
Advanced tools
Comparing version 1.4.1 to 1.4.2
{ | ||
"name": "react-native-storybook-loader", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"repository": "https://github.com/elderfo/react-native-storybook-loader.git", | ||
@@ -12,2 +12,3 @@ "bugs": "https://github.com/elderfo/react-native-storybook-loader/issues", | ||
"devDependencies": { | ||
"cz-conventional-changelog": "^2.0.0", | ||
"eslint": "^3.16.1", | ||
@@ -48,4 +49,7 @@ "eslint-config-elderfo": "^1.1.1", | ||
"outputFile": "./output/storyLoader.js" | ||
}, | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
} | ||
} |
# react-native-storybook-loader | ||
A CLI for dynamically importing stories into [react-native-storybook](https://github.com/storybooks/react-native-storybook). | ||
[![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) | ||
[![AppVeyor](https://ci.appveyor.com/api/projects/status/github/elderfo/react-native-storybook-loader?svg=true)](https://ci.appveyor.com/project/elderfo/react-native-storybook-loader) | ||
A CLI for dynamically import stories into [react-native-storybook](https://github.com/storybooks/react-native-storybook). | ||
## Purpose | ||
@@ -86,5 +88,3 @@ | ||
"name": "AwesomeProject", | ||
... | ||
"scripts": { | ||
... | ||
"prestorybook": "rnstl" | ||
@@ -91,0 +91,0 @@ }, |
const glob = require('glob'); | ||
const loadStories = pattern => glob.sync(pattern); | ||
const { formatPath } = require('../utils/pathHelper'); | ||
/** | ||
* Locates files matching the specified pattern | ||
* | ||
* @param {String} pattern - Pattern to use to locate files | ||
* @returns {Array<String>} - Array of located files | ||
*/ | ||
const loadStories = pattern => glob.sync(pattern) | ||
.map(file => formatPath(file)); | ||
module.exports = { loadStories }; |
@@ -8,4 +8,6 @@ // required imports | ||
// test file import | ||
const { formatPath } = require('../utils/pathHelper'); | ||
const { loadStories } = require('./'); | ||
jest.mock('../utils/pathHelper.js'); | ||
jest.mock('glob'); | ||
@@ -21,2 +23,3 @@ | ||
glob.sync = jest.fn(() => files); | ||
formatPath.mockImplementation(f => f); | ||
} | ||
@@ -27,3 +30,5 @@ | ||
const actual = loadStories(); | ||
expect(actual).toBe(files); | ||
expect(actual).toEqual(files); | ||
expect(formatPath).toHaveBeenCalledWith(files[0]); | ||
expect(formatPath).toHaveBeenCalledWith(files[1]); | ||
}); | ||
@@ -30,0 +35,0 @@ |
@@ -5,2 +5,4 @@ const logger = require('./logger'); | ||
const sortFiles = files => files.concat().sort(); | ||
const writeOutStoryLoader = (pathConfig) => { | ||
@@ -21,3 +23,4 @@ logger.debug('writeOutStoryLoader', pathConfig); | ||
if (storyFiles.length > 0) { | ||
writeFile(storyFiles, outputFileConfig.outputFile); | ||
const sortedFiles = sortFiles(storyFiles); | ||
writeFile(sortedFiles, outputFileConfig.outputFile); | ||
logger.info(`Compiled story loader for ${storyFiles.length} files:\n`, ` ${storyFiles.join('\n ')}`); | ||
@@ -24,0 +27,0 @@ } else { |
@@ -41,5 +41,5 @@ const faker = require('faker'); | ||
expect(writer.writeFile) | ||
.toHaveBeenCalledWith(firstFiles.concat(secondFiles), config.outputFiles[0].outputFile); | ||
.toHaveBeenCalledWith(firstFiles.concat(secondFiles).sort(), config.outputFiles[0].outputFile); | ||
expect(writer.writeFile) | ||
.toHaveBeenCalledWith(thirdFiles, config.outputFiles[1].outputFile); | ||
.toHaveBeenCalledWith(thirdFiles.concat().sort(), config.outputFiles[1].outputFile); | ||
}); |
const fs = require('fs'); | ||
const path = require('path'); | ||
const dot = require('dot'); | ||
const dot = require('dot'); | ||
const { getRelativePath, ensureFileDirectoryExists } = require('../utils/pathHelper'); | ||
const { encoding } = require('../constants'); | ||
@@ -9,54 +10,9 @@ | ||
/** | ||
* Determines if the path is prefixed or not | ||
* | ||
* @param {String} relativePath - Relative path to check for directory prefixes | ||
* @returns True if path prefix exists, otherwise false | ||
*/ | ||
function hasPathPrefix(relativePath) { | ||
return relativePath.substr(0, 2) === '..' | ||
|| relativePath.substr(0, 2) === './' | ||
|| relativePath.substr(0, 2) === '.\\'; | ||
} | ||
/** | ||
* Correctly formats path separators | ||
* | ||
* @param {String} path - Path to format | ||
* @returns Path with the correct separators | ||
*/ | ||
function formatPath(dir) { | ||
const oppositeSep = path.sep === '/' ? '\\' : '/'; | ||
return dir.replace(new RegExp(`\\${oppositeSep}`, 'g'), path.sep); | ||
} | ||
function getRelativePaths(fromDir, files) { | ||
const workingFiles = files | ||
.map(file => formatPath(file)) | ||
.map(file => path.resolve(file)); | ||
workingFiles.sort(); | ||
return workingFiles.map((file) => { | ||
let relativePath = path.relative(fromDir, file); | ||
if (!hasPathPrefix(relativePath)) { | ||
relativePath = `.${path.sep}${relativePath}`; | ||
} | ||
return { | ||
relative: relativePath, | ||
full: file, | ||
}; | ||
}); | ||
return files | ||
.map(file => getRelativePath(file, fromDir)) | ||
.concat() | ||
.sort(); | ||
} | ||
function ensureFileDirectoryExists(filePath) { | ||
const directory = path.dirname(filePath); | ||
if (!fs.existsSync(directory)) { | ||
fs.mkdirSync(directory); | ||
} | ||
} | ||
const templateContents = ` | ||
@@ -67,3 +23,3 @@ // template for doT (https://github.com/olado/doT) | ||
{{~it.files :value:index}}require('{{=value.relative}}'); | ||
{{~it.files :value:index}}require('{{=value}}'); | ||
{{~}} | ||
@@ -77,11 +33,11 @@ } | ||
const writeFile = (files, outputPath) => { | ||
const writeFile = (files, outputFile) => { | ||
const template = dot.template(templateContents); | ||
const relativePaths = getRelativePaths(path.dirname(outputPath), files); | ||
const relativePaths = getRelativePaths(path.dirname(outputFile), files); | ||
const output = template({ files: relativePaths }); | ||
ensureFileDirectoryExists(outputPath); | ||
ensureFileDirectoryExists(outputFile); | ||
fs.writeFileSync(outputPath, output, { encoding }); | ||
fs.writeFileSync(outputFile, output, { encoding }); | ||
}; | ||
@@ -88,0 +44,0 @@ |
Sorry, the diff of this file is not supported yet
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
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
168446
32
882
10
6