Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Improve console output
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Sep 6, 2018
1 parent a046185 commit 30ec92e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Lyo](https://i.imgur.com/nt5bYNJ.png)

> Node.js to browser. The easy way
> Node.js to browser - The easy way
[![Npm version](https://runkit.io/bokub/npm-version/branches/master/lyo)](https://npmjs.com/package/lyo)
[![Build status](https://badgen.net/travis/bokub/lyo)](https://travis-ci.org/bokub/lyo)
Expand Down Expand Up @@ -34,9 +34,9 @@ $ lyo --help
$ lyo usage [options] Show how to use the output file
Options
--input -i Entry file
--output -o Output file / folder
--name -n Module name in browser
--input -i Entry file
--output -o Output file / folder
--name -n Module name in browser
Examples
$ lyo
Expand Down
6 changes: 3 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const cli = meow(`
$ lyo usage [options] Show how to use the output file
Options
--input -i Entry file
--output -o Output file / folder
--name -n Module name in browser
--input -i Entry file
--output -o Output file / folder
--name -n Module name in browser
Examples
$ lyo
Expand Down
31 changes: 30 additions & 1 deletion lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,65 @@
const fs = require('fs');
const editJsonFile = require('edit-json-file');
const detectIndent = require('detect-indent');
const ora = require('ora');

function init(flags) {
const pkg = fs.readFileSync('package.json', 'utf8');
const indent = detectIndent(pkg).indent || ' ';
const file = editJsonFile('package.json', {stringify_width: indent}); // eslint-disable-line camelcase
const done = {script: false, dependency: false, config: false};

if (Object.keys(file.get('')).length === 0) {
throw new Error('error reading package.json');
ora().fail('Error reading package.json');
process.exit(1);
}

// Set prePublish script
const prePub = file.get('scripts.prepublishOnly');
if (!prePub) {
file.set('scripts.prepublishOnly', 'lyo');
done.script = 'Created';
} else if (prePub.indexOf('lyo') === -1) {
file.set('scripts.prepublishOnly', `lyo && ${prePub}`);
done.script = 'Edited';
}

// Set dependencies
if (!file.get('devDependencies.lyo') && !file.get('dependencies.lyo')) {
file.set('devDependencies.lyo', 'latest');
done.dependency = true;
}

// Set config
const previousConf = JSON.stringify(file.get('lyo'));
file.unset('lyo');
for (const f in flags) {
if (f.length > 1 && flags[f]) {
file.set(`lyo.${f}`, flags[f]);
}
}
if (JSON.stringify(file.get('lyo')) !== previousConf) {
done.config = true;
}

const successMessages = {
dependency: 'Added lyo to the dev dependencies',
script: done.script + ' "prepublishOnly" script',
config: 'Edited default lyo config'
};

let hasDifferences = false;
for (const key in successMessages) {
if (done[key]) {
hasDifferences = true;
ora().succeed(successMessages[key]);
}
}

if (!hasDifferences) {
ora().warn('Lyo is already configured, nothing has changed');
return;
}

file.save();
}
Expand Down
34 changes: 21 additions & 13 deletions lib/task.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
#! /usr/bin/env node
'use strict';

const chalk = require('chalk');
const getStream = require('get-stream');
const ora = require('ora');
const prettyMs = require('pretty-ms');

const browserify = require('browserify');
const babel = require('@babel/core');
const babelEnvPreset = require('@babel/preset-env');
const uglifyJS = require('uglify-js');

const runBrowserify = opts => new Promise((resolve, reject) => {
const spinner = ora('Browserify').start();
const durationSince = startTime => chalk.blue(prettyMs(new Date().getTime() - startTime));

const runTask = (name, task) => new Promise((resolve, reject) => {
const startTime = new Date().getTime();
const spinner = ora(name).start();
const succeed = result => {
spinner.succeed(`${name} ${durationSince(startTime)}`);
resolve(result);
};
const fail = err => {
spinner.fail();
reject(err);
};
task(succeed, fail);
});

const runBrowserify = opts => runTask('Browserify', (resolve, reject) => {
const bundle = browserify([opts.input], {
standalone: opts.name
}).bundle();

getStream(bundle).then(result => {
spinner.succeed();
resolve(result);
}).catch(err => {
spinner.fail();
reject(err.message);
});
});

const runBabel = (code, opts) => new Promise((resolve, reject) => {
const spinner = ora('Babel').start();
const runBabel = (code, opts) => runTask('Babel', (resolve, reject) => {
const configItem = babel.createConfigItem(babelEnvPreset);
opts = {presets: [configItem]};

babel.transformAsync(code, opts).then(result => {
spinner.succeed();
resolve(result.code);
}).catch(err => {
spinner.fail();
reject(err);
});
});

const runUglify = code => new Promise((resolve, reject) => {
const spinner = ora('Uglify').start();
const runUglify = code => runTask('Uglify', (resolve, reject) => {
const result = uglifyJS.minify(code, {});
if (result.error) {
spinner.fail();
reject(result.error);
return;
}

spinner.succeed();
resolve(result.code);
});

Expand Down
6 changes: 2 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lyo",
"description": "Node.js to browser. The easy way",
"version": "0.4.1",
"description": "Node.js to browser - The easy way",
"version": "0.4.2",
"author": "Boris K",
"bin": {
"lyo": "bin/cli.js"
Expand All @@ -20,6 +20,7 @@
"meow": "^5.0.0",
"mkdirp": "^0.5.1",
"ora": "^3.0.0",
"pretty-ms": "^3.2.0",
"uglify-js": "^3.4.8"
},
"devDependencies": {
Expand Down

0 comments on commit 30ec92e

Please sign in to comment.