Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better config resolving #37

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const runCLICommand = require('./runCLICommand')

class Maid {
constructor(opts = {}) {
this.maidfile = readMaidFile(opts.section)
this.maidfile = readMaidFile(opts)
logger.setOptions({ quiet: opts.quiet })

if (!this.maidfile) {
throw new MaidError('No maidfile was found. Stop.')
throw new MaidError('No config file was found or was not valid. Stop.')
}
}

Expand Down
5 changes: 4 additions & 1 deletion lib/parseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!-- maid-tasks -->` comment
if (!section && !isMaidfile) {
Expand Down
49 changes: 40 additions & 9 deletions lib/readMaidFile.js
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh.. i forgot that, but it's not needed for find-file-up to work it uses the cwd by default anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use of the --cwd flag is not encouraged anyway, because it also may conflict with other CLIs (#35)


// 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 })
}
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
]
}
Expand Down
51 changes: 49 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down