Skip to content

Commit

Permalink
Merge pull request #65 from marcomontalbano/i64-add-styles-command
Browse files Browse the repository at this point in the history
Add `styles` command to `figma-export`
  • Loading branch information
marcomontalbano committed Sep 7, 2020
2 parents 2be4a5c + 9c50472 commit 32d256e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 24 deletions.
23 changes: 2 additions & 21 deletions packages/cli/src/commands/components.ts
Expand Up @@ -3,31 +3,12 @@ import { Command, flags as commandFlags } from '@oclif/command';
import * as figmaExport from '@figma-export/core';
import * as FigmaExport from '@figma-export/types';

import fs = require('fs');
import path = require('path');
import { requirePackages } from '../utils';

import ora = require('ora');

const spinner = ora({});

const resolveNameOrPath = (nameOrPath: string): string => {
const absolutePath = path.resolve(nameOrPath);
return fs.existsSync(absolutePath) ? absolutePath : nameOrPath;
};

// eslint-disable-next-line @typescript-eslint/ban-types
const requirePackages = <T extends Function>(packages: (T | string)[], baseOptions = {}): T[] => {
return packages.map((pkg) => {
if (typeof pkg === 'function') {
return pkg;
}

const pkgNameOrPath = resolveNameOrPath(pkg);

// eslint-disable-next-line import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires
return require(pkgNameOrPath)(baseOptions);
});
};

class ComponentsCommand extends Command {
async run(): Promise<void> {
const {
Expand Down
66 changes: 66 additions & 0 deletions packages/cli/src/commands/styles.ts
@@ -0,0 +1,66 @@
import { Command, flags as commandFlags } from '@oclif/command';

import * as figmaExport from '@figma-export/core';
import * as FigmaExport from '@figma-export/types';

import { requirePackages } from '../utils';

import ora = require('ora');

const spinner = ora({});

class StylesCommand extends Command {
async run(): Promise<void> {
const {
args: {
fileId,
},
flags: {
output,
outputter = [],
},
} = this.parse(StylesCommand);

spinner.info(`Exporting ${fileId} as [${outputter.join(', ')}]`);

spinner.start();

figmaExport.styles({
fileId,
token: process.env.FIGMA_TOKEN || '',
outputters: requirePackages<FigmaExport.StyleOutputter>(outputter, { output }),
log: (message: string) => { spinner.text = message; },
}).then(() => {
spinner.stop();
}).catch((err: Error) => {
spinner.stop();
this.error(err, { exit: 1 });
});
}
}

StylesCommand.description = `export styles from a Figma file
`;

StylesCommand.args = [
{
name: 'fileId',
required: true,
},
];

StylesCommand.flags = {
output: commandFlags.string({
char: 'o',
description: 'Output directory',
default: 'output',
multiple: false,
}),
outputter: commandFlags.string({
char: 'O',
description: 'Outputter module or path',
multiple: true,
}),
};

module.exports = StylesCommand;
21 changes: 21 additions & 0 deletions packages/cli/src/utils.ts
@@ -0,0 +1,21 @@
import fs = require('fs');
import path = require('path');

const resolveNameOrPath = (nameOrPath: string): string => {
const absolutePath = path.resolve(nameOrPath);
return fs.existsSync(absolutePath) ? absolutePath : nameOrPath;
};

// eslint-disable-next-line @typescript-eslint/ban-types
export const requirePackages = <T extends Function>(packages: (T | string)[], baseOptions = {}): T[] => {
return packages.map((pkg) => {
if (typeof pkg === 'function') {
return pkg;
}

const pkgNameOrPath = resolveNameOrPath(pkg);

// eslint-disable-next-line import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires
return require(pkgNameOrPath)(baseOptions);
});
};
3 changes: 0 additions & 3 deletions packages/core/src/lib/export-styles.ts
Expand Up @@ -6,16 +6,13 @@ import { fetchStyles, parseStyles } from './figmaStyles';
type Options = {
token: string;
fileId: string;
onlyFromPages?: string[];
// transformers?: FigmaExport.StringTransformer[];
outputters?: FigmaExport.StyleOutputter[];
log?: (msg: string) => void;
}

export const styles = async ({
token,
fileId,
// transformers = [],
outputters = [],
log = (msg): void => {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 32d256e

Please sign in to comment.