Skip to content

Commit

Permalink
[#40] fix: stop providing file information in the core library (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelmmiguel authored Feb 15, 2022
1 parent 9cf075f commit 5dabc5a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 41 deletions.
21 changes: 17 additions & 4 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

// Requires (Native)
const fs = require('fs');
const path = require('path');
// External
const filesize = require('filesize');
const { program, Option } = require('commander');
const colors = require('colors/safe');
// Project
Expand Down Expand Up @@ -62,22 +64,33 @@ const app = (filePath, opts) => {

dataPromise
.then((data) => {
let svg = new SVG(data, filePath);
let svg = new SVG(data);
let output;
let file;

if(filePath && !process.stdin.isTTY) {
commander.dualInputs = true;
};

if(filePath) {
let fileStats = fs.statSync(__dirname, filePath);

file = {
name: path.resolve(filePath),
path: path.basename(filePath),
size: filesize(fileStats.size)
}
}

switch (opts.output) {
case 'json':
output = jsonFormatter(svg, opts);
output = jsonFormatter(svg, file, opts);
break;
case 'yaml':
output = yamlFormatter(svg, opts);
output = yamlFormatter(svg, file, opts);
break;
default:
output = humanFormatter(svg, opts);
output = humanFormatter(svg, file, opts);
}

// Display the output
Expand Down
11 changes: 6 additions & 5 deletions lib/formatters/human.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ const treeLegend = () => {
* Format the result of the CLI.
*
* @param {SVG} svg SVG Instance
* @param {object} file Provides information about the file. Undefined for STDIN
* @param {object} options Options for the formatter from the ARGV
* @return {string} Information of the SVG as a string ready for
* display in the console.
*/
const format = (svg, options) => {
const format = (svg, file, options) => {
let output = '';
let report = svg.report(options.allStats);
let all = (!options.tree && !options.stats && !options.basic);
Expand All @@ -120,11 +121,11 @@ const format = (svg, options) => {
if (all || options.basic) {
output += title('Basic information');
let table = new Table();
if (report.file && !options.dualInputs) {
if (file && !options.dualInputs) {
table.push(
['Name', report.file.name],
['Path', report.file.path],
['Size', report.file.size]
['Name', file.name],
['Path', file.path],
['Size', file.size]
);
} else {
table.push(
Expand Down
5 changes: 3 additions & 2 deletions lib/formatters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
* Format the result of the CLI.
*
* @param {SVG} svg SVG Instance
* @param {object} file Provides information about the file. Undefined for STDIN
* @param {object} options Options for the formatter from the ARGV
* @return {String} JSON object as a string ready for display in the console.
*/
const format = (svg, options) => {
const format = (svg, file, options) => {
let output = {};
let report = svg.report(options.allStats);
let all = (!options.tree && !options.stats && !options.basic);

if (all || options.basic) {
output.file = report.file ? report.file : {
output.file = file ? file : {
name: 'stdin',
};
}
Expand Down
5 changes: 3 additions & 2 deletions lib/formatters/yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ const YAML = require('js-yaml');
* Format the result of the CLI.
*
* @param {SVG} svg SVG Instance
* @param {object} file Provides information about the file. Undefined for STDIN
* @param {object} options Options for the formatter from the ARGV
* @return {String} YAML object as a string ready for display in the console.
*/
const format = (svg, options) => {
const format = (svg, file, options) => {
let output = {};
let report = svg.report(options.allStats);
let all = (!options.tree && !options.stats && !options.basic);

if (all || options.basic) {
output.file = report.file ? report.file : {
output.file = file ? file : {
name: 'stdin',
};
}
Expand Down
29 changes: 1 addition & 28 deletions lib/svg.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Native
const path = require('path');
const fs = require('fs');
// External
const xml2js = require('xml2js');
const filesize = require('filesize');
// Project
const Node = require('./node');
const {allCategories, allTypes} = require('./utils/nodeTypes');
Expand All @@ -19,9 +15,8 @@ class SVG extends Node {
* Data of the node.
*
* @param {string} svgString A string with the content of the SVG file
* @param {string} filePath The path of the current file
*/
constructor(svgString, filePath = '') {
constructor(svgString) {
// Output
let error;
let data;
Expand All @@ -43,19 +38,6 @@ class SVG extends Node {

// Base properties
super('svg', data.svg);

if (filePath !== '') {
// Store additional data
let fileStats = fs.statSync(__dirname, filePath);

// Store data
this.filePath = path.resolve(filePath);
this.fileName = path.basename(filePath);
this.fileSize = filesize(fileStats.size);
this.fileInfo = true;
} else {
this.fileInfo = false;
}
}

/**
Expand Down Expand Up @@ -105,15 +87,6 @@ class SVG extends Node {
nodes: super.report(),
};

// Only add the file information if it's available
if (this.fileInfo) {
result.file = {
name: this.fileName,
path: this.filePath,
size: this.fileSize,
};
}

return result;
}
}
Expand Down

0 comments on commit 5dabc5a

Please sign in to comment.