Skip to content

Commit

Permalink
Use ESM, add ignore paths option (#7)
Browse files Browse the repository at this point in the history
* Add ignore paths option
* Upgrade dependencies, move to es modules
* Use node 16 for build
  • Loading branch information
aeharding authored May 12, 2022
1 parent a1dddfa commit 2fd5be0
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [15.x]
node-version: [16.x]

steps:
- uses: actions/checkout@v2
Expand Down
15 changes: 9 additions & 6 deletions lib/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import execa from "execa";
import { execa } from "execa";
import glob from "glob-promise";
import { eachDayOfInterval, format, isEqual, parse } from "date-fns";
import * as fs from "fs/promises";
import sloc from "sloc";
import * as git from "./git";
import { Options, SlocData, Stat, Stats } from "./";
import * as git from "./git.js";
import { Options, SlocData, Stat, Stats } from "./index.js";
import path from "path";
import * as cache from "./cache";
import { UncleanGitWorkingTreeError, InterruptError } from "./errors";
import * as cache from "./cache.js";
import { UncleanGitWorkingTreeError, InterruptError } from "./errors.js";

let currentGitRef: string | undefined;
let interrupt = false;
Expand Down Expand Up @@ -122,7 +122,10 @@ async function getStatsFor(
const files = await glob(
path.resolve(options.path, "**", `*.{${type},${type}x}`),
{
ignore: "**/node_modules/**",
ignore: [
"**/node_modules/**",
...(options.ignore?.map((p) => path.resolve(p)) || []),
],
}
);
const fileContents = await Promise.all(files.map((f) => fs.readFile(f)));
Expand Down
12 changes: 8 additions & 4 deletions lib/cache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import pathFs from "path";
import * as fse from "fs-extra";
import { Options, Stat } from "./";
import { uniqBy } from "lodash";
import { sortStats } from "./analyze";
import fse from "fs-extra";
import { Options, Stat } from "./index.js";
import uniqBy from "lodash/uniqBy.js";
import { sortStats } from "./analyze.js";
import { dirname } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));

