Skip to content

Commit

Permalink
Merge pull request #25 from scriptex/feature/optional-components
Browse files Browse the repository at this point in the history
Make separate components optional
  • Loading branch information
scriptex authored Sep 30, 2019
2 parents 4e64c05 + caf22cd commit db97f2d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 37 deletions.
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ _.config.yml
create-pwa.sketch

# Tests
favicons
icons
launch-screens
config.xml
*.appcache
manifest.json
service-worker.js
*.appcache
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ const appIcon = require('fs').resolve(__dirname, './your_icon.png');
setIcons(appIcon);
```

**The generated icons are located in the `/icons` folder.**

3. To create only the launch screens:

```javascript
Expand All @@ -449,6 +451,8 @@ const launchScreen = require('fs').resolve(__dirname, './your_launch_screen.png'
setLaunchScreens(launchScreen);
```

**The generated files are located in the `/launch-screens` folder.**

4. To create only manifest file:

```javascript
Expand All @@ -460,31 +464,27 @@ setManifest(appName);

**The generated `manifest.json` file contains references to the application icons. You must have these already generated otherwise you must edit your `manifest.json` file and remove them.**

5. To create only service worker file:
5. To create only favicon files:

```javascript
const { setServiceWorker } = require('create-pwa');
const appName = 'Your application name';
const { setFavicons } = require('create-pwa');
const appIcon = require('fs').resolve(__dirname, './your_icon.png');

setServiceWorker(appName);
setFavicons(appIcon);
```

**The generated `service-worker.js` file contains references to the application icons and application launch screens. You must have these already generated otherwise you must edit your `service-worker.js` file and remove them.**
**The generated files are located in the `/favicons` folder.**

6. To create all files:
6. To create only service worker file:

```javascript
const createPWA = require('create-pwa');
const icon = require('fs').resolve(__dirname, './your_icon.png');
const launch = require('fs').resolve(__dirname, './your_launch_screen.png');

createPWA({
icon,
launch
});
const { setServiceWorker } = require('create-pwa');
const appName = 'Your application name';

setServiceWorker(appName);
```

**This command assumes that you have a `package.json` file in the folder you run the command from and this `package.json` file contains a non-empty `name` member.**
**The generated `service-worker.js` file contains references to the application icons and application launch screens. You must have these already generated otherwise you must edit your `service-worker.js` file and remove them.**

## LICENSE

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "create-pwa",
"version": "2.1.0",
"version": "2.2.0",
"description": "Easily create a progressive web app",
"scripts": {
"pwa": "src/index.js --icon=\"./icon.png\" --launch=\"./launch.png\" --icons=false --app-cache=false --manifest=false --favicons=false --service-worker=false --launch-screens=false",
"test": "tape test.js"
},
"bin": {
Expand Down
91 changes: 71 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,51 @@
const { resolve, sep } = require('path');
const { existsSync, writeFileSync, mkdirSync } = require('fs');

/**
* Default options
*/
const DEFAULTS = {
icon: './icon.png',
launch: './launch.png'
};

/**
* External dependencies
*/
const argv = require('yargs').argv;
const argv = require('yargs').options({
icon: {
default: DEFAULTS.icon,
type: 'string'
},
launch: {
default: DEFAULTS.launch,
type: 'string'
},
icons: {
default: true,
type: 'boolean'
},
manifest: {
default: true,
type: 'boolean'
},
appCache: {
default: true,
type: 'boolean'
},
favicons: {
default: true,
type: 'boolean'
},
serviceWorker: {
default: true,
type: 'boolean'
},
launchScreens: {
default: true,
type: 'boolean'
}
}).argv;

/**
* Internal dependencies
Expand All @@ -27,14 +68,6 @@ const serviceWorkerTemplate = require('./sw');
*/
const pwd = process.env.PWD;

/**
* Default options
*/
const DEFAULTS = {
icon: './icon.png',
launch: './launch.png'
};

/**
* Get application's name
*/
Expand Down Expand Up @@ -122,23 +155,41 @@ const setFavicons = icon => generateImages(icon, 'favicons', generateFavicons);
* Create all PWA required files
* @param {Object} => { icon: File, launch: File}
*/
const create = (options = DEFAULTS) => {
const create = () => {
const name = getAppName();

let { icon, launch } = options;
const { icon, launch, icons, manifest, favicons, appCache, serviceWorker, launchScreens } = argv;
const iconToUse = icon || DEFAULTS.icon;
const launchToUse = launch || DEFAULTS.launch;

if (icons) {
setIcons(iconToUse);
}

if (manifest) {
setManifest(name);
}

if (appCache) {
setAppCache(name);
}

icon = argv.icon || icon;
launch = argv.launch || launch;
if (favicons) {
setFavicons(iconToUse);
setMsTileConfig();
}

setIcons(icon);
setAppCache(name);
setManifest(name);
setFavicons(icon);
setMsTileConfig();
setServiceWorker(name);
setLaunchScreens(launch);
if (serviceWorker) {
setServiceWorker(name);
}

if (launchScreens) {
setLaunchScreens(launchToUse);
}
};

create();

module.exports = create;
module.exports.setIcons = setIcons;
module.exports.setAppCache = setAppCache;
Expand Down

0 comments on commit db97f2d

Please sign in to comment.