Skip to content

Commit

Permalink
feat: create project on backend with Init cmd (#14)
Browse files Browse the repository at this point in the history
* feat: create project on backend with Init cmd

* fix: regex for repo

* fix: write db projectId back to settings.yml

* chore: update comments
  • Loading branch information
ndom91 authored Aug 3, 2021
1 parent 53210b6 commit 691314d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
3 changes: 3 additions & 0 deletions sdk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function init({ api, apiKey, baseURL }) {
getAll() {
return _api.get('http://localhost:3000/next/projects')
},
create(project) {
return _api.post('http://localhost:3000/next/projects', project)
},
}

const socket = {
Expand Down
43 changes: 32 additions & 11 deletions src/commands/init.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const fs = require('fs')
const { readFile } = require('fs/promises')
const path = require('path')
const consola = require('consola')
const { Command } = require('@oclif/command')
const { account } = require('./../services/api')
const { account, projects } = require('./../services/api')
const config = require('./../services/config')
const { defaultCheckTemplate, settingsTemplate } = require('../templates/init')

Expand Down Expand Up @@ -34,19 +35,9 @@ class InitCommand extends Command {

config.set('accountId', accountId)

// Initial Account Settings
const accountSettingsYml = settingsTemplate({
accountId,
name,
projectName: args.projectName,
})

// Example Check YML
const exampleCheckYml = defaultCheckTemplate()

// Create Settings File
fs.writeFileSync(path.join(dirName, 'settings.yml'), accountSettingsYml)

// Create Checks Directory
fs.mkdirSync(path.join(dirName, 'checks'))

Expand All @@ -56,6 +47,36 @@ class InitCommand extends Command {
exampleCheckYml
)

// Get package.json
const pkg = JSON.parse(
await readFile(path.join(__dirname, '../../package.json'))
)

// Grab repository name from repo url
const repo = pkg.repository.url.match(
/.*\/(?<author>[\w,\-,_]+)\/(?<project>[\w,\-,_]+)(.git)?$/
)

// Create project on backend
const savedProject = await projects.create({
accountId,
repoUrl: `${repo.groups.author}/${repo.groups.project}`,
name: args.projectName,
activated: true,
muted: false,
})

// Generate initial account settings
const accountSettingsYml = settingsTemplate({
accountId,
accountName: name,
projectName: args.projectName,
projectId: savedProject.data.id,
})

// Write settings file
fs.writeFileSync(path.join(dirName, 'settings.yml'), accountSettingsYml)

consola.success(' Project initialized 🎉 \n')
consola.info(' You can now create checks via `checkly checks create`')
consola.info(
Expand Down
13 changes: 10 additions & 3 deletions src/templates/init.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
const settingsTemplate = ({ accountId, name, projectName }) => {
const settingsTemplate = ({
accountId,
accountName,
projectId,
projectName,
}) => {
return `account:
- id: ${accountId}
name: ${name}
project: ${projectName}
name: ${accountName}
project:
- id: ${projectId}
name: ${projectName}
checkDefaults:
- locations: ['us-east-1', 'eu-central-1']
interval: 5min
Expand Down

0 comments on commit 691314d

Please sign in to comment.