const VERSION: string = fse.readJSONSync(
pathFs.resolve(__dirname, "../package.json")
Expand Down
8 changes: 7 additions & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { subDays } from "date-fns";
import path from "path";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import run from "./";
import run from "./index.js";

yargs(hideBin(process.argv))
.option("verbose", {
Expand Down Expand Up @@ -34,6 +34,12 @@ yargs(hideBin(process.argv))
description: "First day (start bound) of analysis",
default: subDays(new Date(), 90).toISOString().slice(0, 10),
})
.option("ignore", {
alias: "i",
type: "array",
description: "Ignore certain paths",
default: [],
})
.alias("start", "from")
.option("end", {
alias: "e",
Expand Down
8 changes: 6 additions & 2 deletions lib/generateReport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import path from "path";
import * as fse from "fs-extra";
import fse from "fs-extra";
import { ChartJSNodeCanvas } from "chartjs-node-canvas";
import Chart, { ChartConfiguration } from "chart.js";
import { Options, Stat } from "./";
import { Options, Stat } from "./index.js";
import { dirname } from "path";
import { fileURLToPath } from "url";

const projectName: string = (() => {
if (!fse.existsSync("package.json")) return "My Project";
Expand All @@ -13,6 +15,8 @@ const projectName: string = (() => {
const width = 1200;
const height = 800;

const __dirname = dirname(fileURLToPath(import.meta.url));

export async function generateReport(statsPerDay: Stat[], options: Options) {
const chartConfig: ChartConfiguration = {
plugins: [
Expand Down
2 changes: 1 addition & 1 deletion lib/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import execa from "execa";
import { execa } from "execa";

export async function getRef() {
const { stdout: ref } = await execa("git", [
Expand Down
14 changes: 8 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import path from "path";
import * as fse from "fs-extra";
import fse from "fs-extra";
import sloc from "sloc";
import { analyze } from "./analyze";
import { generateReport } from "./generateReport";
import * as cache from "./cache";
import { InterruptError, UncleanGitWorkingTreeError } from "./errors";
import { analyze } from "./analyze.js";
import { generateReport } from "./generateReport.js";
import * as cache from "./cache.js";
import { InterruptError, UncleanGitWorkingTreeError } from "./errors.js";
import chalk from "chalk";

export * from "./errors";
export * from "./errors.js";
export { generateReport, analyze };

export interface Options {
output: string;
path: string;
start: string;
end: string;
clean?: boolean;
dark?: boolean;
ignore?: string[];
}

export type SlocData = Record<sloc.Key, number>;
Expand Down
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.0.9",
"description": "Track typescript conversion velocity for your big projects",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"author": "Alexander Harding <[email protected]>",
"license": "MIT",
"type": "module",
"scripts": {
"build": "yarn clean; tsc; chmod +x dist/cli.js",
"clean": "rm -rf ./dist",
Expand All @@ -23,27 +23,27 @@
"ts-reporter": "./dist/cli.js"
},
"devDependencies": {
"@types/chart.js": "^2.9.32",
"@types/fs-extra": "^9.0.11",
"@types/lodash": "^4.14.168",
"@types/node": "^15.0.1",
"@types/chart.js": "^2.9.37",
"@types/fs-extra": "^9.0.13",
"@types/lodash": "^4.14.182",
"@types/node": "^17.0.32",
"@types/sloc": "^0.2.0",
"@types/yargs": "^16.0.1",
"ts-node": "^9.1.1",
"typescript": "^4.2.4",
"@types/yargs": "^17.0.10",
"ts-node": "^10.7.0",
"typescript": "^4.6.4",
"watch": "^1.0.2"
},
"dependencies": {
"chalk": "^4.1.1",
"chalk": "^5.0.1",
"chart.js": "^2.9.4",
"chartjs-node-canvas": "^3.1.0",
"date-fns": "^2.21.1",
"execa": "^5.0.0",
"fs-extra": "^9.1.0",
"glob": "^7.1.6",
"glob-promise": "^4.1.0",
"chartjs-node-canvas": "^3.2.0",
"date-fns": "^2.28.0",
"execa": "^6.1.0",
"fs-extra": "^10.1.0",
"glob": "^8.0.1",
"glob-promise": "^4.2.2",
"lodash": "^4.17.21",
"sloc": "^0.2.1",
"yargs": "^16.2.0"
"yargs": "^17.5.0"
}
}
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ for TESTDIR in $(ls -d test/??); do
cd $DIR
../../../"$TESTDIR"/setup.sh "$DIR"
cd $OLDPWD
npx ts-node "$FILE" "$DIR"
node --loader ts-node/esm "$FILE" "$DIR"
if [ $? = 0 ]; then
echo "ok $N $FILE"
else
Expand Down
7 changes: 5 additions & 2 deletions test/01/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import assert from "assert";
import * as fse from "fs-extra";
import { run } from "../utils/misc";
import fse from "fs-extra";
import { run } from "../utils/misc.js";
import { dirname } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const DIR = process.argv[2];

run(async () => {
Expand Down
87 changes: 87 additions & 0 deletions test/02/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"data": [
{
"date": "2021-01-01T00:00:00.000Z",
"stats": {
"js": {
"total": 1,
"source": 1,
"comment": 0,
"single": 0,
"block": 0,
"mixed": 0,
"empty": 0,
"todo": 0,
"blockEmpty": 0
},
"ts": {
"total": 0,
"source": 0,
"comment": 0,
"single": 0,
"block": 0,
"mixed": 0,
"blockEmpty": 0,
"empty": 0,
"todo": 0
}
}
},
{
"date": "2021-01-02T00:00:00.000Z",
"stats": {
"js": {
"total": 1,
"source": 1,
"comment": 0,
"single": 0,
"block": 0,
"mixed": 0,
"empty": 0,
"todo": 0,
"blockEmpty": 0
},
"ts": {
"total": 1,
"source": 1,
"comment": 0,
"single": 0,
"block": 0,
"mixed": 0,
"empty": 0,
"todo": 0,
"blockEmpty": 0
}
}
},
{
"date": "2021-01-03T00:00:00.000Z",
"stats": {
"js": {
"total": 1,
"source": 1,
"comment": 0,
"single": 0,
"block": 0,
"mixed": 0,
"empty": 0,
"todo": 0,
"blockEmpty": 0
},
"ts": {
"total": 2,
"source": 2,
"comment": 0,
"single": 0,
"block": 0,
"mixed": 0,
"empty": 0,
"todo": 0,
"blockEmpty": 0
}
}
}
],
"path": "/Users/aeharding/ts-reporter/temp/2021-04-30_05:03:08/0",
"version": "0.0.8"
}
1 change: 1 addition & 0 deletions test/02/image.png.shasum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
664dba8f23929c7dbbe1d5b64baeb5f25d380a7c
31 changes: 31 additions & 0 deletions test/02/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import assert from "assert";
import fse from "fs-extra";
import { run } from "../utils/misc.js";
import { dirname } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const DIR = process.argv[2];

run(async () => {
const resultDataPath = `${DIR}/reports/ts-migration/data.json`;
assert.ok(fse.existsSync(resultDataPath));

const result = fse.readJSONSync(resultDataPath);
const expected = fse.readJSONSync(`${__dirname}/data.json`);

delete result.path;
delete expected.path;
delete result.version;
delete expected.version;

assert.deepStrictEqual(result, expected);

// TODO - need better solution
// assert.strictEqual(
// (
// await execa("shasum", [`${DIR}/reports/ts-migration/image.png`])
// ).stdout.split(" ")[0],
// fse.readFileSync(`${__dirname}/image.png.shasum`).toString()
// );
});
17 changes: 17 additions & 0 deletions test/02/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
git init
echo "{}" > package.json
echo "const foo = 0;\n const bar = 1;\n const baz = 2;" > code1.js
git add .
git commit -m "commit 1" --date "01-01-21T00:00:00.000Z"
echo "ts codes 1" > code2.ts
mkdir ignorethispath
echo "ts codes 1" > ignorethispath/blah.ts
git add .
git commit -m "commit 2" --date "01-02-21T00:00:00.000Z"
echo "ts codes 2" > code3.ts
git add .
git commit -m "commit 3" --date "01-03-21T00:00:00.000Z"

git rebase --root --committer-date-is-author-date

npx ts-reporter build . --from 2021-01-01 --to 2021-01-03 --ignore "ignorethispath/*"
2 changes: 1 addition & 1 deletion test/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export async function run(test: () => Promise<void>) {
try {
await test();
//process.exit(0);
} catch (err) {
} catch (err: any) {
console.log(" ---");
console.log(` message: ${err.message}`);
console.log(` actual: ${JSON.stringify(err.actual)}`);
Expand Down
21 changes: 10 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"target": "es2020",
"module": "es2020",
"lib": ["es2020", "DOM"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"baseUrl": "lib",
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": true,
"outDir": "./dist",
"lib": ["ES2015", "DOM"]
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"outDir": "dist",
"typeRoots": ["node_modules/@types"]
},
"exclude": ["test/**", "temp/**"]
}

0 comments on commit 2fd5be0

Please sign in to comment.