From 1f23ae35aa718280fb88c9b02fca69bf90cea81a Mon Sep 17 00:00:00 2001 From: Charlike Mike Reagent Date: Wed, 6 Jun 2018 11:11:32 +0300 Subject: [PATCH] feat: resolves #4 and closes #36 - rewrite resolving mechanism - add `yarn maid` script - add implementations of #36 - replace occurances pf `node bin/cli` with `yarn maid` because it uses the --maidfile flag Signed-off-by: Charlike Mike Reagent --- bin/cli.js | 5 ++--- lib/parseMarkdown.js | 5 ++++- lib/readMaidFile.js | 49 ++++++++++++++++++++++++++++++++++-------- package.json | 8 ++++--- yarn.lock | 51 ++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 100 insertions(+), 18 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index dac8aa1..0d27e0e 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -31,11 +31,10 @@ cli.option('quiet', { default: false }) -cli.option('path', { +cli.option('maidfile', { desc: 'Path to markdown file', type: 'string', - default: 'maidfile.md', - alias: 'p' + default: 'maidfile.md' }) cli.option('section', { diff --git a/lib/parseMarkdown.js b/lib/parseMarkdown.js index c77955e..759417b 100644 --- a/lib/parseMarkdown.js +++ b/lib/parseMarkdown.js @@ -87,10 +87,13 @@ const getSectionByComment = tokens => { return section } +const MF = 'maidfile.md' +const isMaid = fp => path.basename(fp) === MF || path.basename(fp) === `.${MF}` + module.exports = (content, { section, filepath } = {}) => { let tokens = md.parse(content) - const isMaidfile = !filepath || path.basename(filepath) === 'maidfile.md' + const isMaidfile = !filepath || isMaid(filepath) // Automatically get maid section from non maidfile by `` comment if (!section && !isMaidfile) { diff --git a/lib/readMaidFile.js b/lib/readMaidFile.js index e1de473..9cf8fff 100644 --- a/lib/readMaidFile.js +++ b/lib/readMaidFile.js @@ -1,13 +1,44 @@ +const fs = require('fs') const parseMarkdown = require('./parseMarkdown') -const loadFile = require('./loadFile') +const find = require('find-file-up') -module.exports = ({ section } = {}) => { - const { path: filepath, data } = loadFile.loadSync([ - 'maidfile.md', - 'README.md', - 'readme.md' - ]) - if (!filepath) return null +// a bit duplication with what we have in parseMarkdown.js, but we can't DRY it +const MAIDFILE = 'maidfile.md' - return parseMarkdown(data, { section, filepath }) +/** + * Resolving mechanism. Always starts to search for `maidfile.md` from + * the current working directory to 5 levels upwards (probably enough). + * Then repeats this for `.maidfile.md` (with dot) but 10 levels upwards, + * because who knows, someone may have big folder trees. + * + * Second step. If it can't find the Maid files (above ones) and there is + * no given `--config-path / -c` flag on the CLI it throws with error message + * thrown from the main Maid class constructor. + * + * Third step. If config path is given, it treats that config file + * a bit more specifically, as described in the (current) readme - task names should be + * h3 headers and should have some h2 header, which in turn can also be defined on + * the CLI through the `--section` flag. + * + * @param {Object} opts command line global flags + * @returns {Object} like `{ filepath, tasks }`, coming from parseMarkdown + */ +module.exports = (opts = {}) => { + let filepath = + find.sync(MAIDFILE, opts.cwd, 5) || find.sync(`.${MAIDFILE}`, opts.cwd, 10) + + // in case when it cannot resolve `maidfile.md` or `.maidfile.md` + // files anywhere upwards, and there is no other file given + // through `--maidfile` flag, then inform that you don't have config file + // or it is invalid eg. `[03:39:50] No config file was found. Stop.` + if (!filepath && opts.maidfile === MAIDFILE) return null + + // otherwise resolve the given file from `--config-path` flag + if (opts.maidfile !== MAIDFILE && opts.maidfile !== `.${MAIDFILE}`) { + filepath = opts.maidfile + } + + const content = fs.readFileSync(filepath, 'utf8') + + return parseMarkdown(content, { section: opts.section, filepath }) } diff --git a/package.json b/package.json index 93575cc..bb1f90d 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "lib" ], "scripts": { - "test": "node bin/cli lint && node bin/cli test" + "maid": "node bin/cli --maidfile README.md", + "test": "yarn maid lint && yarn maid test" }, "author": "egoist <0x142857@gmail.com>", "license": "MIT", @@ -22,6 +23,7 @@ "chalk": "^2.4.1", "cross-spawn": "^6.0.5", "fancy-log": "^1.3.2", + "find-file-up": "^2.0.1", "joycon": "^1.0.4", "markdown-it": "^8.4.1", "micromatch": "^3.1.10", @@ -58,11 +60,11 @@ }, "lint-staged": { "*.js": [ - "node bin/cli lint --fix", + "yarn maid lint --fix", "git add" ], "README.md": [ - "node bin/cli toc", + "yarn maid toc", "git add" ] } diff --git a/yarn.lock b/yarn.lock index c61f5c4..3b67510 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1479,6 +1479,12 @@ expand-range@^1.8.1: dependencies: fill-range "^2.1.0" +expand-tilde@^2.0.0, expand-tilde@^2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/expand-tilde/-/expand-tilde-2.0.2.tgz#97e801aa052df02454de46b02bf621642cdc8502" + dependencies: + homedir-polyfill "^1.0.1" + extend-shallow@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f" @@ -1598,6 +1604,12 @@ find-cache-dir@^1.0.0: make-dir "^1.0.0" pkg-dir "^2.0.0" +find-file-up@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/find-file-up/-/find-file-up-2.0.1.tgz#4932dd81551af643893f8cda7453f221e3e28261" + dependencies: + resolve-dir "^1.0.1" + find-parent-dir@^0.3.0: version "0.3.0" resolved "https://registry.yarnpkg.com/find-parent-dir/-/find-parent-dir-0.3.0.tgz#33c44b429ab2b2f0646299c5f9f718f376ff8d54" @@ -1744,6 +1756,24 @@ global-dirs@^0.1.0: dependencies: ini "^1.3.4" +global-modules@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/global-modules/-/global-modules-1.0.0.tgz#6d770f0eb523ac78164d72b5e71a8877265cc3ea" + dependencies: + global-prefix "^1.0.1" + is-windows "^1.0.1" + resolve-dir "^1.0.0" + +global-prefix@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/global-prefix/-/global-prefix-1.0.2.tgz#dbf743c6c14992593c655568cb66ed32c0122ebe" + dependencies: + expand-tilde "^2.0.2" + homedir-polyfill "^1.0.1" + ini "^1.3.4" + is-windows "^1.0.1" + which "^1.2.14" + globals@^11.0.1: version "11.5.0" resolved "https://registry.yarnpkg.com/globals/-/globals-11.5.0.tgz#6bc840de6771173b191f13d3a9c94d441ee92642" @@ -1859,6 +1889,12 @@ home-or-tmp@^2.0.0: os-homedir "^1.0.0" os-tmpdir "^1.0.1" +homedir-polyfill@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.1.tgz#4c2bbc8a758998feebf5ed68580f76d46768b4bc" + dependencies: + parse-passwd "^1.0.0" + hosted-git-info@^2.1.4: version "2.6.0" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.6.0.tgz#23235b29ab230c576aab0d4f13fc046b0b038222" @@ -2269,7 +2305,7 @@ is-whitespace-character@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/is-whitespace-character/-/is-whitespace-character-1.0.2.tgz#ede53b4c6f6fb3874533751ec9280d01928d03ed" -is-windows@^1.0.2: +is-windows@^1.0.1, is-windows@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d" @@ -3104,6 +3140,10 @@ parse-ms@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/parse-ms/-/parse-ms-1.0.1.tgz#56346d4749d78f23430ca0c713850aef91aa361d" +parse-passwd@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6" + pascalcase@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14" @@ -3517,6 +3557,13 @@ resolve-cwd@^2.0.0: dependencies: resolve-from "^3.0.0" +resolve-dir@^1.0.0, resolve-dir@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/resolve-dir/-/resolve-dir-1.0.1.tgz#79a40644c362be82f26effe739c9bb5382046f43" + dependencies: + expand-tilde "^2.0.0" + global-modules "^1.0.0" + resolve-from@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226" @@ -4224,7 +4271,7 @@ well-known-symbols@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/well-known-symbols/-/well-known-symbols-1.0.0.tgz#73c78ae81a7726a8fa598e2880801c8b16225518" -which@^1.2.10, which@^1.2.9: +which@^1.2.10, which@^1.2.14, which@^1.2.9: version "1.3.1" resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a" dependencies: