Skip to content

Feature/file watcher #50

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 5 commits into from
Nov 10, 2019
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
36 changes: 35 additions & 1 deletion src/actions/setupActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,49 @@ const runCommands = async (commands: string[], language: string = 'JAVASCRIPT')
}
}

// collect active file watchers (listeners)
const watchers: {[key: string]: vscode.FileSystemWatcher} = {}

const setupActions = async (workspaceRoot: vscode.WorkspaceFolder, {commands, commits, files}: G.StepActions): Promise<void> => {
const disposeWatcher = (listener: string) => {
watchers[listener].dispose()
delete watchers[listener]
}

const setupActions = async (workspaceRoot: vscode.WorkspaceFolder, {commands, commits, files, listeners}: G.StepActions): Promise<void> => {
// run commits
if (commits) {
for (const commit of commits) {
await git.loadCommit(commit)
}
}

// run file watchers (listeners)
if (listeners) {
for (const listener of listeners) {
if (!watchers[listener]) {
const pattern = new vscode.RelativePattern(
vscode.workspace.getWorkspaceFolder(workspaceRoot.uri)!,
listener
)
watchers[listener] = vscode.workspace.createFileSystemWatcher(
pattern
)
watchers[listener].onDidChange(() => {
// trigger save
vscode.commands.executeCommand('coderoad.run_test', null, () => {
// cleanup watcher on success
disposeWatcher(listener)
})
})
}
}
} else {
// remove all watchers
for (const listener of Object.keys(watchers)) {
disposeWatcher(listener)
}
}

// run command
if (commands) {
await runCommands(commands)
Expand Down
3 changes: 2 additions & 1 deletion src/editor/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ export const createCommands = ({extensionPath, workspaceState, workspaceRoot}: C
// set from last setup stepAction
currentStepId = stepId
},
[COMMANDS.RUN_TEST]: (current: {stepId: string} | undefined) => {
[COMMANDS.RUN_TEST]: (current: {stepId: string} | undefined, onSuccess: () => void) => {
console.log('-------- command.run_test ------ ')
// use stepId from client, or last set stepId
const payload = {stepId: current ? current.stepId : currentStepId}
runTest({
onSuccess: () => {
// send test pass message back to client
webview.send({type: 'TEST_PASS', payload})
onSuccess()
vscode.window.showInformationMessage('PASS')
},
onFail: () => {
Expand Down
8 changes: 2 additions & 6 deletions typings/graphql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export type StepActions = {
commits: Array<Scalars['Sha1']>
files?: Maybe<Array<Scalars['String']>>
commands?: Maybe<Array<Scalars['String']>>
listeners?: Maybe<Array<Scalars['String']>>
}

export type TestRunner = 'JEST'
Expand Down Expand Up @@ -494,6 +495,7 @@ export type StepActionsResolvers<
commits?: Resolver<Array<ResolversTypes['Sha1']>, ParentType, ContextType>
files?: Resolver<Maybe<Array<ResolversTypes['String']>>, ParentType, ContextType>
commands?: Resolver<Maybe<Array<ResolversTypes['String']>>, ParentType, ContextType>
listeners?: Resolver<Maybe<Array<ResolversTypes['String']>>, ParentType, ContextType>
}

export type TutorialResolvers<
Expand Down Expand Up @@ -632,9 +634,3 @@ export interface IntrospectionResultData {
}[]
}
}
const result: IntrospectionResultData = {
__schema: {
types: [],
},
}
export default result
Loading