Skip to content

Commit

Permalink
refactor: rename file name
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Sep 8, 2020
1 parent 89091bc commit 354cad2
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 53 deletions.
12 changes: 0 additions & 12 deletions .github/FUNDING.yml

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "limit-size",
"version": "0.1.1",
"version": "0.1.2",
"description": "Lightweight, Convenient, Fast command tool to control your file size, size-limit is too bloated.",
"license": "MIT",
"bin": {
Expand Down
4 changes: 2 additions & 2 deletions src/parse-size.ts → src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function unitToRate(unit: string) {
* 解析 size string
* @param sizeString
*/
export function parseSizeString(sizeString: string): number {
export function parse(sizeString: string): number {
const match = sizeString.match(/^(\d*\.*\d*)\s*([gGkKmM]{0,1}[bB]{0,1})$/);
if (!match) {
throw new Error(`file size string '${sizeString}' syntax error, e.g. 100 b, 1.2 Kb, 2 Mb, 20.5 Gb!`);
Expand All @@ -25,7 +25,7 @@ export function parseSizeString(sizeString: string): number {
* @param bytes
* @param fixed
*/
export function formatSize(bytes: number, fixed = 1): string {
export function format(bytes: number, fixed = 1): string {
bytes = Math.abs(bytes);

const { radix, unit } = JEDEC;
Expand Down
31 changes: 31 additions & 0 deletions src/cmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import chalk from 'chalk';
import program from 'commander';
import { parseConfig } from './config';
import { lint } from './lint';
import { logResust } from './log';

program
.version('0.1.2', '-v, --version')
.usage('<lint-md> [options]')
.description('limit size for your files.')
.option('-c, --config [configure-file]', 'default .limit-size.json')
.action(async (cmd) => {
// 1. parse config
const config = parseConfig(cmd.config);
// 2. lint size limit
const result = await lint(config);
// 3. log
logResust(result);
// 4. help doc
const success = result.every((r) => r.passed);
if (!success) {
console.log(
chalk.bold.yellow('Try to reduce size or increase limit in "limit-size" section of package.json'),
'\n',
);
}
// 5. exit
process.exit(success ? 0 : 1);
});

program.parse(process.argv);
32 changes: 1 addition & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
#!/usr/bin/env node

import chalk from 'chalk';
import program from 'commander';
import { parseConfig } from './config';
import { lint } from './lint';
import { logResust } from './log';

program
.version('0.1.1', '-v, --version')
.usage('<lint-md> [options]')
.description('limit size for your files.')
.option('-c, --config [configure-file]', 'default .limit-size.json')
.action(async (cmd) => {
// 1. parse config
const config = parseConfig(cmd.config);
// 2. lint size limit
const result = await lint(config);
// 3. log
logResust(result);
// 4. help doc
const success = result.every((r) => r.passed);
if (!success) {
console.log(
chalk.bold.yellow('Try to reduce size or increase limit in "limit-size" section of package.json'),
'\n',
);
}
// 5. exit
process.exit(success ? 0 : 1);
});

program.parse(process.argv);
import './cmd';
8 changes: 4 additions & 4 deletions src/lint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Config, SingleConfig, Result } from './types';
import { fileSize } from './file-size';
import { parseSizeString } from './parse-size';
import { Config, Result } from './types';
import { fileSize } from './size';
import { parse } from './bytes';

/**
* 做检查,并返回检查结果
Expand All @@ -11,7 +11,7 @@ export async function lint(config: Config): Promise<Result> {
for (const c of config) {
const { path, limit, gzip } = c;
const bytes = await fileSize(path, gzip);
const limitBytes = parseSizeString(limit);
const limitBytes = parse(limit);

result.push({
config: c,
Expand Down
6 changes: 3 additions & 3 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk';
import { Result, SingleResult } from './types';
import { formatSize } from './parse-size';
import { format } from './bytes';
/**
dist/g2plot.min.js
Expand All @@ -23,14 +23,14 @@ export function logResust(result: Result) {
'\n',
...(passed
? []
: [' ', chalk.red(`Package size limit has exceeded by ${chalk.bold(formatSize(bytes - limitBytes))}`), '\n']),
: [' ', chalk.red(`Package size limit has exceeded by ${chalk.bold(format(bytes - limitBytes))}`), '\n']),
' ',
'Size limit: ',
passed ? chalk.bold.green(limit) : chalk.bold.red(limit),
'\n',
' ',
'Size: ',
passed ? chalk.bold.green(formatSize(bytes)) : chalk.bold.red(formatSize(bytes)),
passed ? chalk.bold.green(format(bytes)) : chalk.bold.red(format(bytes)),
'\n',
];

Expand Down
File renamed without changes.

0 comments on commit 354cad2

Please sign in to comment.