@@ -20,10 +20,7 @@ export class CustomEditorService implements ICustomEditorService {
2020 @inject ( IApplicationEnvironment ) private readonly appEnvironment : IApplicationEnvironment ,
2121 @inject ( IFileSystem ) private readonly fileSystem : IFileSystem
2222 ) {
23- // Double check the package json has the necessary entries for contributing a custom editor
24- if ( this . useCustomEditorApi && ! appEnvironment . packageJson . contributes ?. customEditors ) {
25- this . rewritePackageJson ( ) . catch ( ( e ) => traceError ( `Error rewriting package json: ` , e ) ) ;
26- }
23+ this . verifyPackageJson ( ) . catch ( ( e ) => traceError ( `Error rewriting package json: ` , e ) ) ;
2724 }
2825
2926 public registerCustomEditorProvider (
@@ -48,12 +45,23 @@ export class CustomEditorService implements ICustomEditorService {
4845 }
4946 }
5047
51- private async rewritePackageJson ( ) {
48+ private async verifyPackageJson ( ) : Promise < void > {
49+ // Double check the package json has the necessary entries for contributing a custom editor. Note
50+ // we have to actually read it because appEnvironment.packageJson is the webpacked version
51+ const packageJson = JSON . parse ( await this . fileSystem . readFile ( path . join ( EXTENSION_ROOT_DIR , 'package.json' ) ) ) ;
52+ if ( this . useCustomEditorApi && ! packageJson . contributes ?. customEditors ) {
53+ return this . addCustomEditors ( packageJson ) ;
54+ } else if ( ! this . useCustomEditorApi && packageJson . contributes . customEditors ) {
55+ return this . removeCustomEditors ( ) ;
56+ }
57+ }
58+
59+ // tslint:disable-next-line: no-any
60+ private async addCustomEditors ( currentPackageJson : any ) {
5261 // tslint:disable-next-line:no-require-imports no-var-requires
5362 const _mergeWith = require ( 'lodash/mergeWith' ) as typeof import ( 'lodash/mergeWith' ) ;
54- const current = this . appEnvironment . packageJson ;
5563 const improvedContents = await this . fileSystem . readFile ( path . join ( EXTENSION_ROOT_DIR , 'customEditor.json' ) ) ;
56- const improved = _mergeWith ( { ...current } , JSON . parse ( improvedContents ) , ( l , r ) => {
64+ const improved = _mergeWith ( { ...currentPackageJson } , JSON . parse ( improvedContents ) , ( l , r ) => {
5765 if ( Array . isArray ( l ) && Array . isArray ( r ) ) {
5866 return [ ...l , ...r ] ;
5967 }
@@ -64,4 +72,14 @@ export class CustomEditorService implements ICustomEditorService {
6472 ) ;
6573 this . commandManager . executeCommand ( 'python.reloadVSCode' , DataScience . reloadCustomEditor ( ) ) ;
6674 }
75+ private async removeCustomEditors ( ) {
76+ // Note, to put it back, use the shipped version. This packageJson is required into the product
77+ // so it's packed by webpack into the source.
78+ const original = { ...this . appEnvironment . packageJson } ;
79+ await this . fileSystem . writeFile (
80+ path . join ( EXTENSION_ROOT_DIR , 'package.json' ) ,
81+ JSON . stringify ( original , null , 4 )
82+ ) ;
83+ this . commandManager . executeCommand ( 'python.reloadVSCode' , DataScience . reloadCustomEditor ( ) ) ;
84+ }
6785}
0 commit comments