1- const vscode = require ( "vscode" ) ;
2- const path = require ( "path" ) ;
3- const findFiles = require ( "./findFiles" ) ;
4- const fs = require ( "fs" ) ;
5- const { getProperties, getInput, getQuickPick } = require ( "./util" ) ;
6- const { UI_String } = require ( "./constant" ) ;
1+ /* eslint-disable no-unused-vars */
2+ const vscode = require ( 'vscode' )
3+ const path = require ( 'path' )
4+ const findFiles = require ( './findFiles' )
5+ const fs = require ( 'fs' )
6+ const { getProperties, getInput, getQuickPick } = require ( './util' )
7+ const { UIString } = require ( './constant' )
78
89// fetch setting value from vscode's settings
910// See package.json's contributes's configuration
10- const maxLogDisplayCnt = vscode . workspace . getConfiguration ( ) . get ( 'code.replacer.setting' ) ;
11+ const maxLogDisplayCnt = vscode . workspace . getConfiguration ( ) . get ( 'code.replacer.setting' )
1112
1213/**
1314 * @param {vscode.ExtensionContext } context
1415 */
1516
16- function validateTemplate ( template ) {
17- return template . includes ( "->" ) ;
17+ function validateTemplate ( template ) {
18+ return template . includes ( '->' )
1819}
1920
2021function handleBooleanFlags ( ) {
@@ -27,35 +28,44 @@ function handleBooleanFlags () {
2728 'overwrite' ,
2829 'excludeReg' ,
2930 'startLine' ,
30- 'endLine' ,
31- ] ;
31+ 'endLine'
32+ ]
3233
3334 return new Promise ( ( resolve , reject ) => {
3435 vscode . window
3536 . showQuickPick ( flagItems , {
3637 canPickMany : true ,
37- placeHolder : 'Check the flags to apply' ,
38+ placeHolder : 'Check the flags to apply.'
3839 } )
3940 . then ( ( selection ) => {
40- resolve ( selection ) ;
41- } ) ;
42- } ) ;
41+ resolve ( selection )
42+ } )
43+ } )
4344}
4445
4546async function handleTemplate ( { usageLogPath } ) {
46- const usageLogs = fetchLog ( { jsonPath : usageLogPath , keyName : 'template' } ) ;
47+ const usageLogs = fetchLog ( { jsonPath : usageLogPath , keyName : 'template' } )
4748 const uiButtons = [
4849 ...usageLogs ,
49- UI_String . TYPE_INPUT ,
50- ] ;
51- const template = await getQuickPick ( { items : uiButtons , placeHolder : 'Check template to apply' } )
52-
53- if ( template === UI_String . TYPE_INPUT ) {
50+ UIString . TYPE_INPUT ,
51+ UIString . EXIT
52+ ]
53+ const template = await getQuickPick ( {
54+ items : uiButtons ,
55+ placeHolder : 'Check template to apply.'
56+ } )
57+
58+ if ( template === UIString . TYPE_INPUT ) {
5459 return await getInput ( {
55- placeHolder : "Enter template" ,
56- } ) ;
60+ placeHolder : 'Enter template.' ,
61+ validateInput : ( str ) => {
62+ return ! validateTemplate ( str )
63+ }
64+ } )
65+ } else if ( template === UIString . EXIT ) {
66+ return - 1
5767 } else {
58- return template ;
68+ return template
5969 }
6070}
6171
@@ -65,10 +75,13 @@ const fetchLog = ({ jsonPath, keyName }) => {
6575
6676 let displayCnt = 0
6777 const maxDisplayCnt = maxLogDisplayCnt
68- console . log ( maxLogDisplayCnt )
6978 const keys = Object . keys ( usageLogJson ) . reverse ( )
7079 for ( const usageLogKey of keys ) {
71- if ( usageLogJson [ usageLogKey ] [ keyName ] && ! logs . includes ( usageLogJson [ usageLogKey ] [ keyName ] ) && ( displayCnt < maxDisplayCnt ) ) {
80+ if (
81+ usageLogJson [ usageLogKey ] [ keyName ] &&
82+ ! logs . includes ( usageLogJson [ usageLogKey ] [ keyName ] ) &&
83+ displayCnt < maxDisplayCnt
84+ ) {
7285 logs . push ( usageLogJson [ usageLogKey ] [ keyName ] )
7386 displayCnt ++
7487 }
@@ -79,103 +92,115 @@ const fetchLog = ({ jsonPath, keyName }) => {
7992
8093const activate = ( context ) => {
8194 const disposable = vscode . commands . registerCommand (
82- " code-replacer-vscode-plugin.entry" ,
95+ ' code-replacer-vscode-plugin.entry' ,
8396 async function ( ) {
84- if ( ! vscode . window . activeTextEditor || ! vscode . window . activeTextEditor . document ) {
85- vscode . window . showErrorMessage ( "Jobs canceled. open the source file first." ) ;
86- return ;
97+ if (
98+ ! vscode . window . activeTextEditor ||
99+ ! vscode . window . activeTextEditor . document
100+ ) {
101+ vscode . window . showErrorMessage (
102+ 'Jobs canceled. open the source file first.'
103+ )
104+ return
87105 }
88106
89107 const currentlyOpenTabfilePath =
90- vscode . window . activeTextEditor . document . fileName ;
108+ vscode . window . activeTextEditor . document . fileName
91109
92- const currentlyOpenTabfileName = path . basename ( currentlyOpenTabfilePath ) ;
110+ const currentlyOpenTabfileName = path . basename ( currentlyOpenTabfilePath )
93111
94- const codeReplacerPath = `${ __dirname } ${ path . sep } node_modules${
95- path . sep
96- } code-replacer${ path . sep } dist`;
97- const binPath = path . resolve (
98- `${ codeReplacerPath } ${ path . sep } index.js`
99- ) ;
100- const envPath = path . resolve ( `${ codeReplacerPath } ${ path . sep } ${ ".env" } ` ) ;
112+ const codeReplacerPath = `${ __dirname } ${ path . sep } node_modules${ path . sep } code-replacer${ path . sep } dist`
113+ const binPath = path . resolve ( `${ codeReplacerPath } ${ path . sep } index.js` )
114+ const envPath = path . resolve ( `${ codeReplacerPath } ${ path . sep } ${ '.env' } ` )
101115 const usageLogPath = path . resolve (
102116 `${ codeReplacerPath } ${ path . sep } usageLog.json`
103- ) ;
104- const workspaceName = vscode . workspace . name ;
105- const workspacePath = vscode . workspace . rootPath ;
117+ )
118+ const workspaceName = vscode . workspace . name
119+ const workspacePath = vscode . workspace . rootPath
106120 const csvFiles = await findFiles ( {
107121 dir : workspacePath ,
108- ext : "csv" ,
109- } ) ;
122+ ext : 'csv'
123+ } )
124+
110125 const selectedCSV = await getQuickPick ( {
111- items : csvFiles ,
112- placeHolder : " Select your csv file or type esc to pass csv option" ,
113- } ) ;
126+ items : [ UIString . EXIT , ... csvFiles ] ,
127+ placeHolder : ' Select your csv file or type esc to pass csv option.'
128+ } )
114129
115- vscode . window . showInformationMessage ( "csv option is passed." ) ;
130+ if ( ! selectedCSV ) {
131+ vscode . window . showInformationMessage ( 'csv option is passed.' )
132+ } else if ( selectedCSV === UIString . EXIT ) {
133+ vscode . window . showInformationMessage ( UIString . EXIT )
134+ return
135+ }
116136
117137 const flags = {
118138 src : currentlyOpenTabfilePath ,
119- csv : selectedCSV ,
120- } ;
139+ csv : selectedCSV
140+ }
121141
122- flags . template = await handleTemplate ( { usageLogPath } ) ;
142+ flags . template = await handleTemplate ( { usageLogPath } )
123143
124144 if ( ! flags . template ) {
125- vscode . window . showErrorMessage ( "Jobs canceled. template value is required argument." ) ;
126- return ;
145+ vscode . window . showErrorMessage (
146+ 'Jobs canceled. template value is required argument.'
147+ )
148+ return
149+ } else if ( flags . template === - 1 ) {
150+ vscode . window . showInformationMessage ( UIString . EXIT )
151+ return
127152 } else if ( ! validateTemplate ( flags . template ) ) {
128- vscode . window . showErrorMessage ( " Wrong template value. See README.md" ) ;
129- return ;
153+ vscode . window . showErrorMessage ( ' Wrong template value. See README.md' )
154+ return
130155 }
131156
132- const booleanFlags = await handleBooleanFlags ( ) ;
157+ const booleanFlags = await handleBooleanFlags ( )
133158
134159 for ( const booleanFlag of booleanFlags . values ( ) ) {
135- flags [ booleanFlag ] = true ;
160+ flags [ booleanFlag ] = true
136161 }
137162
138- if ( flags [ ' excludeReg' ] ) {
139- flags [ ' excludeReg' ] = await getInput ( { placeHolder : " Enter excludeReg" } ) ;
163+ if ( flags . excludeReg ) {
164+ flags . excludeReg = await getInput ( { placeHolder : ' Enter excludeReg.' } )
140165 }
141166
142- if ( flags [ ' startLine' ] ) {
143- flags [ ' startLine' ] = await getInput ( { placeHolder : " Enter startLine" } ) ;
167+ if ( flags . startLine ) {
168+ flags . startLine = await getInput ( { placeHolder : ' Enter startLine.' } )
144169 }
145170
146- if ( flags [ ' endLine' ] ) {
147- flags [ ' endLine' ] = await getInput ( { placeHolder : " Enter endLine" } ) ;
171+ if ( flags . endLine ) {
172+ flags . endLine = await getInput ( { placeHolder : ' Enter endLine.' } )
148173 }
149174
150175 const terminal = vscode . window . activeTerminal
151176 ? vscode . window . activeTerminal
152- : vscode . window . createTerminal ( ) ;
153- terminal . show ( ) ;
177+ : vscode . window . createTerminal ( )
178+ terminal . show ( )
154179
155- let command = `node ${ binPath } ` ;
180+ const command = `node ${ binPath } `
156181
157182 fs . writeFile (
158183 envPath ,
159184 getProperties ( flags ) ,
160185 {
161- encoding : " utf8" ,
186+ encoding : ' utf8'
162187 } ,
163188 ( ) => {
164- terminal . sendText ( command ) ;
189+ terminal . sendText ( command )
165190 }
166- ) ;
191+ )
167192 }
168- ) ;
193+ )
169194
170- context . subscriptions . push ( disposable ) ;
171- } ;
195+ context . subscriptions . push ( disposable )
196+ }
172197
173- exports . activate = activate ;
198+ exports . activate = activate
174199
175200// this method is called when your extension is deactivated
176- function deactivate ( ) { }
201+ function deactivate ( ) { }
177202
178203module . exports = {
179204 activate,
180- deactivate,
181- } ;
205+ deactivate
206+ }
0 commit comments