@@ -25,11 +25,8 @@ import {
2525 DimLockJSON ,
2626 LockContent ,
2727} from "./types.ts" ;
28- import { Encoder } from "./postprocess/encoder.ts" ;
29- import { Unzipper } from "./postprocess/unzipper.ts" ;
30- import { XLSXConverter } from "./postprocess/xlsx_converter.ts" ;
31- import { Command } from "./postprocess/command.ts" ;
3228import { CkanApiClient } from "./ckan_api_client.ts" ;
29+ import { PostprocessDispatcher } from "./postprocess/postprocess_dispatcher.ts" ;
3330
3431const initDimFile = async ( ) => {
3532 const dimData : DimJSON = { fileVersion : DIM_FILE_VERSION , contents : [ ] } ;
@@ -256,70 +253,15 @@ const installFromCatalog = async (
256253 return fullPath ;
257254} ;
258255
256+ const postprocessDispatcher = new PostprocessDispatcher ( ) ;
257+
259258const executePostprocess = async (
260259 postProcesses : string [ ] ,
261260 targetPath : string ,
262261) => {
263262 for ( const postProcess of postProcesses ) {
264263 const [ type , ...argumentList ] = postProcess . split ( " " ) ;
265- if ( type === "encode" ) {
266- if ( argumentList . length === 0 ) {
267- console . log (
268- Colors . red ( "Argument not specified." ) ,
269- ) ;
270- Deno . exit ( 1 ) ;
271- } else if ( argumentList . length > 1 ) {
272- console . log (
273- Colors . red ( "error: Too many arguments:" ) ,
274- Colors . red ( type + " " + argumentList . join ( " " ) ) ,
275- ) ;
276- Deno . exit ( 1 ) ;
277- }
278- const encodingTo = argumentList [ 0 ] . toUpperCase ( ) ;
279- await new Encoder ( ) . encodeFile ( targetPath , encodingTo ) ;
280- console . log ( "Converted encoding to" , encodingTo ) ;
281- } else if ( type === "unzip" ) {
282- if ( argumentList . length > 0 ) {
283- console . log (
284- Colors . red ( "error: Too many arguments:" ) ,
285- Colors . red ( type + " " + argumentList . join ( " " ) ) ,
286- ) ;
287- Deno . exit ( 1 ) ;
288- }
289- const targetDir = await new Unzipper ( ) . unzip ( targetPath ) ;
290- console . log ( `Unzip the file to ${ targetDir } ` ) ;
291- } else if ( type === "xlsx-to-csv" ) {
292- if ( argumentList . length > 0 ) {
293- console . log (
294- Colors . red ( "error: Too many arguments:" ) ,
295- Colors . red ( type + " " + argumentList . join ( " " ) ) ,
296- ) ;
297- Deno . exit ( 1 ) ;
298- }
299- await new XLSXConverter ( ) . convertToCSV ( targetPath ) ;
300- console . log ( `Convert xlsx to csv.` ) ;
301- } else if ( postProcess . startsWith ( "CMD:" ) ) {
302- const script = postProcess . replace ( "CMD:" , "" ) . trim ( ) ;
303- if ( script === "" ) {
304- console . log (
305- Colors . red ( "No command entered" ) ,
306- ) ;
307- Deno . exit ( 1 ) ;
308- }
309- try {
310- console . log ( "Execute Command: " , script , targetPath ) ;
311- const result = await new Command ( ) . execute ( script , targetPath ) ;
312- console . log ( result ) ;
313- } catch ( e ) {
314- console . log (
315- Colors . red ( `Failed to execute the "${ script } "\n` ) ,
316- Colors . red ( `${ e } ` ) ,
317- ) ;
318- // Do not `exit` because there are commands that do not correspond when obtained from external dim.json.
319- }
320- } else {
321- console . log ( `No support a postprocess '${ postProcess } '.` ) ;
322- }
264+ await postprocessDispatcher . dispatch ( type , argumentList , targetPath ) ;
323265 }
324266} ;
325267
@@ -716,9 +658,9 @@ export class SearchAction {
716658 message :
717659 "Enter the post-processing you want to add. Enter blank if not required." ,
718660 hint :
719- "(ex.: > unzip, xlsx-to-csv, encode utf-8 or CMD: [some cli command])" ,
661+ "(ex.: > unzip, xlsx-to-csv, encode utf-8 or cmd [some cli command])" ,
720662 validate : ( text ) => {
721- return text === "" || text . startsWith ( "CMD: " ) ||
663+ return text === "" || text . startsWith ( "cmd " ) ||
722664 availablePostProcesses . includes ( text ) ;
723665 } ,
724666 suggestions : availablePostProcesses ,
0 commit comments