From e28d89f68660e50ebef6cd07346ed608b8bb2fe5 Mon Sep 17 00:00:00 2001 From: bobgeorgethe3rd Date: Tue, 18 Apr 2023 13:13:08 +0000 Subject: [PATCH 1/6] Add yarn new plugin functionality --- config/templates/plugin/package.json.mustache | 12 ++ .../templates/plugin/src/index.mjs.mustache | 23 ++++ scripts/add-software.mjs | 122 ++++++++++++++++-- 3 files changed, 149 insertions(+), 8 deletions(-) create mode 100644 config/templates/plugin/package.json.mustache create mode 100644 config/templates/plugin/src/index.mjs.mustache diff --git a/config/templates/plugin/package.json.mustache b/config/templates/plugin/package.json.mustache new file mode 100644 index 00000000000..0d3e911a502 --- /dev/null +++ b/config/templates/plugin/package.json.mustache @@ -0,0 +1,12 @@ +{ + "name": "{{pluginName}}", + "version": "0.0.1", + "description": "{{description}}", + "author": "Joost De Cock (https://github.com/joostdecock)", + "homepage": "https://freesewing.org/", + "repository": "github:freesewing/freesewing", + "license": "MIT", + "main": "dist/index.mjs", + "module": "dist/index.mjs", + "scripts": {} +} diff --git a/config/templates/plugin/src/index.mjs.mustache b/config/templates/plugin/src/index.mjs.mustache new file mode 100644 index 00000000000..9dde15587ec --- /dev/null +++ b/config/templates/plugin/src/index.mjs.mustache @@ -0,0 +1,23 @@ +import { name, version } from '../data.mjs' + +export const plugin = { + name, + version, + macros: { + {{name}}: function (so, {points, paths, Path}) { //Example shorthand, you may wish to add other elements like utils + +const defaults = { +//note these are common examples and can be removed + scale: 1, + rotation: 0, + } + so = { ...defaults, ...so } + + + }, + }, +} + +// More specifically named exports +export const {{name}}Plugin = plugin +export const plugin{{capitalized_name}} = plugin diff --git a/scripts/add-software.mjs b/scripts/add-software.mjs index 8e226909ad2..5d134e3b2ed 100644 --- a/scripts/add-software.mjs +++ b/scripts/add-software.mjs @@ -7,6 +7,7 @@ import mustache from 'mustache' import { execSync } from 'child_process' // Software import designs from '../config/software/designs.json' assert { type: 'json' } +import plugins from '../config/software/plugins.json' assert { type: 'json' } const type = process.argv[2] @@ -85,7 +86,7 @@ async function addDesign() { type: 'text', name: 'name', message: 'What name would you like the design to have? ([a-z] only)', - validate: validateName, + validate: validateNameDesign, }) if (name && type) { @@ -142,17 +143,77 @@ async function addDesign() { async function addPlugin() { console.log(` - ${chalk.bold.yellow('🙈 Oh no; You called our bluf!')} - ${chalk.gray('≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡')} + ${chalk.bold.yellow('👕 Add a new plugin')} + ${chalk.gray('≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡')} - Adding plugins is not (yet) implemented 😬 - - Sorry 🤥 + We're going to add a new plugin to this repository. That's awesome 🎉 + Let's start by picking the name for this plugin 🏷️ + Try to keep it to one word that explains what the plugin does e.g. ${chalk.green( + 'flip' + )}, ${chalk.green('mirror')}, + ${chalk.green('round')}. `) + + const { name } = await prompts({ + type: 'text', + name: 'name', + message: 'What name would you like the plugin to have? ([a-z] only)', + validate: validateNamePlugin, + }) + + if (name) { + console.log('\n' + ` Alright, let's add ${chalk.green(name)} to plugins 🪄`) + createPlugin(name) + execSync('npm run reconfigure') + console.log(` All done 🎉`) + + try { + console.log(` + + ${chalk.bold.yellow('✨ Summary')} + ${chalk.gray('≡≡≡≡≡≡≡≡≡≡')} + + 👉 We've created your plugin skeleton at ${chalk.green('plugins/plugin-' + name)} + 👉 We've configured the packages via the ${chalk.green('pacakge.json')} file + 👉 We've added ${chalk.green('plugins/plugin-/' + name)} to the lab + + + ${chalk.bold.yellow('✏️ Make it your own')} + ${chalk.gray('≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡')} + + Hhere's a few other things you can configure: + + 👉 ${chalk.yellow('Author')}: Credit where credit is due; Add yourself as author in ${chalk.green( + 'config/exceptions.yaml' + )} + 👉 ${chalk.yellow('Description')}: We used a placeholder description; Update it in ${chalk.green( + 'config/software/plugins.json' + )} + 👉 ${chalk.yellow( + 'Dependencies' + )}: If you need additional plugins or patterns to extend, update ${chalk.green( + 'config/dependecies.yaml' + )} + + If you change any of these, run ${chalk.blue('yarn reconfigure')} to update the package(s). + + + ${chalk.bold.yellow('👷 Get to work')} + ${chalk.gray('≡≡≡≡≡≡≡≡≡≡≡≡≡≡')} + + 🛠️ You can now start the development environment with ${chalk.blue('yarn lab')} + 📖 Documentation is available at ${chalk.green('https://freesewing.dev/')} + 🤓 Happy hacking + + `) + } catch (err) { + console.log(err) + } + } } -function validateName(name) { +function validateNameDesign(name) { if ( [ ...Object.keys(designs.accessories), @@ -167,10 +228,19 @@ function validateName(name) { else return ' 🙈 Please use only [a-z], no spaces, no capitals, no nothing 🤷' } +function validateNamePlugin(name) { + const pluginName = 'plugin-' + name + if ([...Object.keys(plugins)].indexOf(pluginName) !== -1) + return `Sorry but ${pluginName} is already taken so you'll need to pick something else` + + if (/^([a-z]+)$/.test(name)) return true + else return ' 🙈 Please use only [a-z], no spaces, no capitals, no nothing 🤷' +} + function createDesign(name, type) { const template = ['config', 'templates', 'design'] const design = ['designs', name] - const description = 'A FreeSewing pattern that needs a description' + const description = 'A pattern that needs a description' const capitalized_name = name.charAt(0).toUpperCase() + name.slice(1) // Add to designs config file @@ -207,6 +277,33 @@ function createDesign(name, type) { } } +function createPlugin(name) { + const pluginName = 'plugin-' + name + const template = ['config', 'templates', 'plugin'] + const description = 'A plugin that needs a description' + const plugin = ['plugins', pluginName] + const capitalized_name = name.charAt(0).toUpperCase() + name.slice(1) + + // Create folders + mkdir([...plugin, 'src']) + mkdir([...plugin, 'tests']) + + // Create package.json + templateOut([...template, 'package.json.mustache'], [...plugin, 'package.json'], { + pluginName, + description, + }) + + plugins[pluginName] = description + write(['config', 'software', 'plugins.json'], JSON.stringify(orderPlugins(plugins), null, 2)) + + // Create index.mjs + templateOut([...template, 'src', 'index.mjs.mustache'], [...plugin, 'src', 'index.mjs'], { + name, + capitalized_name, + }) +} + function templateOut(from, to, data) { try { fs.writeFileSync( @@ -262,3 +359,12 @@ function orderDesigns(designs) { return newDesigns } +function orderPlugins(plugins) { + // Ensure plugins are listed alphabetically + const newPlugins = {} + for (const plugin of Object.keys(plugins).sort()) { + newPlugins[plugin] = plugins[plugin] + } + + return newPlugins +} From 4b186f16543f06d63bb7e914a4e93893efbf17bd Mon Sep 17 00:00:00 2001 From: bobgeorgethe3rd Date: Tue, 18 Apr 2023 13:20:40 +0000 Subject: [PATCH 2/6] More logical validate names --- scripts/add-software.mjs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/add-software.mjs b/scripts/add-software.mjs index 5d134e3b2ed..715ba974a3c 100644 --- a/scripts/add-software.mjs +++ b/scripts/add-software.mjs @@ -86,7 +86,7 @@ async function addDesign() { type: 'text', name: 'name', message: 'What name would you like the design to have? ([a-z] only)', - validate: validateNameDesign, + validate: validateDesignName, }) if (name && type) { @@ -159,7 +159,7 @@ async function addPlugin() { type: 'text', name: 'name', message: 'What name would you like the plugin to have? ([a-z] only)', - validate: validateNamePlugin, + validate: validatePluginName, }) if (name) { @@ -213,7 +213,7 @@ async function addPlugin() { } } -function validateNameDesign(name) { +function validateDesignName(name) { if ( [ ...Object.keys(designs.accessories), @@ -228,7 +228,7 @@ function validateNameDesign(name) { else return ' 🙈 Please use only [a-z], no spaces, no capitals, no nothing 🤷' } -function validateNamePlugin(name) { +function validatePluginName(name) { const pluginName = 'plugin-' + name if ([...Object.keys(plugins)].indexOf(pluginName) !== -1) return `Sorry but ${pluginName} is already taken so you'll need to pick something else` From 5f15d10bbfe0fb9a7edac05cfbb0e979ae027724 Mon Sep 17 00:00:00 2001 From: bobgeorgethe3rd Date: Tue, 18 Apr 2023 13:29:54 +0000 Subject: [PATCH 3/6] add back FreeSewing to descriptions --- scripts/add-software.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/add-software.mjs b/scripts/add-software.mjs index 715ba974a3c..60112455137 100644 --- a/scripts/add-software.mjs +++ b/scripts/add-software.mjs @@ -240,7 +240,7 @@ function validatePluginName(name) { function createDesign(name, type) { const template = ['config', 'templates', 'design'] const design = ['designs', name] - const description = 'A pattern that needs a description' + const description = 'A FreeSewing pattern that needs a description' const capitalized_name = name.charAt(0).toUpperCase() + name.slice(1) // Add to designs config file @@ -280,7 +280,7 @@ function createDesign(name, type) { function createPlugin(name) { const pluginName = 'plugin-' + name const template = ['config', 'templates', 'plugin'] - const description = 'A plugin that needs a description' + const description = 'A FreeSewing plugin that needs a description' const plugin = ['plugins', pluginName] const capitalized_name = name.charAt(0).toUpperCase() + name.slice(1) From aafef2022c54b0adc396754c3e9a507b162602bf Mon Sep 17 00:00:00 2001 From: bobgeorgethe3rd Date: Tue, 18 Apr 2023 18:29:57 +0000 Subject: [PATCH 4/6] fix typos --- scripts/add-software.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/add-software.mjs b/scripts/add-software.mjs index 60112455137..aea863e52fb 100644 --- a/scripts/add-software.mjs +++ b/scripts/add-software.mjs @@ -120,7 +120,7 @@ async function addDesign() { 👉 ${chalk.yellow( 'Dependencies' )}: If you need additional plugins or patterns to extend, update ${chalk.green( - 'config/dependecies.yaml' + 'config/dependencies.yaml' )} If you change any of these, run ${chalk.blue('yarn reconfigure')} to update the package(s). @@ -176,7 +176,7 @@ async function addPlugin() { 👉 We've created your plugin skeleton at ${chalk.green('plugins/plugin-' + name)} 👉 We've configured the packages via the ${chalk.green('pacakge.json')} file - 👉 We've added ${chalk.green('plugins/plugin-/' + name)} to the lab + 👉 We've added ${chalk.green('plugins/plugin-' + name)} to the lab ${chalk.bold.yellow('✏️ Make it your own')} @@ -193,7 +193,7 @@ async function addPlugin() { 👉 ${chalk.yellow( 'Dependencies' )}: If you need additional plugins or patterns to extend, update ${chalk.green( - 'config/dependecies.yaml' + 'config/dependencies.yaml' )} If you change any of these, run ${chalk.blue('yarn reconfigure')} to update the package(s). From 62620e4a0f2b1bd45cb427396c6f04294eb6a45a Mon Sep 17 00:00:00 2001 From: bobgeorgethe3rd Date: Tue, 18 Apr 2023 18:36:15 +0000 Subject: [PATCH 5/6] indentation + comment --- config/templates/plugin/src/index.mjs.mustache | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/config/templates/plugin/src/index.mjs.mustache b/config/templates/plugin/src/index.mjs.mustache index 9dde15587ec..7d592d99feb 100644 --- a/config/templates/plugin/src/index.mjs.mustache +++ b/config/templates/plugin/src/index.mjs.mustache @@ -5,15 +5,13 @@ export const plugin = { version, macros: { {{name}}: function (so, {points, paths, Path}) { //Example shorthand, you may wish to add other elements like utils - -const defaults = { -//note these are common examples and can be removed - scale: 1, - rotation: 0, - } - so = { ...defaults, ...so } - - + const defaults = { + //note these are common examples and can be removed + scale: 1, + rotation: 0, + } + so = { ...defaults, ...so } + //write plugin here }, }, } From 627253d538fd269834e02dcb6ffa11209559fe32 Mon Sep 17 00:00:00 2001 From: bobgeorgethe3rd Date: Tue, 18 Apr 2023 18:47:26 +0000 Subject: [PATCH 6/6] more typos and line removal --- scripts/add-software.mjs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/add-software.mjs b/scripts/add-software.mjs index aea863e52fb..15d26b70b99 100644 --- a/scripts/add-software.mjs +++ b/scripts/add-software.mjs @@ -102,7 +102,7 @@ async function addDesign() { ${chalk.gray('≡≡≡≡≡≡≡≡≡≡')} 👉 We've created your design skeleton at ${chalk.green('designs/' + name)} - 👉 We've configured the packages via the ${chalk.green('pacakge.json')} file + 👉 We've configured the packages via the ${chalk.green('package.json')} file 👉 We've added ${chalk.green('designs/' + name)} to the lab @@ -175,8 +175,7 @@ async function addPlugin() { ${chalk.gray('≡≡≡≡≡≡≡≡≡≡')} 👉 We've created your plugin skeleton at ${chalk.green('plugins/plugin-' + name)} - 👉 We've configured the packages via the ${chalk.green('pacakge.json')} file - 👉 We've added ${chalk.green('plugins/plugin-' + name)} to the lab + 👉 We've configured the packages via the ${chalk.green('package.json')} file ${chalk.bold.yellow('✏️ Make it your own')}