Skip to content

Commit

Permalink
feat(module use): update use to single function
Browse files Browse the repository at this point in the history
reduced exports to a single function createDocs, and allowing the build options to be passed as
parameters.

moving to a single function breaks any use-cases of previous versions, since the module is imported
and used very differently.
  • Loading branch information
MutableLoss committed Mar 11, 2018
1 parent 5564aa4 commit bd1cff8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 62 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ branches:
notifications:
email: false
node_js:
- '9'
- '7'
- '6'
- '4'
Expand Down
63 changes: 32 additions & 31 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var fs = require('fs');
// var fs = require('fs');
var path = require('path');
var os = require('os');
var fsx = require('fs-extra');
var fs = require('fs-extra');
var arch = os.platform();

var spawn = require('child_process').spawn;
Expand All @@ -23,15 +23,18 @@ var count = 0;

var child;

var buildOptions = {
buildDir: process.cwd(),
buildVersion: process.version.slice(1),
buildName: 'node-documents',
updateApi: true
function updateOptions(args) {
var buildOptions = {
buildDir: args[0] || process.cwd(),
buildName: args[1] || 'node-documents',
buildVersion: args[2] || process.version.slice(1),
updateApi: args[3] || true
}
checkOptions(buildOptions);
}

// Check for modular options
function convertFile(file) {
function convertFile(file, buildOptions) {
// take file and generate html file
count++;
flags[2] = apiDir + file;
Expand All @@ -44,54 +47,52 @@ function convertFile(file) {
}

// grab template file and assets
function checkFiles() {
function checkFiles(buildOptions) {
var ext = path.extname('*.md');
fs.readdir(apiDir, function(err, res) {
var ext = path.extname('*.md');
if (err) { console.error(err.stack); }
res.forEach((file) => {
if (path.extname(file) === ext) { convertFile(file); }
if (path.extname(file) === ext) { convertFile(file, buildOptions); }
});
});
}

// copy document assets
function copyAssets() {
fsx.copy(apiAssets, buildOptions.buildDir + 'assets', function(err) {
// Quiet this error since it will show if assets already exist
if (err) { return console.log('NODe: Error copying assets:', err); }
});
checkFiles();
function copyAssets(buildOptions) {
fs.mkdir(buildOptions.buildDir + 'assets', function (err, out) {
fs.copy(apiAssets, buildOptions.buildDir + 'assets', function(err) {
if (err) { return console.error('NODe: Error copying assets:', err); }
// Quiet this error since it will show if assets already exist
});
checkFiles(buildOptions);
})
}

// create document directories
function checkFolders() {
function checkFolders(buildOptions) {
fs.stat(buildOptions.buildDir, function(err, out) {
if (out) {
fs.stat(buildOptions.buildDir + 'assets', function(err, out) {
if (err || buildOptions.updateApi) { copyAssets(); }
fs.stat(buildOptions.buildDir + 'assets', function(err, out) {
if (err || buildOptions.updateApi) { copyAssets(buildOptions); }
});
} else {
fs.mkdir(buildOptions.buildDir, (err, out) => {
copyAssets();
fs.mkdir(buildOptions.buildDir, function(err, out) {
copyAssets(buildOptions);
});
}
});
}

function checkOptions() {
function checkOptions(buildOptions) {
flags[3] = '--node-version=' + buildOptions.buildVersion;
if(buildOptions.buildDir.slice(0,1)) { buildOptions.buildDir = buildOptions.buildDir.replace(/^~/, os.homedir); }
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;
checkFolders();
checkFolders(buildOptions);
}

function createDocs() {
checkOptions();
updateOptions(arguments)
}

module.exports = {
createDocs: createDocs,
buildOptions: buildOptions,
documentPath: buildOptions.buildDir
}
module.exports = createDocs
16 changes: 4 additions & 12 deletions example/test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
var createDocs = require('../app/index.js').createDocs;
var buildOptions = require('../app/index.js').buildOptions;
var createDocs = require('../app/index.js');

// Test on OSX and Linux
// buildOptions.buildDir = '~/'
// createDocs('~/')

// Test on Windows
// '~' converts to home directory in Windows too
// buildOptions.buildDir = '~\\My Documents'
// createDocs('~\\My Documents')

// Test version change
buildOptions.buildVersion = '4.4.0';
buildOptions.buildName = 'apidoc';

// Use this to overwrite existing API documents
// buildOptions.updateApi = true;

createDocs();
createDocs('./', 'api-docs', '9.2.1');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"node": ">=4.0.0"
},
"dependencies": {
"fs-extra": "3.0.1",
"fs-extra": "5.0.0",
"js-yaml": "3.8.3",
"jsdoc": "3.4.3",
"marked": "0.3.9"
Expand Down
27 changes: 16 additions & 11 deletions test/module.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
var expect = require('chai').expect;
var fs = require('fs');
var fsx = require('fs-extra');
var buildOptions = require('../app/index.js').buildOptions;

buildOptions.buildDir = process.cwd();
buildOptions.buildName = 'node-docs';
var createDocs = require('../app/index');

describe('node-offline-api', () => {
it('should create files', () => {
var createDocs = require('../app/index').createDocs;
createDocs();
const build = fs.stat(`${buildOptions.buildDir}`, (err, out) => {
it('should create files', async () => {
const docs = await createDocs()

const build = await fs.stat('node-documents', (err, out) => {
expect(build).to.satisfy;
expect(out).to.satisfy;
});
})
it('should change options', () => {
buildOptions.version = '4.4.0';

expect(buildOptions.version).to.equal('4.4.0');
it('should change options from function', async () => {
let targetDir = process.cwd()
let version = '9.2.1'
let folderName = 'test2'
const docs = await createDocs(targetDir, folderName, version, false)

const build = await fs.stat(targetDir + folderName, (err, out) => {
expect(err).not.toBe('ENONT')
expect(build).to.satisfy;
expect(out).to.satisfy;
});
})
})
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -806,12 +806,12 @@ fs-exists-sync@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/fs-exists-sync/-/fs-exists-sync-0.1.0.tgz#982d6893af918e72d08dec9e8673ff2b5a8d6add"

fs-extra@3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-3.0.1.tgz#3794f378c58b342ea7dbbb23095109c4b3b62291"
fs-extra@5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-5.0.0.tgz#414d0110cdd06705734d055652c5411260c31abd"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^3.0.0"
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-extra@^1.0.0:
Expand Down Expand Up @@ -1325,9 +1325,9 @@ jsonfile@^2.1.0:
optionalDependencies:
graceful-fs "^4.1.6"

jsonfile@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-3.0.0.tgz#92e7c7444e5ffd5fa32e6a9ae8b85034df8347d0"
jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
optionalDependencies:
graceful-fs "^4.1.6"

Expand Down

0 comments on commit bd1cff8

Please sign in to comment.