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

correct spelling #114

Merged
merged 1 commit into from
Oct 30, 2023
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
File renamed without changes.
166 changes: 89 additions & 77 deletions src/commands/sites/init.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
// 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
}
}
112 changes: 59 additions & 53 deletions src/prompts/sites/init.ts
Original file line number Diff line number Diff line change
@@ -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<PromptDeployOutput | null> => {
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<PromptDeployOutput | null> => {
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
export default promptSitesInit
Loading