Skip to content

Commit

Permalink
feat(main): refactored main and added index.d.ts
Browse files Browse the repository at this point in the history
Refactored main.js file to return saved images array, html content of  meta tags and manifest json
icons array. Added interfaces and docs via index.d.ts and updated readme.

fix #5
  • Loading branch information
onderceylan committed Sep 12, 2019
1 parent afce4af commit 72a7fe4
Show file tree
Hide file tree
Showing 15 changed files with 452 additions and 96 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"ecmaVersion": 2018
},
"rules": {
"no-prototype-builtins": "off"
}
}
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ __snapshots__/*
.prettierrc
.travis.yml
.releaserc
.idea
.github
*.test.js
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ $ pwa-asset-generator --help
-b --background Page background to use when image source is provided: css value [default: transparent]
-o --opaque Making screenshots to be saved with a background color [default: true]
-p --padding Padding to use when image source provided: css value [default: "10%"]
-s --scrape Scraping Apple Human Interface Guidelines to fetch splash screen specs [default: true]
-m --manifest Web app manifest file path to automatically update manifest file with the generated images
-i --index Index html file path to automatically put splash screen meta tags in
-s --scrape Scraping Apple Human Interface guidelines to fetch splash screen specs [default: true]
-m --manifest Web app manifest file path to automatically update manifest file with the generated icons
-i --index Index html file path to automatically put splash screen and icon meta tags in
-t --type Image type: png|jpeg [default: png]
-q --quality Image quality: 0...100 (Only for JPEG) [default: 100]
-h --splash-only Only generate splash screens [default: false]
-c --icon-only Only generate icons [default: false]
-l --landscape-only Only generate landscape splash screens [default: false]
-r --portrait-only Only generate portrait splash screens [default: false]
-g --log Logs the steps of the library process [default: true]
Examples
$ pwa-asset-generator logo.html .
Expand All @@ -95,6 +96,26 @@ $ pwa-asset-generator --help
--icon-only
--landscape-only
--portrait-only
--log=false
```

### Module

```javascript
const pwaAssetGenerator = require('pwa-asset-generator');

(async () => {
const { savedImages, htmlContent, manifestJsonContent } = await pwaAssetGenerator.generateImages(
'https://raw.githubusercontent.com/onderceylan/pwa-asset-generator/HEAD/static/logo.png',
'./temp',
{
scrape: false,
background: "linear-gradient(to right, #fa709a 0%, #fee140 100%)",
splashOnly: true,
portraitOnly: true,
log: false
});
})();
```

## Troubleshooting
Expand Down
7 changes: 7 additions & 0 deletions __snapshots__/cli.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports[`generates icons and splash screens when both only flags exist 1`] = `
]
<link rel=\\"apple-touch-icon\\" sizes=\\"180x180\\" href=\\"temp/apple-icon-180.png\\">
<link rel=\\"apple-touch-icon\\" sizes=\\"167x167\\" href=\\"temp/apple-icon-167.png\\">
<link rel=\\"apple-touch-icon\\" sizes=\\"152x152\\" href=\\"temp/apple-icon-152.png\\">
Expand Down Expand Up @@ -82,6 +83,7 @@ exports[`generates icons and splash screens when both only flags exist 1`] = `
<link rel=\\"apple-touch-startup-image\\"
href=\\"temp/apple-splash-1136-640.png\\"
media=\\"(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) and (orientation: landscape)\\">
"
`;
Expand All @@ -101,12 +103,14 @@ exports[`generates icons only 1`] = `
]
<link rel=\\"apple-touch-icon\\" sizes=\\"180x180\\" href=\\"temp/apple-icon-180.png\\">
<link rel=\\"apple-touch-icon\\" sizes=\\"167x167\\" href=\\"temp/apple-icon-167.png\\">
<link rel=\\"apple-touch-icon\\" sizes=\\"152x152\\" href=\\"temp/apple-icon-152.png\\">
<link rel=\\"apple-touch-icon\\" sizes=\\"120x120\\" href=\\"temp/apple-icon-120.png\\">
<meta name=\\"apple-mobile-web-app-capable\\" content=\\"yes\\">
"
`;
Expand Down Expand Up @@ -144,6 +148,7 @@ exports[`generates landscape splash screens only 1`] = `
<link rel=\\"apple-touch-startup-image\\"
href=\\"temp/apple-splash-1136-640.png\\"
media=\\"(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) and (orientation: landscape)\\">
"
`;
Expand Down Expand Up @@ -181,6 +186,7 @@ exports[`generates portrait splash screens only 1`] = `
<link rel=\\"apple-touch-startup-image\\"
href=\\"temp/apple-splash-640-1136.png\\"
media=\\"(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) and (orientation: portrait)\\">
"
`;
Expand Down Expand Up @@ -248,5 +254,6 @@ exports[`generates splash screens only 1`] = `
<link rel=\\"apple-touch-startup-image\\"
href=\\"temp/apple-splash-1136-640.png\\"
media=\\"(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2) and (orientation: landscape)\\">
"
`;
72 changes: 8 additions & 64 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const meow = require('meow');
const preLogger = require('./helpers/logger');
const main = require('./main');
const { FLAGS: flags } = require('./config/constants');

const logger = preLogger('cli');
const cli = meow(
`
$ pwa-asset-generator --help
Expand All @@ -18,15 +18,16 @@ $ pwa-asset-generator --help
-b --background Page background to use when image source is provided: css value [default: transparent]
-o --opaque Making screenshots to be saved with a background color [default: true]
-p --padding Padding to use when image source provided: css value [default: "10%"]
-s --scrape Scraping Apple Human Interface Guidelines to fetch splash screen specs [default: true]
-m --manifest Web app manifest file path to automatically update manifest file with the generated images
-i --index Index html file path to automatically put splash screen meta tags in
-s --scrape Scraping Apple Human Interface guidelines to fetch splash screen specs [default: true]
-m --manifest Web app manifest file path to automatically update manifest file with the generated icons
-i --index Index html file path to automatically put splash screen and icon meta tags in
-t --type Image type: png|jpeg [default: png]
-q --quality Image quality: 0...100 (Only for JPEG) [default: 100]
-h --splash-only Only generate splash screens [default: false]
-c --icon-only Only generate icons [default: false]
-l --landscape-only Only generate landscape splash screens [default: false]
-r --portrait-only Only generate portrait splash screens [default: false]
-g --log Logs the steps of the library process [default: true]
Examples
$ pwa-asset-generator logo.html .
Expand All @@ -47,70 +48,13 @@ $ pwa-asset-generator --help
--icon-only
--landscape-only
--portrait-only
--log=false
`,
{
flags: {
background: {
type: 'string',
alias: 'b',
default: 'transparent',
},
manifest: {
type: 'string',
alias: 'm',
},
index: {
type: 'string',
alias: 'i',
},
opaque: {
type: 'boolean',
alias: 'o',
default: true,
},
scrape: {
type: 'boolean',
alias: 's',
default: true,
},
padding: {
type: 'string',
alias: 'p',
default: '10%',
},
type: {
type: 'string',
alias: 't',
default: 'png',
},
quality: {
type: 'number',
alias: 'q',
default: 100,
},
splashOnly: {
type: 'boolean',
alias: 'h',
default: false,
},
iconOnly: {
type: 'boolean',
alias: 'c',
default: false,
},
landscapeOnly: {
type: 'boolean',
alias: 'l',
default: false,
},
portraitOnly: {
type: 'boolean',
alias: 'r',
default: false,
},
},
flags,
},
);
const logger = preLogger('cli', cli.flags);

(async () => {
try {
Expand Down
10 changes: 5 additions & 5 deletions cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('generates icons only', async () => {
const { stdout } = await execa(
'./cli.js',
['./static/logo.png', './temp', '-s=false', '--icon-only'],
{ env: { PAG_NO_TRACE: '1' } },
{ env: { PAG_TEST_MODE: '1' } },
);

expect(stdout).toMatchSnapshot();
Expand All @@ -41,7 +41,7 @@ test('generates splash screens only', async () => {
const { stdout } = await execa(
'./cli.js',
['./static/logo.png', './temp', '-s=false', '--splash-only'],
{ env: { PAG_NO_TRACE: '1' } },
{ env: { PAG_TEST_MODE: '1' } },
);

expect(stdout).toMatchSnapshot();
Expand All @@ -59,7 +59,7 @@ test('generates portrait splash screens only', async () => {
'--splash-only',
'--portrait-only',
],
{ env: { PAG_NO_TRACE: '1' } },
{ env: { PAG_TEST_MODE: '1' } },
);

expect(stdout).toMatchSnapshot();
Expand All @@ -77,7 +77,7 @@ test('generates landscape splash screens only', async () => {
'--splash-only',
'--landscape-only',
],
{ env: { PAG_NO_TRACE: '1' } },
{ env: { PAG_TEST_MODE: '1' } },
);

expect(stdout).toMatchSnapshot();
Expand All @@ -89,7 +89,7 @@ test('generates icons and splash screens when both only flags exist', async () =
const { stdout } = await execa(
'./cli.js',
['./static/logo.png', './temp', '-s=false', '--splash-only', '--icon-only'],
{ env: { PAG_NO_TRACE: '1' } },
{ env: { PAG_TEST_MODE: '1' } },
);

expect(stdout).toMatchSnapshot();
Expand Down
66 changes: 66 additions & 0 deletions config/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,70 @@
module.exports = {
FLAGS: {
background: {
type: 'string',
alias: 'b',
default: 'transparent',
},
manifest: {
type: 'string',
alias: 'm',
},
index: {
type: 'string',
alias: 'i',
},
opaque: {
type: 'boolean',
alias: 'o',
default: true,
},
scrape: {
type: 'boolean',
alias: 's',
default: true,
},
padding: {
type: 'string',
alias: 'p',
default: '10%',
},
type: {
type: 'string',
alias: 't',
default: 'png',
},
quality: {
type: 'number',
alias: 'q',
default: 100,
},
splashOnly: {
type: 'boolean',
alias: 'h',
default: false,
},
iconOnly: {
type: 'boolean',
alias: 'c',
default: false,
},
landscapeOnly: {
type: 'boolean',
alias: 'l',
default: false,
},
portraitOnly: {
type: 'boolean',
alias: 'r',
default: false,
},
log: {
type: 'boolean',
alias: 'g',
default: true,
},
},

PUPPETEER_LAUNCH_ARGS: [
'--log-level=3', // Fatal only
'--no-default-browser-check',
Expand Down
8 changes: 7 additions & 1 deletion helpers/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ const isHtmlFile = file => {
};

const getAppDir = () => {
return path.dirname(require.main.filename);
let appPath;
try {
appPath = require.resolve('pwa-asset-generator');
} catch (e) {
appPath = require.main.filename;
}
return path.dirname(appPath);
};

const getShellHtmlFilePath = () => {
Expand Down
13 changes: 13 additions & 0 deletions helpers/flags.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const constants = require('../config/constants');

const normalizeOnlyFlagPairs = (flag1Key, flag2Key, opts, logger) => {
const stripOnly = key => key.replace('Only', '');
if (opts[flag1Key] && opts[flag2Key]) {
Expand All @@ -23,7 +25,18 @@ const normalizeOutput = output => {
return output;
};

const getDefaultOptions = () => {
const { FLAGS: flags } = constants;

return Object.keys(flags)
.filter(flagKey => flags[flagKey].hasOwnProperty('default'))
.reduce((acc, curr) => {
return { ...acc, [curr]: flags[curr].default };
}, {});
};

module.exports = {
normalizeOnlyFlagPairs,
normalizeOutput,
getDefaultOptions,
};
Loading

0 comments on commit 72a7fe4

Please sign in to comment.