-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
737 additions
and
312 deletions.
There are no files selected for viewing
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,3 +1,205 @@ | ||
export * from "./src/cfwf.ts"; | ||
export * from "./src/table.ts"; | ||
export * from "./src/types.ts"; | ||
|
||
import { CFWFDataset } from "./src/types.ts"; | ||
import { version } from "./src/version.ts"; | ||
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/mod.ts"; | ||
import { existsSync } from "https://deno.land/std@0.205.0/fs/exists.ts"; | ||
import { parseCSV } from "./src/utils.ts"; | ||
import { CFWF } from "./src/cfwf.ts"; | ||
|
||
// deno-lint-ignore no-explicit-any | ||
function initDatasetFile(options: any): void { | ||
const cfwf: CFWFDataset = { | ||
dataset: { | ||
metadatas: {}, | ||
}, | ||
tables: {}, | ||
}; | ||
|
||
Deno.writeTextFileSync(options.configname, JSON.stringify(cfwf)); | ||
} | ||
|
||
function readConfig(configname: string): CFWFDataset { | ||
if (existsSync(configname) === false) { | ||
return {}; | ||
} | ||
|
||
return JSON.parse(Deno.readTextFileSync(configname)); | ||
} | ||
|
||
// deno-lint-ignore no-explicit-any | ||
async function saveCFWF(options: any): Promise<void> { | ||
const config = readConfig(options.configname); | ||
|
||
const cfwf = new CFWF(config); | ||
console.log(await cfwf.toCFWF(options.saveto)); | ||
} | ||
|
||
// deno-lint-ignore no-explicit-any | ||
function setDatasetProperties(options: any): void { | ||
const cfwf: CFWFDataset = readConfig(options.configname) || {}; | ||
cfwf.dataset = cfwf.dataset || {}; | ||
cfwf.dataset.metadatas = cfwf.dataset.metadatas || {}; | ||
|
||
if (options.title) { | ||
cfwf.dataset.title = options.title; | ||
cfwf.dataset.metadatas.font = options.font || "doom"; | ||
cfwf.dataset.metadatas.removetitlelines = options.removetitlelines || 2; | ||
} | ||
if (options.description) { | ||
cfwf.dataset.description = options.description; | ||
} | ||
if (options.generatedtitle) { | ||
cfwf.dataset.generatedtitle = options.generatedtitle; | ||
} | ||
|
||
if (options.metadatas) { | ||
Object.assign(cfwf.dataset.metadatas, JSON.parse(options.metadatas)); | ||
} | ||
|
||
Deno.writeTextFileSync(options.configname, JSON.stringify(cfwf)); | ||
} | ||
|
||
// deno-lint-ignore no-explicit-any | ||
function addTable(options: any): void { | ||
const cfwf: CFWFDataset = readConfig(options.configname) || {}; | ||
cfwf.tables = cfwf.tables || {}; | ||
|
||
if (!options.tablename) { | ||
throw new Error("No table name"); | ||
} | ||
|
||
const table = cfwf.tables[options.tablename] || { | ||
columns: [], | ||
rows: [], | ||
}; | ||
|
||
table.columns = table.columns || []; | ||
table.rows = table.rows || []; | ||
table.metadatas = table.metadatas || {}; | ||
|
||
if (options.columns) { | ||
table.columns = options.columns; | ||
} | ||
|
||
if (options.filename) { | ||
const content = Deno.readTextFileSync(options.filename); | ||
const datas = parseCSV(content); | ||
|
||
if (table.columns && table.columns.length === 0) { | ||
table.columns = datas.columns; | ||
} | ||
table.rows = datas.rows; | ||
} | ||
|
||
if (options.subtitle) { | ||
table.subtitle = options.subtitle; | ||
} | ||
|
||
if (options.description) { | ||
table.description = options.description; | ||
} | ||
|
||
if (options.metadatas) { | ||
Object.assign( | ||
// deno-lint-ignore no-explicit-any | ||
table.metadatas as Record<string, any>, | ||
JSON.parse(options.metadatas), | ||
); | ||
} | ||
|
||
if (options.aligns) { | ||
table.metadatas.aligns = options.aligns; | ||
} | ||
|
||
cfwf.tables[options.tablename] = table; | ||
|
||
Deno.writeTextFileSync(options.configname, JSON.stringify(cfwf)); | ||
} | ||
|
||
if (import.meta.main) { | ||
const cmdtable = new Command() | ||
.description("Configure table") | ||
// Add table | ||
.command("add", "Add table configuration") | ||
.option("-c, --configname <configname:file>", "Config filenme", { | ||
required: true, | ||
}) | ||
.option("-f, --filename <filename:file>", "File to be load", { | ||
required: true, | ||
}) | ||
.option("-t, --tablename <tablename:string>", "Define table name", { | ||
required: true, | ||
}) | ||
.option("-s, --subtitle <subtitle:string>", "Define table subtitle", {}) | ||
.option("-l, --columns <column:string[]>", "Columns name", { default: [] }) | ||
.option("-a, --aligns <column:string[]>", "Aligns for each columns", { | ||
required: true, | ||
}) | ||
.option( | ||
"-d, --description <description:string>", | ||
"Define table description", | ||
{}, | ||
) | ||
.option( | ||
"-m, --metadatas <metadatas:string>", | ||
"Define dataset metadatas (JSON format)", | ||
{}, | ||
) | ||
.action((options) => addTable(options)); | ||
|
||
const cmddataset = new Command() | ||
.description("Configure dataset") | ||
.command("set", "Set dataset properties") | ||
.option("-c, --configname <configname:file>", "Config filenme", { | ||
required: true, | ||
}) | ||
.option("-f, --font <title:string>", "Define title font", { | ||
default: "doom", | ||
}) | ||
.option("-t, --title <title:string>", "Define dataset title", {}) | ||
.option( | ||
"-d, --description <title:string>", | ||
"Define dataset description", | ||
) | ||
.option( | ||
"-g, --generatedtitle <generated:string>", | ||
"Generated title", | ||
{ conflicts: ["title", "font"] }, | ||
) | ||
.option( | ||
"-m, --metadatas <metadatas:string>", | ||
"Define dataset metadatas (JSON format)", | ||
{}, | ||
) | ||
.action((options) => setDatasetProperties(options)) | ||
// Init | ||
.command("init", "Init dataset file") | ||
.option("-c, --configname <configname:file>", "Config filenme", { | ||
required: true, | ||
}) | ||
.action((options) => initDatasetFile(options)) | ||
// Save | ||
.command("save", "Save dataset to cfwf format file") | ||
.option("-c, --configname <configname:file>", "Config filenme", {}) | ||
.option("-s, --saveto <saveto:file>", "Save to filenme", { | ||
required: true, | ||
}) | ||
.action((options) => saveCFWF(options)); | ||
|
||
await new Command() | ||
.name("cfwf") | ||
.version(version) | ||
.description("CFWF Command line") | ||
.command( | ||
"dataset", | ||
cmddataset, | ||
) | ||
.command( | ||
"table", | ||
cmdtable, | ||
) | ||
.parse(Deno.args); | ||
} |
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
Oops, something went wrong.