Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mixin options #65

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bin/tikuicli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ try {
program
.command('create <component> [destination]')
.option('-p, --prefix <name>', 'prefix')
.option('--mo, --mixin-options', 'add options parameter to mixin')
.description('create a component.')
.addHelpText('after', '\nExample:\n $ tikui create -p tikui component src/atom')
.action((component, destination, options) => {
createComponent(destination, component, options.prefix);
createComponent(destination, component, options.prefix, options.mixinOptions);
console.log(`Creating component ${component} to ${path.resolve(destination)}`); // eslint-disable-line no-console
});

Expand Down
15 changes: 11 additions & 4 deletions src/cli/create-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ const createDocumentationTitle = (component: string): string => {

const capitalize = (sentence: string): string => sentence.length > 1 ? sentence.charAt(0).toUpperCase() + sentence.slice(1) : sentence.toUpperCase();

const createMixin = (componentDirectory: string, component: string, prefix?: string): void => {
const content = `mixin ${dashPrefix(prefix)}${component}\n .${dashPrefix(prefix)}${component} ${component}\n`;
const createMixin = (componentDirectory: string, component: string, prefix?: string, mixinOptions?: boolean): void => {
let content = `mixin ${dashPrefix(prefix)}${component}\n .${dashPrefix(prefix)}${component} ${component}\n`;
if(mixinOptions) {
content = `mixin ${dashPrefix(prefix)}${component}(options)\n` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you can extract to a function that returns content in order to prevent content mutation here

'\t-const opt = options || {};\n' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you need to put spaces instead of tabulation

'\t-const classList = [];\n' +
'\t-const classes = classList.length > 0 ? classList.join(\' \') : null;\n' +
`\t.${dashPrefix(prefix)}${component}(class=classes) ${component}\n`;
}
const file = `${component}.mixin.pug`;
createFile(componentDirectory, file);
fs.writeFileSync(path.resolve(componentDirectory, file), content);
Expand Down Expand Up @@ -92,13 +99,13 @@ const assertForComponentName = (componentName: string) => {
}
};

export const createComponent = (basePath: string, component: string, prefix?: string): void => {
export const createComponent = (basePath: string, component: string, prefix?: string, mixinOptions?: boolean): void => {
assertForComponentName(component);
assertForPrefix(prefix);

const componentDirectory = path.resolve(basePath, component);
createDocumentation(componentDirectory, component);
createMixin(componentDirectory, component, prefix);
createMixin(componentDirectory, component, prefix, mixinOptions);
createCode(componentDirectory, component, prefix);
createRender(componentDirectory, component);
createStyle(componentDirectory, component, prefix);
Expand Down
16 changes: 16 additions & 0 deletions test/cli/create-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,20 @@ describe('CLI tests', () => {
.toThrow('The prefix should have alphabetic characters separated by dashes: "-" at position 4 is not allowed');
expect(() => createComponent(FAKE_SRC_DIR, 'a-long-component')).not.toThrow();
});

it('Should style manage mixin with options on component class', () => {
// When
createComponent(FAKE_SRC_DIR, 'component', 'prefix', true);

// Then
const { documentation, mixin, render, style } = componentFiles();
expect(documentation).toBe('## Component\n');
expect(mixin).toBe('mixin prefix-component(options)\n' +
'\t-const opt = options || {};\n' +
'\t-const classList = [];\n' +
'\t-const classes = classList.length > 0 ? classList.join(\' \') : null;\n' +
'\t.prefix-component(class=classes) component\n');
expect(render).toBe('extends /layout\n\nblock body\n include component.code.pug\n');
expect(style).toBe('.prefix-component {\n // component code\n}\n');
});
});