Skip to content

Commit

Permalink
Add TypeScript to 'cli' package (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Feb 20, 2020
1 parent f32488f commit 61dbc14
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 2,007 deletions.
1,913 changes: 0 additions & 1,913 deletions packages/cli/package-lock.json

This file was deleted.

24 changes: 15 additions & 9 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,53 @@
"name": "@figma-export/cli",
"description": "Command line for @figma-export",
"version": "1.2.0",
"author": "Marco Montalbano @marcomontalbano",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "Marco Montalbano <me@marcomontalbano.com>",
"bin": {
"figma-export": "./bin/run"
},
"bugs": "https://github.com/marcomontalbano/figma-export/issues",
"dependencies": {
"@figma-export/core": "^1.2.0",
"@oclif/command": "~1.5.19",
"@oclif/config": "~1.13.3",
"@oclif/config": "~1.14.0",
"@oclif/errors": "~1.2.2",
"@oclif/plugin-help": "~2.2.3",
"ora": "~4.0.3"
"ora": "~4.0.3",
"tslib": "~1.10.0"
},
"devDependencies": {
"@oclif/dev-cli": "~1.22.2",
"@oclif/test": "~1.2.5",
"globby": "~11.0.0"
},
"engines": {
"node": ">=8.0.0"
"node": ">=10.0.0"
},
"files": [
"/bin",
"/dist",
"/npm-shrinkwrap.json",
"/oclif.manifest.json",
"/src"
"/oclif.manifest.json"
],
"homepage": "https://github.com/marcomontalbano/figma-export",
"keywords": [
"oclif"
],
"license": "MIT",
"main": "src/index.js",
"oclif": {
"commands": "./src/commands",
"commands": "./dist/commands",
"bin": "figma-export",
"plugins": [
"@oclif/plugin-help"
]
},
"repository": "marcomontalbano/figma-export",
"repository": {
"type": "git",
"url": "git+https://github.com/marcomontalbano/figma-exporter.git",
"directory": "packages/cli"
},
"scripts": {
"postpack": "rm -f oclif.manifest.json",
"prepack": "oclif-dev manifest && oclif-dev readme",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable import/no-extraneous-dependencies */

const sinon = require('sinon');
const { expect, test } = require('@oclif/test');
import sinon from 'sinon';
import { expect, test } from '@oclif/test';

const figmaExport = require('@figma-export/core');
import * as figmaExport from '@figma-export/core';

describe('components', () => {
afterEach(() => {
sinon.restore();
});

it('should stdout a proper message with a fileId and an outputter', () => {
sinon.stub(figmaExport, 'components').returns(Promise.resolve());
sinon.stub(figmaExport, 'components').returns(Promise.resolve([]));

test
.stdout()
Expand All @@ -27,7 +27,7 @@ describe('components', () => {
test
.stdout()
.command(['components', 'RSzpKJcnb6uBRQ3rOfLIyUs5', '-O', '@figma-export/output-components-as-svg'])
.exit(true)
.exit(1)
.it();
});
});
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
const { Command, flags: commandFlags } = require('@oclif/command');
const spinner = require('ora')({});

const fs = require('fs');
const path = require('path');
import { Command, flags as commandFlags } from '@oclif/command';

const figmaExport = require('@figma-export/core');
import * as figmaExport from '@figma-export/core';

const resolveNameOrPath = (nameOrPath) => {
import fs = require('fs');
import path = require('path');
import ora = require('ora');

const spinner = ora({});

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

const requirePackages = (packages, baseOptions = {}) => {
const requirePackages = (packages: Function[], baseOptions = {}): Function[] => {
return packages.map((pkg) => {
if (typeof pkg === 'function') {
return pkg;
}

const pkgNameOrPath = resolveNameOrPath(pkg);

// eslint-disable-next-line import/no-dynamic-require, global-require
// 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() {
async run(): Promise<void> {
const {
args: {
fileId,
Expand All @@ -42,18 +45,18 @@ class ComponentsCommand extends Command {

spinner.start();

return figmaExport.components({
figmaExport.components({
fileId,
token: process.env.FIGMA_TOKEN,
token: process.env.FIGMA_TOKEN || '',
onlyFromPages: page,
transformers: requirePackages(transformer),
outputters: requirePackages(outputter, { output }),
log: (message) => { spinner.text = message; },
log: (message: string) => { spinner.text = message; },
}).then(() => {
spinner.stop();
}).catch((err) => {
}).catch((err: Error) => {
spinner.stop();
this.error(err, { exit: true });
this.error(err, { exit: 1 });
});
}
}
Expand Down
50 changes: 0 additions & 50 deletions packages/cli/src/commands/use-config.js

This file was deleted.

65 changes: 65 additions & 0 deletions packages/cli/src/commands/use-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Command } from '@oclif/command';

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

import fs = require('fs');
import path = require('path');
import ora = require('ora');

type FigmaExportCommand = [string, {}];

const spinner = ora({});

class UseConfigCommand extends Command {
async run(): Promise<void> {
const {
args: {
config,
},
} = this.parse(UseConfigCommand);

const configPath = path.resolve(config);

// eslint-disable-next-line import/no-dynamic-require, global-require
const { commands = [] } = fs.existsSync(configPath) ? require(configPath) : {};

Promise.all(commands.map((command: FigmaExportCommand) => {
const [commandName, options] = command;

spinner.start();

let figmaExporter;
switch (commandName) {
case 'components':
figmaExporter = figmaExport.components;
break;
default:
throw new Error(`Command ${commandName} is not found.`);
}

return figmaExporter({
token: process.env.FIGMA_TOKEN || '',
...options,
log: (message: string) => { spinner.text = message; },
}).then(() => {
spinner.stop();
}).catch((err: Error) => {
spinner.stop();
this.error(err, { exit: 1 });
});
}));
}
}

UseConfigCommand.description = `export using a configuration file
`;

UseConfigCommand.args = [
{
name: 'config',
default: '.figmaexportrc.js',
required: true,
},
];

module.exports = UseConfigCommand;
1 change: 0 additions & 1 deletion packages/cli/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { run } from '@oclif/command';
10 changes: 10 additions & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"references": [
{"path": "../core"},
]
}
2 changes: 1 addition & 1 deletion packages/output-components-as-es6/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import sinon from 'sinon';
import { expect } from 'chai';

import fs from 'fs';
import { camelCase } from '@figma-export/output-components-utils';

import { FigmaExportPageNode } from '@figma-export/types';

import * as figmaDocument from '../../core/src/lib/_config.test';
import * as figma from '../../core/src/lib/figma';

import fs = require('fs');
import outputter = require('./index');

describe('outputter as es6', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/output-components-as-es6/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs';
import path from 'path';
import makeDir from 'make-dir';
import { camelCase } from '@figma-export/output-components-utils';

Expand All @@ -9,6 +7,9 @@ import {
OptionType,
} from './types';

import fs = require('fs');
import path = require('path');

// eslint-disable-next-line @typescript-eslint/no-var-requires
const svgToMiniDataURI = require('mini-svg-data-uri');

Expand Down
3 changes: 1 addition & 2 deletions packages/output-components-as-svg/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import sinon from 'sinon';
import { expect } from 'chai';

import fs from 'fs';

import * as figmaDocument from '../../core/src/lib/_config.test';
import * as figma from '../../core/src/lib/figma';

import fs = require('fs');
import outputter = require('./index');

describe('outputter as svg', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/output-components-as-svg/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs';
import path from 'path';
import makeDir from 'make-dir';

import {
OutputComponentsAsSvgOptionType,
TransformerType,
} from './types';

import fs = require('fs');
import path = require('path');
import makeDir = require('make-dir');

export = ({
output,
getDirname = (options): string => `${options.pageName}${path.sep}${options.dirname}`,
Expand Down
1 change: 0 additions & 1 deletion packages/output-components-as-svg/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
},
"references": [
{"path": "../types"},
{"path": "../output-components-utils"},
]
}
3 changes: 1 addition & 2 deletions packages/output-components-as-svgstore/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import sinon from 'sinon';
import { expect } from 'chai';

import fs from 'fs';

import * as figmaDocument from '../../core/src/lib/_config.test';
import * as figma from '../../core/src/lib/figma';

import fs = require('fs');
import outputter = require('./index');

describe('outputter as svgstore', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/output-components-as-svgstore/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs';
import path from 'path';
import makeDir from 'make-dir';
import svgstore from 'svgstore';

Expand All @@ -8,6 +6,9 @@ import {
OutputComponentsAsSvgstoreOptionType,
} from './types';

import fs = require('fs');
import path = require('path');

export = ({
output,
getIconId = (options): string => `${options.pageName}/${options.componentName}`,
Expand Down
1 change: 0 additions & 1 deletion packages/output-components-as-svgstore/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
},
"references": [
{"path": "../types"},
{"path": "../output-components-utils"},
]
}

0 comments on commit 61dbc14

Please sign in to comment.