Add yarn new plugin functionality
This commit is contained in:
parent
86d0b17f7f
commit
e28d89f686
3 changed files with 149 additions and 8 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue