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

botonic-cli: no read/write bot.config.json #2895

Merged
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
4 changes: 2 additions & 2 deletions packages/botonic-cli/src/botonic-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
Me,
OAuth,
} from './interfaces'
import { BotConfigJson } from './util/bot-config-json'
import { BotConfigJSON } from './util/bot-config'
import { pathExists } from './util/file-system'

const exec = util.promisify(childProcess.exec)
Expand Down Expand Up @@ -314,7 +314,7 @@ export class BotonicAPIService {

async deployBot(
bundlePath: string,
botConfigJson: BotConfigJson
botConfigJson: BotConfigJSON
): Promise<any> {
try {
await this.getMe()
Expand Down
7 changes: 3 additions & 4 deletions packages/botonic-cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ZipAFolder } from 'zip-a-folder'
import { Telemetry } from '../analytics/telemetry'
import { BotonicAPIService } from '../botonic-api-service'
import { CLOUD_PROVIDERS } from '../constants'
import { BotConfig, BotConfigJson } from '../util/bot-config-json'
import { BotConfig, BotConfigJSON } from '../util/bot-config'
import {
copy,
createDir,
Expand Down Expand Up @@ -360,7 +360,7 @@ Deploying to AWS...

/* istanbul ignore next */
async deployBundle(
botConfigJson: BotConfigJson
botConfigJson: BotConfigJSON
): Promise<{ hasDeployErrors: boolean }> {
const spinner = ora({
text: 'Deploying...',
Expand Down Expand Up @@ -443,8 +443,7 @@ Deploying to AWS...
return
}

const botConfig = new BotConfig(process.cwd())
const botConfigJson = await botConfig.createJson()
const botConfigJson = await BotConfig.get(process.cwd())

await this.createBundle()
const { hasDeployErrors } = await this.deployBundle(botConfigJson)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import childProcess from 'child_process'
import { promises as fs } from 'fs'
import ora from 'ora'
import path from 'path'
import * as util from 'util'

const OUTPUT_JSON_NAME = 'bot.config.json'
const BOTONIC_PREFIX_PACKAGE = '@botonic'
const BOTONIC_CORE_PACKAGE = `${BOTONIC_PREFIX_PACKAGE}/core`
const NPM_DEPTH_1 = 1
Expand All @@ -12,42 +12,46 @@
// Also get alpha and beta versions
const versionRegex = /@botonic\/[^@]+@(\d+\.\d+\.\d+(-[a-zA-Z]+\.\d+)?)/

export interface BotConfigJson {
type BotonicDependencies = Record<string, { version: string }>

export interface BotConfigJSON {
build_info: {
node_version: string
npm_version: string
botonic_cli_version: string
}
packages: Record<string, any>
packages: BotonicDependencies
}
export class BotConfig {
constructor(public appDirectory: string) {}

async createJson(): Promise<BotConfigJson> {
const packages = await this.readPackageJson()
static async get(appDirectory: string): Promise<BotConfigJSON> {
const spinner = ora({

Check warning on line 27 in packages/botonic-cli/src/util/bot-config.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-cli/src/util/bot-config.ts#L27

Added line #L27 was not covered by tests
text: 'Getting bot config...',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would remove this message, I think it is not needed for the user to know this is happening in the background

spinner: 'bouncingBar',
}).start()
const packages = await this.getBotonicDependencies(appDirectory)

Check warning on line 31 in packages/botonic-cli/src/util/bot-config.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-cli/src/util/bot-config.ts#L31

Added line #L31 was not covered by tests
const [nodeVersion, npmVersion, botonicCli] = await Promise.all(
['node -v', 'npm -v', 'botonic -v'].map(command =>
this.getOutputByCommand(command)
)
)
const oldBotConfigJSON = await this.readOldBotConfigJSON()
const newBotConfigJSON = {
...oldBotConfigJSON,
spinner.succeed()

Check warning on line 37 in packages/botonic-cli/src/util/bot-config.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-cli/src/util/bot-config.ts#L37

Added line #L37 was not covered by tests

return {

Check warning on line 39 in packages/botonic-cli/src/util/bot-config.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-cli/src/util/bot-config.ts#L39

Added line #L39 was not covered by tests
build_info: {
node_version: nodeVersion,
npm_version: npmVersion,
botonic_cli_version: botonicCli,
},
packages,
}
await this.writeBotConfigJSON(newBotConfigJSON)
return newBotConfigJSON
}

private async readPackageJson(): Promise<Record<string, any>> {
private static async getBotonicDependencies(
appDirectory: string
): Promise<BotonicDependencies> {
const packages = {}
try {
const packageJsonPath = path.join(this.appDirectory, 'package.json')
const packageJsonPath = path.join(appDirectory, 'package.json')

Check warning on line 54 in packages/botonic-cli/src/util/bot-config.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-cli/src/util/bot-config.ts#L54

Added line #L54 was not covered by tests
const data = await fs.readFile(packageJsonPath, 'utf8')
const packageJson = JSON.parse(data)

Expand Down Expand Up @@ -75,11 +79,11 @@
return packages
}

private async setDependenciesVersion(
private static async setDependenciesVersion(
dependency: string,
packages: Record<string, any>,
depth: number = NPM_DEPTH_0
) {
): Promise<BotonicDependencies> {
try {
const output = await this.getOutputByCommand(
`npm ls ${dependency} --depth=${depth}`
Expand All @@ -93,35 +97,9 @@
return packages
}

private async getOutputByCommand(command: string): Promise<string> {
private static async getOutputByCommand(command: string): Promise<string> {
const exec = util.promisify(childProcess.exec)
const { stdout } = await exec(command)
return stdout.trim()
}

private async readOldBotConfigJSON(): Promise<Record<string, any>> {
try {
const oldBotConfigJSON = path.join(this.appDirectory, OUTPUT_JSON_NAME)
const data = await fs.readFile(oldBotConfigJSON, 'utf8')
return JSON.parse(data)
} catch (err: any) {
console.error(`Cannot read ${OUTPUT_JSON_NAME} in app directory`)
return {}
}
}

private async writeBotConfigJSON(
botConfig: Record<string, any>,
paths: string[] = []
): Promise<void> {
paths.push(OUTPUT_JSON_NAME)
try {
const botConfigPath = path.join(this.appDirectory, ...paths)
await fs.writeFile(botConfigPath, JSON.stringify(botConfig, null, 2))
} catch (err: any) {
console.error(
`Error writing ${OUTPUT_JSON_NAME} in ${this.appDirectory}/${paths.join('/')}: ${err.message}`
)
}
}
}
4 changes: 2 additions & 2 deletions packages/botonic-cli/tests/commands/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe('TEST: Deploy pipeline', () => {
botonic_cli_version: '0.29.0',
},
packages: {
'@botonic/core': '0.29.0',
'@botonic/react': '0.29.0',
'@botonic/core': { version: '0.29.0' },
'@botonic/react': { version: '0.29.0' },
},
}
const { hasDeployErrors } =
Expand Down
Loading