From c47bef61bc9baedd9069ec5cc4f4f18a780ecd6c Mon Sep 17 00:00:00 2001 From: Enoch Riese Date: Sun, 5 Mar 2023 21:24:58 -0600 Subject: [PATCH 1/4] ask before overwriting files when running new-design --- packages/new-design/lib/config.mjs | 1 - packages/new-design/lib/utils.mjs | 116 +++++++++++++++++++++++------ 2 files changed, 94 insertions(+), 23 deletions(-) diff --git a/packages/new-design/lib/config.mjs b/packages/new-design/lib/config.mjs index 45e4a4aa707..bf8d931a478 100644 --- a/packages/new-design/lib/config.mjs +++ b/packages/new-design/lib/config.mjs @@ -171,7 +171,6 @@ yarn-error.log* 'shared/components/workbench/inputs/design-option-pct-deg.mjs', 'shared/components/workbench/inputs/measurement.mjs', 'shared/components/workbench/measurements/index.mjs', - 'shared/components/workbench/measurements/non-human.mjs', 'shared/components/workbench/draft/circle.mjs', 'shared/components/workbench/draft/defs.mjs', 'shared/components/workbench/draft/error.mjs', diff --git a/packages/new-design/lib/utils.mjs b/packages/new-design/lib/utils.mjs index f3fab68c334..1a84ae9d7fe 100644 --- a/packages/new-design/lib/utils.mjs +++ b/packages/new-design/lib/utils.mjs @@ -1,5 +1,5 @@ import { config } from './config.mjs' -import { mkdir, readFile, writeFile, copyFile } from 'node:fs/promises' +import { mkdir, readFile, writeFile, copyFile, open, opendir } from 'node:fs/promises' import { join, dirname, relative } from 'path' import mustache from 'mustache' import rdir from 'recursive-readdir' @@ -91,15 +91,62 @@ export const getChoices = async () => { initial: 0, }) - 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, - }) + let finalName = false // we're going to use this to track whether we stay in the naming loop + let overwrite = true // should we overwrite existing files? + const cwd = process.cwd() + let name // name will go here + + // while we're not finalized on a name + while (finalName === false) { + // request a name + name = + template === 'tutorial' && name === undefined + ? 'tutorial' + : ( + await prompts({ + type: 'text', + name: 'name', + message: 'What name would you like the design to have? 🏷️ ([a-z] only)', + validate: validateDesignName, + }) + ).name + + // check whether a folder with that name already exists + config.dest = join(cwd, name) + try { + const dir = await opendir(config.dest) + dir.close() + } catch { + // the folder didn't exist, so we're good to go + finalName = true + break + } + + // the folder did exist, so now we need to ask what to do + const { nextStep } = await prompts({ + type: 'select', + name: 'nextStep', + message: + 'It looks like you already have a design by that name in progress. What should we do?', + choices: [ + { title: 'Rename', value: 'rename', description: 'Choose a new name for this design' }, + { title: 'Overwrite', value: 'overwrite', description: 'Overwrite the existing design' }, + { + title: 'Re-initialize', + value: 'reinit', + description: + "Bring in a fresh workbench, but don't overwrite existing design files (useful for updating to the latest dev environment)", + }, + ], + }) + + // if they said rename, we loop again. otherwise + if (nextStep !== 'rename') { + finalName = true + // set the overwrite choice + overwrite = nextStep === 'overwrite' + } + } const { manager } = await prompts({ type: 'select', @@ -112,7 +159,7 @@ export const getChoices = async () => { initial: 0, }) - return { template, name, manager } + return { template, name, manager, overwrite } } const capitalize = (string) => string.charAt(0).toUpperCase() + string.slice(1) @@ -128,9 +175,28 @@ const ensureDir = async (file, suppress = false) => { } // Helper method to copy template files -const copyFileOrTemplate = async (fromRootOrTemplate, toRoot, relativeDest, templateVars) => { +const copyFileOrTemplate = async ( + fromRootOrTemplate, + toRoot, + relativeDest, + templateVars, + overwrite = true +) => { const to = join(toRoot, relativeDest) + // if the file shouldn't be overwritten, open it to see if it exists + if (!overwrite) { + try { + // if the file doesn't exist, this will throw an error + const fd = await open(to) + fd.close() + // we only reach this return if the file exists, which means we're safe to leave + return + } catch { + // don't do anything with the error because it just means the file wasn't there and we can continue + } + } + await ensureDir(to) if (templateVars) { @@ -148,6 +214,7 @@ const copyPackageJson = async (config, choices) => { config.relativeFiles.templates['package.json'], 'utf-8' ) + await copyFileOrTemplate(packageJsonTemplate, config.dest, 'package.json', { name: choices.name, tag: config.tag, @@ -165,11 +232,17 @@ const copyIndexFile = async (config, choices) => { ? config.templateData.parts.map((p) => p.part) : config.templateData.parts // write the file - await copyFileOrTemplate(indexTemplate, config.dest, `${designSrcDir}/index.mjs`, { - name: choices.name, - Name: capitalize(choices.name), - parts: partNames, - }) + await copyFileOrTemplate( + indexTemplate, + config.dest, + `${designSrcDir}/index.mjs`, + { + name: choices.name, + Name: capitalize(choices.name), + parts: partNames, + }, + choices.overwrite + ) } // Template the part files @@ -213,7 +286,8 @@ const copyPartFiles = async (config, choices) => { partTemplate, config.dest, `${designSrcDir}/${templateArgs.part}.mjs`, - templateArgs + templateArgs, + choices.overwrite ) }) } @@ -271,7 +345,7 @@ const downloadLabFiles = async (config) => { // Helper method to initialize a git repository const initGitRepo = async (config, choices) => { - await writeFile(join(config.dest, '.gitignore'), config.gitignore, 'utf-8') + await copyFileOrTemplate(config.gitignore, config.dest, '.gitignore', {}, choices.overwrite) return execa( `git init -b main && git add . && git commit -m ":tada: Initialized ${choices.name} repository"`, @@ -345,8 +419,6 @@ export const createEnvironment = async (choices) => { shared: join(newDesignDir, `shared`), } - config.dest = join(process.cwd(), choices.name) - // Create target directory await mkdir(config.dest, { recursive: true }) @@ -427,7 +499,7 @@ export const createEnvironment = async (choices) => { chalk.white.dim(' | This does not stop you from developing your design'), }) } catch (err) { - /* no git no worries */ + console.log(err) } // All done. Show tips From 8c04bec9bd8240086b28400fa643359f8b6c2039 Mon Sep 17 00:00:00 2001 From: Enoch Riese Date: Sun, 12 Mar 2023 17:19:36 -0500 Subject: [PATCH 2/4] chore (new-design) update file list --- packages/new-design/lib/config.mjs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/new-design/lib/config.mjs b/packages/new-design/lib/config.mjs index bf8d931a478..fa3b8d0fd16 100644 --- a/packages/new-design/lib/config.mjs +++ b/packages/new-design/lib/config.mjs @@ -122,7 +122,6 @@ yarn-error.log* 'shared/components/error/view.mjs', 'shared/components/icons/flip.js', 'shared/components/icons/rotate.js', - 'shared/components/icons/sheet.js', 'shared/components/locale-picker/index.mjs', 'shared/components/locale-picker/locales.de.yaml', 'shared/components/locale-picker/locales.en.yaml', @@ -133,7 +132,6 @@ yarn-error.log* 'shared/components/logos/cc.mjs', 'shared/components/logos/freesewing.mjs', 'shared/components/logos/osi.mjs', - 'shared/components/mdx/examples.mjs', 'shared/components/mdx/figure.mjs', 'shared/components/mdx/highlight.mjs', 'shared/components/mdx/http.mjs', @@ -185,11 +183,12 @@ yarn-error.log* 'shared/components/workbench/draft/utils.mjs', 'shared/components/workbench/layout/default.mjs', 'shared/components/workbench/layout/cut/index.mjs', + 'shared/components/workbench/layout/cut/plugin-cut-layout.mjs', 'shared/components/workbench/layout/cut/settings.mjs', 'shared/components/workbench/layout/print/index.mjs', 'shared/components/workbench/layout/print/orientation-picker.mjs', 'shared/components/workbench/layout/print/pagesize-picker.mjs', - 'shared/components/workbench/layout/print/plugin.mjs', + 'shared/components/workbench/layout/plugin-layout-part.mjs', 'shared/components/workbench/layout/print/settings.mjs', 'shared/components/workbench/layout/draft/buttons.mjs', 'shared/components/workbench/layout/draft/index.mjs', From c90635234a15d4866b82e880695157a07b621821 Mon Sep 17 00:00:00 2001 From: Enoch Riese Date: Sun, 12 Mar 2023 17:20:28 -0500 Subject: [PATCH 3/4] fix (new-design) hopefully a better way of doing paths that works on windows --- packages/new-design/lib/utils.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/new-design/lib/utils.mjs b/packages/new-design/lib/utils.mjs index 1a84ae9d7fe..4aa6094b127 100644 --- a/packages/new-design/lib/utils.mjs +++ b/packages/new-design/lib/utils.mjs @@ -15,7 +15,7 @@ let filename try { filename = __filename } catch { - filename = fileURLToPath(import.meta.url) + filename = fileURLToPath(new URL(import.meta.url)) } const newDesignDir = join(filename, '../..') const designSrcDir = 'design/src' From b55f94f8b34af6d3137ba97606475a55041e8ba2 Mon Sep 17 00:00:00 2001 From: Enoch Riese Date: Tue, 14 Mar 2023 18:37:53 -0500 Subject: [PATCH 4/4] chore (new-design) add files and plugins to make new-design work with latest lab --- config/scripts.yaml | 2 +- packages/new-design/lib/config.mjs | 2 ++ packages/new-design/package.json | 2 +- packages/new-design/templates/shared/package.json.mustache | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/config/scripts.yaml b/config/scripts.yaml index e125d4b2bac..1a1f688cef3 100644 --- a/config/scripts.yaml +++ b/config/scripts.yaml @@ -29,7 +29,7 @@ i18n: models: test: 'npx mocha tests/*.test.mjs' new-design: - 18n-only: 'SITE="new-design/shared" node ../../sites/shared/prebuild/i18n-only.mjs' + i18n-only: 'SITE="new-design/shared" node ../../sites/shared/prebuild/i18n-only.mjs' wbuild: '!' lint: "npx eslint 'lib/*.mjs'" mbuild: '!' diff --git a/packages/new-design/lib/config.mjs b/packages/new-design/lib/config.mjs index fa3b8d0fd16..40c091aa60b 100644 --- a/packages/new-design/lib/config.mjs +++ b/packages/new-design/lib/config.mjs @@ -160,6 +160,8 @@ yarn-error.log* 'shared/components/workbench/logs.mjs', 'shared/components/workbench/preloaders.mjs', 'shared/components/workbench/sample.mjs', + 'shared/components/workbench/edit/index.mjs', + 'shared/components/workbench/edit/gist-validator.mjs', 'shared/components/workbench/exporting/export-handler.mjs', 'shared/components/workbench/exporting/export-worker.js', 'shared/components/workbench/exporting/index.mjs', diff --git a/packages/new-design/package.json b/packages/new-design/package.json index 11ef9365544..9c1fd2d57e8 100644 --- a/packages/new-design/package.json +++ b/packages/new-design/package.json @@ -25,7 +25,7 @@ "lab": "cd ../../sites/lab && yarn start", "tips": "node ../../scripts/help.mjs", "lint": "npx eslint 'lib/*.mjs'", - "18n-only": "SITE=\"new-design/shared\" node ../../sites/shared/prebuild/i18n-only.mjs", + "i18n-only": "SITE=\"new-design/shared\" node ../../sites/shared/prebuild/i18n-only.mjs", "cibuild_step6": "node build.mjs", "wbuild": "node build.mjs", "wcibuild_step6": "node build.mjs" diff --git a/packages/new-design/templates/shared/package.json.mustache b/packages/new-design/templates/shared/package.json.mustache index 64c790c3aad..0b595c2d636 100644 --- a/packages/new-design/templates/shared/package.json.mustache +++ b/packages/new-design/templates/shared/package.json.mustache @@ -45,6 +45,8 @@ "@freesewing/plugin-bundle": "$$ tag $$" }, "devDependencies": { + "@freesewing/plugin-cutlist": "$$ tag $$", + "@freesewing/plugin-flip": "$$ tag $$", "@freesewing/plugin-svgattr": "$$ tag $$", "@freesewing/plugin-theme": "$$ tag $$", "@freesewing/plugin-i18n": "$$ tag $$",