Skip to content

Commit 1f86978

Browse files
committed
Simplify create-figma-plugin CLI and templates
1 parent 2a1694c commit 1f86978

File tree

14 files changed

+1007
-982
lines changed

14 files changed

+1007
-982
lines changed

packages/create-figma-plugin/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
},
3737
"dependencies": {
3838
"@create-figma-plugin/common": "^1.2.0",
39-
"degit": "^2.8.4",
4039
"fs-extra": "^10.0.0",
41-
"git-user-name": "^2.0.0",
4240
"globby": "^11.0.4",
4341
"inquirer": "^8.1.1",
4442
"is-utf8": "^0.2.1",

packages/create-figma-plugin/plugin-templates/default/package.json

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
11
{
2-
"name": "{{{name}}}",
3-
{{#version}}
4-
"version": "{{{version}}}",
5-
{{/version}}
6-
{{#description}}
7-
"description": "{{{description}}}",
8-
{{/description}}
9-
"keywords": [
10-
"create-figma-plugin",
11-
"figma",
12-
"figma-plugin"
13-
],
14-
{{#license}}
15-
"license": "{{{license}}}",
16-
{{/license}}
17-
{{#author}}
18-
"author": "{{{author}}}",
19-
{{/author}}
20-
{{#repositoryUrl}}
21-
"repository": "{{{repositoryUrl}}}",
22-
{{/repositoryUrl}}
232
"dependencies": {
243
"@create-figma-plugin/utilities": "^{{{versions.utilities}}}"
254
},

packages/create-figma-plugin/plugin-templates/ui/package.json

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
11
{
2-
"name": "{{{name}}}",
3-
{{#version}}
4-
"version": "{{{version}}}",
5-
{{/version}}
6-
{{#description}}
7-
"description": "{{{description}}}",
8-
{{/description}}
9-
"keywords": [
10-
"create-figma-plugin",
11-
"figma",
12-
"figma-plugin"
13-
],
14-
{{#license}}
15-
"license": "{{{license}}}",
16-
{{/license}}
17-
{{#author}}
18-
"author": "{{{author}}}",
19-
{{/author}}
20-
{{#repositoryUrl}}
21-
"repository": "{{{repositoryUrl}}}",
22-
{{/repositoryUrl}}
232
"dependencies": {
243
"@create-figma-plugin/ui": "^{{{versions.ui}}}",
254
"@create-figma-plugin/utilities": "^{{{versions.utilities}}}",

packages/create-figma-plugin/src/cli.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ sade('create-figma-plugin [name]', true)
99
'-t, --template',
1010
'Pass in the URL of a GitHub repository to use as a template'
1111
)
12-
.option('-y, --yes', 'Use defaults')
12+
.option('-y, --yes', 'Use defaults', false)
1313
.action(async function (
1414
name: string,
1515
options: { yes: boolean; template: string }
1616
): Promise<void> {
17-
const { yes, template } = options
18-
await createFigmaPluginAsync({ name, template }, yes)
17+
await createFigmaPluginAsync({
18+
name,
19+
template: options.template,
20+
useDefaults: options.yes
21+
})
1922
})
2023
.parse(process.argv)

packages/create-figma-plugin/src/create-figma-plugin-async.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,31 @@ import { log } from '@create-figma-plugin/common'
22
import fs from 'fs-extra'
33
import { join } from 'path'
44

5-
import { Settings } from './types/settings.js'
6-
import { cloneFromTemplateAsync } from './utilities/clone-from-template-async.js'
5+
import { copyTemplateAsync } from './utilities/copy-template-async.js'
76
import { installDependenciesAsync } from './utilities/install-dependencies-async.js'
87
import { interpolateValuesIntoFilesAsync } from './utilities/interpolate-values-into-files-async.js'
98
import { resolveLatestStableVersions } from './utilities/resolve-latest-stable-versions.js'
109
import { createDefaultSettings } from './utilities/settings/create-default-settings.js'
1110
import { promptForUserInputAsync } from './utilities/settings/prompt-for-user-input-async.js'
1211

13-
export async function createFigmaPluginAsync(
14-
options: Settings,
12+
export async function createFigmaPluginAsync(options: {
13+
name?: string
14+
template?: string
1515
useDefaults: boolean
16-
): Promise<void> {
16+
}): Promise<void> {
17+
const { name, template, useDefaults } = options
1718
try {
18-
if (typeof options.name !== 'undefined') {
19-
await throwIfDirectoryExistsAsync(join(process.cwd(), options.name))
19+
if (typeof name !== 'undefined') {
20+
await throwIfDirectoryExistsAsync(join(process.cwd(), name))
2021
}
2122
log.info('Scaffolding a new plugin...')
2223
const settings = useDefaults
23-
? createDefaultSettings(options)
24-
: await promptForUserInputAsync(options)
24+
? createDefaultSettings({ name, template })
25+
: await promptForUserInputAsync({ name, template })
2526
const pluginDirectoryPath = join(process.cwd(), settings.name)
2627
await throwIfDirectoryExistsAsync(pluginDirectoryPath)
27-
log.info('Cloning template...')
28-
await cloneFromTemplateAsync(pluginDirectoryPath, settings.template)
28+
log.info('Copying template...')
29+
await copyTemplateAsync(pluginDirectoryPath, settings.template)
2930
const versions = await resolveLatestStableVersions()
3031
await interpolateValuesIntoFilesAsync(pluginDirectoryPath, {
3132
...settings,

packages/create-figma-plugin/src/types/settings.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@ export type Settings = {
22
readonly name: string
33
readonly displayName?: string
44
readonly template: string
5-
readonly version?: string
6-
readonly author?: string
7-
readonly license?: string
85
}

packages/create-figma-plugin/src/utilities/clone-from-template-async.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import fs from 'fs-extra'
2+
import { dirname, join, resolve } from 'path'
3+
import { fileURLToPath } from 'url'
4+
5+
const __dirname = dirname(fileURLToPath(import.meta.url))
6+
7+
export async function copyTemplateAsync(
8+
pluginDirectoryPath: string,
9+
template: string
10+
): Promise<void> {
11+
const templateDirectory = resolve(
12+
__dirname,
13+
'..',
14+
'..',
15+
'plugin-templates',
16+
template
17+
)
18+
await fs.ensureDir(pluginDirectoryPath, 0o2775)
19+
await fs.copy(templateDirectory, pluginDirectoryPath)
20+
const npmIgnoreFile = join(pluginDirectoryPath, '.npmignore')
21+
if ((await fs.pathExists(npmIgnoreFile)) === true) {
22+
// When running via npm/npx, the .gitignore file is renamed to .npmignore,
23+
// so we need to rename it back
24+
const gitIgnoreFile = join(pluginDirectoryPath, '.gitignore')
25+
await fs.move(npmIgnoreFile, gitIgnoreFile)
26+
}
27+
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import { constants } from '@create-figma-plugin/common'
2-
import gitUserName from 'git-user-name'
32

43
import { Settings } from '../../types/settings.js'
54
import { createPluginDisplayName } from './create-plugin-display-name.js'
65

7-
export function createDefaultSettings(options: Settings): Settings {
6+
export function createDefaultSettings(options: {
7+
name?: string
8+
template?: string
9+
}): Settings {
810
const { name, template } = options
9-
const author = gitUserName()
1011
return {
11-
author: author === null ? undefined : author,
1212
displayName:
1313
typeof name === 'undefined'
1414
? constants.packageJson.defaultPluginDisplayName
1515
: createPluginDisplayName(name),
16-
license: constants.packageJson.defaultLicense,
1716
name:
1817
typeof name === 'undefined'
1918
? constants.packageJson.defaultPluginName
2019
: name,
2120
template:
22-
typeof template === 'undefined' ? constants.defaultTemplate : template,
23-
version: constants.packageJson.defaultVersion
21+
typeof template === 'undefined' ? constants.defaultTemplate : template
2422
}
2523
}

packages/create-figma-plugin/src/utilities/settings/prompt-for-user-input-async.ts

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,56 @@
11
import { constants } from '@create-figma-plugin/common'
2-
import gitUserName from 'git-user-name'
32
import inquirer from 'inquirer'
43

54
import { Settings } from '../../types/settings.js'
65
import { createPluginDisplayName } from './create-plugin-display-name.js'
76

8-
export async function promptForUserInputAsync(
9-
options: Settings
10-
): Promise<Settings> {
7+
export async function promptForUserInputAsync(options: {
8+
name?: string
9+
template?: string
10+
}): Promise<Settings> {
1111
const { name, template } = options
1212
const questions = [
1313
typeof name === 'undefined'
14-
? false
15-
: {
14+
? {
1615
filter,
17-
message: 'name',
16+
message: 'Name',
1817
name: 'name',
1918
type: 'input',
2019
validate
21-
},
20+
}
21+
: false,
2222
{
23-
default: function (values: { name: string }): undefined | string {
23+
default: function (answers: { name: string }): undefined | string {
2424
if (typeof name !== 'undefined') {
2525
return createPluginDisplayName(name)
2626
}
27-
if (typeof values.name !== 'undefined') {
28-
return createPluginDisplayName(values.name)
27+
if (typeof answers.name !== 'undefined') {
28+
return createPluginDisplayName(answers.name)
2929
}
3030
return undefined
3131
},
3232
filter,
33-
message: 'display name',
33+
message: 'Display name',
3434
name: 'displayName',
3535
type: 'input',
3636
validate
3737
},
3838
typeof template === 'undefined'
39-
? false
40-
: {
39+
? {
40+
choices: [constants.defaultTemplate, 'ui'],
4141
default: constants.defaultTemplate,
4242
filter,
43-
message: 'template',
43+
message: 'Template',
4444
name: 'template',
45-
type: 'input'
46-
},
47-
{
48-
default: constants.packageJson.defaultVersion,
49-
filter,
50-
message: 'version',
51-
name: 'version',
52-
type: 'input'
53-
},
54-
{
55-
filter,
56-
message: 'description',
57-
name: 'description',
58-
type: 'input'
59-
},
60-
{
61-
filter,
62-
message: 'repository url',
63-
name: 'repositoryUrl',
64-
type: 'input'
65-
},
66-
{
67-
default: gitUserName(),
68-
filter,
69-
message: 'author',
70-
name: 'author',
71-
type: 'input'
72-
},
73-
{
74-
default: constants.packageJson.defaultLicense,
75-
filter,
76-
message: 'license',
77-
name: 'license',
78-
type: 'input'
79-
}
45+
type: 'list'
46+
}
47+
: false
8048
].filter(Boolean)
49+
const answers = await inquirer.prompt(questions)
8150
return {
82-
...(await inquirer.prompt(questions)),
83-
name,
84-
template
51+
displayName: answers.displayName,
52+
name: typeof name === 'undefined' ? answers.name : name,
53+
template: typeof template === 'undefined' ? answers.template : template
8554
}
8655
}
8756

0 commit comments

Comments
 (0)