From a7178f38fefbeb5891703598534d7e6d0ca161ec Mon Sep 17 00:00:00 2001 From: Dennis Brown Date: Sat, 13 May 2017 18:05:38 -0500 Subject: [PATCH] feat(build options): silent building Additional option to silence builds, clean up wild test logging, and code cleanup --- README.md | 52 +++++++++++++++++++++++++++++------------- app/index.js | 60 +++++++++++++++++-------------------------------- example/test.js | 5 +++-- 3 files changed, 60 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 4d276e3..2e0a799 100644 --- a/README.md +++ b/README.md @@ -5,19 +5,46 @@ [![Build Status](https://travis-ci.org/3DEsprit/node-offline-api.svg?branch=master)](https://travis-ci.org/3DEsprit/node-offline-api) [![codecov](https://codecov.io/gh/3DEsprit/node-offline-api/branch/master/graph/badge.svg?style=flat-square)](https://codecov.io/gh/3DEsprit/node-offline-api) [![dependencies](https://img.shields.io/david/expressjs/express.svg?style=flat-square)](https://codecov.io/gh/3DEsprit/node-offline-api) -[![dev-dependencies](https://img.shields.io/david/dev/expressjs/express.svg?style=flat-square)](https://codecov.io/gh/3DEsprit/node-offline-api) -[![license](https://img.shields.io/github/license/mashape/apistatus.svg)]() -The Node.js Offline Document API (NODe API) module allows anyone to create an offline version of the Node.js documentation. + +The Node.js Offline Document API (NODe API) module allows anyone to create an offline version of the Node.js documentation for any version of Node.js. ## Requirements -This module has been created to work with Node.js versions greater than 4.0.0, but many older versions will work, but it has not been tested or created to support these older versions. +While you can create documentation for any version of Node.js, this module has been created versions greater than 4.0.0 in mind. Since it has been created with ES5 for compatibility, older versions will work, but it has not been tested or created to support these older versions. + + +### Installation + +You can clone this repository, or install via NPM. + +*Via Source* + +``` +$ git clone https://github.com/3DEsprit/node-offline-api.git + +$ npm i +``` + +*Via NPM* + +``` +$ npm i node-offline-api +``` + +_or_ + +``` +$ yarn add node-offline-api +``` + ## Usage There are two different ways to use the NODe API, through the CLI, and as an added module to your Node.js applications. + + ### As a module in your JavaScript code To use the NODe API in your own code, all that is needed is to require the module's createDoc method, and call it. @@ -42,21 +69,16 @@ buildOptions.version: '4.4.0'; createDocs(); ``` -### As a Script - -You can also use NODe as your own script simply to pull and create Node.js documentation. - -After cloning the repository: https://github.com/3DEsprit/node-offline-api.git +By default the build can be quite verbose when creating the documents. If you would prefer that the builds be silent, the buildQuiet option is offered allowing the build to complete silently, except when actual errors occur. ``` -$ npm i +buildOptions.buildQuiet: true ``` -_or_ -``` -$ yarn install -``` +### As a Script + +You can also use NODe as your own script simply to pull and create Node.js documentation. ``` $ npm start @@ -84,6 +106,6 @@ $ node app/ -f [folder] ## License -The Node Offline Doucmentation API is under the MIT license. +The Node Offline Doucmentation API has been written with the [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](). The Node.js Document API is property of the [Node.js Project](https://github.com/nodejs/node). [Node.js License](https://github.com/nodejs/node/blob/master/LICENSE) \ No newline at end of file diff --git a/app/index.js b/app/index.js index 08ae95e..c13fe5f 100644 --- a/app/index.js +++ b/app/index.js @@ -26,31 +26,25 @@ var child; var buildOptions = { buildDir: process.cwd(), buildVersion: process.version.slice(1), - buildName: 'node-documents' + buildName: 'node-documents', + buildQuiet: false, } var options = process.argv.slice(2); -console.log(options); // check for command line scripting options if(options[0] !== []) { options.forEach(function(ops, index) { - console.log(ops); if (ops.startsWith('-v')) { - var version = options[index + 1]; - buildOptions.buildVersion = version; + buildOptions.buildVersion = options[index + 1]; flags[3] = '--node-version=' + version; } else if (ops.startsWith('-f')) { buildDir = options[index + 1]; - if (buildDir.slice(-1) !== docEnd) { - buildDir += docEnd; - } + if (buildDir.slice(-1) !== docEnd) { buildDir += docEnd; } buildOptions.buildDir = buildDir + buildOptions.buildName; - console.log('Custom build dir: ', buildOptions.buildDir); - } - if (index === options.length - 1) { - checkOptions(); + if (!buildOptions.buildQuiet) console.log('Custom build dir: ', buildOptions.buildDir); } + if (index === options.length - 1) { checkOptions(); } }); } @@ -60,36 +54,32 @@ function convertFile(file) { count++; flags[2] = apiDir + file; var filename = file.slice(0,-3); - console.log('Creating doc ' + filename); + if (!buildOptions.buildQuiet) console.log('Creating doc ' + filename); var pathname = buildOptions.buildDir + filename + '.html'; var fileStream = fs.createWriteStream(pathname); var createFile = spawn('node', flags); createFile.stdout.pipe(fileStream); createFile.on('close', (code) => { - if (code !== 0) { - console.log('NODe API: process exited with code ' + code); - } + if (code !== 0) { if (!buildOptions.buildQuiet) console.log('NODe API: process exited with code ' + code); } }); - createFile.on('error', (err) => console.log('Error writing: ', err)); + createFile.on('error', (err) => console.error('Error writing: ', err)); } // grab template file and assets function checkFiles() { fs.readdir(apiDir, function(err, res) { var ext = path.extname('*.md'); - if (err) console.error(err.stack); + if (err) { console.error(err.stack); } res.forEach((file) => { - if (path.extname(file) === ext) { - convertFile(file); - } + if (path.extname(file) === ext) { convertFile(file); } }); }); } function copyAssets() { fsx.copy(apiAssets, buildOptions.buildDir + 'assets', function(err) { - if (err) return console.error('copy', err); - console.log('NODe: asset copy success'); + if (err) { return console.error('copy', err); } + if (!buildOptions.buildQuiet) { console.log('NODe: asset copy success'); } }); checkFiles(); } @@ -98,14 +88,12 @@ function copyAssets() { function checkFolders() { fs.stat(buildOptions.buildDir, function(err, out) { if (out) { - console.log('NODe API: build folder exists'); + if (!buildOptions.buildQuiet) console.log('NODe API: build folder exists'); fs.stat(buildOptions.buildDir + 'assets', function(err, out) { - if (err) { - copyAssets(); - } + if (err) { copyAssets(); } }); } else { - console.log('NODe API: creating build folder'); + if (!buildOptions.buildQuiet) console.log('NODe API: creating build folder'); fs.mkdirSync(buildOptions.buildDir); copyAssets(); } @@ -113,19 +101,11 @@ function checkFolders() { } function checkOptions() { - console.log(buildOptions.buildVersion); - var version = buildOptions.buildVersion; - flags[3] = '--node-version=' + version; - - if(buildOptions.buildDir.slice(0,1)) { - buildOptions.buildDir = buildOptions.buildDir.replace(/^~/, os.homedir); - } - - if (buildOptions.buildDir.slice(-1) !== docEnd) { - buildOptions.buildDir += docEnd; - } + flags[3] = '--node-version=' + buildOptions.buildVersion; + if(buildOptions.buildDir.slice(0,1)) { buildOptions.buildDir = buildOptions.buildDir.replace(/^~/, os.homedir); } + if (buildOptions.buildDir.slice(-1) !== docEnd) { buildOptions.buildDir += docEnd; } buildOptions.buildDir += buildOptions.buildName + docEnd; - console.log('NODe API: Building to Directory: %s', buildOptions.buildDir); + if (!buildOptions.buildQuiet) { console.log('NODe API: Building to Directory: %s', buildOptions.buildDir); } checkFolders(); } diff --git a/example/test.js b/example/test.js index 59cff7c..b4a16a8 100644 --- a/example/test.js +++ b/example/test.js @@ -9,7 +9,8 @@ var buildOptions = require('../app/index.js').buildOptions; // buildOptions.buildDir = '~\\My Documents' // Test version change -buildOptions.buildVersion = '4.4.0' -buildOptions.buildName = 'apidoc' +buildOptions.buildVersion = '4.4.0'; +buildOptions.buildName = 'apidoc'; +buildOptions.buildQuiet = true; createDocs(); \ No newline at end of file