-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
3,662 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
.idea/ | ||
node_modules/ | ||
screenshots/ | ||
tests/coverage | ||
|
||
.lens.config.js | ||
.lens.config.json | ||
yarn-error.log |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
.eslintrc.json | ||
.github/ | ||
.idea/ | ||
.lens.config.js | ||
.lens.config.json | ||
.yarnrc | ||
.vscode/ | ||
jest.config.js | ||
package-lock.json | ||
tsconfig.json | ||
screenshots | ||
tests | ||
yarn.lock |
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,24 @@ | ||
module.exports = { | ||
collectCoverageFrom: [ | ||
"**/*.{js,ts}", | ||
"!**/node_modules/**", | ||
"!**/vendor/**", | ||
"!**/tests/**" | ||
], | ||
coverageDirectory: '<rootDir>/tests/coverage', | ||
moduleFileExtensions: [ | ||
'ts', 'js' | ||
], | ||
moduleNameMapper: { | ||
'^@/(.*)$': '<rootDir>/src/$1' | ||
}, | ||
roots: [ | ||
'<rootDir>/src', | ||
'<rootDir>/tests' | ||
], | ||
testEnvironment: 'node', | ||
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$', | ||
transform: { | ||
'^.+\\.ts?$': 'ts-jest' | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,78 @@ | ||
import path from 'path' | ||
import nodeUrl, { UrlWithStringQuery } from 'url' | ||
|
||
import { ArgumentParser, LensArguments, ParsedLensArguments, Resolution } from '@/typings/types' | ||
import { defaultResolutions } from '@/resolutions' | ||
import { LensResolutionError } from '@/errors' | ||
|
||
export default class DefaultArgumentParser implements ArgumentParser { | ||
parse (args: LensArguments): ParsedLensArguments { | ||
return { | ||
urls: this.parseUrls(args.url), | ||
resolutions: this.parseResolution(args.resolution), | ||
tag: args.tag, | ||
|
||
outputDir: this.parseDirectory(args.outputDir) | ||
} | ||
} | ||
|
||
/** | ||
* Resolve path to directory if present. | ||
* Return undefined otherwise. | ||
* | ||
* @param dir | ||
* @private | ||
*/ | ||
private parseDirectory (dir?: string): string | undefined { | ||
if (dir) { | ||
return path.resolve(dir) | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
/** | ||
* Retrieve all resolutions from the input argument. | ||
* If argument is not present, use default resolutions list. | ||
* | ||
* @param resolution | ||
* @private | ||
*/ | ||
private parseResolution (resolution?: string): Record<string, Resolution[]> { | ||
if (!resolution) { | ||
return defaultResolutions | ||
} | ||
|
||
const resolutions = resolution.split(' ') | ||
.map(res => { | ||
const parsedResolution = res.trim() | ||
.split('x') | ||
.map(x => parseInt(x, 10)) | ||
|
||
if (parsedResolution.length !== 2) { | ||
throw new LensResolutionError(`Invalid resolution ${res}. It should follow format [width]x[height]`) | ||
} | ||
|
||
return parsedResolution | ||
}) | ||
.map(res => { | ||
return { | ||
width: res[0], | ||
height: res[1] | ||
} | ||
}) | ||
|
||
return { default: resolutions } | ||
} | ||
|
||
/** | ||
* Retrieve all urls from the input arguments | ||
* | ||
* @param url | ||
* @private | ||
*/ | ||
private parseUrls (url: string): UrlWithStringQuery[] { | ||
return url.split(' ') | ||
.map(u => nodeUrl.parse(u)) | ||
} | ||
} |
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,51 @@ | ||
import { cosmiconfig } from 'cosmiconfig' | ||
import { CosmiconfigResult } from 'cosmiconfig/dist/types' | ||
import isInstalledGlobally from 'is-installed-globally' | ||
import { defaultsDeep } from 'lodash' | ||
import os from 'os' | ||
import packageDir from 'pkg-dir' | ||
import path from 'path' | ||
|
||
import { LensConfig } from '@/typings/types' | ||
|
||
const defaultConfig: LensConfig = { | ||
directories: { | ||
output: path.resolve('./screenshots') | ||
}, | ||
puppeteer: { | ||
headless: true, | ||
waitUntil: [ | ||
'load', 'networkidle2' | ||
] | ||
} | ||
} | ||
|
||
const config = async (): Promise<LensConfig> => { | ||
const searchDirectory = isInstalledGlobally | ||
? os.homedir() | ||
: await packageDir() | ||
|
||
const searchPlaces = [ | ||
'.lens.config.json', | ||
'.lens.config.js' | ||
] | ||
|
||
const explorer = cosmiconfig('lens', { | ||
searchPlaces: searchPlaces, | ||
stopDir: searchDirectory, | ||
transform (result: CosmiconfigResult): CosmiconfigResult { | ||
// Resolve output filepath | ||
if (result.config.directories && result.config.directories.output) { | ||
result.config.directories.output = path.resolve(result.config.directories.output) | ||
} | ||
|
||
return result | ||
} | ||
}) | ||
|
||
const { config } = (await explorer.search(searchDirectory)) || { config: {} } | ||
|
||
return defaultsDeep(config, defaultConfig) | ||
} | ||
|
||
export default config |
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 @@ | ||
class LensError extends Error { | ||
constructor (message: string) { | ||
super(message) | ||
|
||
this.name = 'LensError' | ||
} | ||
} | ||
|
||
class LensResolutionError extends LensError { | ||
constructor (message: string) { | ||
super(message) | ||
|
||
this.name = 'LensResolutionError' | ||
} | ||
} | ||
|
||
export { | ||
LensError, | ||
LensResolutionError | ||
} |
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
Oops, something went wrong.