This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in merge-line-counter (pull request #2)
Merge line counter * line counter * line counter * fixed lint errors * removed unnecessary debugger statements Approved-by: Trey Shugart <tshugart@atlassian.com>
- Loading branch information
1 parent
d2f2ca0
commit 642f64f
Showing
7 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@parcel/reporter-loc", | ||
"version": "2.0.0-alpha.1", | ||
"main": "src/LOCReporter.js", | ||
"engines": { | ||
"node": ">= 8.0.0", | ||
"parcel": "^2.0.0-alpha.1" | ||
}, | ||
"scripts": { | ||
"test": "echo this package has no tests yet", | ||
"test-ci": "yarn test" | ||
}, | ||
"dependencies": { | ||
"@parcel/logger": "^2.0.0-alpha.1", | ||
"@parcel/plugin": "^2.0.0-alpha.1", | ||
"@parcel/types": "^2.0.0-alpha.1", | ||
"@parcel/utils": "^2.0.0-alpha.1", | ||
"nullthrows": "^1.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @flow strict-local | ||
|
||
import type {ParcelOptions, ReporterEvent} from '@parcel/types'; | ||
|
||
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: ReporterEvent, options: ParcelOptions) { | ||
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())) | ||
); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// @flow | ||
|
||
import type {Bundle} from '@parcel/types'; | ||
|
||
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: Array<Bundle>) => { | ||
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: Buffer) => { | ||
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; |