2022-06-20 18:31:22 +02:00
|
|
|
import { config } from './config.mjs'
|
|
|
|
import { mkdir, readFile, writeFile, copyFile } from 'node:fs/promises'
|
2022-07-12 20:09:17 +02:00
|
|
|
import { join, dirname } from 'path'
|
2022-06-20 18:31:22 +02:00
|
|
|
import mustache from 'mustache'
|
|
|
|
import rdir from 'recursive-readdir'
|
|
|
|
import chalk from 'chalk'
|
|
|
|
import prompts from 'prompts'
|
2022-06-23 15:17:18 +02:00
|
|
|
import { oraPromise } from 'ora'
|
2022-06-20 18:31:22 +02:00
|
|
|
import { execa } from 'execa'
|
|
|
|
import axios from 'axios'
|
2022-10-04 23:37:03 +02:00
|
|
|
import { fileURLToPath } from 'url'
|
2022-06-20 18:31:22 +02:00
|
|
|
|
2022-06-23 10:18:52 +02:00
|
|
|
// Current working directory
|
2022-10-04 23:37:03 +02:00
|
|
|
let cwd
|
|
|
|
try {
|
|
|
|
cwd = __dirname
|
|
|
|
} catch {
|
|
|
|
cwd = dirname(fileURLToPath(import.meta.url))
|
|
|
|
}
|
2022-06-23 10:18:52 +02:00
|
|
|
|
2022-10-04 23:37:03 +02:00
|
|
|
const nl = '\n'
|
|
|
|
const tab = ' '
|
|
|
|
const nlt = nl + tab
|
2022-06-20 18:31:22 +02:00
|
|
|
|
2022-10-06 14:15:13 +02:00
|
|
|
// Checks for node 16 or higher
|
2022-06-20 18:31:22 +02:00
|
|
|
export const checkNodeVersion = () => {
|
|
|
|
const node_version = process.version.slice(1).split('.')[0]
|
|
|
|
if (parseInt(node_version) < config.node) {
|
|
|
|
console.log(
|
2022-10-04 23:37:03 +02:00
|
|
|
chalk.yellow(nlt + `⚠️ FreeSewing requires Node v${config.node} or newer`) +
|
|
|
|
nl +
|
|
|
|
nlt +
|
|
|
|
'We hightly recommend using NVM to manage your Node versions:' +
|
|
|
|
nlt +
|
|
|
|
chalk.blue('https://github.com/nvm-sh/nvm') +
|
|
|
|
nl +
|
|
|
|
nlt +
|
|
|
|
'When in doubt, pick an active LTS version:' +
|
|
|
|
nlt +
|
|
|
|
chalk.blue('https://nodejs.org/en/about/releases/') +
|
|
|
|
nl +
|
|
|
|
nl
|
2022-06-20 18:31:22 +02:00
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method to validate the design name
|
|
|
|
const validateDesignName = (name) => {
|
|
|
|
if (/^([a-z]+)$/.test(name)) return true
|
|
|
|
else return ' 🙈 Please use only [a-z], no spaces, no capitals, no nothing 🤷'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets user input to figure out what to do
|
|
|
|
export const getChoices = async () => {
|
|
|
|
const { template } = await prompts({
|
|
|
|
type: 'select',
|
|
|
|
name: 'template',
|
|
|
|
message: 'What template would you like to use? 📑',
|
|
|
|
choices: [
|
2022-10-09 21:32:43 +02:00
|
|
|
{ title: 'Tutorial', value: 'tutorial', description: 'Setup the pattern design tutorial' },
|
2022-06-20 18:31:22 +02:00
|
|
|
{ title: 'From Scratch', value: 'scratch', description: 'Create a design from scratch' },
|
2022-10-04 23:37:03 +02:00
|
|
|
{
|
|
|
|
title: 'Extend Brian',
|
|
|
|
value: 'brian',
|
|
|
|
description: 'Extend the Brian design (basic torso block for menswear)',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Extend Bent',
|
|
|
|
value: 'bent',
|
|
|
|
description: 'Extend the Bent design (like brian with added two-part sleeve)',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Extend Bella',
|
|
|
|
value: 'bella',
|
|
|
|
description: 'Extend the Bella design (womenswear torso block)',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Extend Breanna',
|
|
|
|
value: 'breanna',
|
|
|
|
description: 'Extend the Breanna design (womenswear torso block - YMMV)',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Extend Titan',
|
|
|
|
value: 'titan',
|
|
|
|
description: 'Extend the Titan design (gender-neutral trouser block)',
|
|
|
|
},
|
2022-06-20 18:31:22 +02:00
|
|
|
],
|
|
|
|
initial: 0,
|
|
|
|
})
|
|
|
|
|
2022-10-09 21:32:43 +02:00
|
|
|
const { name } =
|
|
|
|
template === 'tutorial'
|
|
|
|
? { name: 'tutorial' }
|
|
|
|
: await prompts({
|
|
|
|
type: 'text',
|
|
|
|
name: 'name',
|
|
|
|
message: 'What name would you like the design to have? 🏷️ ([a-z] only)',
|
|
|
|
validate: validateDesignName,
|
|
|
|
})
|
2022-06-20 18:31:22 +02:00
|
|
|
|
|
|
|
const { manager } = await prompts({
|
|
|
|
type: 'select',
|
|
|
|
name: 'manager',
|
2022-10-09 21:32:43 +02:00
|
|
|
message: 'What package manager should we use? 📦',
|
2022-06-20 18:31:22 +02:00
|
|
|
choices: [
|
|
|
|
{ title: 'yarn', value: 'yarn', description: 'Yarn - Nice if you have it' },
|
2022-10-04 23:37:03 +02:00
|
|
|
{ title: 'npm', value: 'npm', description: 'NPM - Comes with NodeJS' },
|
2022-06-20 18:31:22 +02:00
|
|
|
],
|
|
|
|
initial: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
return { template, name, manager }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of directories that need to be created
|
|
|
|
const dirs = {}
|
2022-10-04 23:37:03 +02:00
|
|
|
const ensureDir = async (file, suppress = false) => {
|
|
|
|
const dir = suppress ? dirname(file.replace(suppress)) : dirname(file)
|
2022-06-20 18:31:22 +02:00
|
|
|
if (!dirs[dir]) {
|
|
|
|
await mkdir(dir, { recursive: true })
|
|
|
|
dirs[dir] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:10:19 -06:00
|
|
|
const writeTemplateFile = async (to, template, args) => {
|
|
|
|
if (!dirs[to]) await ensureDir(to)
|
|
|
|
|
|
|
|
return writeFile(to, mustache.render(template, args))
|
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
// Helper method to copy template files
|
|
|
|
const copyTemplate = async (config, choices) => {
|
|
|
|
// Copy files in parallel rather than using await
|
|
|
|
const promises = []
|
|
|
|
|
2022-11-29 16:10:19 -06:00
|
|
|
// const templateConfig
|
2022-06-20 18:31:22 +02:00
|
|
|
// Copy shared files
|
|
|
|
for (const from of config.files.shared) {
|
2022-06-23 10:18:52 +02:00
|
|
|
// FIXME: Explain the -7
|
|
|
|
const to = join(config.dest, from.slice(config.source.shared.length - 7))
|
2022-06-20 18:31:22 +02:00
|
|
|
if (!dirs[to]) await ensureDir(to)
|
|
|
|
promises.push(copyFile(from, to))
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:10:19 -06:00
|
|
|
// Template the package.json
|
|
|
|
const packageJsonTemplate = await readFile(config.files.templates['package.json'], 'utf-8')
|
|
|
|
const packageJsonTo = join(config.dest, 'package.json')
|
|
|
|
promises.push(
|
|
|
|
writeTemplateFile(packageJsonTo, packageJsonTemplate, {
|
|
|
|
name: choices.name,
|
|
|
|
tag: config.tag,
|
|
|
|
dependencies: config.templateData.dependencies,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
const designSrcDir = join(config.dest, 'design/src')
|
|
|
|
|
|
|
|
// Template the index file
|
2022-11-29 17:57:03 -06:00
|
|
|
const complexParts = typeof config.templateData.parts[0] === 'object'
|
2022-11-29 16:10:19 -06:00
|
|
|
const indexTemplate = await readFile(config.files.templates['index'], 'utf-8')
|
|
|
|
const indexTo = join(designSrcDir, 'index.mjs')
|
2022-11-29 17:57:03 -06:00
|
|
|
const partNames = complexParts
|
|
|
|
? config.templateData.parts.map((p) => p.part)
|
|
|
|
: config.templateData.parts
|
2022-11-29 16:10:19 -06:00
|
|
|
promises.push(
|
|
|
|
writeTemplateFile(indexTo, indexTemplate, {
|
|
|
|
name: choices.name,
|
2022-11-29 17:57:03 -06:00
|
|
|
parts: partNames,
|
2022-11-29 16:10:19 -06:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// Template the parts
|
2022-11-29 17:57:03 -06:00
|
|
|
const doesInherit = !config.templateData.noInheritance
|
|
|
|
const partTemplate = await readFile(config.files.templates.part, 'utf-8')
|
|
|
|
const baseConfig = {
|
|
|
|
name: choices.name,
|
|
|
|
doesInherit,
|
|
|
|
}
|
2022-11-29 16:10:19 -06:00
|
|
|
|
2022-11-29 17:57:03 -06:00
|
|
|
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: {},
|
2022-11-29 16:10:19 -06:00
|
|
|
part: p,
|
2022-11-29 17:57:03 -06:00
|
|
|
}
|
|
|
|
: {
|
2022-11-29 16:10:19 -06:00
|
|
|
...baseConfig,
|
2022-11-29 17:57:03 -06:00
|
|
|
draftUses: {},
|
|
|
|
...p,
|
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
|
2022-11-29 17:57:03 -06:00
|
|
|
templateArgs.partUpcase = templateArgs.part[0].toUpperCase() + templateArgs.part.slice(1)
|
|
|
|
|
|
|
|
const to = join(designSrcDir, `${templateArgs.part}.mjs`)
|
|
|
|
promises.push(writeTemplateFile(to, partTemplate, templateArgs))
|
|
|
|
})
|
2022-11-29 16:10:19 -06:00
|
|
|
|
2022-06-20 18:31:22 +02:00
|
|
|
await Promise.all(promises)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method to run [yarn|npm] install
|
2022-10-04 23:37:03 +02:00
|
|
|
const installDependencies = async (config, choices) =>
|
|
|
|
await execa(`${choices.manager} install`, {
|
2022-06-20 18:31:22 +02:00
|
|
|
cwd: config.dest,
|
2022-10-04 23:37:03 +02:00
|
|
|
shell: true,
|
|
|
|
})
|
2022-06-20 18:31:22 +02:00
|
|
|
|
|
|
|
// Helper method to download web environment
|
|
|
|
const downloadLabFiles = async (config) => {
|
|
|
|
const promises = []
|
|
|
|
for (const dir in config.fetch) {
|
|
|
|
for (const file of config.fetch[dir]) {
|
2022-10-04 23:37:03 +02:00
|
|
|
const to = typeof file === 'string' ? join(config.dest, file) : join(config.dest, file.to)
|
2022-06-20 18:31:22 +02:00
|
|
|
if (!dirs[to]) await ensureDir(to)
|
|
|
|
promises.push(
|
2022-10-04 23:37:03 +02:00
|
|
|
axios
|
|
|
|
.get(
|
|
|
|
`${config.fileUri}/${config.repo}/${config.branch}/${dir}/${
|
|
|
|
typeof file === 'string' ? file : file.from
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
.catch((err) => console.log(err))
|
|
|
|
.then((res) => promises.push(writeFile(to, res.data)))
|
2022-06-20 18:31:22 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:17:18 +02:00
|
|
|
// Helper method to initialize a git repository
|
|
|
|
const initGitRepo = async (config, choices) => {
|
|
|
|
await writeFile(join(config.dest, '.gitignore'), config.gitignore, 'utf-8')
|
|
|
|
|
|
|
|
return execa(
|
|
|
|
`git init -b main && git add . && git commit -m ":tada: Initialized ${choices.name} repository"`,
|
|
|
|
{
|
|
|
|
cwd: config.dest,
|
2022-10-04 23:37:03 +02:00
|
|
|
shell: true,
|
2022-06-23 15:17:18 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:47:37 +02:00
|
|
|
// Tips
|
2022-10-09 21:32:43 +02:00
|
|
|
const showTips = (config, choices) => {
|
2022-10-04 23:37:03 +02:00
|
|
|
console.log(`
|
|
|
|
All done 🤓 Your new design ${chalk.yellow.bold(
|
|
|
|
choices.name
|
|
|
|
)} was initialized in: ${chalk.green.bold(config.dest)}
|
2022-06-23 15:47:37 +02:00
|
|
|
|
2022-06-23 16:16:39 +02:00
|
|
|
The code for your design is in the ${chalk.yellow.bold('design')} folder.
|
|
|
|
The other files and folders are the development environment. You can safely ignore those.
|
|
|
|
|
2022-06-23 15:47:37 +02:00
|
|
|
To start your development environment, follow these three steps:
|
|
|
|
|
2022-06-23 16:16:39 +02:00
|
|
|
1) Start by entering the directory: ${chalk.blue.bold('cd ' + config.dest)}
|
2022-10-04 23:37:03 +02:00
|
|
|
2) Then run this command: ${chalk.blue.bold(
|
|
|
|
choices.manager === 'yarn' ? 'yarn dev' : 'npm run dev'
|
|
|
|
)}
|
2022-06-23 15:47:37 +02:00
|
|
|
3) Now open your browser and navigate to ${chalk.green('http://localhost:8000/')}
|
|
|
|
|
|
|
|
${chalk.bold.yellow('🤔 More info & help')}
|
2022-10-09 21:32:43 +02:00
|
|
|
${chalk.gray('≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡')}`)
|
|
|
|
|
|
|
|
if (choices.template === 'tutorial')
|
|
|
|
console.log(`
|
|
|
|
Our pattern design tutorial is available at: ${chalk.green(
|
|
|
|
'https://freesewing.dev/tutorials/pattern-design'
|
|
|
|
)}
|
|
|
|
|
|
|
|
It will walk your through the process step by step.
|
|
|
|
If you get stuck, reach out to our community on Discord: ${chalk.green(
|
|
|
|
'https://discord.freesewing.dev/'
|
|
|
|
)}
|
|
|
|
The ${chalk.bold('development-help')} channel is a good place to ask questions
|
|
|
|
|
|
|
|
Don't be shy to reach out. If something is not clear, that's on us, not on you.
|
|
|
|
So your feedback really helps us improve our tutorial/documentation.
|
|
|
|
|
|
|
|
Thanks for giving FreeSewing a shot. We hope you'll 💜 it.
|
|
|
|
|
|
|
|
Have fun 🤓
|
|
|
|
`)
|
|
|
|
else
|
|
|
|
console.log(`
|
2022-06-23 15:47:37 +02:00
|
|
|
|
2022-10-04 23:37:03 +02:00
|
|
|
FreeSewing's documentation for developers is available at: ${chalk.green(
|
|
|
|
'https://freesewing.dev/'
|
|
|
|
)}
|
2022-06-23 15:47:37 +02:00
|
|
|
|
|
|
|
Our community is on Discord: ${chalk.green('https://discord.freesewing.dev/')}
|
|
|
|
The ${chalk.bold('development-help')} channel is a good place to ask for help if you get stuck
|
|
|
|
|
|
|
|
Happy hacking 🤓
|
2022-10-09 21:32:43 +02:00
|
|
|
`)
|
|
|
|
}
|
2022-06-23 15:47:37 +02:00
|
|
|
|
2022-06-20 18:31:22 +02:00
|
|
|
// Creates the environment based on the user's choices
|
|
|
|
export const createEnvironment = async (choices) => {
|
|
|
|
// Store directories for re-use
|
2022-10-04 23:37:03 +02:00
|
|
|
;(config.cwd = cwd),
|
|
|
|
(config.source = {
|
|
|
|
root: cwd,
|
2022-11-29 16:10:19 -06:00
|
|
|
templateData: cwd + `/../templates/from-${choices.template}.mjs`,
|
|
|
|
templates: join(cwd, `/../templates/shared`),
|
2022-10-04 23:37:03 +02:00
|
|
|
shared: cwd + `/../shared`,
|
|
|
|
})
|
2022-06-23 10:18:52 +02:00
|
|
|
config.dest = join(process.cwd(), choices.name)
|
2022-06-20 18:31:22 +02:00
|
|
|
|
|
|
|
// Create target directory
|
|
|
|
await mkdir(config.dest, { recursive: true })
|
|
|
|
|
2022-11-29 16:10:19 -06:00
|
|
|
const templateFiles = await rdir(config.source.templates)
|
|
|
|
const templates = {}
|
|
|
|
templateFiles.forEach(
|
|
|
|
(f) =>
|
|
|
|
(templates[f.replace(`${config.source.templates}/`, '').replace(/(\.mjs)*\.mustache/, '')] =
|
|
|
|
f)
|
|
|
|
)
|
|
|
|
|
2022-06-20 18:31:22 +02:00
|
|
|
// Find files
|
|
|
|
config.files = {
|
2022-11-29 16:10:19 -06:00
|
|
|
templates,
|
2022-06-20 18:31:22 +02:00
|
|
|
shared: await rdir(config.source.shared),
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:10:19 -06:00
|
|
|
config.templateData = await import(config.source.templateData)
|
|
|
|
|
2022-06-23 15:17:18 +02:00
|
|
|
// Output a linebreak
|
|
|
|
console.log()
|
|
|
|
|
2022-06-20 18:31:22 +02:00
|
|
|
// Copy/Template files
|
2022-06-23 16:02:08 +02:00
|
|
|
try {
|
2022-10-04 23:37:03 +02:00
|
|
|
await oraPromise(copyTemplate(config, choices), {
|
|
|
|
text:
|
|
|
|
chalk.white.bold('🟨⬜⬜⬜ Copying template files') +
|
|
|
|
chalk.white.dim(' | Just a moment'),
|
|
|
|
successText: chalk.white.bold('🟩⬜⬜⬜ Copied template files'),
|
|
|
|
failText: chalk.white.bold(
|
|
|
|
'🟥⬜⬜⬜ Failed to copy template files | Development environment will not function'
|
|
|
|
),
|
|
|
|
})
|
|
|
|
} catch (err) {
|
2022-11-29 16:10:19 -06:00
|
|
|
console.log(err)
|
2022-10-04 23:37:03 +02:00
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
|
|
|
|
// Install dependencies
|
2022-06-23 16:02:08 +02:00
|
|
|
try {
|
2022-10-04 23:37:03 +02:00
|
|
|
await oraPromise(installDependencies(config, choices), {
|
|
|
|
text:
|
|
|
|
chalk.white.bold('🟩🟨⬜⬜ Installing dependencies') +
|
|
|
|
chalk.white.dim(' | Please wait, this will take a while'),
|
|
|
|
successText: chalk.white.bold('🟩🟩⬜⬜ Installed dependencies'),
|
|
|
|
failText: chalk.white.bold(
|
|
|
|
'🟩🟥⬜⬜ Failed to install dependencies | Development environment will not function'
|
|
|
|
),
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
/* no feedback here */
|
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
|
|
|
|
// Fetch web components
|
2022-06-23 16:02:08 +02:00
|
|
|
try {
|
2022-10-04 23:37:03 +02:00
|
|
|
await oraPromise(downloadLabFiles(config), {
|
|
|
|
text:
|
|
|
|
chalk.white.bold('🟩🟩🟨⬜ Downloading web components') +
|
|
|
|
chalk.white.dim(' | Almost there'),
|
|
|
|
successText: chalk.white.bold('🟩🟩🟩⬜ Downloaded web components'),
|
|
|
|
failText: chalk.white.bold(
|
|
|
|
'🟩🟩🟥⬜ Failed to download web components | Development environment will not function'
|
|
|
|
),
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
/* no feedback here */
|
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
|
2022-06-23 15:17:18 +02:00
|
|
|
// Initialize git repository
|
2022-06-23 16:02:08 +02:00
|
|
|
try {
|
2022-10-04 23:37:03 +02:00
|
|
|
await oraPromise(initGitRepo(config, choices), {
|
|
|
|
text:
|
|
|
|
chalk.white.bold('🟩🟩🟩⬜ Initializing git repository') +
|
|
|
|
chalk.white.dim(' | You have git, right?'),
|
|
|
|
successText: chalk.white.bold('🟩🟩🟩🟩 Initialized git repository'),
|
|
|
|
failText:
|
|
|
|
chalk.white.bold('🟩🟩🟩🟥 Failed to initialize git repository') +
|
|
|
|
chalk.white.dim(' | This does not stop you from developing your design'),
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
/* no git no worries */
|
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
|
2022-06-23 15:47:37 +02:00
|
|
|
// All done. Show tips
|
|
|
|
showTips(config, choices)
|
2022-06-20 18:31:22 +02:00
|
|
|
}
|