Skip to content

Validate tutorial dependencies #245

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

Merged
merged 6 commits into from
Apr 11, 2020
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
3 changes: 3 additions & 0 deletions errors/MissingTutorialDependency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Missing Tutorial Dependency

The tutorial cannot run because it a dependency is not yet installed. Install the dependency and click "Check Again".
5 changes: 5 additions & 0 deletions errors/UnmetTutorialDependency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Unmet Tutorial Dependency

### Unmet Tutorial Dependency

Tutorial cannot reun because a dependency version doesn't match. Install the correct dependency and click "Check Again".
32 changes: 29 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/jest": "^25.2.1",
"@types/jsdom": "^16.2.0",
"@types/node": "^13.11.0",
"@types/semver": "^7.1.0",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0",
"chokidar": "^3.3.0",
Expand All @@ -49,6 +50,7 @@
"jest": "^25.2.7",
"jsdom": "^16.2.2",
"prettier": "^2.0.2",
"semver": "^7.2.2",
"ts-jest": "^25.3.1",
"typescript": "^3.8.3"
},
Expand Down
64 changes: 61 additions & 3 deletions src/channel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import tutorialConfig from '../actions/tutorialConfig'
import { COMMANDS } from '../editor/commands'
import logger from '../services/logger'
import Context from './context'
import { version as gitVersion } from '../services/git'
import { version, compareVersions } from '../services/dependencies'
import { openWorkspace, checkWorkspaceEmpty } from '../services/workspace'
import { readFile } from 'fs'
import { join } from 'path'
import { promisify } from 'util'
import { compare } from 'semver'

const readFileAsync = promisify(readFile)

Expand Down Expand Up @@ -94,6 +95,63 @@ class Channel implements Channel {
// setup tutorial config (save watcher, test runner, etc)
await this.context.setTutorial(this.workspaceState, data)

// validate dependencies
const dependencies = data.config.dependencies
if (dependencies && dependencies.length) {
for (const dep of dependencies) {
// check dependency is installed
const currentVersion: string | null = await version(dep.name)
if (!currentVersion) {
// use a custom error message
const error = {
type: 'MissingTutorialDependency',
message: dep.message || `Process "${dep.name}" is required but not found. It may need to be installed`,
actions: [
{
label: 'Check Again',
transition: 'TRY_AGAIN',
},
],
}
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}

// check dependency version
const satisfiedDependency = await compareVersions(currentVersion, dep.version)

if (!satisfiedDependency) {
const error = {
type: 'UnmetTutorialDependency',
message: `Expected ${dep.name} to have version ${dep.version}, but found version ${currentVersion}`,
actions: [
{
label: 'Check Again',
transition: 'TRY_AGAIN',
},
],
}
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}

if (satisfiedDependency !== true) {
const error = satisfiedDependency || {
type: 'UnknownError',
message: `Something went wrong comparing dependency for ${name}`,
actions: [
{
label: 'Try Again',
transition: 'TRY_AGAIN',
},
],
}
this.send({ type: 'TUTORIAL_CONFIGURE_FAIL', payload: { error } })
return
}
}
}

const error: E.ErrorMessage | void = await tutorialConfig({ config: data.config }).catch((error: Error) => ({
type: 'UnknownError',
message: `Location: tutorial config.\n\n${error.message}`,
Expand Down Expand Up @@ -144,7 +202,7 @@ class Channel implements Channel {
}
// 2. check Git is installed.
// Should wait for workspace before running otherwise requires access to root folder
const isGitInstalled = await gitVersion()
const isGitInstalled = await version('git')
if (!isGitInstalled) {
const error: E.ErrorMessage = {
type: 'GitNotFound',
Expand Down Expand Up @@ -197,7 +255,7 @@ class Channel implements Channel {

if (errorMarkdown) {
// add a clearer error message for the user
error.message = `${errorMarkdown}\n${error.message}`
error.message = `${errorMarkdown}\n\n${error.message}`
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/services/dependencies/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { satisfies } from 'semver'
import node from '../node'

const semverRegex = /(?<=^v?|\sv?)(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*)(?:\.(?:0|[1-9]\d*|[\da-z-]*[a-z-][\da-z-]*))*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?(?=$|\s)/gi

export const version = async (name: string): Promise<string | null> => {
try {
const { stdout, stderr } = await node.exec(`${name} --version`)
if (!stderr) {
const match = stdout.match(semverRegex)
if (match) {
return match[0]
}
}
return null
} catch (error) {
return null
}
}

export const compareVersions = async (currentVersion: string, expectedVersion: string): Promise<never | boolean> => {
// see node-semver docs: https://github.com/npm/node-semver
return satisfies(currentVersion, expectedVersion)
}
13 changes: 0 additions & 13 deletions src/services/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,6 @@ export async function clear(): Promise<Error | void> {
throw new Error('Error cleaning up current unsaved work')
}

export async function version(): Promise<string | null> {
const { stdout, stderr } = await node.exec('git --version')
if (!stderr) {
const match = stdout.match(/^git version (\d+\.)?(\d+\.)?(\*|\d+)/)
if (match) {
// eslint-disable-next-line
const [_, major, minor, patch] = match
return `${major}${minor}${patch}`
}
}
return null
}

async function init(): Promise<Error | void> {
const { stderr } = await node.exec('git init')
if (stderr) {
Expand Down
7 changes: 7 additions & 0 deletions typings/tutorial.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type Maybe<T> = T | null
export type TutorialConfig = {
testRunner: TutorialTestRunner
repo: TutorialRepo
dependencies?: TutorialDependency[]
}

/** Logical groupings of tasks */
Expand Down Expand Up @@ -57,3 +58,9 @@ export interface TutorialRepo {
uri: string
branch: string
}

export interface TutorialDependency {
name: string
version: string
message?: string
}