@@ -2,17 +2,18 @@ import * as path from "path";
22import { homedir } from "os" ;
33import * as fs from "fs-extra" ;
44import { logMessage } from "./logger" ;
5+ import { format } from "util" ;
56
67const winJupyterPath = path . join ( "AppData" , "Roaming" , "jupyter" , "kernels" ) ;
78const linuxJupyterPath = path . join ( ".local" , "share" , "jupyter" , "kernels" ) ;
89const 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 ) {
1213 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 ) ) {
1415 return macJupyterPath ;
15- } else if ( / ^ l i n u x / . test ( platform ) ) {
16+ } else if ( / ^ l i n u x / . test ( process . platform ) ) {
1617 return linuxJupyterPath ;
1718 } else {
1819 throw new Error ( "Unable to determine the OS" ) ;
@@ -30,19 +31,43 @@ async function ensureKernelsDirectory() {
3031 await fs . ensureDir ( kernelsDir ) ;
3132}
3233
34+ const tslabExecutable = isWindows ? "tslab.cmd" : "tslab" ;
35+
3336const kernelSpec = {
34- argv : [ "tslab" , "kernel" , "--config-path" , "{connection_file}" ] ,
37+ argv : [ tslabExecutable , "kernel" , "--config-path" , "{connection_file}" ] ,
3538 // eslint-disable-next-line @typescript-eslint/naming-convention
3639 display_name : "TypeScript" ,
3740 language : "typescript" ,
3841} ;
3942const kernelSpecName = "typescript" ;
4043
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+
4165export async function installKernelSpec ( ) {
4266 await ensureKernelsDirectory ( ) ;
4367 const kernelSpecDirectory = path . join ( kernelsDir , kernelSpecName ) ;
4468 const kernelSpecFile = path . join ( kernelSpecDirectory , "kernel.json" ) ;
4569 if ( await fs . pathExists ( kernelSpecFile ) ) {
70+ updateKernelSpec ( kernelSpecFile ) ;
4671 logMessage ( `KernelSpec already exists ${ kernelSpecFile } ` ) ;
4772 return ;
4873 }
0 commit comments