Skip to content

Commit

Permalink
Merge pull request #1 from kadirahq/file-watcher
Browse files Browse the repository at this point in the history
Add file watcher
  • Loading branch information
roonyh authored Sep 14, 2016
2 parents f5e82cd + 61f49b6 commit a4dec8c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 36 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"babel-polyfill": "^6.13.0",
"babel-runtime": "6.x.x",
"chalk": "^1.1.3",
"chokidar": "^1.6.0",
"jest-diff": "^15.1.0",
"jest-snapshot": "^15.1.1",
"promptly": "^2.1.0",
Expand Down
56 changes: 23 additions & 33 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import runTests from './test_runner';
import { getStorybook } from '@kadira/storybook';
import path from 'path';
import program from 'commander';
import chokidar from 'chokidar';

const { jasmine } = global;

Expand All @@ -12,44 +13,33 @@ program
.option('-i, --update-interactive [boolean]',
'Update saved story snapshots interactively')
.option('-g, --grep [string]', 'only test stories matching regexp')
.option('-w, --watch [boolean]', 'watch file changes and rerun tests')
.parse(process.argv);

const configDir = program.configDir || './.storybook';

const configPath = path.resolve(`${configDir}`, 'config');
require(configPath);

let storybook = getStorybook();

const { update, updateInteractive:updateI, grep } = program;

if(grep) {
const filteredStorybook = [];
for (const group of storybook) {
const re = new RegExp(grep);
if(re.test(group.kind)){
filteredStorybook.push(group);
continue;
}

const filteredGroup = {
kind: group.kind,
stories: []
};

for (const story of group.stories) {
if(re.test(story.name)){
filteredGroup.stories.push(story);
continue;
}
}

if(filteredGroup.stories.length > 0){
filteredStorybook.push(filteredGroup);
}
}

storybook = filteredStorybook;
if(program.watch) {
var watcher = chokidar.watch('.', {
ignored: 'node_modules', // TODO: Should node_modules also be watched?
persistent: true
});

watcher.on('ready', () => {
console.log('storybook-snapshot-test is in watch mode.');
watcher.on('all', () => {
// Need to remove the require cache. Because it containes modules before
// changes were made.
Object.keys(require.cache).forEach(key => {
delete require.cache[key];
})
require(configPath);
const changedStorybook = require('@kadira/storybook').getStorybook()
runTests(changedStorybook, program);
});
})
}

runTests(storybook, {configDir, update, updateI});
require(configPath);
runTests(getStorybook(), program);
14 changes: 11 additions & 3 deletions src/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import diff from 'jest-diff';
import chalk from 'chalk';
import promptly from 'promptly';
import path from 'path';
import { filterStorybook } from './util';

export default async function runTests(storybook, options) {
const {
configDir = './.storybook',
update,
updateInteractive: updateI,
grep
} = options;

export default async function runTests(storybook, {configDir, update, updateI}) {
const allTestState = {
added: 0,
matched: 0,
Expand All @@ -14,7 +22,7 @@ export default async function runTests(storybook, {configDir, update, updateI})
obsolete: 0,
}

for (const group of storybook) {
for (const group of filterStorybook(storybook, grep)) {

const filePath = path.resolve(`${configDir}`, `${group.kind}`);
console.log(chalk.underline(`${group.kind}`));
Expand Down Expand Up @@ -138,7 +146,7 @@ function logAllState(state) {
}
}

async function confirmUpate(callback) {
async function confirmUpate() {
process.stdout.write('\nReceived story is different from stored snapshot.\n');

let ans = await promptly.prompt('Should this snapshot be updated?(y/n)');
Expand Down
32 changes: 32 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export function filterStorybook(storybook, grep) {
if(!grep || grep.length === 0 ){
return storybook
}

const filteredStorybook = [];
for (const group of storybook) {
const re = new RegExp(grep);
if(re.test(group.kind)){
filteredStorybook.push(group);
continue;
}

const filteredGroup = {
kind: group.kind,
stories: []
};

for (const story of group.stories) {
if(re.test(story.name)){
filteredGroup.stories.push(story);
continue;
}
}

if(filteredGroup.stories.length > 0){
filteredStorybook.push(filteredGroup);
}
}

return filteredStorybook;
}

0 comments on commit a4dec8c

Please sign in to comment.