-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from jy95/refactoring
Allow config file for command to be a CJS file
- Loading branch information
Showing
42 changed files
with
4,287 additions
and
4,378 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,67 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
// lodash methodes | ||
import isPlainObject from "lodash/isPlainObject"; | ||
import isEmpty from "lodash/isEmpty"; | ||
import uniq from "lodash/uniq"; | ||
import isPlainObject from 'lodash/isPlainObject'; | ||
import isEmpty from 'lodash/isEmpty'; | ||
import uniq from 'lodash/uniq'; | ||
|
||
// validation for filename option | ||
export const FILENAME_CHECK = async (argv : any) => { | ||
let filename : unknown = argv["filename"]; | ||
if (path.extname(filename as string).length !== 0) { | ||
return new Error(`${filename} has an extension : Remove it please`); | ||
} else { | ||
return true; | ||
} | ||
} | ||
export const FILENAME_CHECK = async (argv: any) => { | ||
let filename: unknown = argv['filename']; | ||
if (path.extname(filename as string).length !== 0) { | ||
return new Error(`${filename} has an extension : Remove it please`); | ||
} else { | ||
return true; | ||
} | ||
}; | ||
|
||
// validations for files option | ||
export const FILES_CHECK = async (argv : any) => { | ||
let files = argv.files as any; | ||
if (!isPlainObject(files)) { | ||
return new Error("Option files is not a JSON Object"); | ||
} | ||
if (isEmpty(files)) { | ||
return new Error("Option files should have at least one entry"); | ||
} | ||
let entries : [string, any][] = Object.entries(files); | ||
if (uniq(Object.values(files)).length !== entries.length) { | ||
return new Error(`At least a duplicated value in files JSON object was detected`); | ||
} | ||
return Promise.all( | ||
entries.map( | ||
entry => verify_files_entry(entry) | ||
) | ||
).then(_ => { | ||
// validated | ||
return true; | ||
}) | ||
.catch(/* istanbul ignore next */ err => { | ||
// failed | ||
return err; | ||
}); | ||
} | ||
export const FILES_CHECK = async (argv: any) => { | ||
let files = argv.files as any; | ||
if (!isPlainObject(files)) { | ||
return new Error('Option files is not a JSON Object'); | ||
} | ||
if (isEmpty(files)) { | ||
return new Error('Option files should have at least one entry'); | ||
} | ||
let entries: [string, any][] = Object.entries(files); | ||
if (uniq(Object.values(files)).length !== entries.length) { | ||
return new Error( | ||
`At least a duplicated value in files JSON object was detected` | ||
); | ||
} | ||
|
||
try { | ||
await Promise.all(entries.map((entry) => verify_files_entry(entry))); | ||
return true; | ||
} catch (error) { | ||
return error as Error; | ||
} | ||
}; | ||
|
||
// verify if an entry from files option meet requirements | ||
async function verify_files_entry([_, i18nPath] : [string, any]) : Promise<boolean | Error> { | ||
let potentialJSON; | ||
// check if file is readable | ||
try { | ||
await fs.promises.access(i18nPath); | ||
potentialJSON = await fs.promises.readFile(i18nPath); | ||
} catch (error) { | ||
return Promise.reject(`${i18nPath} cannot be read : check permissions`); | ||
} | ||
// check if the file is a JSON | ||
try { | ||
JSON.parse(potentialJSON.toString()); | ||
return Promise.resolve(true); | ||
} catch (error) { | ||
return Promise.reject(`${i18nPath} isn't a valid JSON`); | ||
} | ||
async function verify_files_entry([_, i18nPath]: [string, any]): Promise< | ||
boolean | Error | ||
> { | ||
let potentialJSON; | ||
// check if file is readable | ||
try { | ||
await fs.promises.access(i18nPath); | ||
potentialJSON = await fs.promises.readFile(i18nPath); | ||
} catch (error) { | ||
return Promise.reject( | ||
new Error(`${i18nPath} cannot be read : check permissions`) | ||
); | ||
} | ||
// check if the file is a JSON | ||
try { | ||
JSON.parse(potentialJSON.toString()); | ||
return Promise.resolve(true); | ||
} catch (error) { | ||
return Promise.reject(new Error(`${i18nPath} isn't a valid JSON`)); | ||
} | ||
} | ||
|
||
// export checks in expected order into a single array | ||
export const CHECKS = [FILENAME_CHECK, FILES_CHECK]; | ||
export const CHECKS = [FILENAME_CHECK, FILES_CHECK]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { COLUMNS_CHECK, COLUMNS_AND_FILES_CHECK } from './export_xlsx_checks'; | ||
|
||
// export checks in expected order into a single array | ||
export const CHECKS = [COLUMNS_CHECK, COLUMNS_AND_FILES_CHECK]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
// common check for this command | ||
export * from "./export_common_checks"; | ||
export * from './export_common_checks'; | ||
|
||
// check for xlsx sub command | ||
export * as XLSX from "./export_xlsx_checks"; | ||
export * as XLSX from './export_xlsx_checks'; | ||
|
||
// check for csv sub command | ||
// as it is identical (at that time) to xlsx, simply re-export same module | ||
export * as CSV from "./export_xlsx_checks"; | ||
export * as CSV from './export_csv_checks'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
// common check for this command | ||
export * from "./import_common_checks"; | ||
export * from './import_common_checks'; | ||
|
||
// check for xlsx sub command | ||
export * as XLSX from "./import_xlsx_checks"; | ||
export * as XLSX from './import_xlsx_checks'; | ||
|
||
// check for csv sub command | ||
// as it is identical (at that time) to xlsx, simply re-export same module | ||
export * as CSV from "./import_xlsx_checks"; | ||
export * as CSV from './import_xlsx_checks'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
// re export stuff for easier import | ||
export * as EXPORT_CHECKS from "./export/index"; | ||
export * as IMPORT_CHECKS from "./import/index"; | ||
export * as DIFF_CHECKS from "./diff_checks"; | ||
export * as EXPORT_CHECKS from './export/index'; | ||
export * as IMPORT_CHECKS from './import/index'; | ||
export * as DIFF_CHECKS from './diff_checks'; | ||
|
||
// Yargs parser doesn't stop when issue(s) occurs and only returns last error. | ||
// So I need something that resolves promises sequentially and return first error | ||
// See https://github.com/yargs/yargs/issues/1399 | ||
// See https://github.com/yargs/yargs/issues/1975 | ||
|
||
type PromiseCheck = (argv : any) => Promise<boolean | Error>; | ||
type PromiseCheck = (argv: any) => Promise<boolean | Error>; | ||
|
||
export const resolveChecksInOrder = (checks : PromiseCheck[]) => { | ||
return async (argv : any) => { | ||
for(let check of checks) { | ||
try { | ||
//console.log(`Check ${check.name}`); // to make easier debugging in the future | ||
let result = await check(argv); | ||
if (result !== true) { | ||
return result; | ||
} | ||
} catch (error) { | ||
/* istanbul ignore next */ | ||
return error; | ||
} | ||
export const resolveChecksInOrder = (checks: PromiseCheck[]) => { | ||
return async (argv: any) => { | ||
for (let check of checks) { | ||
try { | ||
//console.log(`Check ${check.name}`); // to make easier debugging in the future | ||
let result = await check(argv); | ||
if (result !== true) { | ||
return result; | ||
} | ||
return true; | ||
} catch (error) { | ||
/* istanbul ignore next */ | ||
return error; | ||
} | ||
} | ||
} | ||
return true; | ||
}; | ||
}; |
Oops, something went wrong.