Skip to content

Commit 297476c

Browse files
committed
feat(cli): add analytics CLI for automate plugin docs & more to come
1 parent 5544c44 commit 297476c

File tree

10 files changed

+252
-0
lines changed

10 files changed

+252
-0
lines changed

packages/analytics-cli/.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

packages/analytics-cli/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*-debug.log
2+
*-error.log
3+
/.nyc_output
4+
/dist
5+
/tmp
6+
/yarn.lock
7+
node_modules

packages/analytics-cli/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Analytics CLI
2+
3+
CLI for [`analytics`](https://www.npmjs.com/package/analytics) package

packages/analytics-cli/bin/run

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
require('@oclif/command').run()
4+
.then(require('@oclif/command/flush'))
5+
.catch(require('@oclif/errors/handle'))

packages/analytics-cli/bin/run.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
3+
node "%~dp0\run" %*

packages/analytics-cli/package.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "analytics-cli",
3+
"description": "CLI for `analytics` pkg",
4+
"version": "0.0.1",
5+
"author": "David Wells <hello@davidwells.io>",
6+
"repository": "DavidWells/analytics",
7+
"license": "MIT",
8+
"bin": {
9+
"analytics": "./bin/run"
10+
},
11+
"bugs": "https://github.com/DavidWells/analytics/issues",
12+
"dependencies": {
13+
"@oclif/command": "^1.5.1",
14+
"@oclif/config": "^1.7.6",
15+
"@oclif/plugin-help": "^2.1.2",
16+
"dox": "^0.9.0",
17+
"markdown-magic": "^0.1.25",
18+
"prettier": "^1.17.1"
19+
},
20+
"devDependencies": {
21+
"@oclif/dev-cli": "^1.17.0",
22+
"globby": "^8.0.1"
23+
},
24+
"engines": {
25+
"node": ">=8.0.0"
26+
},
27+
"files": [
28+
"/bin",
29+
"/npm-shrinkwrap.json",
30+
"/oclif.manifest.json",
31+
"/src"
32+
],
33+
"homepage": "https://github.com/DavidWells/aws-profile-utils",
34+
"keywords": [
35+
"oclif"
36+
],
37+
"main": "src/index.js",
38+
"oclif": {
39+
"commands": "./src/commands",
40+
"bin": "analytics",
41+
"hooks": {
42+
"init": [
43+
"./src/hooks/init"
44+
]
45+
},
46+
"plugins": [
47+
"@oclif/plugin-help"
48+
]
49+
},
50+
"scripts": {
51+
"postpack": "rm -f oclif.manifest.json npm-shrinkwrap.json",
52+
"prepack": "oclif-dev manifest && oclif-dev readme && npm shrinkwrap",
53+
"test": "echo NO TESTS",
54+
"version": "oclif-dev readme && git add README.md"
55+
}
56+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
const { Command, flags } = require('@oclif/command')
2+
const path = require('path')
3+
const fs = require('fs')
4+
const markdownMagic = require('markdown-magic')
5+
const prettier = require('prettier')
6+
const dox = require('dox')
7+
8+
class DocsCommand extends Command {
9+
async run() {
10+
const markdownFiles = [
11+
path.join(process.cwd(), 'README.md'),
12+
'!node_modules'
13+
]
14+
markdownMagic(markdownFiles, config, () => {
15+
console.log(`Analytics documentation updated ${process.cwd()}`)
16+
})
17+
}
18+
}
19+
20+
DocsCommand.description = `Generate Analytic plugin documentation`
21+
22+
module.exports = DocsCommand
23+
24+
var config = {
25+
matchWord: 'ANALYTICS_DOCS',
26+
transforms: {
27+
API(content, options, context) {
28+
let updatedContent = ''
29+
const opts = options || {}
30+
const basePath = path.dirname(context.originalPath)
31+
const browserPath = opts.browser || path.join(basePath, 'src/browser.js')
32+
const nodePath = opts.node || path.join(basePath, 'src/node.js')
33+
34+
if (browserPath && fs.existsSync(browserPath)) {
35+
const fileContents = fs.readFileSync(browserPath, 'utf-8')
36+
const docBlocs = dox.parseComments(fileContents, { raw: true, skipSingleStar: true })
37+
docBlocs.forEach((data) => {
38+
// console.log('data', data)
39+
// updatedContent += `### ${data.ctx.name}\n\n`
40+
updatedContent += `## Plugin Options\n\n`
41+
// updatedContent += `${data.description.full}\n\n`
42+
updatedContent += `${formatArguments(data.tags)}`
43+
updatedContent += formatExample(data.tags).join('\n')
44+
updatedContent += `\n`
45+
})
46+
}
47+
return updatedContent.replace(/^\s+|\s+$/g, '')
48+
},
49+
USAGE(content, options, context) {
50+
let updatedContent = ''
51+
const opts = options || {}
52+
const basePath = path.dirname(context.originalPath)
53+
const browserPath = opts.browser || path.join(basePath, 'src/browser.js')
54+
const nodePath = opts.node || path.join(basePath, 'src/node.js')
55+
const packageJson = fs.readFileSync(path.join(basePath, 'package.json'), 'utf-8')
56+
const pkg = JSON.parse(packageJson)
57+
58+
if (browserPath && fs.existsSync(browserPath)) {
59+
const fileContents = fs.readFileSync(browserPath, 'utf-8')
60+
const docBlocs = dox.parseComments(fileContents, { raw: true, skipSingleStar: true })
61+
62+
docBlocs.forEach((data) => {
63+
const example = data.tags.filter((tag) => {
64+
return tag.type === 'example'
65+
}).map((tag) => {
66+
return tag.string
67+
})
68+
if (example && example[0]) {
69+
updatedContent += renderUsageExample(example[0], data.ctx.name, pkg.name)
70+
updatedContent += `\n`
71+
}
72+
})
73+
}
74+
75+
return updatedContent.replace(/^\s+|\s+$/g, '')
76+
},
77+
METHODZ(content, options, context) {
78+
let updatedContent = ''
79+
const opts = options || {}
80+
const basePath = path.dirname(context.originalPath)
81+
const browserPath = opts.browser || path.join(basePath, 'src/browser.js')
82+
const nodePath = opts.node || path.join(basePath, 'src/node.js')
83+
const packageJson = fs.readFileSync(path.join(basePath, 'package.json'), 'utf-8')
84+
const pkg = JSON.parse(packageJson)
85+
86+
// TODO parse AST and get available methods
87+
const lib = require(path.resolve(pkg.main))
88+
// console.log('lib', lib)
89+
return updatedContent
90+
}
91+
}
92+
}
93+
94+
function formatExample(tags) {
95+
return tags.filter((tag) => {
96+
return tag.type === 'example'
97+
}).map((tag) => {
98+
return renderExample(tag.string)
99+
})
100+
}
101+
102+
function formatArguments(tags) {
103+
const theArgs = tags.filter((tag) => {
104+
return tag.type === 'param'
105+
}).map((tag) => {
106+
return renderArg(tag)
107+
})
108+
// console.log('theArgs', theArgs)
109+
if (theArgs.length) {
110+
return `**Arguments**
111+
112+
${theArgs.join('\n')}
113+
114+
`
115+
}
116+
return ''
117+
}
118+
119+
function renderArg(tag) {
120+
const optionalText = (tag.name.match(/^\[/)) ? '(optional) ' : ''
121+
return `- **${tag.name}** ${optionalText}${tag.typesDescription} ${tag.description}`
122+
}
123+
124+
function renderExample(example) {
125+
return `**Example**
126+
127+
\`\`\`js
128+
${example.replace(/^\s+|\s+$/g, '')}
129+
\`\`\`
130+
`
131+
}
132+
133+
function renderUsageExample(example, name, pkg) {
134+
const code = `
135+
import Analytics from 'analytics'
136+
import ${name} from '${pkg}'
137+
138+
const analytics = Analytics({
139+
app: 'awesome-app',
140+
plugins: [
141+
${example.replace(/^\s+|\s+$/g, '')}
142+
]
143+
})
144+
`
145+
const formattedCode = prettier.format(code, { semi: false, singleQuote: true, parser: 'babel' })
146+
147+
return `## Usage
148+
149+
Install \`analytics\` and \`${pkg}\` packages
150+
151+
\`\`\`
152+
npm install analytics ${pkg}
153+
\`\`\`
154+
155+
Import and initialize in project
156+
157+
\`\`\`js
158+
${formattedCode}
159+
\`\`\``
160+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
module.exports = async context => {
3+
// If no command passed in run switcher
4+
console.log('> Analytics CLI')
5+
}

packages/analytics-cli/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('@oclif/command')

packages/analytics-core/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ analytics.storage.removeItem('storage_key')
536536
The `analytics` has a robust plugin system. Here is a list of currently available plugins:
537537

538538
<!-- AUTO-GENERATED-CONTENT:START (PLUGINS) -->
539+
- [analytics-cli](https://github.com/DavidWells/analytics/tree/master/packages/analytics-cli) CLI for `analytics` pkg [npm link](https://www.npmjs.com/package/analytics-cli).
539540
- [analytics-plugin-customerio](https://github.com/DavidWells/analytics/tree/master/packages/analytics-plugin-customerio) Customer.io plugin for 'analytics' [npm link](https://www.npmjs.com/package/analytics-plugin-customerio).
540541
- [analytics-plugin-do-not-track](https://github.com/DavidWells/analytics/tree/master/packages/analytics-plugin-do-not-track) Disable tracking for opted out visitors [npm link](https://www.npmjs.com/package/analytics-plugin-do-not-track).
541542
- [analytics-plugin-ga](https://github.com/DavidWells/analytics/tree/master/packages/analytics-plugin-ga) Google analytics integration for 'analytics' pkg [npm link](https://www.npmjs.com/package/analytics-plugin-ga).

0 commit comments

Comments
 (0)