import fs from 'fs' ; import path from 'path' ; import { CommandParameters , JSON } from './types' ; returns export function getParams ( ) { const [ sourcePath , targetPath ] = process . argv . slice ( 2 ) ; if ( sourcePath === undefined ) { throw Error ( 'getParams, sourcePath not found' ) ; } return { sourcePath , targetPath } ; } param sourcePath returns export function loadFile ( sourcePath ) { if ( ! fs . existsSync ( sourcePath ) ) { throw Error ( `loadFile, file not found ${ sourcePath }` ) ; } if ( path . extname ( sourcePath ) === '.json' ) { return require ( sourcePath ) ; } else { return fs . readFileSync ( sourcePath , { encoding : 'utf8' } ) ; } } param source param extension returns export function getSourcePaths ( source , extension ) { const filePaths = fs . lstatSync ( source ) . isDirectory ( ) ? fs . readdirSync ( source ) . map ( fileName => path . join ( source , fileName ) ) : [ source ] ; return filePaths . filter ( filePath => filePath . endsWith ( extension ) ) . map ( filePath => path . resolve ( process . cwd ( ) , filePath ) ) ; } param fileName param data export function exportToFile ( fileName , data ) { const { targetPath = 'output' } = getParams ( ) ; const outputPath = path . join ( targetPath , fileName ) ; if ( ! fs . existsSync ( targetPath ) ) { fs . mkdirSync ( targetPath , { recursive : true } ) ; } fs . writeFileSync ( outputPath , data ) ; }