From 9034a3b713696d3b8095e68416cc5dca96aa4b62 Mon Sep 17 00:00:00 2001 From: Chris Campbell Date: Wed, 20 Nov 2024 20:45:37 -0800 Subject: [PATCH] fix: skip questions in create script if config and/or emsdk directories exist --- packages/create/package.json | 1 + packages/create/src/index.ts | 32 +++++++++++++++++++++++++------ packages/create/src/step-emsdk.ts | 12 +++++++++++- pnpm-lock.yaml | 3 +++ 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/create/package.json b/packages/create/package.json index 732163e2..292b1900 100644 --- a/packages/create/package.json +++ b/packages/create/package.json @@ -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", diff --git a/packages/create/src/index.ts b/packages/create/src/index.ts index 741308fb..f63f0081 100644 --- a/packages/create/src/index.ts +++ b/packages/create/src/index.ts @@ -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' @@ -25,6 +26,11 @@ export async function main(): Promise { 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`) @@ -33,6 +39,9 @@ export async function main(): Promise { 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() @@ -52,10 +61,21 @@ export async function main(): Promise { } 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 diff --git a/packages/create/src/step-emsdk.ts b/packages/create/src/step-emsdk.ts index 8fecea6d..26e92eb2 100644 --- a/packages/create/src/step-emsdk.ts +++ b/packages/create/src/step-emsdk.ts @@ -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' @@ -14,7 +15,16 @@ import type { Arguments } from 'yargs-parser' const version = '2.0.34' export async function chooseInstallEmsdk(projDir: string, args: Arguments): Promise { - // 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') diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 55bac7f9..9ad9ac74 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -359,6 +359,9 @@ importers: execa: specifier: ^6.1.0 version: 6.1.0 + find-up: + specifier: ^6.3.0 + version: 6.3.0 fs-extra: specifier: ^10.1.0 version: 10.1.0