Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed May 10, 2023
1 parent 14e991a commit 3060094
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
7 changes: 2 additions & 5 deletions extension/scripts/virtualenv-install.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
const { join, resolve } = require('path')
require('../src/vscode/mockModule')
require('../dist/vscode/mockModule')

const importModuleAfterMockingVsCode = async () => {
const { getEsmModules } = require('../src/process/execution')
await getEsmModules()
const { setupTestVenv } = require('../src/python')

const { setupTestVenv } = require('../dist/python')
return setupTestVenv
}

Expand Down
4 changes: 2 additions & 2 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { GitExecutor } from './cli/git/executor'
import { GitReader } from './cli/git/reader'
import { Setup } from './setup'
import { definedAndNonEmpty } from './util/array'
import { getEsmModules, stopProcesses } from './process/execution'
import { esmModulesImported, stopProcesses } from './process/execution'
import { Flag } from './cli/dvc/constants'
import { LanguageClient } from './languageClient'
import { collectRunningExperimentPids } from './experiments/processExecution/collect'
Expand Down Expand Up @@ -304,7 +304,7 @@ class Extension extends Disposable {
let extension: undefined | Extension

export function activate(context: ExtensionContext): void {
void getEsmModules().then(() => {
void esmModulesImported.then(() => {
extension = new Extension(context)
context.subscriptions.push(extension)
})
Expand Down
13 changes: 12 additions & 1 deletion extension/src/process/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@ import { ChildProcess } from 'child_process'
import { Readable } from 'stream'
import { Event, EventEmitter } from 'vscode'
import { Disposable } from '@hediet/std/disposable'
import { Deferred } from '@hediet/std/synchronization'
import kill from 'tree-kill'
import { getProcessPlatform } from '../env'

const deferred = new Deferred()
export const esmModulesImported = deferred.promise

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
type EsmExeca = typeof import('execa').execa
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
type EsmProcessExists = typeof import('process-exists').processExists

const envCanImportEsm = process.env.NODE_ENV !== 'test'

let execa: EsmExeca
let doesProcessExist: EsmProcessExists
export const getEsmModules = async () => {
const importEsmModules = async () => {
const [{ execa: esmExeca }, { processExists: esmProcessExists }] =
await Promise.all([import('execa'), import('process-exists')])
execa = esmExeca
doesProcessExist = esmProcessExists
deferred.resolve()
}

if (envCanImportEsm) {
void importEsmModules()
}

interface RunningProcess extends ChildProcess {
Expand Down
4 changes: 2 additions & 2 deletions extension/src/python/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { exists } from '../fileSystem'
import { Logger } from '../common/logger'
import {
createProcess,
esmModulesImported,
executeProcess,
getEsmModules,
Process
} from '../process/execution'

Expand Down Expand Up @@ -39,7 +39,7 @@ export const setupTestVenv = async (
envDir: string,
...installArgs: string[]
) => {
await getEsmModules()
await esmModulesImported
if (!exists(join(cwd, envDir))) {
const initVenv = createProcess({
args: ['-m', 'venv', envDir],
Expand Down
8 changes: 4 additions & 4 deletions extension/src/test/suite/cli/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require('../../../vscode/mockModule')

const importModuleAfterMockingVsCode = () => {
const { Cli } = require('../../../cli')
const { getEsmModules } = require('../../../process/execution')
return { Cli, getEsmModules }
const { esmModulesImported } = require('../../../process/execution')
return { Cli, esmModulesImported }
}

const main = async () => {
const { Cli, getEsmModules } = importModuleAfterMockingVsCode()
const { Cli, esmModulesImported } = importModuleAfterMockingVsCode()

await getEsmModules()
await esmModulesImported
const cli = new Cli()

const options = getOptions('background')
Expand Down
3 changes: 1 addition & 2 deletions extension/src/test/util/mocha/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ export const runMocha = async (
teardown: () => Promise<void> | void,
timeout = 6000
) => {
const { getEsmModules } = require('../../../process/execution')
await Promise.all([setup(), getEsmModules()])
await setup()

const mocha = new Mocha({
checkLeaks: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"postinstall": "husky install && git submodule init && git submodule update",
"storybook": "yarn workspace dvc-vscode-webview storybook",
"build-storybook": "yarn turbo run build-storybook --filter=dvc-vscode-webview",
"setup:venv": "yarn workspace dvc run setup-venv",
"setup:venv": "yarn turbo run lint:build && yarn workspace dvc run setup-venv",
"scheduled:cli:test": "ts-node ./extension/src/test/cli/index.ts",
"create-svgs": "ts-node ./scripts/create-svgs.ts",
"svgr": "yarn workspace dvc-vscode-webview svgr"
Expand Down

0 comments on commit 3060094

Please sign in to comment.