1
0
Fork 0

chore(yarn-new-design): Port to v3.

This commit is contained in:
Benjamin F 2022-10-02 13:11:29 -07:00
parent f847010561
commit 820004f3dd
3 changed files with 98 additions and 67 deletions

View file

@ -22,9 +22,8 @@ else if (type === 'plugin') {
console.clear()
console.log(banner)
addPlugin()
}
else console.log(`
} else
console.log(`
Usage:
${chalk.bold.blue('yarn new design')} 👉 Adds a new design
@ -32,7 +31,6 @@ else console.log(`
${chalk.bold.blue('yarn new')} ${chalk.yellow('[anything else]')} 👉 Shows this help
`)
async function addDesign() {
console.log(`
@ -47,10 +45,22 @@ async function addDesign() {
name: 'type',
message: 'In what category should we add the design?',
choices: [
{ title: 'Accessories', value: 'accessories', description: 'Hats, bags, plushies, and so on' },
{
title: 'Accessories',
value: 'accessories',
description: 'Hats, bags, plushies, and so on',
},
{ title: 'Blocks', value: 'blocks', description: 'Blocks/Slopers to base other designs on' },
{ title: 'Garments', value: 'garments', description: 'For clothes. The most common category' },
{ title: 'Utilities', value: 'utilities', description: 'For utility designs such as our rendertest or legend' },
{
title: 'Garments',
value: 'garments',
description: 'For clothes. The most common category',
},
{
title: 'Utilities',
value: 'utilities',
description: 'For utility designs such as our rendertest or legend',
},
],
initial: 2,
})
@ -61,8 +71,12 @@ async function addDesign() {
We'd appreciate if you pick:
- a firstname like ${chalk.green('alex')}, ${chalk.green('jordan')}, ${chalk.green('ezra')}, or ${chalk.green('logan')}
- that is an aliteration with the kind of design, like ${chalk.green('wahid')} for a ${chalk.green('w')}aistcoat
- a firstname like ${chalk.green('alex')}, ${chalk.green('jordan')}, ${chalk.green(
'ezra'
)}, or ${chalk.green('logan')}
- that is an aliteration with the kind of design, like ${chalk.green(
'wahid'
)} for a ${chalk.green('w')}aistcoat
Bonus points for picking a name that embraces diversity 🌈
`)
@ -75,9 +89,9 @@ async function addDesign() {
})
if (name && type) {
console.log("\n"+` Alright, let's add ${chalk.green(name)} under ${chalk.green(type)} 🪄`)
console.log('\n' + ` Alright, let's add ${chalk.green(name)} under ${chalk.green(type)} 🪄`)
createDesign(name, type)
execSync("npm run reconfigure")
execSync('npm run reconfigure')
console.log(` All done 🎉`)
try {
@ -86,9 +100,9 @@ async function addDesign() {
${chalk.bold.yellow('✨ Summary')}
${chalk.gray('≡≡≡≡≡≡≡≡≡≡')}
👉 We've created your design skeleton at ${chalk.green('designs/'+name)}
👉 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 added ${chalk.green('designs/'+name)} to the lab
👉 We've added ${chalk.green('designs/' + name)} to the lab
${chalk.bold.yellow('✏️ Make it your own')}
@ -96,9 +110,17 @@ async function addDesign() {
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/designs.json')}
👉 ${chalk.yellow('Dependencies')}: If you need additional plugins or patterns to extend, update ${chalk.green('config/dependecies.yaml')}
👉 ${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/designs.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).
@ -111,7 +133,7 @@ async function addDesign() {
🤓 Happy hacking
`)
} catch(err) {
} catch (err) {
console.log(err)
}
}
@ -130,14 +152,16 @@ async function addPlugin() {
`)
}
function validateName(name) {
if ([
...Object.keys(designs.accessories),
...Object.keys(designs.blocks),
...Object.keys(designs.garments),
...Object.keys(designs.utilities)
].indexOf(name) !== -1) return `Sorry but ${name} is already taken so you'll need to pick something else`
if (
[
...Object.keys(designs.accessories),
...Object.keys(designs.blocks),
...Object.keys(designs.garments),
...Object.keys(designs.utilities),
].indexOf(name) !== -1
)
return `Sorry but ${name} 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 🤷'
@ -146,41 +170,36 @@ function validateName(name) {
function createDesign(name, type) {
const template = ['config', 'templates', 'design']
const design = ['designs', name]
const description = 'FIXME: A FreeSewing 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
designs[type][name] = description
write(
['config', 'software', 'designs.json'],
JSON.stringify(orderDesigns(designs), null, 2)
)
designs[type][name] = {
description: description,
code: 'Coder name',
design: 'Designer name',
difficulty: 1,
tags: ['tagname'],
}
write(['config', 'software', 'designs.json'], JSON.stringify(orderDesigns(designs), null, 2))
// Create folders
mkdir([...design, 'src'])
mkdir([...design, 'tests'])
// Create package.json
templateOut(
[...template, 'package.json'],
[...design, 'package.json'],
{ name, description }
)
templateOut([...template, 'package.json'], [...design, 'package.json'], { name, description })
// Create config file
templateOut(
['config', 'templates', 'design', 'config.js'],
['designs', name, 'config.js'],
{ name, description }
)
// Create index.mjs
templateOut([...template, 'src', 'index.mjs'], [...design, 'src', 'index.mjs'], {
capitalized_name,
})
// Create tests file
cp(
[...template, 'tests', 'shared.test.mjs'],
[...design, 'tests', 'shared.test.mjs']
)
cp([...template, 'tests', 'shared.test.mjs'], [...design, 'tests', 'shared.test.mjs'])
// Copy source
for (const file of ['index.js', 'box.js']) {
for (const file of ['box.mjs']) {
cp([...template, 'src', file], [...design, 'src', file])
}
}
@ -189,12 +208,9 @@ function templateOut(from, to, data) {
try {
fs.writeFileSync(
path.join(process.cwd(), ...to),
mustache.render(
fs.readFileSync(path.join(process.cwd(), ...from), 'utf-8'),
data
)
mustache.render(fs.readFileSync(path.join(process.cwd(), ...from), 'utf-8'), data)
)
} catch(err) {
} catch (err) {
console.log(err)
}
@ -204,7 +220,7 @@ function templateOut(from, to, data) {
function write(to, data) {
try {
fs.writeFileSync(path.join(process.cwd(), ...to), data)
} catch(err) {
} catch (err) {
console.log(err)
}
@ -213,11 +229,8 @@ function write(to, data) {
function mkdir(dir) {
try {
fs.mkdirSync(
path.join(process.cwd(), ...dir),
{ recursive: true }
)
} catch(err) {
fs.mkdirSync(path.join(process.cwd(), ...dir), { recursive: true })
} catch (err) {
console.log(err)
}
@ -226,11 +239,8 @@ function mkdir(dir) {
function cp(from, to) {
try {
fs.copyFileSync(
path.join(process.cwd(), ...from),
path.join(process.cwd(), ...to)
)
} catch(err) {
fs.copyFileSync(path.join(process.cwd(), ...from), path.join(process.cwd(), ...to))
} catch (err) {
console.log(err)
}