2022-06-20 18:31:22 +02:00
|
|
|
import { config } from './config.mjs'
|
|
|
|
import { mkdir, readFile, writeFile, copyFile } from 'node:fs/promises'
|
|
|
|
import { join, resolve, dirname, extname } from 'path'
|
|
|
|
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-06-23 10:18:52 +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
|
|
|
|
const cwd = __dirname
|
|
|
|
? __dirname
|
|
|
|
: dirname(fileURLToPath(import.meta.url))
|
|
|
|
|
2022-06-20 18:31:22 +02:00
|
|
|
const nl = "\n"
|
|
|
|
const tab = " "
|
|
|
|
const nlt = nl+tab
|
|
|
|
|
|
|
|
// Checks for node 14 or higher
|
|
|
|
export const checkNodeVersion = () => {
|
|
|
|
const node_version = process.version.slice(1).split('.')[0]
|
|
|
|
if (parseInt(node_version) < config.node) {
|
|
|
|
console.log(
|
|
|
|
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
|
|
|
|
)
|
|
|
|
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: [
|
|
|
|
{ title: 'From Scratch', value: 'scratch', description: 'Create a design from scratch' },
|
|
|
|
{ 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)" },
|
|
|
|
],
|
|
|
|
initial: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
const { name } = await prompts({
|
|
|
|
type: 'text',
|
|
|
|
name: 'name',
|
|
|
|
message: 'What name would you like the design to have? 🏷️ ([a-z] only)',
|
|
|
|
validate: validateDesignName,
|
|
|
|
})
|
|
|
|
|
|
|
|
const { manager } = await prompts({
|
|
|
|
type: 'select',
|
|
|
|
name: 'manager',
|
|
|
|
message: 'Last but not least, what package manager do you use? 📦',
|
|
|
|
choices: [
|
|
|
|
{ title: 'yarn', value: 'yarn', description: 'Yarn - Nice if you have it' },
|
|
|
|
{ title: 'npm', value: 'npm', description: "NPM - Comes with NodeJS" },
|
|
|
|
],
|
|
|
|
initial: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
return { template, name, manager }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of directories that need to be created
|
|
|
|
const dirs = {}
|
|
|
|
const ensureDir = async (file, suppress=false) => {
|
|
|
|
const dir = suppress
|
|
|
|
? dirname(file.replace(suppress))
|
|
|
|
: dirname(file)
|
|
|
|
if (!dirs[dir]) {
|
|
|
|
await mkdir(dir, { recursive: true })
|
|
|
|
dirs[dir] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method to copy template files
|
|
|
|
const copyTemplate = async (config, choices) => {
|
|
|
|
|
|
|
|
// Copy files in parallel rather than using await
|
|
|
|
const promises = []
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Template files
|
|
|
|
for (const from of config.files.template) {
|
2022-06-23 10:18:52 +02:00
|
|
|
const to = join(config.dest, from.slice(config.source.template.length -7))
|
2022-06-20 18:31:22 +02:00
|
|
|
if (!dirs[to]) await ensureDir(to)
|
|
|
|
if (extname(from) === '.json') {
|
|
|
|
// Template out package.json
|
|
|
|
const src = await readFile(from, 'utf-8')
|
|
|
|
promises.push(
|
|
|
|
writeFile(to, mustache.render(src, { name: choices.name }))
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Just copy the file
|
|
|
|
promises.push(copyFile(from, to))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(promises)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method to run [yarn|npm] install
|
|
|
|
const installDependencies = async (config, choices) => await execa(
|
|
|
|
`${choices.manager} install`,
|
|
|
|
{
|
|
|
|
cwd: config.dest,
|
|
|
|
shell: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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]) {
|
|
|
|
const to = (typeof file === 'string')
|
|
|
|
? join(config.dest, file)
|
|
|
|
: join(config.dest, file.to)
|
|
|
|
if (!dirs[to]) await ensureDir(to)
|
|
|
|
promises.push(
|
2022-06-23 10:18:52 +02:00
|
|
|
axios.get(`${config.fileUri}/${config.repo}/${config.branch}/${dir}/${typeof file === 'string' ? file : file.from}`)
|
2022-06-20 18:31:22 +02:00
|
|
|
.catch(err => console.log(err))
|
|
|
|
.then(res => promises.push(writeFile(to, res.data)))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
shell: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-06-23 10:18:52 +02:00
|
|
|
config.cwd = cwd,
|
2022-06-20 18:31:22 +02:00
|
|
|
config.source = {
|
2022-06-23 10:18:52 +02:00
|
|
|
root: cwd,
|
|
|
|
template: cwd + `/../templates/from-${choices.template}`,
|
|
|
|
shared: cwd + `/../shared`
|
2022-06-20 18:31:22 +02:00
|
|
|
}
|
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 })
|
|
|
|
|
|
|
|
// Find files
|
|
|
|
config.files = {
|
|
|
|
template: await rdir(config.source.template),
|
|
|
|
shared: await rdir(config.source.shared),
|
|
|
|
}
|
|
|
|
|
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 10:18:52 +02:00
|
|
|
await oraPromise(
|
|
|
|
copyTemplate(config, choices),
|
|
|
|
{
|
2022-06-23 15:17:18 +02:00
|
|
|
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'),
|
2022-06-23 10:18:52 +02:00
|
|
|
}
|
|
|
|
)
|
2022-06-20 18:31:22 +02:00
|
|
|
|
|
|
|
// Install dependencies
|
|
|
|
await oraPromise(
|
|
|
|
installDependencies(config, choices),
|
2022-06-23 10:18:52 +02:00
|
|
|
{
|
2022-06-23 15:17:18 +02:00
|
|
|
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'),
|
2022-06-23 10:18:52 +02:00
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Fetch web components
|
|
|
|
await oraPromise(
|
|
|
|
downloadLabFiles(config),
|
2022-06-23 10:18:52 +02:00
|
|
|
{
|
2022-06-23 15:17:18 +02:00
|
|
|
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'),
|
2022-06-23 10:18:52 +02:00
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
)
|
|
|
|
|
2022-06-23 15:17:18 +02:00
|
|
|
// Initialize git repository
|
|
|
|
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'),
|
|
|
|
}
|
2022-06-20 18:31:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|