Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip questions in create script if config and/or emsdk directories exist #562

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@sdeverywhere/compile": "^0.7.20",
"execa": "^6.1.0",
"find-up": "^6.3.0",
"fs-extra": "^10.1.0",
"giget": "^1.2.3",
"kleur": "^4.1.5",
Expand Down
32 changes: 26 additions & 6 deletions packages/create/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) 2022 Climate Interactive / New Venture Fund

import { relative } from 'path'
import { existsSync } from 'fs'
import { relative, resolve as resolvePath } from 'path'

import { bgCyan, black, bold, cyan, green } from 'kleur/colors'
import { bgCyan, black, bold, cyan, dim, green } from 'kleur/colors'
import ora from 'ora'
import prompts from 'prompts'
import detectPackageManager from 'which-pm-runs'
Expand All @@ -25,6 +26,11 @@ export async function main(): Promise<void> {
const args = yargs(process.argv)
prompts.override(args)

if (args.dryRun) {
console.log()
ora().info(dim(`--dry-run enabled, no files will be written.`))
}

// Display welcome message
console.log(`\n${bold('Welcome to SDEverywhere!')}`)
console.log(`Let's create a new SDEverywhere project for your model.\n`)
Expand All @@ -33,6 +39,9 @@ export async function main(): Promise<void> {
const projDir = await chooseProjectDir(args)
console.log()

// See if there is a pre-existing `config` directory
const configDirExisted = existsSync(resolvePath(projDir, 'config'))

// Prompt the user to select a template
const templateName = await chooseTemplate(projDir, args, pkgManager)
console.log()
Expand All @@ -52,10 +61,21 @@ export async function main(): Promise<void> {
}
console.log()

// If the user chose the default template, offer to set up CSV files
if (templateName === 'template-default' && !args.dryRun) {
await chooseGenConfig(projDir, mdlPath)
console.log()
// If the user chose the default template, and there isn't already an
// existing `config` directory, offer to set up CSV files
if (templateName === 'template-default') {
if (configDirExisted) {
ora().succeed(`Found existing "${bold('config')}" directory.`)
ora().info(
dim(`You can edit the files in the "${cyan('config')}" directory later to configure graphs and sliders.`)
)
console.log()
} else {
if (!args.dryRun) {
await chooseGenConfig(projDir, mdlPath)
console.log()
}
}
}

// If the user chose C as the code generation format, prompt the user to
Expand Down
12 changes: 11 additions & 1 deletion packages/create/src/step-emsdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { existsSync, rmSync } from 'fs'
import { join as joinPath, resolve as resolvePath } from 'path'

import { execa } from 'execa'
import { findUp } from 'find-up'
import { bold, cyan, dim, green, red } from 'kleur/colors'
import ora from 'ora'
import prompts from 'prompts'
Expand All @@ -14,7 +15,16 @@ import type { Arguments } from 'yargs-parser'
const version = '2.0.34'

export async function chooseInstallEmsdk(projDir: string, args: Arguments): Promise<void> {
// TODO: Use findUp and skip this step if emsdk directory already exists
// Walk up the directory structure to see if there is an existing `emsdk` directory
const existingEmsdkDir = await findUp('emsdk', {
cwd: projDir,
type: 'directory'
})
if (existingEmsdkDir) {
ora().succeed('Found existing Emscripten SDK installation.')
ora().info(dim(`Your project will use "${cyan(existingEmsdkDir)}".`))
return
}

// Prompt the user
const underParentDir = resolvePath(projDir, '..', 'emsdk')
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.