1
1
'use strict' ;
2
2
import * as vscode from 'vscode' ;
3
3
import * as process from 'child_process' ;
4
+ import * as path from 'path' ;
4
5
5
6
export function activate ( context : vscode . ExtensionContext ) {
6
7
let disposable = vscode . commands . registerCommand ( 'pico8.runCart' , ( ) => {
@@ -17,18 +18,35 @@ export function activate(context: vscode.ExtensionContext) {
17
18
}
18
19
let additionalParams = config . pico8additionalParameters || "" ;
19
20
20
- process . exec ( `${ picoPath } -run ${ vscode . window . activeTextEditor . document . fileName } ${ additionalParams } ` , ( err , stdout , stderr ) => {
21
- if ( stdout && stdout . length > 0 ) {
22
- console . log ( `pico8 output: ${ stdout } ` ) ;
23
- }
24
-
25
- if ( stderr && stderr . length > 0 ) {
26
- console . log ( `pico8 error output: ${ stderr } ` ) ;
27
- }
28
-
29
- if ( err ) {
30
- vscode . window . showErrorMessage ( `pico8 error: ${ err . name } - ${ err . message } ` ) ;
21
+ let document = vscode . window . activeTextEditor . document ;
22
+ let filename = document . fileName ;
23
+ let folder = path . dirname ( filename ) ;
24
+
25
+ if ( ! filename || ! folder ) {
26
+ vscode . window . showErrorMessage ( "Error: Current file is not saved, or unable to find parent folder." ) ;
27
+ return ;
28
+ }
29
+
30
+ document . save ( ) . then ( s => {
31
+ if ( ! s ) {
32
+ vscode . window . showErrorMessage ( "Error: unable to save current document." ) ;
33
+ return ;
31
34
}
35
+
36
+ // Would be good to set the root path to the folder the file is in, but doesn't seem to be a commandline option?
37
+ process . exec ( `${ picoPath } -run ${ filename } ${ additionalParams } ` , ( err , stdout , stderr ) => {
38
+ if ( stdout && stdout . length > 0 ) {
39
+ console . log ( `pico8 output: ${ stdout } ` ) ;
40
+ }
41
+
42
+ if ( stderr && stderr . length > 0 ) {
43
+ console . log ( `pico8 error output: ${ stderr } ` ) ;
44
+ }
45
+
46
+ if ( err ) {
47
+ vscode . window . showErrorMessage ( `pico8 error: ${ err . name } - ${ err . message } ` ) ;
48
+ }
49
+ } ) ;
32
50
} ) ;
33
51
} ) ;
34
52
context . subscriptions . push ( disposable ) ;
@@ -47,18 +65,35 @@ export function activate(context: vscode.ExtensionContext) {
47
65
}
48
66
let additionalParams = config . pico8additionalParameters || "" ;
49
67
50
- process . exec ( `${ picoPath } ${ vscode . window . activeTextEditor . document . fileName } ${ additionalParams } ` , ( err , stdout , stderr ) => {
51
- if ( stdout && stdout . length > 0 ) {
52
- console . log ( `pico8 output: ${ stdout } ` ) ;
53
- }
54
-
55
- if ( stderr && stderr . length > 0 ) {
56
- console . log ( `pico8 error output: ${ stderr } ` ) ;
57
- }
58
-
59
- if ( err ) {
60
- vscode . window . showErrorMessage ( `pico8 error: ${ err . name } - ${ err . message } ` ) ;
68
+ let document = vscode . window . activeTextEditor . document ;
69
+ let filename = document . fileName ;
70
+ let folder = path . dirname ( filename ) ;
71
+
72
+ if ( ! filename || ! folder ) {
73
+ vscode . window . showErrorMessage ( "Error: Current file is not saved, or unable to find parent folder." ) ;
74
+ return ;
75
+ }
76
+
77
+ document . save ( ) . then ( s => {
78
+ if ( ! s ) {
79
+ vscode . window . showErrorMessage ( "Error: unable to save current document." ) ;
80
+ return ;
61
81
}
82
+
83
+ // Would be good to set the root path to the folder the file is in, but doesn't seem to be a commandline option?
84
+ process . exec ( `${ picoPath } ${ filename } ${ additionalParams } ` , ( err , stdout , stderr ) => {
85
+ if ( stdout && stdout . length > 0 ) {
86
+ console . log ( `pico8 output: ${ stdout } ` ) ;
87
+ }
88
+
89
+ if ( stderr && stderr . length > 0 ) {
90
+ console . log ( `pico8 error output: ${ stderr } ` ) ;
91
+ }
92
+
93
+ if ( err ) {
94
+ vscode . window . showErrorMessage ( `pico8 error: ${ err . name } - ${ err . message } ` ) ;
95
+ }
96
+ } ) ;
62
97
} ) ;
63
98
} ) ;
64
99
context . subscriptions . push ( disposable ) ;
0 commit comments