Skip to content

Commit

Permalink
feat: changelogger initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
dcalsky committed Jan 25, 2018
0 parents commit c6166a3
Show file tree
Hide file tree
Showing 13 changed files with 1,349 additions and 0 deletions.
120 changes: 120 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

dist/
.idea/

.changelogger

.vscode/
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "changelogger",
"version": "0.0.1",
"bin": "dist/cli/changelogger.js",
"main": "dist/index.js",
"author": "dcalsky",
"license": "Apache-2.0",
"keywords": [
"changelog"
],
"scripts": {
"build": "tsc",
"cli": "node dist/src/changelogger.js",
"test": "mocha -r ts-node/register ./test/*.test.ts"
},
"dependencies": {
"chalk": "^2.3.0",
"changelog-parser": "^2.1.0",
"cheerio": "^1.0.0-rc.2",
"marked": "^0.3.7",
"yargs": "^11.0.0"
},
"devDependencies": {
"@types/chai": "^4.0.10",
"@types/chalk": "^2.2.0",
"@types/cheerio": "^0.22.6",
"@types/marked": "^0.3.0",
"@types/mocha": "^2.2.44",
"@types/node": "^8.5.1",
"@types/yargs": "^10.0.1",
"chai": "^4.1.2",
"mocha": "^4.0.1",
"ts-node": "^4.0.2",
"typescript": "^2.6.2"
}
}
15 changes: 15 additions & 0 deletions src/changelogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Logger } from "./logger";
import { Parser, Store } from "./parser/index";

export class Changelogger {
private parser: Parser;
private store: Store = new Store();
private logger: Logger;
constructor(public changelogPath: string, public loggerPath: string) {
this.parser = new Parser(changelogPath, this.store);
}
public run() {
this.parser.parse();
this.logger = new Logger(this.loggerPath, this.store);
}
}
14 changes: 14 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import yargs = require("yargs");
import path = require("path");
import { Changelogger } from "./changelogger";

const defaultChangelogPath = path.join(process.cwd(), "CHANGELOG");
const defaultLoggerPath = path.join(process.cwd(), ".changelogger");

const argv = yargs
.usage("Usage: $0 -c [changelog path] -l [logger path]")
.default("c", defaultChangelogPath)
.default("l", defaultLoggerPath).argv;

const changelogger = new Changelogger(argv.c, argv.l);
changelogger.run();
44 changes: 44 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs = require("fs");
import chalk = require("chalk");
import { Store, Version } from "./parser/index";

export class Logger {
constructor(private loggerPath: string, private store: Store) {
if (this.findLoggerFile()) {
const freshVersions = this.getFreshVersions();
this.log(freshVersions);
}
this.wrtieLoggerFile();
}

private getFreshVersions() {
const index = parseInt(fs.readFileSync(this.loggerPath).toString());
return this.store.getFreshVersions(index);
}

private findLoggerFile(): boolean {
return fs.existsSync(this.loggerPath);
}

private wrtieLoggerFile(): void {
fs.writeFileSync(this.loggerPath, 0);
}

public log(versions: Version[]) {
versions.forEach(version => {
this.displayVersion(version);
console.log("#".repeat(20) + "\n");
});
}

private displayVersion(version: Version) {
const log = console.log;
log(chalk.default.bgBlue.white(version.title));
version.changes.forEach(change => {
log("\n" + chalk.default.greenBright(change.type));
change.items.forEach(item => {
log(item);
});
});
}
}
2 changes: 2 additions & 0 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./parser";
export * from "./store";
125 changes: 125 additions & 0 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import marked = require("marked");
import cheerio = require("cheerio");
import fs = require("fs");
import { Version, Change, Store } from "./store";

const VERSION_FLAG = "h2";
const CHANGE_FLAG = "h3";
const LIST_FLAG = "ul";

enum ParserStatus {
VERSION,
CHANGE,
LIST,
END
}

export class Parser {
private $: CheerioStatic;
private status: ParserStatus = ParserStatus.VERSION;
private currentElement: Cheerio;
private currentVersion: Version;
private currentChange: Change;

constructor(private filePath: string, public store: Store, options = {}) {
if (!this.changelogExist()) {
throw "Changlog is not existed.";
}
const content = fs.readFileSync(filePath).toString();
const html = marked(content);
this.$ = cheerio.load(html);
this.currentElement = this.$("h2");
}

private changelogExist(): boolean {
return fs.existsSync(this.filePath);
}

public parse(): void {
const el = this.currentElement.first()[0];
if (!el) {
return;
}
const name = el.tagName;
let stop = false;
switch (this.status) {
case ParserStatus.VERSION:
if (name == CHANGE_FLAG) {
this.status = ParserStatus.CHANGE;
stop = true;
break;
}
if (this.currentVersion) {
this.pushVersion();
}
this.addVersion();
break;
case ParserStatus.CHANGE:
this.addChange();
this.status = ParserStatus.LIST;
break;
case ParserStatus.LIST:
if (name == VERSION_FLAG) {
this.status = ParserStatus.VERSION;
this.pushChange();
stop = true;
break;
} else if (name == CHANGE_FLAG) {
this.status = ParserStatus.CHANGE;
this.pushChange();
stop = true;
break;
}
this.addList();
break;
default:
break;
}

if (!stop) {
this.currentElement = this.currentElement.next();
}
this.parse();
}
private pushVersion() {
this.store.addVersion(this.currentVersion);
}
private pushChange() {
this.currentVersion.changes.push(this.currentChange);
}
private addVersion() {
const el = this.currentElement.first();
const name = el[0].tagName;
if (name !== VERSION_FLAG) {
throw "error h2";
}
this.currentVersion = new Version(el.text());
}

private addChange() {
const name = this.currentElement.first()[0].tagName;
if (name !== CHANGE_FLAG) {
throw "error h3";
}
this.currentChange = new Change();
const text = this.currentElement[0].children[0].data;
if (!this.currentChange.setType(text)) {
throw "error set type";
}
}

private addList() {
const name = this.currentElement.first()[0].tagName;
if (name !== LIST_FLAG) {
throw "error ul";
}
const list = this.currentElement.first().children("li");
list.each((i, el) => {
this.currentChange.items.push(this.getLiText(el));
});
}

private getLiText(li: CheerioElement): string {
return li.children[0].data;
}
}
Loading

0 comments on commit c6166a3

Please sign in to comment.