Skip to content

Commit

Permalink
Lint the files.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterp committed Sep 9, 2019
1 parent 3a55348 commit 243aba4
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 119 deletions.
44 changes: 22 additions & 22 deletions packages/hammer-cli/src/commands/Generate/Generate.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import path from 'path';
import React from 'react';
import { Box, Text, Color } from 'ink';
import path from 'path'
import React from 'react'
import { Box, Text, Color } from 'ink'

import { hammerWorkspaceDir, writeFile, bytes } from 'src/lib';
import { hammerWorkspaceDir, writeFile, bytes } from 'src/lib'

import component from './generators/component';
import component from './generators/component'

/**
* A generator is a function that takes a name and returns a list of filenames
* and contents that should be written to the disk.
*/
const DEFAULT_GENERATORS = {
component,
};
}

const DEFAULT_COMPONENT_DIR = () =>
path.join(hammerWorkspaceDir(), './web/src/components/');
path.join(hammerWorkspaceDir(), './web/src/components/')

const Generate = ({
args,
Expand All @@ -27,17 +27,17 @@ const Generate = ({
<Color red>
The `generate` command has to be run in your hammer project directory.
</Color>
);
)
}

const [
_commandName,
generatorName,
name,
targetDir = DEFAULT_COMPONENT_DIR(),
] = args;
] = args

const generator = generators[generatorName];
const generator = generators[generatorName]

if (!generator || !name) {
return (
Expand All @@ -61,35 +61,35 @@ const Generate = ({
</Box>
</Box>
</>
);
)
}

const files = generator(name);
const results = Object.keys(files).map(filename => {
const contents = files[filename];
const files = generator(name)
const results = Object.keys(files).map((filename) => {
const contents = files[filename]
try {
fileWriter(path.join(targetDir, filename), contents);
fileWriter(path.join(targetDir, filename), contents)
return (
<Text key={`wrote-${filename}`}>
Wrote {filename} {bytes(contents)} bytes
</Text>
);
)
} catch (e) {
return (
<Text key={`error-${filename}`}>
<Color red>{e}</Color>
</Text>
);
)
}
});
})

return results;
};
return results
}

export const commandProps = {
name: 'generate',
alias: 'g',
description: 'Save time by generating boilerplate code',
};
}

export default Generate;
export default Generate
22 changes: 11 additions & 11 deletions packages/hammer-cli/src/commands/Generate/Generate.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import { render } from 'ink-testing-library';
import React from 'react'
import { render } from 'ink-testing-library'

import Generate from './Generate';
import Generate from './Generate'

describe('Command: Generate', () => {
const generators = {
testGenerator: () => ({ 'a.js': 'a', 'a.test.js': 'b' }),
};
}

it('command usage is shown when no or a bad generator is selected', () => {
const { lastFrame } = render(
<Generate args={['generate']} generators={generators} />
);
expect(lastFrame()).toMatch(/Usage:/g);
});
)
expect(lastFrame()).toMatch(/Usage:/g)
})

it('routes to the correct command', () => {
const { lastFrame } = render(
Expand All @@ -22,7 +22,7 @@ describe('Command: Generate', () => {
generators={generators}
fileWriter={() => {}}
/>
);
expect(lastFrame()).toMatch(/Wrote a.js/g);
});
});
)
expect(lastFrame()).toMatch(/Wrote a.js/g)
})
})
30 changes: 15 additions & 15 deletions packages/hammer-cli/src/commands/Generate/generators/component.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import camelcase from 'camelcase';
import camelcase from 'camelcase'

const pascalCase = string => camelcase(string, { pascalCase: true });
const pascalCase = (string) => camelcase(string, { pascalCase: true })

const component = componentName => {
const component = (componentName) => {
return `
/**
* This amazing component does...
Expand All @@ -20,10 +20,10 @@ ${componentName}.queryProps = {
};
export default ${componentName};
`;
};
`
}

const test = componentName => {
const test = (componentName) => {
return `
import React from 'react';
import { fireEvent, cleanup } from '@testing-library/react';
Expand All @@ -42,26 +42,26 @@ describe('${componentName}', () => {
expect(true).toBe(false);
})
})
`;
};
`
}

