diff --git a/src/commands/sites/fameworks.ts b/src/commands/sites/frameworks.ts similarity index 100% rename from src/commands/sites/fameworks.ts rename to src/commands/sites/frameworks.ts diff --git a/src/commands/sites/init.ts b/src/commands/sites/init.ts index b864621..c9b82df 100644 --- a/src/commands/sites/init.ts +++ b/src/commands/sites/init.ts @@ -1,99 +1,111 @@ import fs from 'fs' import { resolve } from 'path' -import Chalk from "chalk" -import { logger } from "../../lib/logger" -import { getNpmConfigInitVersion, getNpmInstallationStatus, handleNpmInstallation, parseNpmConfigVersion } from '../../lib/npm' +import Chalk from 'chalk' +import { logger } from '../../lib/logger' +import { + getNpmConfigInitVersion, + getNpmInstallationStatus, + handleNpmInstallation, + parseNpmConfigVersion +} from '../../lib/npm' import { slugify } from '../../lib/strings' import promptSitesInit from '../../prompts/sites/init' import { generateBaseConfig, saveBlsConfig } from '../../lib/blsConfig' -import { generateFramework } from './fameworks' +import { generateFramework } from './frameworks' import { JsonMap } from '@iarna/toml' export const run = async (options: any) => { - let { - name, - path = process.cwd(), - private: isPrivate = true - } = options + let { name, path = process.cwd(), private: isPrivate = true } = options - try { - // Check and warn if project has been initialized, if yes, bail. - const baseConfigExists = fs.existsSync(!!name ? resolve(path, name, 'bls.toml') : resolve(path, 'bls.toml')) - if (baseConfigExists) { - console.log(Chalk.red(`A bls.toml configuration file already exists! Please remove it before running this command again.`)) - return - } + try { + // Check and warn if project has been initialized, if yes, bail. + const baseConfigExists = fs.existsSync( + !!name ? resolve(path, name, 'bls.toml') : resolve(path, 'bls.toml') + ) + if (baseConfigExists) { + console.log( + Chalk.red( + `A bls.toml configuration file already exists! Please remove it before running this command again.` + ) + ) + return + } - // Check whether NPM is installed - if (!getNpmInstallationStatus()) { - await handleNpmInstallation() - return - } + // Check whether NPM is installed + if (!getNpmInstallationStatus()) { + await handleNpmInstallation() + return + } - // Load up site name and details - const prompts = await promptSitesInit({ - name: options.name, - siteExists: false - }) - if (!prompts) return + // Load up site name and details + const prompts = await promptSitesInit({ + name: options.name, + siteExists: false + }) + if (!prompts) return - const { name: siteName, framework } = prompts - if (!!siteName) name = siteName + const { name: siteName, framework } = prompts + if (!!siteName) name = siteName - const sanitizedName = slugify(name) - const installationPath = resolve(process.cwd(), path, sanitizedName) - const configPath = resolve(installationPath, 'bls.toml') - const version = getNpmConfigInitVersion() + const sanitizedName = slugify(name) + const installationPath = resolve(process.cwd(), path, sanitizedName) + const configPath = resolve(installationPath, 'bls.toml') + const version = getNpmConfigInitVersion() - // Check and warn if project has been initialized, if yes, bail. - const configExists = fs.existsSync(configPath) - if (configExists) { - console.log(Chalk.red(`A bls.toml configuration file already exists! Please remove it before running this command again.`)) - return - } + // Check and warn if project has been initialized, if yes, bail. + const configExists = fs.existsSync(configPath) + if (configExists) { + console.log( + Chalk.red( + `A bls.toml configuration file already exists! Please remove it before running this command again.` + ) + ) + return + } - // Validate installation environment - let isValidated = !!siteName - - if (!isValidated) { - return - } + // Validate installation environment + let isValidated = !!siteName - console.log(`${Chalk.yellow("Initalizing:")} new site in ${Chalk.blue(installationPath)}`) + if (!isValidated) { + return + } - try { - const config = await generateFramework({ - id: framework, - name: sanitizedName, - path, - installationPath - }) + console.log(`${Chalk.yellow('Initalizing:')} new site in ${Chalk.blue(installationPath)}`) - const baseConfig = generateBaseConfig({ - framework: 'site', - name: sanitizedName, - version: parseNpmConfigVersion(version), - isPrivate - }); + try { + const config = await generateFramework({ + id: framework, + name: sanitizedName, + path, + installationPath + }) - (baseConfig.build as JsonMap).command = config.build; - (baseConfig.build as JsonMap).public_dir = config.publicDir; - (baseConfig.build_release as JsonMap).command = config.build; - (baseConfig.build_release as JsonMap).public_dir = config.publicDir; + const baseConfig = generateBaseConfig({ + framework: 'site', + name: sanitizedName, + version: parseNpmConfigVersion(version), + isPrivate + }) - saveBlsConfig(baseConfig, installationPath) - } catch (error: any) { - logger.error('Failed to initalize framework.', error.message) - return - } + ;(baseConfig.build as JsonMap).command = config.build + ;(baseConfig.build as JsonMap).public_dir = config.publicDir + ;(baseConfig.build_release as JsonMap).command = config.build + ;(baseConfig.build_release as JsonMap).public_dir = config.publicDir - // Success Message - console.log(`${Chalk.green("Success:")} blockless site initalized at ${Chalk.blue(installationPath)}`) - console.log("") - console.log(`Change into the directory ${installationPath} to get started`) + saveBlsConfig(baseConfig, installationPath) + } catch (error: any) { + logger.error('Failed to initalize framework.', error.message) + return + } - } catch (error: any) { - logger.error('Failed to initialize site, please try again.', error.message) - return - } -} \ No newline at end of file + // Success Message + console.log( + `${Chalk.green('Success:')} blockless site initalized at ${Chalk.blue(installationPath)}` + ) + console.log('') + console.log(`Change into the directory ${installationPath} to get started`) + } catch (error: any) { + logger.error('Failed to initialize site, please try again.', error.message) + return + } +} diff --git a/src/prompts/sites/init.ts b/src/prompts/sites/init.ts index 69e82a6..b42d46e 100644 --- a/src/prompts/sites/init.ts +++ b/src/prompts/sites/init.ts @@ -1,64 +1,70 @@ -import fs from "fs" -import prompts from "prompts" -import { randomName } from "../../lib/randomName" -import { listFrameworks } from "../../commands/sites/fameworks" +import fs from 'fs' +import prompts from 'prompts' +import { randomName } from '../../lib/randomName' +import { listFrameworks } from '../../commands/sites/frameworks' interface PromptDeployOptions { - name: string, - siteExists: boolean + name: string + siteExists: boolean } interface PromptDeployOutput { - name: string - framework: string + name: string + framework: string } -const frameworks = listFrameworks().map(f => ({ title: f.name, value: f.id })) +const frameworks = listFrameworks().map((f) => ({ title: f.name, value: f.id })) -const promptSitesInit = async (options: PromptDeployOptions): Promise => { - try { - const nameResponse = !options.name ? await prompts( - [ - { - type: 'text', - name: 'name', - message: `What would you like to name your site?`, - initial: randomName() - } - ], - { - onCancel: () => { - console.log("Cancelled by user, exiting...") - process.exit(1) - } - } - ) : { name: options.name } - - const templateResponse = !options.siteExists ? await prompts( - [ - { - type: 'select', - name: 'framework', - message: 'Pick a framework', - choices: frameworks, - initial: 0 - } - ], - { - onCancel: () => { - console.log("Cancelled by user, exiting...") - process.exit(1) - } - } - ) : { framework: null } +const promptSitesInit = async ( + options: PromptDeployOptions +): Promise => { + try { + const nameResponse = !options.name + ? await prompts( + [ + { + type: 'text', + name: 'name', + message: `What would you like to name your site?`, + initial: randomName() + } + ], + { + onCancel: () => { + console.log('Cancelled by user, exiting...') + process.exit(1) + } + } + ) + : { name: options.name } - return { - ...nameResponse, - ...templateResponse - } - } catch (error) { - return null - } + const templateResponse = !options.siteExists + ? await prompts( + [ + { + type: 'select', + name: 'framework', + message: 'Pick a framework', + choices: frameworks, + initial: 0 + } + ], + { + onCancel: () => { + console.log('Cancelled by user, exiting...') + process.exit(1) + } + } + ) + : { framework: null } + + return { + ...nameResponse, + ...templateResponse + } + } catch (error) { + return null + } } -export default promptSitesInit \ No newline at end of file +export default promptSitesInit