Skip to content

Commit d47db43

Browse files
committed
Refactor
1 parent 06c4587 commit d47db43

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

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

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

5-
import { copyTemplateAsync } from './utilities/copy-template-async.js'
5+
import { copyPluginTemplateAsync } from './utilities/copy-plugin-template-async.js'
66
import { installDependenciesAsync } from './utilities/install-dependencies-async.js'
77
import { interpolateValuesIntoFilesAsync } from './utilities/interpolate-values-into-files-async.js'
8+
import { readPluginTemplateNamesAsync } from './utilities/read-template-names-async.js'
89
import { resolveLatestStableVersions } from './utilities/resolve-latest-stable-versions.js'
910
import { createDefaultSettings } from './utilities/settings/create-default-settings.js'
1011
import { promptForUserInputAsync } from './utilities/settings/prompt-for-user-input-async.js'
@@ -20,14 +21,11 @@ export async function createFigmaPluginAsync(options: {
2021
await throwIfDirectoryExistsAsync(join(process.cwd(), name))
2122
}
2223
if (typeof template !== 'undefined') {
23-
const templateDirectory = resolve(
24-
__dirname,
25-
'..',
26-
'plugin-templates',
27-
template
28-
)
29-
if ((await fs.pathExists(templateDirectory)) === false) {
30-
throw new Error(`Invalid template: ${template}`)
24+
const templateNames = await readPluginTemplateNamesAsync()
25+
if (templateNames.indexOf(template) === -1) {
26+
throw new Error(
27+
`Template must be one of "${templateNames.join('", "')}"`
28+
)
3129
}
3230
}
3331
log.info('Scaffolding a new plugin...')
@@ -36,8 +34,8 @@ export async function createFigmaPluginAsync(options: {
3634
: await promptForUserInputAsync({ name, template })
3735
const pluginDirectoryPath = join(process.cwd(), settings.name)
3836
await throwIfDirectoryExistsAsync(pluginDirectoryPath)
39-
log.info('Copying template...')
40-
await copyTemplateAsync(pluginDirectoryPath, settings.template)
37+
log.info(`Copying "${settings.template}" template...`)
38+
await copyPluginTemplateAsync(pluginDirectoryPath, settings.template)
4139
const versions = await resolveLatestStableVersions()
4240
await interpolateValuesIntoFilesAsync(pluginDirectoryPath, {
4341
...settings,

packages/create-figma-plugin/src/utilities/copy-template-async.ts renamed to packages/create-figma-plugin/src/utilities/copy-plugin-template-async.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'url'
44

55
const __dirname = dirname(fileURLToPath(import.meta.url))
66

7-
export async function copyTemplateAsync(
7+
export async function copyPluginTemplateAsync(
88
pluginDirectoryPath: string,
99
template: string
1010
): Promise<void> {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import globby from 'globby'
2+
import { dirname, resolve } from 'path'
3+
import { fileURLToPath } from 'url'
4+
5+
const __dirname = dirname(fileURLToPath(import.meta.url))
6+
7+
export async function readPluginTemplateNamesAsync(): Promise<Array<string>> {
8+
const pluginTemplatesDirectory = resolve(
9+
__dirname,
10+
'..',
11+
'..',
12+
'plugin-templates'
13+
)
14+
const pluginTemplateNames = await globby('*', {
15+
cwd: pluginTemplatesDirectory,
16+
onlyDirectories: true
17+
})
18+
return pluginTemplateNames
19+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { constants } from '@create-figma-plugin/common'
22
import inquirer from 'inquirer'
33

44
import { Settings } from '../../types/settings.js'
5+
import { readPluginTemplateNamesAsync } from '../read-template-names-async.js'
56
import { createPluginDisplayName } from './create-plugin-display-name.js'
67

78
export async function promptForUserInputAsync(options: {
@@ -37,7 +38,7 @@ export async function promptForUserInputAsync(options: {
3738
},
3839
typeof template === 'undefined'
3940
? {
40-
choices: [constants.defaultTemplate, 'ui'],
41+
choices: await readPluginTemplateNamesAsync(),
4142
default: constants.defaultTemplate,
4243
filter,
4344
message: 'template',

0 commit comments

Comments
 (0)