1
0
Fork 0

Merge pull request #3632 from eriese/eriese-nd-overwriting

Enhancement (new-design) Ask before overwriting files when running new-design
This commit is contained in:
Joost De Cock 2023-03-15 18:18:23 +01:00 committed by GitHub
commit fbbb5581bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 29 deletions

View file

@ -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: '!'

View file

@ -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',
@ -162,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',
@ -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',
@ -186,11 +185,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',

View file

@ -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'
@ -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'
@ -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

View file

@ -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"

View file

@ -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 $$",