1
0
Fork 0

all bases use the same part template

This commit is contained in:
Enoch Riese 2022-11-29 17:57:03 -06:00
parent af4185b126
commit 27a9e639f6
7 changed files with 378 additions and 56 deletions

View file

@ -29,7 +29,7 @@ i18n:
models:
test: 'npx mocha tests/*.test.mjs'
new-design:
i18n-only: 'node ../../sites/shared/prebuild/i18n-only.mjs'
i18n-only: 'SITE="new-design/shared" node ../../sites/shared/prebuild/i18n-only.mjs'
build: '!'
wbuild: '!'
lint: "npx eslint 'lib/*.mjs'"

View file

@ -156,45 +156,50 @@ const copyTemplate = async (config, choices) => {
const designSrcDir = join(config.dest, 'design/src')
// Template the index file
const complexParts = typeof config.templateData.parts[0] === 'object'
const indexTemplate = await readFile(config.files.templates['index'], 'utf-8')
const indexTo = join(designSrcDir, 'index.mjs')
const partNames = complexParts
? config.templateData.parts.map((p) => p.part)
: config.templateData.parts
promises.push(
writeTemplateFile(indexTo, indexTemplate, {
name: choices.name,
parts: config.templateData.parts,
parts: partNames,
})
)
// Template the parts
if (choices.template !== 'scratch') {
const partTemplate = await readFile(config.files.templates['inherited-part'], 'utf-8')
const doesInherit = !config.templateData.noInheritance
const partTemplate = await readFile(config.files.templates.part, 'utf-8')
const baseConfig = {
baseName: choices.template,
baseNameUpcase: choices.template[0].toUpperCase() + choices.template.slice(1),
name: choices.name,
doesInherit,
}
config.templateData.parts.forEach(async (p) => {
const to = join(designSrcDir, `${p}.mjs`)
promises.push(
writeTemplateFile(to, partTemplate, {
part: p,
partUpcase: p[0].toUpperCase() + p.slice(1),
if (doesInherit) {
baseConfig.baseName = choices.template
baseConfig.baseNameUpcase = choices.template[0].toUpperCase() + choices.template.slice(1)
}
config.templateData.parts.forEach((p) => {
const templateArgs = doesInherit
? {
...baseConfig,
})
)
})
draftUses: {},
part: p,
}
: {
...baseConfig,
draftUses: {},
...p,
}
// // Template files
// for (const from of config.files.template) {
// let to = join(config.dest, from.slice(config.source.template.length - 7))
// if (to.slice(-9) === '.mustache') to = to.slice(0, -9)
// if (!dirs[to]) await ensureDir(to)
// // Template out file
// const src = await readFile(from, 'utf-8')
// promises.push(writeFile(to, mustache.render(src, { name: choices.name, tag: config.tag })))
// }
templateArgs.partUpcase = templateArgs.part[0].toUpperCase() + templateArgs.part.slice(1)
const to = join(designSrcDir, `${templateArgs.part}.mjs`)
promises.push(writeTemplateFile(to, partTemplate, templateArgs))
})
await Promise.all(promises)

View file

@ -24,7 +24,7 @@
"lab": "cd ../../sites/lab && yarn start",
"tips": "node ../../scripts/help.mjs",
"lint": "npx eslint 'lib/*.mjs'",
"i18n-only": "node ../../sites/shared/prebuild/i18n-only.mjs"
"i18n-only": "SITE=\"new-design/shared\" node ../../sites/shared/prebuild/i18n-only.mjs"
},
"peerDependencies": {},
"dependencies": {

View file

@ -0,0 +1,68 @@
export const noInheritance = true
export const parts = [
{
part: 'box',
pluginBundle: true,
options: `size: { pct: 50, min: 10, max: 100 }`,
draftUses: {
options: true,
points: true,
paths: true,
Point: true,
Path: true,
complete: true,
paperless: true,
sa: true,
snippets: true,
Snippet: true,
macro: true,
},
draft: `// Add points to make a box
const w = 500 * options.size
points.topLeft = new Point(0, 0)
points.topRight = new Point(w, 0)
points.bottomLeft = new Point(0, w / 2)
points.bottomRight = new Point(w, w / 2)
// Create a path for the box outline
paths.seam = new Path()
.move(points.topLeft)
.line(points.bottomLeft)
.line(points.bottomRight)
.line(points.topRight)
.line(points.topLeft)
.close()
.setClass('fabric')
// Complete?
if (complete) {
// Add a logo
points.logo = points.topLeft.shiftFractionTowards(points.bottomRight, 0.5)
snippets.logo = new Snippet('logo', points.logo)
// Add some text
points.text = points.logo
.shift(-90, w / 8)
.addText('FreeSewing', 'center')
if (sa) {
// Add seam allowance
paths.sa = paths.seam.offset(sa).setClass('fabric sa')
}
}
// Add dimensions for paperless mode
if (paperless) {
macro('hd', {
from: points.bottomLeft,
to: points.bottomRight,
y: points.bottomLeft.y + sa + 15,
})
macro('vd', {
from: points.bottomRight,
to: points.topRight,
x: points.topRight.x + sa + 15,
})
}`,
},
]

View file

@ -0,0 +1,9 @@
export const noInheritance = true
export const parts = [
{
part: 'bib',
draft: '',
pluginBundle: false,
},
]
export const dependencies = []

View file

@ -0,0 +1,192 @@
{{#doesInherit}}
import { {{part}} as {{baseName}}{{partUpcase}} } from '@freesewing/{{baseName}}'
{{/doesInherit}}
{{#pluginBundle}}
import { pluginBundle } from '@freesewing/plugin-bundle'
{{/pluginBundle}}
function draft{{partUpcase}} ({
// Uncomment below to destructure what you need
/*
* Content constructors
*/
{{^draftUses.Path}}//{{/draftUses.Path}}Path, // A Path constructor to create new paths
{{^draftUses.Point}}//{{/draftUses.Point}}Point, // A Point constructor to create new points
{{^draftUses.Snippet}}//{{/draftUses.Snippet}}Snippet, // A Snippet constructor to create new snippets
/*
* Content constainers
*/
{{^draftUses.paths}}//{{/draftUses.paths}}paths, // Add a Path to your part by adding it to this object
{{^draftUses.points}}//{{/draftUses.points}}points, // Add a Points to your part by adding it to this object
{{^draftUses.snippets}}//{{/draftUses.snippets}}snippets, // Add a Snippet to your part by adding it to this object
/*
* Access to settings
*/
{{^draftUses.absoluteOptions}}//{{/draftUses.absoluteOptions}}absoluteOptions, // Access to settings.absoluteOptions
{{^draftUses.complete}}//{{/draftUses.complete}}complete, // Access to settings.complete
{{^draftUses.measurements}}//{{/draftUses.measurements}}measurements, // Access to settings.measurements
{{^draftUses.options}}//{{/draftUses.options}}options, // Access to settings.options
{{^draftUses.paperless}}//{{/draftUses.paperless}}paperless, // Access to settings.paperless
{{^draftUses.sa}}//{{/draftUses.sa}}sa, // Access to settings.sa
{{^draftUses.scale}}//{{/draftUses.scale}}scale, // Access to settings.scale
/*
* Access to utilities
*/
{{^draftUses.getId}}//{{/draftUses.getId}}getId, //See the getId documentation
{{^draftUses.hide}}//{{/draftUses.hide}}hide, //See the hide documentation
{{^draftUses.log}}//{{/draftUses.log}}log, //See the logging documentation
{{^draftUses.macro}}//{{/draftUses.macro}}macro, //See the macros documentation
{{^draftUses.setHidden}}//{{/draftUses.setHidden}}setHidden, //See the setHidden documentation
{{^draftUses.store}}//{{/draftUses.store}}store, //See the store documentation
{{^draftUses.unhide}}//{{/draftUses.unhide}}unhide, //See the unhide documentation
{{^draftUses.units}}//{{/draftUses.units}}units, //See the units documentation
/{{^draftUses.utils}}//{{/draftUses.utils}}utils, //See the utils documentation
/*
* Return value
*/
part, // Your draft method must return this
}) {
{{{draft}}}
{{^draft}}
// Work your magic here
{{/draft}}
return part
}
export const {{part}} = {
/*
* name: Holds the name of this part.
*
* We STRONGLY recommend naming your parts in the format of
* design.part to avoid naming conflicts when people re-use
* parts across designs.
*/
name: '{{ name }}.{{part}}',
/*
* draft: Holds the draft method for this part
*
* This should be a function that drafts and returns the part
*
* Documentation: https://freesewing.dev/reference/api/part/draft
*/
draft: draft{{partUpcase}},
after: [
/*
* after: Holds a list of parts that should be drafted prior to this part.
*
* You'll need to import these parts, just as with the from key above.
*
* If you don't have any parts to draft prior to this part,
* you can remove this options key entirely.
*
* Documentation: https://freesewing.dev/reference/api/part/config/dependencies
*/
],
/*
* from: Holds the part you want to extend.
*
* Since you opted to extend {{baseNameUpcase}}, and this is the {{part}} part,
* we're extending {{baseNameUpcase}}'s {{part}} part here.
* It was imported at the top of this file from @freesewing/{{baseNameUpcase}}
*
* Documentation: https://freesewing.dev/reference/api/part/config/dependencies
*/
{{#doesInherit}}
from: {{baseName}}{{partUpcase}},
{{/doesInherit}}
{{^doesInherit}}
from: false,
{{/doesInherit}}
/*
* hide: Set this to true to hide a part.
*
* We've set this to false here to clarify its use.
* I you don't want to hide this part,
* you can remove the hide key entirely.
*/
hide: false,
/*
* hideDependecies: Set this to true to hide a part's dependencies.
*
* We've set this to true here since you're extending {{baseNameUpcase}}'s {{part}} part.
* I you don't want to hide this part's dependencies,
* you can remove the hideDependencies key entirely.
*/
hideDependencies: {{doesInherit}},
/*
* hideAll: Set this to true to hide both the part and its dependencies.
*
* This is a combination of the hide and hideDependencies keys in case
* you want to both hide this part and its dependencies.
* We've included it here with a value of false to its use.
* I you don't want to hide this a part and its dependencies,
* you can remove the hideAll key entirely.
*/
hideAll: false,
options: {
/*
* options: Holds (the configuration of) options for this part
*
* Declare options used in this part here.
{{#doesInherit}}
* You only need to add additional options.
* All options coming from {{baseNameUpcase}}'s {{part}} part are already loaded.
{{/doesInherit}}
*
* If you don't have any options to add,
* you can remove this options key entirely.
*
* Documentation: https://freesewing.dev/reference/api/part/config/options
*/
{{{options}}}
},
measurements: [
/*
* measurements: Holds a list of measurements required by this part.
*
* Declare measurements required by this part here.
{{#doesInherit}}
* You only need to add additional measurements.
* All measurements coming from {{baseNameUpcase}}'s {{part}} part are already loaded.
{{/doesInherit}}
*
* If you don't have any required measurements to add,
* you can remove this measurements key entirely.
*
* Documentation: https://freesewing.dev/reference/api/part/config/measurements
*/
],
optionalMeasurements: [
/*
* optionalMeasurements: Holds a list of measurements optional in this part.
*
* Declare measurements that are optional for this part here.
*
* If you don't have any optional measurements to add,
* you can remove this optionalMeasurements key entirely.
*
* Documentation: https://freesewing.dev/reference/api/part/config/measurements
*/
],
plugins: [
/*
* plugins: Holds a list of plugins this part relies on.
*
* Add all the plugins here that you need in this part.
{{#doesInherit}}
* You only need to add additional plugins.
* All plugins coming from {{baseNameUpcase}}'s {{part}} part are already loaded.
{{/doesInherit}}
*
* If you don't have any plugins to add,
* you can remove this plugins key entirely.
*
* Documentation: https://freesewing.dev/reference/api/part/config/plugins
*/
{{#pluginBundle}}
pluginBundle,
{{/pluginBundle}}
]
}

View file

@ -1,28 +1,30 @@
import { {{part}} as {{baseName}}{{partUpcase}} } from '@freesewing/{{baseName}}'
{{#pluginBundle}}
import { pluginBundle } from '@freesewing/plugin-bundle'
{{/pluginBundle}}
function draft{{partUpcase}} ({
function draft{{partUpcase}}({
// Uncomment below to destructure what you need
/*
* Content constructors
*/
//Path, // A Path constructor to create new paths
//Point, // A Point constructor to create new points
//Snippet, // A Snippet constructor to create new snippets
Path, // A Path constructor to create new paths
Point, // A Point constructor to create new points
Snippet, // A Snippet constructor to create new snippets
/*
* Content constainers
*/
//paths, // Add a Path to your part by adding it to this object
//points, // Add a Points to your part by adding it to this object
//snippets, // Add a Snippet to your part by adding it to this object
paths, // Add a Path to your part by adding it to this object
points, // Add a Points to your part by adding it to this object
snippets, // Add a Snippet to your part by adding it to this object
/*
* Access to settings
*/
//absoluteOptions, // Access to settings.absoluteOptions
//complete, // Access to settings.complete
complete, // Access to settings.complete
//measurements, // Access to settings.measurements
//options, // Access to settings.options
//paperless, // Access to settings.paperless
//sa, // Access to settings.sa
options, // Access to settings.options
paperless, // Access to settings.paperless
sa, // Access to settings.sa
//scale, // Access to settings.scale
/*
* Access to utilities
@ -30,7 +32,7 @@ function draft{{partUpcase}} ({
//getId, //See the getId documentation
//hide, //See the hide documentation
//log, //See the logging documentation
//macro, //See the macros documentation
macro, //See the macros documentation
//setHidden, //See the setHidden documentation
//store, //See the store documentation
//unhide, //See the unhide documentation
@ -42,7 +44,53 @@ function draft{{partUpcase}} ({
part, // Your draft method must return this
}) {
// Work your magic here
// Add points to make a box
const w = 500 * options.size
points.topLeft = new Point(0, 0)
points.topRight = new Point(w, 0)
points.bottomLeft = new Point(0, w / 2)
points.bottomRight = new Point(w, w / 2)
// Create a path for the box outline
paths.seam = new Path()
.move(points.topLeft)
.line(points.bottomLeft)
.line(points.bottomRight)
.line(points.topRight)
.line(points.topLeft)
.close()
.setClass('fabric')
// Complete?
if (complete) {
// Add a logo
points.logo = points.topLeft.shiftFractionTowards(points.bottomRight, 0.5)
snippets.logo = new Snippet('logo', points.logo)
// Add some text
points.text = points.logo
.shift(-90, w / 8)
.addText('FreeSewing', 'center')
if (sa) {
// Add seam allowance
paths.sa = paths.seam.offset(sa).setClass('fabric sa')
}
}
// Add dimensions for paperless mode
if (paperless) {
macro('hd', {
from: points.bottomLeft,
to: points.bottomRight,
y: points.bottomLeft.y + sa + 15,
})
macro('vd', {
from: points.bottomRight,
to: points.topRight,
x: points.topRight.x + sa + 15,
})
}
return part
}
@ -79,13 +127,14 @@ export const {{part}} = {
/*
* from: Holds the part you want to extend.
*
* Since you opted to extend {{baseNameUpcase}}, and this is the {{part}} part,
* we're extending {{baseNameUpcase}}'s {{part}} part here.
* It was imported at the top of this file from @freesewing/{{baseNameUpcase}}
* Since we're not extending anything here we set it to false.
*
* In a situation like this where you are not extending a part
* you can remove the from key entirely.
*
* Documentation: https://freesewing.dev/reference/api/part/config/dependencies
*/
from: {{baseName}}{{partUpcase}},
from: false,
/*
* hide: Set this to true to hide a part.
*
@ -97,11 +146,10 @@ export const {{part}} = {
/*
* hideDependecies: Set this to true to hide a part's dependencies.
*
* We've set this to true here since you're extending {{baseNameUpcase}}'s {{part}} part.
* I you don't want to hide this part's dependencies,
* you can remove the hideDependencies key entirely.
*/
hideDependencies: true,
hideDependencies: false,
/*
* hideAll: Set this to true to hide both the part and its dependencies.
*
@ -117,22 +165,21 @@ export const {{part}} = {
* options: Holds (the configuration of) options for this part
*
* Declare options used in this part here.
* You only need to add additional options.
* All options coming from {{baseNameUpcase}}'s {{part}} part are already loaded.
*
* If you don't have any options to add,
* you can remove this options key entirely.
*
* Documentation: https://freesewing.dev/reference/api/part/config/options
*/
{{#options}}
{{.}},
{{/options}}
},
measurements: [
/*
* measurements: Holds a list of measurements required by this part.
*
* Declare measurements required by this part here.
* You only need to add additional measurements.
* All measurements coming from {{baseNameUpcase}}'s {{part}} part are already loaded.
*
* If you don't have any required measurements to add,
* you can remove this measurements key entirely.
@ -157,13 +204,14 @@ export const {{part}} = {
* plugins: Holds a list of plugins this part relies on.
*
* Add all the plugins here that you need in this part.
* You only need to add additional plugins.
* All plugins coming from {{baseNameUpcase}}'s {{part}} part are already loaded.
*
* If you don't have any plugins to add,
* you can remove this plugins key entirely.
*
* Documentation: https://freesewing.dev/reference/api/part/config/plugins
*/
{{#pluginBundle}}
pluginBundle,
{{/pluginBundle}}
]
}