diff --git a/bin/cli b/bin/cli index 458262b..f86dd4f 100755 --- a/bin/cli +++ b/bin/cli @@ -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')() diff --git a/dist/cli.js b/dist/cli.js new file mode 100644 index 0000000..fc85d46 --- /dev/null +++ b/dist/cli.js @@ -0,0 +1,36 @@ +'use strict'; + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + +var _fs = require('fs'); + +var _fs2 = _interopRequireDefault(_fs); + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _consoler = require('consoler'); + +var _consoler2 = _interopRequireDefault(_consoler); + +var _docor = require('./docor'); + +var _docor2 = _interopRequireDefault(_docor); + +var files = ['README.md', 'LICENSE', '.gitignore', '.npmignore']; + +var checkPackage = (function (file) { + return _fs2['default'].existsSync(_path2['default'].join(process.cwd(), file)); +})(function () { + if (!checkPackage('package.json')) return _consoler2['default'].error('Docor.init(); `package.json` file not found'); + + files.forEach(function (file) { + _docor2['default'].createFile(file, function (err) { + if (err) return _consoler2['default'].error(err); + + _consoler2['default'].success(file + ' created'); + }); + }); +})(); +//# sourceMappingURL=cli.js.map \ No newline at end of file diff --git a/dist/cli.js.map b/dist/cli.js.map new file mode 100644 index 0000000..f347aa4 --- /dev/null +++ b/dist/cli.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../lib/cli.js"],"names":[],"mappings":";;;;kBAAe,IAAI;;;;oBACF,MAAM;;;;wBACF,UAAU;;;;qBACb,SAAS;;;;AAE3B,IAAM,KAAK,GAAG,CAAC,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,YAAY,CAAC,CAAA;;AAElE,IAAI,YAAY,GAAG,CAAA,UAAC,IAAI,EAAK;AAC3B,SAAO,gBAAG,UAAU,CAClB,kBAAK,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,CAC/B,CAAA;CACF,CAAA,CAEA,YAAM;AACL,MAAI,CAAC,YAAY,CAAC,cAAc,CAAC,EAC/B,OAAO,sBAAS,KAAK,CAAC,6CAA6C,CAAC,CAAA;;AAEtE,OAAK,CAAC,OAAO,CAAC,UAAC,IAAI,EAAK;AACtB,uBAAM,UAAU,CAAC,IAAI,EAAE,UAAC,GAAG,EAAK;AAC9B,UAAI,GAAG,EACL,OAAO,sBAAS,KAAK,CAAC,GAAG,CAAC,CAAA;;AAE5B,4BAAS,OAAO,CAAC,IAAI,GAAG,UAAU,CAAC,CAAA;KACpC,CAAC,CAAA;GACH,CAAC,CAAA;CACH,CAAC,EAAE,CAAA","file":"lib/cli.js","sourcesContent":["import fs from 'fs'\nimport path from 'path'\nimport consoler from 'consoler'\nimport docor from './docor'\n\nconst files = ['README.md', 'LICENSE', '.gitignore', '.npmignore']\n\nlet checkPackage = (file) => {\n return fs.existsSync(\n path.join(process.cwd(), file)\n )\n}\n\n(() => {\n if (!checkPackage('package.json')) \n return consoler.error('Docor.init(); `package.json` file not found')\n\n files.forEach((file) => {\n docor.createFile(file, (err) => {\n if (err) \n return consoler.error(err)\n\n consoler.success(file + ' created')\n })\n })\n})()\n"]} \ No newline at end of file diff --git a/dist/docor.js b/dist/docor.js new file mode 100644 index 0000000..f87cdc3 --- /dev/null +++ b/dist/docor.js @@ -0,0 +1,82 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { + value: true +}); +exports.fetchLicenses = fetchLicenses; +exports.createFile = createFile; + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + +var _fs = require('fs'); + +var _fs2 = _interopRequireDefault(_fs); + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _render = require('./render'); + +var _render2 = _interopRequireDefault(_render); + +var sys = require(_path2['default'].resolve(__dirname, '../package.json')); +var templates = _path2['default'].resolve(__dirname, '../templates'); + +function fetchLicenses(license) { + if (!license) return null; + + license = license.toString().toLowerCase(); + + try { + return _fs2['default'].readFileSync(_path2['default'].join(templates, 'licenses', license)); + } catch (e) { + return null; + } +} + +function createFile(filename, callback) { + var cwd = process.cwd(); + var pkg = require(_path2['default'].join(cwd, 'package.json')); + + var locals = { + pkg: pkg, + sys: sys, + 'year': new Date().getFullYear(), + 'apis': null }; + + if (filename.indexOf('README') > -1 || filename.indexOf('LICENSE') > -1) locals.license = fetchLicenses(pkg.license); + + if (pkg.name) pkg.parsedName = parseName(pkg.name); + + if (locals.license) locals.license = locals.license.toString(); + + if (pkg.main) { + try { + locals.apis = require(_path2['default'].join(cwd, pkg.main)); + } catch (err) {} + } + + return _fs2['default'].writeFile(_path2['default'].join(cwd, filename), _render2['default'](filename.indexOf('.') === 0 ? filename.substr(1) : filename)(locals), callback); +} + +function parseName(str) { + if (str.indexOf('-') === 0) return str; + + var token = str.split('-'); + + if (!token.length) return str; + + for (var i = token.length - 1; i >= 0; i--) { + (function (index) { + if (index === 0) return; + + token[index] = token[index].charAt(0).toUpperCase() + token[index].substr(1); + })(i); + } + + return token.join(''); +} + +// just ignore the error +//# sourceMappingURL=docor.js.map \ No newline at end of file diff --git a/dist/docor.js.map b/dist/docor.js.map new file mode 100644 index 0000000..fe77cc3 --- /dev/null +++ b/dist/docor.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../lib/docor.js"],"names":[],"mappings":";;;;;QAOgB,aAAa,GAAb,aAAa;QAeb,UAAU,GAAV,UAAU;;;;kBAtBX,IAAI;;;;oBACF,MAAM;;;;sBACJ,UAAU;;;;AAE7B,IAAM,GAAG,GAAG,OAAO,CAAC,kBAAK,OAAO,CAAC,SAAS,EAAE,iBAAiB,CAAC,CAAC,CAAC;AAChE,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C,IAAI,GAAG,OAAO,CAAC,kBAAK,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAA;KAChD,CAAC,OAAO,GAAG,EAAE,EAEb;GACF;;AAED,SAAO,gBAAG,SAAS,CACjB,kBAAK,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,EACxB,oBAAO,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,MAAM,CAAC,EAC3E,QAAQ,CACT,CAAA;CACF;;AAED,SAAS,SAAS,CAAC,GAAG,EAAE;AACtB,MAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EACxB,OAAO,GAAG,CAAA;;AAEZ,MAAI,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;;AAE1B,MAAI,CAAC,KAAK,CAAC,MAAM,EACf,OAAO,GAAG,CAAA;;AAEZ,OAAK,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE;AAC1C,KAAC,UAAC,KAAK,EAAK;AACV,UAAI,KAAK,KAAK,CAAC,EACb,OAAM;;AAER,WAAK,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;KAC7E,CAAA,CAAE,CAAC,CAAC,CAAC;GACP;;AAED,SAAO,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;CACtB","file":"lib/docor.js","sourcesContent":["import fs from 'fs'\nimport path from 'path'\nimport render from './render'\n\nconst sys = require(path.resolve(__dirname, '../package.json'));\nconst templates = path.resolve(__dirname, '../templates');\n\nexport function fetchLicenses(license) {\n if (!license)\n return null\n\n license = license.toString().toLowerCase()\n\n try {\n return fs.readFileSync(\n path.join(templates, 'licenses', license)\n )\n } catch (e) {\n return null\n }\n}\n\nexport function createFile(filename, callback) {\n var cwd = process.cwd()\n var pkg = require(path.join(cwd, 'package.json'))\n\n var locals = {\n pkg,\n sys,\n 'year': new Date().getFullYear(),\n 'apis': null,\n }\n\n if (filename.indexOf('README') > -1 || filename.indexOf('LICENSE') > -1)\n locals.license = fetchLicenses(pkg.license)\n\n if (pkg.name)\n pkg.parsedName = parseName(pkg.name)\n\n if (locals.license)\n locals.license = locals.license.toString()\n\n if (pkg.main) {\n try {\n locals.apis = require(path.join(cwd, pkg.main))\n } catch (err) {\n // just ignore the error\n }\n }\n\n return fs.writeFile(\n path.join(cwd, filename),\n render(filename.indexOf('.') === 0 ? filename.substr(1) : filename)(locals),\n callback\n )\n}\n\nfunction parseName(str) {\n if (str.indexOf('-') === 0)\n return str\n\n var token = str.split('-')\n\n if (!token.length)\n return str\n\n for (var i = token.length - 1; i >= 0; i--) {\n ((index) => {\n if (index === 0)\n return\n\n token[index] = token[index].charAt(0).toUpperCase() + token[index].substr(1)\n })(i);\n }\n\n return token.join('')\n}\n"]} \ No newline at end of file diff --git a/dist/render.js b/dist/render.js new file mode 100644 index 0000000..cb7d9e5 --- /dev/null +++ b/dist/render.js @@ -0,0 +1,26 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { + value: true +}); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + +var _path = require('path'); + +var _path2 = _interopRequireDefault(_path); + +var _swig = require('swig'); + +var _swig2 = _interopRequireDefault(_swig); + +var templates = _path2['default'].resolve(__dirname, '../templates'); + +exports['default'] = function (filename) { + if (!filename) return; + + return _swig2['default'].compileFile(_path2['default'].join(templates, filename)); +}; + +module.exports = exports['default']; +//# sourceMappingURL=render.js.map \ No newline at end of file diff --git a/dist/render.js.map b/dist/render.js.map new file mode 100644 index 0000000..5b75868 --- /dev/null +++ b/dist/render.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../lib/render.js"],"names":[],"mappings":";;;;;;;;oBAAiB,MAAM;;;;oBACN,MAAM;;;;AAEvB,IAAM,SAAS,GAAG,kBAAK,OAAO,CAAC,SAAS,EAAE,cAAc,CAAC,CAAA;;qBAE1C,UAAS,QAAQ,EAAE;AAChC,MAAI,CAAC,QAAQ,EACX,OAAM;;AAER,SAAO,kBAAK,WAAW,CACrB,kBAAK,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAC/B,CAAA;CACF","file":"lib/render.js","sourcesContent":["import path from 'path'\nimport swig from 'swig'\n\nconst templates = path.resolve(__dirname, '../templates')\n\nexport default function(filename) {\n if (!filename)\n return\n\n return swig.compileFile(\n path.join(templates, filename)\n )\n}\n"]} \ No newline at end of file diff --git a/index.js b/index.js deleted file mode 100755 index b84ae45..0000000 --- a/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./lib/docor'); \ No newline at end of file diff --git a/lib/cli.js b/lib/cli.js new file mode 100644 index 0000000..225009b --- /dev/null +++ b/lib/cli.js @@ -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') + }) + }) +})() diff --git a/lib/docor.js b/lib/docor.js index bb494de..070f623 100644 --- a/lib/docor.js +++ b/lib/docor.js @@ -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 } @@ -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('') } diff --git a/lib/render.js b/lib/render.js index cbae86b..e7461ab 100644 --- a/lib/render.js +++ b/lib/render.js @@ -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) - ); + ) } diff --git a/package.json b/package.json index 33fe7fb..f22254f 100755 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", "bin": "bin/cli", "scripts": { + "build": "node_modules/.bin/babel lib --out-dir dist --source-maps", "example": "cd ./example && ../bin/cli" }, "repository": { @@ -27,5 +28,8 @@ "dependencies":{ "swig": "~1.4.2", "consoler": "~0.2.0" + }, + "devDependencies": { + "babel": "5.1.11" } }