Skip to content

Commit

Permalink
Merge pull request #54 from Turbo87/typescript
Browse files Browse the repository at this point in the history
Convert to TypeScript
  • Loading branch information
Turbo87 authored Jun 6, 2017
2 parents bb4158e + d646f47 commit 26dd69c
Show file tree
Hide file tree
Showing 27 changed files with 458 additions and 1,349 deletions.
15 changes: 0 additions & 15 deletions .babelrc

This file was deleted.

7 changes: 0 additions & 7 deletions .eslintrc

This file was deleted.

7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
src
**/__mocks__/**
**/__snapshots__/**
*.spec.js
.editorconfig
.travis.yml
tsconfig.json
tslint.json
yarn.lock
33 changes: 22 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"url": "git+https://github.com/lerna/lerna-changelog.git"
},
"scripts": {
"build": "npm run clean && babel src --out-dir lib --ignore src/__mocks__",
"build": "npm run clean && tsc",
"changelog": "node ./bin/cli.js",
"clean": "rimraf lib",
"fix": "npm run lint -- --fix",
"lint": "eslint bin src test *.js",
"lint": "tslint src test",
"prepublish": "npm run build",
"test": "jest",
"test-ci": "npm run build && jest",
Expand All @@ -41,17 +41,14 @@
"yargs": "^6.6.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-eslint": "^7.1.1",
"babel-jest": "^20.0.3",
"babel-plugin-transform-object-rest-spread": "6.20.2",
"babel-preset-env": "^1.1.11",
"eslint": "^3.13.1",
"eslint-config-babel": "^6.0.0",
"eslint-plugin-flowtype": "^2.30.0",
"@types/jest": "^19.2.3",
"@types/node": "^7.0.22",
"fs-extra": "^2.0.0",
"jest": "^20.0.4",
"rimraf": "^2.5.4"
"rimraf": "^2.5.4",
"ts-jest": "^20.0.4",
"tslint": "^5.3.2",
"typescript": "^2.3.3"
},
"changelog": {
"repo": "lerna/lerna-changelog",
Expand All @@ -64,5 +61,19 @@
"Tag: Internal": ":house: Internal"
},
"cacheDir": ".changelog"
},
"jest": {
"roots": ["src"],
"transform": {
"^.+\\.jsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js",
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
]
}
}
18 changes: 11 additions & 7 deletions src/ApiDataCache.js → src/ApiDataCache.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import fs from "fs";
import path from "path";
import mkdirp from "mkdirp";
const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");

import ConfigurationError from "./ConfigurationError";

export default class ApiDataCache {
constructor(host, { rootPath, cacheDir }) {
host: string;
dir: string;

constructor(host: string, { rootPath, cacheDir }: { rootPath: string, cacheDir: string }) {
this.host = host;
const dir = this.dir = cacheDir && path.join(rootPath, cacheDir, host);

Expand All @@ -17,7 +21,7 @@ export default class ApiDataCache {
}
}

get(type, key) {
get(type: string, key: string): any {
if (!this.dir) return;
try {
return JSON.parse(fs.readFileSync(this.fn(type, key), "utf-8"));
Expand All @@ -26,12 +30,12 @@ export default class ApiDataCache {
}
}

set(type, key, data) {
set(type: string, key: string, data: any) {
if (!this.dir) return;
return fs.writeFileSync(this.fn(type, key), JSON.stringify(data, null, 2));
}

fn(type, key) {
fn(type: string, key: string): string {
const dir = path.join(this.dir, type);

// Ensure the directory for this type is there.
Expand Down
File renamed without changes.
41 changes: 23 additions & 18 deletions src/Changelog.js → src/Changelog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pMap from "p-map";
const pMap = require("p-map");

import progressBar from "./progressBar";
import RemoteRepo from "./RemoteRepo";
Expand All @@ -9,7 +9,12 @@ const UNRELEASED_TAG = "___unreleased___";
const COMMIT_FIX_REGEX = /(fix|close|resolve)(e?s|e?d)? [T#](\d+)/i;

export default class Changelog {
constructor(options = {}) {
config: any;
remote: RemoteRepo;
tagFrom?: string;
tagTo?: string;

constructor(options: any = {}) {
this.config = this.getConfig();
this.remote = new RemoteRepo(this.config);

Expand Down Expand Up @@ -49,9 +54,9 @@ export default class Changelog {
.filter((category) => category.commits.length > 0);

for (const category of categoriesWithCommits) {
progressBar.tick(category.heading);
progressBar.tick(category.heading || "Other");

const commitsByPackage = category.commits.reduce((acc, commit) => {
const commitsByPackage = category.commits.reduce((acc: any, commit: any) => {
// Array of unique packages.
const changedPackages = this.getListOfUniquePackages(commit.commitSHA);

Expand Down Expand Up @@ -109,14 +114,14 @@ export default class Changelog {
return markdown.substring(0, markdown.length - 3);
}

getListOfUniquePackages(sha) {
getListOfUniquePackages(sha: string) {
return Object.keys(
// turn into an array
execSync(
`git show -m --name-only --pretty='format:' --first-parent ${sha}`
)
.split("\n")
.reduce((acc, files) => {
.reduce((acc: any, files: string) => {
if (files.indexOf("packages/") === 0) {
acc[files.slice(9).split("/", 1)[0]] = true;
}
Expand Down Expand Up @@ -150,7 +155,7 @@ export default class Changelog {
`git log --oneline --pretty="%h;%D;%s;%cd" --date=short ${tagsRange}`
);
if (commits) {
return commits.split("\n").map((commit) => {
return commits.split("\n").map((commit: string) => {
const parts = commit.split(";");
const sha = parts[0];
const refName = parts[1];
Expand All @@ -162,8 +167,8 @@ export default class Changelog {
return [];
}

async getCommitters(commits) {
const committers = {};
async getCommitters(commits: any[]) {
const committers: { [id: string]: string } = {};

for (const commit of commits) {
const login = (commit.user || {}).login;
Expand All @@ -172,7 +177,7 @@ export default class Changelog {
const shouldKeepCommiter = login && (
!this.config.ignoreCommitters ||
!this.config.ignoreCommitters.some(
(c) => c === login || login.indexOf(c) > -1
(c: string) => c === login || login.indexOf(c) > -1
)
);
if (login && shouldKeepCommiter && !committers[login]) {
Expand All @@ -195,14 +200,14 @@ export default class Changelog {

progressBar.init(commits.length);

const commitsInfo = await pMap(commits, async (commit) => {
const commitsInfo = await pMap(commits, async (commit: any) => {
const { sha, refName, summary: message, date } = commit;

let tagsInCommit;
if (refName.length > 1) {
// Since there might be multiple tags referenced by the same commit,
// we need to treat all of them as a list.
tagsInCommit = allTags.reduce((acc, tag) => {
tagsInCommit = allTags.reduce((acc: any, tag: string) => {
if (refName.indexOf(tag) < 0)
return acc;
return acc.concat(tag);
Expand All @@ -211,7 +216,7 @@ export default class Changelog {

progressBar.tick(sha);

const commitInfo = {
let commitInfo = {
commitSHA: sha,
message: message,
// Note: Only merge commits or commits referencing an issue / PR
Expand All @@ -226,7 +231,7 @@ export default class Changelog {
const response = await this.remote.getIssueData(issueNumber);
response.commitSHA = sha;
response.mergeMessage = message;
Object.assign(commitInfo, response);
commitInfo = {...commitInfo, ...response};
}

return commitInfo;
Expand All @@ -236,7 +241,7 @@ export default class Changelog {
return commitsInfo;
}

detectIssueNumber(message) {
detectIssueNumber(message: string): string | null {
if (message.indexOf("Merge pull request ") === 0) {
const start = message.indexOf("#") + 1;
const end = message.slice(start).indexOf(" ");
Expand All @@ -251,7 +256,7 @@ export default class Changelog {
return null;
}

async getCommitsByTag(commits) {
async getCommitsByTag(commits: any[]) {
// Analyze the commits and group them by tag.
// This is useful to generate multiple release logs in case there are
// multiple release tags.
Expand Down Expand Up @@ -294,7 +299,7 @@ export default class Changelog {
}, {});
}

getCommitsByCategory(commits) {
getCommitsByCategory(commits: any[]) {
return this.remote.getLabels().map(
(label) => ({
heading: this.remote.getHeadingForLabel(label),
Expand All @@ -304,7 +309,7 @@ export default class Changelog {
(acc, commit) => {
if (
commit.labels.some(
(l) => l.name.toLowerCase() === label.toLowerCase()
(l: any) => l.name.toLowerCase() === label.toLowerCase()
)
)
return acc.concat(commit);
Expand Down
6 changes: 3 additions & 3 deletions test/Configuration.spec.js → src/Configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from "os";
import fs from "fs-extra";
import path from "path";
const os = require("os");
const fs = require("fs-extra");
const path = require("path");

import { fromGitRoot, fromPath } from "../src/Configuration";
import ConfigurationError from "../src/ConfigurationError";
Expand Down
12 changes: 6 additions & 6 deletions src/Configuration.js → src/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import fs from "fs";
import path from "path";
const fs = require("fs");
const path = require("path");

import ConfigurationError from "./ConfigurationError";
import execSync from "./execSync";

export function fromGitRoot(cwd) {
export function fromGitRoot(cwd: string): any {
const rootPath = execSync("git rev-parse --show-toplevel", { cwd });
return fromPath(rootPath);
}

export function fromPath(rootPath) {
export function fromPath(rootPath: string): any {
const config = fromPackageConfig(rootPath) || fromLernaConfig(rootPath);

if (!config) {
Expand All @@ -24,14 +24,14 @@ export function fromPath(rootPath) {
return config;
}

function fromLernaConfig(rootPath) {
function fromLernaConfig(rootPath: string): any | undefined {
const lernaPath = path.join(rootPath, "lerna.json");
if (fs.existsSync(lernaPath)) {
return JSON.parse(fs.readFileSync(lernaPath)).changelog;
}
}

function fromPackageConfig(rootPath) {
function fromPackageConfig(rootPath: string): any | undefined {
const pkgPath = path.join(rootPath, "package.json");
if (fs.existsSync(pkgPath)) {
return JSON.parse(fs.readFileSync(pkgPath)).changelog;
Expand Down
13 changes: 0 additions & 13 deletions src/ConfigurationError.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/ConfigurationError.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ConfigurationError from "../src/ConfigurationError";

describe("ConfigurationError", function() {
it("can be identified using `instanceof`", function() {
const configError = new ConfigurationError('foobar');
expect(configError instanceof ConfigurationError).toEqual(true);

const error = new Error('foobar');
expect(error instanceof ConfigurationError).toEqual(false);
});
});
7 changes: 7 additions & 0 deletions src/ConfigurationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class ConfigurationError {
name = "ConfigurationError";

constructor(message: string) {
Error.apply(this, arguments);
}
}
Loading

0 comments on commit 26dd69c

Please sign in to comment.