Skip to content
This repository has been archived by the owner on Nov 3, 2019. It is now read-only.

Commit

Permalink
feat: use mos-core
Browse files Browse the repository at this point in the history
BREAKING CHANGE: plugins have to be updated to work with the new version of mos
  • Loading branch information
zkochan committed May 14, 2016
1 parent ca689d4 commit 36dd104
Show file tree
Hide file tree
Showing 27 changed files with 181 additions and 430 deletions.
56 changes: 4 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ The [readme][] you are currently reading uses mos!
- [mos](#mos)
- [mos test](#mos-test)
- [Optional TAP output](#optional-tap-output)
- [API Usage](#api-usage)
- [Mos plugins](#mos-plugins)
- [Who uses mos?](#who-uses-mos)
- [License](#license)
Expand Down Expand Up @@ -150,47 +149,6 @@ mos test --tap | tap-nyan

**NOTE:** the CLI will use your local install of mos when available, even when run globally.

## API Usage

<!--@example('example.js')-->
Require the package

```js
const mos = require('mos')
```

Create a processor and use some mos plugins

```js
const processor = mos().use(require('mos-plugin-scripts'))
```

Process raw markdown

```js
processor.process('# Header', { filePath: 'README.md' })
.then(newmd => console.log(newmd))
//> # Header
```

Process a markdown AST

```js
const m = require('markdownscript')
const h1 = m.h1
const p = m.paragraph
const ast = m.root([
h1(['Foo']),
p(['Bar qar qax']),
])
processor.process(ast, { filePath: 'README.md' })
.then(newmd => console.log(newmd))
//> # Foo
//
// Bar qar qax
```
<!--/@-->

## Mos plugins

In the usage example the `package` variable was used to access the package info. The variables available in the markdown scope are _declared by mos plugins_. The `package` variable is created by the [package-json][mos-plugin-package-json] plugin.
Expand Down Expand Up @@ -226,32 +184,28 @@ Do you want to write a new one? Read the [plugins readme](./docs/PLUGINS.md).
## <a name="dependencies">Dependencies</a> [![Dependency status for master](https://img.shields.io/david/zkochan/mos/master.svg?style=flat-square)](https://david-dm.org/zkochan/mos/master)

- [@zkochan/read-pkg-up](https://github.com/zkochan/read-pkg-up): Read the closest package.json file
- [@zkochan/remark](https://github.com/wooorm/remark): Markdown processor powered by plugins
- [@zkochan/remark-toc](https://github.com/wooorm/remark-toc): Generate a Table of Contents (TOC) for Markdown files
- [@zkochan/tap-diff](https://github.com/zkochan/tap-diff): The most human-friendly TAP reporter
- [async-unist-util-visit](https://github.com/wooorm/unist-util-visit): Recursively walk over unist nodes
- [chalk](https://github.com/chalk/chalk): Terminal string styling done right. Much color.
- [file-exists](https://github.com/scottcorgan/file-exists): Check if filepath exists and is a file
- [github-url-to-object](https://github.com/zeke/github-url-to-object): Extract user, repo, and other interesting properties from GitHub URLs
- [glob](https://github.com/isaacs/node-glob): a little globber
- [loud-rejection](https://github.com/sindresorhus/loud-rejection): Make unhandled promise rejections fail loudly instead of the default silent fail
- [magic-hook](https://github.com/zkochan/magic-hook): Extends functions with pre hooks.
- [meow](https://github.com/sindresorhus/meow): CLI app helper
- [mos-init](https://github.com/zkochan/mos-init): Add mos to your project
- [mos-plugin-dependencies](https://github.com/zkochan/mos-plugin-dependencies): A mos plugin that creates dependencies sections
- [mos-plugin-ejs](https://github.com/zkochan/mos-plugin-ejs): A mos plugin that executes embedded js in markdown files
- [mos-plugin-example](https://github.com/zkochan/mos-plugin-example): A mos plugin that combines example code files with their output
- [mos-plugin-installation](https://github.com/zkochan/mos-plugin-installation): A mos plugin for creating installation section
- [mos-plugin-license](https://github.com/zkochan/mos-plugin-license): A mos plugin for generating a license section
- [mos-plugin-markdownscript](https://github.com/zkochan/mos-plugin-markdownscript): A [mos](https://github.com/zkochan/mos) plugin that adds markownscript helpers to the markdown scope
- [mos-plugin-package-json](https://github.com/zkochan/mos-plugin-package-json): A mos plugin that makes the package.json available in the markdown scope
- [mos-plugin-shields](https://github.com/zkochan/mos-plugin-shields): A mos plugin for creating markdown shields
- [mos-plugin-snippet](https://github.com/zkochan/mos-plugin-snippet): A mos plugin for embedding snippets from files
- [mos-plugin-toc](https://github.com/zkochan/mos-plugin-toc): A mos plugin for creating Table of Contents
- [mos-processor](https://github.com/zkochan/mos-processor): A markdown processor for mos
- [normalize-newline](https://github.com/sindresorhus/normalize-newline): Normalize the newline characters in a string to `\n`
- [normalize-path](https://github.com/jonschlinkert/normalize-path): Normalize file path slashes to be unix-like forward slashes. Also condenses repeat slashes to a single slash and removes and trailing slashes.
- [relative](https://github.com/jonschlinkert/relative): Get the relative filepath from path A to path B. Calculates from file-to-directory, file-to-file, directory-to-file, and directory-to-directory.
- [remark-mos](https://github.com/zkochan/remark-mos): Inject parts of markdown via hidden JavaScript snippets
- [reserved-words](https://github.com/zxqfox/reserved-words): ECMAScript reserved words checker
- [resolve](https://github.com/substack/node-resolve): resolve like require.resolve() on behalf of files asynchronously and synchronously
- [run-async](https://github.com/sboudrias/run-async): Utility method to run function either synchronously or asynchronously using the common `this.async()` style.
- [tape](https://github.com/substack/tape): tap-producing test harness for node and browsers
- [update-notifier](https://github.com/yeoman/update-notifier): Update notifications for your CLI app

Expand All @@ -269,9 +223,7 @@ Do you want to write a new one? Read the [plugins readme](./docs/PLUGINS.md).
- [execa](https://github.com/sindresorhus/execa): A better `child_process`
- [ghooks](https://github.com/gtramontina/ghooks): Simple git hooks
- [istanbul](https://github.com/gotwarlost/istanbul): Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests
- [markdownscript](https://github.com/zkochan/markdownscript): Creates markdown Abstract Syntax Tree
- [mocha](https://github.com/mochajs/mocha): simple, flexible, fun test framework
- [mos-plugin-scripts](https://github.com/zkochan/mos-plugin-scripts): A mos plugin that generates a section with npm scripts descriptions
- [semantic-release](https://github.com/semantic-release/semantic-release): automated semver compliant package publishing
- [validate-commit-msg](https://github.com/kentcdodds/validate-commit-msg): Script to validate a commit message follows the conventional changelog standard

Expand Down
2 changes: 1 addition & 1 deletion bin/index.js → bin/mos.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const cwd = process.cwd()

let localCLI
try {
localCLI = resolve.sync('mos/bin', { basedir: cwd })
localCLI = resolve.sync('mos/bin/mos', { basedir: cwd })
} catch (err) {
localCLI = __filename
}
Expand Down
24 changes: 0 additions & 24 deletions example.js

This file was deleted.

2 changes: 0 additions & 2 deletions index.js

This file was deleted.

2 changes: 0 additions & 2 deletions index.spec.js

This file was deleted.

122 changes: 74 additions & 48 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const path = require('path')
const test = require('tape')
const tapDiff = require('@zkochan/tap-diff')
const readPkgUp = require('@zkochan/read-pkg-up')
const mos = require('.')
const initCustomPlugins = require('./init-custom-plugins')
const mos = require('mos-processor')
const defaultPlugins = require('./default-plugins')
const resolve = require('resolve')

const cwd = process.cwd()
const stdout = process.stdout

const cli = meow([
'Usage',
Expand Down Expand Up @@ -59,64 +60,89 @@ const isTest = ~['test', 't'].indexOf((cli.input[0] || '').toLowerCase())

readPkgUp({ cwd })
.then(result => {
const pkgPlugins = Object
const allDeps = new Set(Object
.keys(result.pkg.dependencies || {})
.concat(Object.keys(result.pkg.devDependencies || {}))
.filter(dep => dep.indexOf('mos-plugin-') === 0)
.map(dep => resolve.sync(dep, { basedir: cwd }))
.map(normalizePath)
.map(require)
.concat(Object.keys(result.pkg.devDependencies || {})))

const processor = initCustomPlugins(mos())
.use(pkgPlugins)
result.pkg.mos = result.pkg.mos || {}

const mdExtensions = ['markdown', 'mdown', 'mkdn', 'mkd', 'md']
const files = cli.input[isTest ? 1 : 0]
const pattern = files
? path.resolve(cwd, files)
: path.resolve(cwd, `{/**/,/}*.{${mdExtensions.join()}}`)
const ignorePattern = cli.flags.x
? path.resolve(cwd, cli.flags.x)
: null
const pkgPlugins = (result.pkg.mos.plugins || [])
.map(plugin => plugin instanceof Array
? { name: plugin[0], options: plugin[1] || {} }
: { name: plugin, options: {}}
)
.map(plugin => {
const namespacedName = 'mos-plugin-' + plugin.name
if (allDeps.has(namespacedName)) {
return Object.assign({}, plugin, {name: namespacedName})
}
if (allDeps.has(plugin.name)) {
return plugin
}
throw new Error(`${plugin.name} is not in the dependencies`)
})
.map(plugin => Object.assign(plugin, {path: resolve.sync(plugin.name, { basedir: cwd })}))
.map(plugin => Object.assign(plugin, {path: normalizePath(plugin.path)}))
.map(plugin => Object.assign(plugin, {register: require(plugin.path)}))

if (isTest) {
if (cli.flags.tap !== true) {
test.createStream()
.pipe(tapDiff())
.pipe(process.stdout)
const defaultPluginsWithOpts = defaultPlugins.map(defPlugin => {
const defPluginName = defPlugin.attributes.pkg && defPlugin.attributes.pkg.name || defPlugin.attributes.name
return {
register: defPlugin,
options: result.pkg.mos[defPluginName] || result.pkg.mos[defPluginName.replace(/^mos-plugin-/, '')] || {}
}
test('markdown', t => {
})
return md => mos(md, defaultPluginsWithOpts.concat(pkgPlugins))
.then(processor => processor.process())
})
.then(process => {
const mdExtensions = ['markdown', 'mdown', 'mkdn', 'mkd', 'md']
const files = cli.input[isTest ? 1 : 0]
const pattern = files
? path.resolve(cwd, files)
: path.resolve(cwd, `{/**/,/}*.{${mdExtensions.join()}}`)
const ignorePattern = cli.flags.x
? path.resolve(cwd, cli.flags.x)
: null

if (isTest) {
if (cli.flags.tap !== true) {
test.createStream()
.pipe(tapDiff())
.pipe(stdout)
}
test('markdown', t => {
processFiles({
process,
pattern,
ignorePattern,
afterEachRender (opts) {
const relativePath = normalizePath(getRelativePath(opts.filePath))
t.equal(normalizeNewline(opts.newMD), normalizeNewline(opts.currentMD), relativePath)
},
})
.then(() => t.end())
.catch(err => { throw err })
})
} else {
processFiles({
processor,
process,
pattern,
ignorePattern,
afterEachRender (opts) {
const relativePath = normalizePath(getRelativePath(opts.filePath))
t.equal(normalizeNewline(opts.newMD), normalizeNewline(opts.currentMD), relativePath)
if (normalizeNewline(opts.newMD) !== normalizeNewline(opts.currentMD)) {
fs.writeFileSync(opts.filePath, opts.newMD, {
encoding: 'utf8',
})
const relativePath = normalizePath(getRelativePath(opts.filePath))
console.log('updated', highlightPath(relativePath))
}
},
})
.then(() => t.end())
.catch(err => { throw err })
})
} else {
processFiles({
processor,
pattern,
ignorePattern,
afterEachRender (opts) {
if (normalizeNewline(opts.newMD) !== normalizeNewline(opts.currentMD)) {
fs.writeFileSync(opts.filePath, opts.newMD, {
encoding: 'utf8',
})
const relativePath = normalizePath(getRelativePath(opts.filePath))
console.log('updated', highlightPath(relativePath))
}
},
})
.catch(err => { throw err })
}
})
.catch(err => { throw err })
}
})
.catch(err => { throw err })

function getRelativePath (filePath) {
return relative(cwd, filePath)
Expand Down
20 changes: 19 additions & 1 deletion lib/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const expect = require('chai').expect
const execa = require('execa')
const pkg = require('../package.json')
const path = require('path')
const cli = path.resolve(__dirname, '../bin/index.js')
const cli = path.resolve(__dirname, '../bin/mos.js')
const testcwd = path.resolve(__dirname, 'test-cli')

describe('cli', () => {
Expand Down Expand Up @@ -65,5 +65,23 @@ describe('cli', () => {
].join('\n'))
})
})

it('should use custom options for third-party plugins', () => {
return execa(cli, ['test', 'plugin-options.md', '--tap'], {
cwd: testcwd,
})
.then(result => expect(result.stdout).to.eq([
'TAP version 13',
'# markdown',
'ok 1 plugin-options.md',
'',
'1..1',
'# tests 1',
'# pass 1',
'',
'# ok',
'',
].join('\n')))
})
})
})
24 changes: 0 additions & 24 deletions lib/create-md-scope.js

This file was deleted.

34 changes: 0 additions & 34 deletions lib/create-md-scope.spec.js

This file was deleted.

0 comments on commit 36dd104

Please sign in to comment.