const mdx = componentName => {
const mdx = (componentName) => {
return `
import ${componentName} from './'
# ${componentName}
- [ ] Document the props/ types
- [ ] Allow user to play with the component
`;
};
`
}

export default name => {
const componentName = pascalCase(name);
export default (name) => {
const componentName = pascalCase(name)

return {
[`${componentName}/${componentName}.js`]: component(componentName),
[`${componentName}/${componentName}.test.js`]: test(componentName),
[`${componentName}/${componentName}.mdx`]: mdx(componentName),
};
};
}
}
12 changes: 6 additions & 6 deletions packages/hammer-cli/src/components/CommandList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Text, Box } from 'ink';
import React from 'react'
import { Text, Box } from 'ink'

const CommandList = ({ commands }) => {
return (
Expand All @@ -17,10 +17,10 @@ const CommandList = ({ commands }) => {
</Box>
<Box flex={1}>{description}</Box>
</Box>
);
)
})}
</Box>
);
};
)
}

export default CommandList;
export default CommandList
10 changes: 5 additions & 5 deletions packages/hammer-cli/src/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { Color, Text, Box } from 'ink';
import React from 'react'
import { Color, Text, Box } from 'ink'

import { version } from '../../package.json';
import { version } from '../../package.json'

export default props => (
export default (props) => (
<Box {...props}>
<Color hex="#FFD900">
<Text bold>⚒ Hammer</Text> - Build something. (https://example.org)
</Color>{' '}
<Color hex="#999">| v{version}</Color>
</Box>
);
)
4 changes: 2 additions & 2 deletions packages/hammer-cli/src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as Header } from './Header';
export { default as CommandList } from './CommandList';
export { default as Header } from './Header'
export { default as CommandList } from './CommandList'
21 changes: 10 additions & 11 deletions packages/hammer-cli/src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#!/usr/bin/env node

import path from 'path';
import React from 'react';
import { render, Box } from 'ink';
import React from 'react'
import { render, Box } from 'ink'

import { getCommands, parseArgs } from 'src/lib';
import { Header, CommandList } from 'src/components';
import { getCommands, parseArgs } from 'src/lib'
import { Header, CommandList } from 'src/components'

const Router = ({ commands, args = [] }) => {
const commandToRun = args[0];
const commandToRun = args[0]
const command = commands.find(({ commandProps: { name, alias } }) =>
[name, alias].includes(commandToRun)
);
)

return (
<Box flexDirection="column">
Expand All @@ -24,11 +23,11 @@ const Router = ({ commands, args = [] }) => {
</>
)}
</Box>
);
};
)
}

if (process.env.NODE_ENV !== 'test') {
render(<Router commands={getCommands()} args={parseArgs()} />);
render(<Router commands={getCommands()} args={parseArgs()} />)
}

export default Router;
export default Router
26 changes: 13 additions & 13 deletions packages/hammer-cli/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { render } from 'ink-testing-library';
import React from 'react'
import { render } from 'ink-testing-library'

import Router from './';
import Router from './'

describe('Router', () => {
const commands = [
Expand All @@ -22,20 +22,20 @@ describe('Router', () => {
"the sound of the tyres on the road roared above that of it's engine.",
},
},
];
]

const renderComponent = props =>
render(<Router commands={commands} args={undefined} {...props} />);
const renderComponent = (props) =>
render(<Router commands={commands} args={undefined} {...props} />)

it('the default menu is shown when no arguments are passed', () => {
const { lastFrame } = renderComponent();
expect(lastFrame()).toMatch(/Commands/g);
});
const { lastFrame } = renderComponent()
expect(lastFrame()).toMatch(/Commands/g)
})

it('routes to the correct command when the name is matched ', () => {
const { lastFrame } = renderComponent({
args: ['command_a'],
});
expect(lastFrame()).toMatch(/I am the a/g);
});
});
})
expect(lastFrame()).toMatch(/I am the a/g)
})
})
Loading

0 comments on commit 243aba4

Please sign in to comment.