-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from marcomontalbano/i64-add-styles-command
Add `styles` command to `figma-export`
- Loading branch information
Showing
4 changed files
with
89 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters