Skip to content

Commit

Permalink
feat(build options): silent building
Browse files Browse the repository at this point in the history
Additional option to silence builds, clean up wild test logging, and code cleanup
  • Loading branch information
MutableLoss committed May 13, 2017
1 parent c7a7ce5 commit a7178f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
52 changes: 37 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
60 changes: 20 additions & 40 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
});
}

Expand All @@ -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();
}
Expand All @@ -98,34 +88,24 @@ 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();
}
});
}

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();
}

Expand Down
5 changes: 3 additions & 2 deletions example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit a7178f3

Please sign in to comment.