Skip to content

Commit 752f1ca

Browse files
author
Charlike Mike Reagent
committed
- rewrite resolving mechanism
- add `yarn maid` script - add implementations of egoist#36 - replace occurances pf `node bin/cli` with `yarn maid` because it uses the --maidfile flag Signed-off-by: Charlike Mike Reagent <[email protected]>
1 parent 6fe8bcf commit 752f1ca

File tree

5 files changed

+100
-18
lines changed

5 files changed

+100
-18
lines changed

bin/cli.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ cli.option('quiet', {
3131
default: false
3232
})
3333

34-
cli.option('path', {
34+
cli.option('maidfile', {
3535
desc: 'Path to markdown file',
3636
type: 'string',
37-
default: 'maidfile.md',
38-
alias: 'p'
37+
default: 'maidfile.md'
3938
})
4039

4140
cli.option('section', {

lib/parseMarkdown.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ const getSectionByComment = tokens => {
8787
return section
8888
}
8989

90+
const MF = 'maidfile.md'
91+
const isMaid = fp => path.basename(fp) === MF || path.basename(fp) === `.${MF}`
92+
9093
module.exports = (content, { section, filepath } = {}) => {
9194
let tokens = md.parse(content)
9295

93-
const isMaidfile = !filepath || path.basename(filepath) === 'maidfile.md'
96+
const isMaidfile = !filepath || isMaid(filepath)
9497

9598
// Automatically get maid section from non maidfile by `<!-- maid-tasks -->` comment
9699
if (!section && !isMaidfile) {

lib/readMaidFile.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
1+
const fs = require('fs')
12
const parseMarkdown = require('./parseMarkdown')
2-
const loadFile = require('./loadFile')
3+
const find = require('find-file-up')
34

4-
module.exports = ({ section } = {}) => {
5-
const { path: filepath, data } = loadFile.loadSync([
6-
'maidfile.md',
7-
'README.md',
8-
'readme.md'
9-
])
10-
if (!filepath) return null
5+
// a bit duplication with what we have in parseMarkdown.js, but we can't DRY it
6+
const MAIDFILE = 'maidfile.md'
117

12-
return parseMarkdown(data, { section, filepath })
8+
/**
9+
* Resolving mechanism. Always starts to search for `maidfile.md` from
10+
* the current working directory to 5 levels upwards (probably enough).
11+
* Then repeats this for `.maidfile.md` (with dot) but 10 levels upwards,
12+
* because who knows, someone may have big folder trees.
13+
*
14+
* Second step. If it can't find the Maid files (above ones) and there is
15+
* no given `--config-path / -c` flag on the CLI it throws with error message
16+
* thrown from the main Maid class constructor.
17+
*
18+
* Third step. If config path is given, it treats that config file
19+
* a bit more specifically, as described in the (current) readme - task names should be
20+
* h3 headers and should have some h2 header, which in turn can also be defined on
21+
* the CLI through the `--section` flag.
22+
*
23+
* @param {Object} opts command line global flags
24+
* @returns {Object} like `{ filepath, tasks }`, coming from parseMarkdown
25+
*/
26+
module.exports = (opts = {}) => {
27+
let filepath =
28+
find.sync(MAIDFILE, opts.cwd, 5) || find.sync(`.${MAIDFILE}`, opts.cwd, 10)
29+
30+
// in case when it cannot resolve `maidfile.md` or `.maidfile.md`
31+
// files anywhere upwards, and there is no other file given
32+
// through `--maidfile` flag, then inform that you don't have config file
33+
// or it is invalid eg. `[03:39:50] No config file was found. Stop.`
34+
if (!filepath && opts.maidfile === MAIDFILE) return null
35+
36+
// otherwise resolve the given file from `--config-path` flag
37+
if (opts.maidfile !== MAIDFILE && opts.maidfile !== `.${MAIDFILE}`) {
38+
filepath = opts.maidfile
39+
}
40+
41+
const content = fs.readFileSync(filepath, 'utf8')
42+
43+
return parseMarkdown(content, { section: opts.section, filepath })
1344
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"lib"
1414
],
1515
"scripts": {
16-
"test": "node bin/cli lint && node bin/cli test"
16+
"maid": "node bin/cli --maidfile README.md",
17+
"test": "yarn maid lint && yarn maid test"
1718
},
1819
"author": "egoist <[email protected]>",
1920
"license": "MIT",
@@ -22,6 +23,7 @@
2223
"chalk": "^2.4.1",
2324
"cross-spawn": "^6.0.5",
2425
"fancy-log": "^1.3.2",
26+
"find-file-up": "^2.0.1",
2527
"joycon": "^1.0.4",
2628
"markdown-it": "^8.4.1",
2729
"micromatch": "^3.1.10",
@@ -58,11 +60,11 @@
5860
},
5961
"lint-staged": {
6062
"*.js": [
61-
"node bin/cli lint --fix",
63+
"yarn maid lint --fix",
6264
"git add"
6365
],
6466
"README.md": [
65-
"node bin/cli toc",
67+
"yarn maid toc",
6668
"git add"
6769
]
6870
}

yarn.lock

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,12 @@ expand-range@^1.8.1:
14791479
dependencies:
14801480
fill-range "^2.1.0"
14811481

1482+
expand-tilde@^2.0.0, expand-tilde@^2.0.2:
1483+
version "2.0.2"
1484+
resolved "https://registry.yarnpkg.com/expand-tilde/-/expand-tilde-2.0.2.tgz#97e801aa052df02454de46b02bf621642cdc8502"
1485+
dependencies:
1486+
homedir-polyfill "^1.0.1"
1487+
14821488
extend-shallow@^2.0.1:
14831489
version "2.0.1"
14841490
resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f"
@@ -1598,6 +1604,12 @@ find-cache-dir@^1.0.0:
15981604
make-dir "^1.0.0"
15991605
pkg-dir "^2.0.0"
16001606

1607+
find-file-up@^2.0.1:
1608+
version "2.0.1"
1609+
resolved "https://registry.yarnpkg.com/find-file-up/-/find-file-up-2.0.1.tgz#4932dd81551af643893f8cda7453f221e3e28261"
1610+
dependencies:
1611+
resolve-dir "^1.0.1"
1612+
16011613
find-parent-dir@^0.3.0:
16021614
version "0.3.0"
16031615
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:
17441756
dependencies:
17451757
ini "^1.3.4"
17461758

1759+
global-modules@^1.0.0:
1760+
version "1.0.0"
1761+
resolved "https://registry.yarnpkg.com/global-modules/-/global-modules-1.0.0.tgz#6d770f0eb523ac78164d72b5e71a8877265cc3ea"
1762+
dependencies:
1763+
global-prefix "^1.0.1"
1764+
is-windows "^1.0.1"
1765+
resolve-dir "^1.0.0"
1766+
1767+
global-prefix@^1.0.1:
1768+
version "1.0.2"
1769+
resolved "https://registry.yarnpkg.com/global-prefix/-/global-prefix-1.0.2.tgz#dbf743c6c14992593c655568cb66ed32c0122ebe"
1770+
dependencies:
1771+
expand-tilde "^2.0.2"
1772+
homedir-polyfill "^1.0.1"
1773+
ini "^1.3.4"
1774+
is-windows "^1.0.1"
1775+
which "^1.2.14"
1776+
17471777
globals@^11.0.1:
17481778
version "11.5.0"
17491779
resolved "https://registry.yarnpkg.com/globals/-/globals-11.5.0.tgz#6bc840de6771173b191f13d3a9c94d441ee92642"
@@ -1859,6 +1889,12 @@ home-or-tmp@^2.0.0:
18591889
os-homedir "^1.0.0"
18601890
os-tmpdir "^1.0.1"
18611891

1892+
homedir-polyfill@^1.0.1:
1893+
version "1.0.1"
1894+
resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.1.tgz#4c2bbc8a758998feebf5ed68580f76d46768b4bc"
1895+
dependencies:
1896+
parse-passwd "^1.0.0"
1897+
18621898
hosted-git-info@^2.1.4:
18631899
version "2.6.0"
18641900
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:
22692305
version "1.0.2"
22702306
resolved "https://registry.yarnpkg.com/is-whitespace-character/-/is-whitespace-character-1.0.2.tgz#ede53b4c6f6fb3874533751ec9280d01928d03ed"
22712307

2272-
is-windows@^1.0.2:
2308+
is-windows@^1.0.1, is-windows@^1.0.2:
22732309
version "1.0.2"
22742310
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
22752311

@@ -3104,6 +3140,10 @@ parse-ms@^1.0.0:
31043140
version "1.0.1"
31053141
resolved "https://registry.yarnpkg.com/parse-ms/-/parse-ms-1.0.1.tgz#56346d4749d78f23430ca0c713850aef91aa361d"
31063142

3143+
parse-passwd@^1.0.0:
3144+
version "1.0.0"
3145+
resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6"
3146+
31073147
pascalcase@^0.1.1:
31083148
version "0.1.1"
31093149
resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14"
@@ -3517,6 +3557,13 @@ resolve-cwd@^2.0.0:
35173557
dependencies:
35183558
resolve-from "^3.0.0"
35193559

3560+
resolve-dir@^1.0.0, resolve-dir@^1.0.1:
3561+
version "1.0.1"
3562+
resolved "https://registry.yarnpkg.com/resolve-dir/-/resolve-dir-1.0.1.tgz#79a40644c362be82f26effe739c9bb5382046f43"
3563+
dependencies:
3564+
expand-tilde "^2.0.0"
3565+
global-modules "^1.0.0"
3566+
35203567
resolve-from@^1.0.0:
35213568
version "1.0.1"
35223569
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226"
@@ -4224,7 +4271,7 @@ well-known-symbols@^1.0.0:
42244271
version "1.0.0"
42254272
resolved "https://registry.yarnpkg.com/well-known-symbols/-/well-known-symbols-1.0.0.tgz#73c78ae81a7726a8fa598e2880801c8b16225518"
42264273

4227-
which@^1.2.10, which@^1.2.9:
4274+
which@^1.2.10, which@^1.2.14, which@^1.2.9:
42284275
version "1.3.1"
42294276
resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"
42304277
dependencies:

0 commit comments

Comments
 (0)