Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

LoC reporter for Parcel/Brisk #194

Open
wants to merge 1 commit into
base: noviny/AFP-362-setting-up-pipeline
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
},
"preconstruct": {
"packages": [
"packages/react-changelogs",
"packages/file-viewer",
"packages/parcel-reporter-loc",
"packages/react-changelogs",
"packages/website"
]
},
Expand Down Expand Up @@ -89,6 +90,11 @@
"@mdx-js/loader": "^0.17.5",
"@mdx-js/mdx": "^0.15.0",
"@mdx-js/tag": "^0.17.5",
"@parcel/config-default": "2.0.0-alpha.1.1",
"@parcel/core": "2.0.0-alpha.1.1",
"@parcel/logger": "^2.0.0-alpha.1.1",
"@parcel/plugin": "^2.0.0-alpha.1.1",
"@parcel/utils": "^2.0.0-alpha.1.1",
"@types/js-yaml": "^3.12.1",
"@types/lodash": "^4.14.123",
"@types/lodash.flatten": "^4.4.6",
Expand Down Expand Up @@ -124,6 +130,7 @@
"lodash.snakecase": "^4.1.1",
"next": "^8.0.4",
"next-images": "^1.1.1",
"nullthrows": "^1.1.1",
"outdent": "^0.7.0",
"preconstruct": "^0.0.62",
"pretty-proptypes": "^0.6.6",
Expand Down Expand Up @@ -197,4 +204,4 @@
"ts-loader": "^5.3.3",
"tti-polyfill": "^0.2.2"
}
}
}
20 changes: 20 additions & 0 deletions packages/parcel-reporter-loc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@brisk-docs/parcel-reporter-loc",
"version": "2.0.0-alpha.0",
"main": "dist/parcel-reporter-loc.cjs.js",
"module": "dist/parcel-reporter-loc.esm.js",
"engines": {
"node": ">= 8.0.0",
"parcel": "^2.0.0-alpha.1.1"
},
"scripts": {
"test": "echo this package has no tests yet",
"test-ci": "yarn test"
},
"dependencies": {
"@parcel/logger": "^2.0.0-alpha.1.1",
"@parcel/plugin": "^2.0.0-alpha.1.1",
"@parcel/utils": "^2.0.0-alpha.1.1",
"nullthrows": "^1.1.1"
}
}
31 changes: 31 additions & 0 deletions packages/parcel-reporter-loc/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Reporter } from '@parcel/plugin';
import Logger from '@parcel/logger';
import lineCounter from './lineCounter';

const LOG_LEVELS = {
none: 0,
error: 1,
warn: 2,
info: 3,
progress: 3,
success: 3,
verbose: 4,
};

export default new Reporter({
async report(event, options) {
let logLevelFilter = options.logLevel || 'info';

if (
event.type !== 'buildSuccess' ||
LOG_LEVELS[logLevelFilter] < LOG_LEVELS.info
) {
return;
}

event.bundleGraph.getBundles();
Logger.info(
'Number of lines: ' + (await lineCounter(event.bundleGraph.getBundles())),
);
},
});
71 changes: 71 additions & 0 deletions packages/parcel-reporter-loc/src/lineCounter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const path = require('path');
const fs = require('fs');
const { PromiseQueue } = require('@parcel/utils');
const os = require('os');
const util = require('util');

const exists = util.promisify(fs.exists);

const runtimesPath = path.resolve('../../runtimes');

const lineCounter = async bundles => {
let set = new Set();

for (let bundle of bundles) {
bundle.traverseAssets(asset => {
let { filePath } = asset;

if (filePath != null && !filePath.startsWith(runtimesPath)) {
set.add(filePath);
}
});
}

let queue = new PromiseQueue({ maxConcurrent: os.cpus().length });
let lineCount = 0;
for (let assetPath of set) {
queue
.add(() => countLinesInFile(assetPath))
.then(count => (lineCount += count));
}

await queue.run();
return lineCount;
};

const NEWLINE_CHAR = 10;
const NULL_BYTE = 0; // Only appears in binary files
const countLinesInFile = async filePath => {
// Parcel sometimes assigns filePaths to assets that don't exist
if (!(await exists(filePath))) {
return 0;
}

return new Promise((resolve, reject) => {
let lineCount = 0;

let stream = fs
.createReadStream(filePath)
.on('data', buf => {
let i = -1;
lineCount--;

if (buf.includes(NULL_BYTE)) {
stream.destroy();
resolve(0);
return;
}

do {
i = buf.indexOf(NEWLINE_CHAR, i + 1);
lineCount++;
} while (i !== -1);
})
.on('end', () => {
resolve(lineCount);
})
.on('error', reject);
});
};

