@@ -2,17 +2,18 @@ import * as path from "path";
2
2
import { homedir } from "os" ;
3
3
import * as fs from "fs-extra" ;
4
4
import { logMessage } from "./logger" ;
5
+ import { format } from "util" ;
5
6
6
7
const winJupyterPath = path . join ( "AppData" , "Roaming" , "jupyter" , "kernels" ) ;
7
8
const linuxJupyterPath = path . join ( ".local" , "share" , "jupyter" , "kernels" ) ;
8
9
const macJupyterPath = path . join ( "Library" , "Jupyter" , "kernels" ) ;
9
-
10
- function getKernelsDir ( platform : string = process . platform ) {
11
- if ( / ^ w i n / . test ( platform ) ) {
10
+ const isWindows = / ^ w i n / . test ( process . platform ) ;
11
+ function getKernelsDir ( ) {
12
+ if ( isWindows ) {
12
13
return winJupyterPath ;
13
- } else if ( / ^ d a r w i n / . test ( platform ) ) {
14
+ } else if ( / ^ d a r w i n / . test ( process . platform ) ) {
14
15
return macJupyterPath ;
15
- } else if ( / ^ l i n u x / . test ( platform ) ) {
16
+ } else if ( / ^ l i n u x / . test ( process . platform ) ) {
16
17
return linuxJupyterPath ;
17
18
} else {
18
19
throw new Error ( "Unable to determine the OS" ) ;
@@ -30,19 +31,43 @@ async function ensureKernelsDirectory() {
30
31
await fs . ensureDir ( kernelsDir ) ;
31
32
}
32
33
34
+ const tslabExecutable = isWindows ? "tslab.cmd" : "tslab" ;
35
+
33
36
const kernelSpec = {
34
- argv : [ "tslab" , "kernel" , "--config-path" , "{connection_file}" ] ,
37
+ argv : [ tslabExecutable , "kernel" , "--config-path" , "{connection_file}" ] ,
35
38
// eslint-disable-next-line @typescript-eslint/naming-convention
36
39
display_name : "TypeScript" ,
37
40
language : "typescript" ,
38
41
} ;
39
42
const kernelSpecName = "typescript" ;
40
43
44
+ export async function updateKernelSpec ( kernelSpecFile : string ) {
45
+ if ( ! isWindows ) {
46
+ return ;
47
+ }
48
+ try {
49
+ const contents : typeof kernelSpec = await fs . readJSON ( kernelSpecFile , {
50
+ encoding : "utf8" ,
51
+ } ) ;
52
+ if ( contents . argv [ 0 ] === tslabExecutable ) {
53
+ return ;
54
+ }
55
+ await fs . writeFile ( kernelSpecFile , JSON . stringify ( kernelSpec ) , {
56
+ flag : "w" ,
57
+ } ) ;
58
+ logMessage ( `Existing KernelSpec updated ${ kernelSpecFile } ` ) ;
59
+ } catch ( ex ) {
60
+ logMessage ( `Failed to update kernel.json ${ format ( ex ) } ` ) ;
61
+ }
62
+ return ;
63
+ }
64
+
41
65
export async function installKernelSpec ( ) {
42
66
await ensureKernelsDirectory ( ) ;
43
67
const kernelSpecDirectory = path . join ( kernelsDir , kernelSpecName ) ;
44
68
const kernelSpecFile = path . join ( kernelSpecDirectory , "kernel.json" ) ;
45
69
if ( await fs . pathExists ( kernelSpecFile ) ) {
70
+ updateKernelSpec ( kernelSpecFile ) ;
46
71
logMessage ( `KernelSpec already exists ${ kernelSpecFile } ` ) ;
47
72
return ;
48
73
}
0 commit comments