Skip to content

Commit

Permalink
Rewriting package docor in ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yu committed May 12, 2015
1 parent de628c9 commit 077d4b8
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 61 deletions.
21 changes: 1 addition & 20 deletions bin/cli
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var consoler = require('consoler');
var docor = require('../index');
var files = ['README.md', 'LICENSE', '.gitignore', '.npmignore'];

if (!checkPackage('package.json'))
return consoler.error('Docor.init(); `package.json` file not found');

files.forEach(function(file) {
docor.createFile(file, function(err) {
if (err)
return consoler.error(err);
consoler.success(file + ' created');
});
});

function checkPackage(file) {
return fs.existsSync(path.join(process.cwd(), file));
}
require('../dist/cli')()
36 changes: 36 additions & 0 deletions dist/cli.js

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

1 change: 1 addition & 0 deletions dist/cli.js.map

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

82 changes: 82 additions & 0 deletions dist/docor.js

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

1 change: 1 addition & 0 deletions dist/docor.js.map

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

26 changes: 26 additions & 0 deletions dist/render.js

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

1 change: 1 addition & 0 deletions dist/render.js.map

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

1 change: 0 additions & 1 deletion index.js

This file was deleted.

26 changes: 26 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'fs'
import path from 'path'
import consoler from 'consoler'
import docor from './docor'

const files = ['README.md', 'LICENSE', '.gitignore', '.npmignore']

let checkPackage = (file) => {
return fs.existsSync(
path.join(process.cwd(), file)
)
}

(() => {
if (!checkPackage('package.json'))
return consoler.error('Docor.init(); `package.json` file not found')

files.forEach((file) => {
docor.createFile(file, (err) => {
if (err)
return consoler.error(err)

consoler.success(file + ' created')
})
})
})()
66 changes: 34 additions & 32 deletions lib/docor.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
var fs = require('fs');
var path = require('path');
var render = require('./render');
var templates = path.resolve(__dirname, '../templates');
var sys = require(path.resolve(__dirname, '../package.json'));
import fs from 'fs'
import path from 'path'
import render from './render'

exports.createFile = createFile;
exports.fetchLicenses = fetchLicenses;
const sys = require(path.resolve(__dirname, '../package.json'));
const templates = path.resolve(__dirname, '../templates');

function fetchLicenses(license) {
export function fetchLicenses(license) {
if (!license)
return null;
return null

license = license.toString().toLowerCase();
license = license.toString().toLowerCase()

try {
return fs.readFileSync(
path.join(templates, 'licenses', license)
);
)
} catch (e) {
return null;
return null
}
}

function createFile(filename, callback) {
var cwd = process.cwd();
var pkg = require(path.join(cwd, 'package.json'));
export function createFile(filename, callback) {
var cwd = process.cwd()
var pkg = require(path.join(cwd, 'package.json'))

var locals = {};
locals.pkg = pkg;
locals.sys = sys;
locals.year = (new Date().getFullYear());
locals.apis = null;
var locals = {
pkg,
sys,
'year': new Date().getFullYear(),
'apis': null,
}

if (filename.indexOf('README') > -1 || filename.indexOf('LICENSE') > -1)
locals.license = fetchLicenses(pkg.license);
locals.license = fetchLicenses(pkg.license)

if (pkg.name)
pkg.parsedName = parseName(pkg.name);
pkg.parsedName = parseName(pkg.name)

if (locals.license)
locals.license = locals.license.toString();
locals.license = locals.license.toString()

if (pkg.main) {
try {
locals.apis = require(path.join(cwd, pkg.main));
locals.apis = require(path.join(cwd, pkg.main))
} catch (err) {
// just ignore the error
}
Expand All @@ -53,23 +52,26 @@ function createFile(filename, callback) {
path.join(cwd, filename),
render(filename.indexOf('.') === 0 ? filename.substr(1) : filename)(locals),
callback
);
)
}

function parseName(str) {
if (str.indexOf('-') === 0)
return str;
var token = str.split('-');
return str

var token = str.split('-')

if (!token.length)
return str;
return str

for (var i = token.length - 1; i >= 0; i--) {
(function(index){
((index) => {
if (index === 0)
return;
return

token[index] = token[index].charAt(0).toUpperCase() + token[index].substr(1)
})(i);
};
}

return token.join('');
return token.join('')
}
13 changes: 6 additions & 7 deletions lib/render.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var path = require('path');
var swig = require('swig');
var templates = path.resolve(__dirname, '../templates');
import path from 'path'
import swig from 'swig'

module.exports = render;
const templates = path.resolve(__dirname, '../templates')

function render(filename) {
export default function(filename) {
if (!filename)
return;
return

return swig.compileFile(
path.join(templates, filename)
);
)
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "docor",
"version": "0.3.0",
"description": "a tiny cli tool to create elegant readme by manifest files, like `package.json`, `bower.json` and `manifest.json`",
"main": "index.js",
"main": "dist/docor.js",
"author": "turing <[email protected]>",
"license": "MIT",
"bin": "bin/cli",
"scripts": {
"build": "node_modules/.bin/babel lib --out-dir dist --source-maps",
"example": "cd ./example && ../bin/cli"
},
"repository": {
Expand All @@ -27,5 +28,8 @@
"dependencies":{
"swig": "~1.4.2",
"consoler": "~0.2.0"
},
"devDependencies": {
"babel": "5.1.11"
}
}

0 comments on commit 077d4b8

Please sign in to comment.