export default lineCounter;
4 changes: 4 additions & 0 deletions packages/website/.parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["@parcel/config-default"],
"reporters": ["...", "@brisk-docs/parcel-reporter-loc"]
}
36 changes: 26 additions & 10 deletions packages/website/_app.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import React, { useState } from 'react';
import React from 'react';
import {
BrowserRouter as Router,
Switch,
Route,
Redirect,
} from 'react-router-dom';
import { asyncComponent } from 'react-async-component';

const HackyLoader = ({ promise }) => {
const [Comp, setComp] = useState(null);
promise.then(res => setComp(res));
if (Comp) return <Comp.default />;
return <div>Loading</div>;
};
const AsyncLoadPages = promise =>
asyncComponent({
resolve: () => promise,
LoadingComponent: () => <div>Loading</div>,
ErrorComponent: ({ error }) => (
<div>There is an error. {error.message} </div>
),
});

export default () => (
<Router>
<Switch>
<Route exact path="/" component={AsyncLoadPages(import('./pages'))} />
<Route
exact
path="/"
component={() => <HackyLoader promise={import('./pages')} />}
path="/healthcheck"
component={AsyncLoadPages(import('./pages/healthcheck.tsx'))}
/>
<Route
path="/index"
component={AsyncLoadPages(import('./pages/index.tsx'))}
/>
<Route
path="/packages"
component={AsyncLoadPages(import('./pages/packages.tsx'))}
/>
<Route
path="/readme"
component={AsyncLoadPages(import('./pages/readme.js'))}
/>
<Redirect to="/" />
</Switch>
</Router>
);
5 changes: 4 additions & 1 deletion packages/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"dist"
],
"dependencies": {
"@parcel/core": "2.0.0-alpha.1.1",
"@parcel/config-default": "2.0.0-alpha.1.1",
"@atlaskit/avatar": "^14.1.8",
"@atlaskit/breadcrumbs": "^8.0.0",
"@atlaskit/button": "^8.2.4",
Expand Down Expand Up @@ -75,6 +77,7 @@
"@babel/runtime": "^7.4.3",
"@brisk-docs/file-viewer": "^0.2.3",
"@brisk-docs/react-changelogs": "^0.1.9",
"@brisk-docs/parcel-reporter-loc": "2.0.0-alpha.0",
"@emotion/core": "^10.0.9",
"@emotion/styled": "^10.0.9",
"@mdx-js/loader": "^0.17.5",
Expand Down Expand Up @@ -141,4 +144,4 @@
"mock-fs": "^4.10.1",
"ts-loader": "^5.3.3"
}
}
}
44 changes: 14 additions & 30 deletions scripts/link-parcel.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,28 @@
const fs = require('fs-extra');
const path = require('path');

function linkParcel() {
async function linkParcel() {
const parcelHome = process.argv[2];

if (!parcelHome) {
console.warn('no parcel home provided');
return;
}
const nodeModulesParcel = path.resolve(__dirname, '../node_modules/@parcel');

const parcelCore = path.resolve(parcelHome, 'packages', 'core', 'core');
const parcelConfig = path.resolve(
parcelHome,
'packages',
'configs',
'default',
);
const nodeCore = path.resolve(
process.cwd(),
'node_modules',
'@parcel',
'core',
);
const nodeConfig = path.resolve(
process.cwd(),
'node_modules',
'@parcel',
'config-default',
);
try {
await fs.remove(nodeModulesParcel);
await fs.ensureSymlink(
path.resolve(parcelHome, 'node_modules/@parcel'),
nodeModulesParcel,
);
} catch (e) {
console.error(e);
}

Promise.all([
fs.ensureSymlink(parcelCore, nodeCore),
fs.ensureSymlink(parcelConfig, nodeConfig),
])
.then(() => {
console.log(
'symlink to parcel should have been successful. Good luck deving!',
);
})
.catch(e => console.error(e));
console.log(
'symlink to parcel should have been successful. Good luck deving!',
);
}

linkParcel();
Loading