Skip to content

Commit a4d8bf9

Browse files
committed
Introduce "output" directory flag
1 parent 58df362 commit a4d8bf9

File tree

6 files changed

+107
-45
lines changed

6 files changed

+107
-45
lines changed

.codebeatsettings

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"JAVASCRIPT": {
3+
"ARITY": [
4+
8,
5+
10,
6+
14,
7+
20
8+
]
9+
},
10+
"TYPESCRIPT": {
11+
"ARITY": [
12+
8,
13+
10,
14+
14,
15+
20
16+
]
17+
}
18+
}

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
strategy:
1010
matrix:
11-
node-version: [14.x, 16.x]
11+
node-version: [18.x]
1212

1313
steps:
1414
- uses: actions/checkout@v3

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Thumbs.db
2121
.github
2222
_config.yml
2323
_.config.yml
24+
.codebeatsettings
2425
.editorconfig
2526
.gitattributes
2627
.gitignore

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ This path is relative to the folder you are located in. It is recommended that t
7373

7474
If the `launch` argument is not provided, the [default launch screen](https://github.com/scriptex/create-pwa/blob/master/launch.png) is used.
7575

76+
3. `output`: Specifies a relative path to the output directory.
77+
78+
This path is relative to the folder you are located in.
79+
80+
**The `output` argument is not required.**
81+
82+
If the `output` argument is not provided, the default value for it is empty string (the root of your project or the location of your `package.json` file).
83+
7684
## Usage
7785

7886
If you want to use if from the command line, you should first install Create PWA globally:
@@ -122,15 +130,15 @@ When the files(`manifest.json` and `service-worker.js`) are ready for production
122130

123131
Feel adviced to edit the content of the `<TileColor>` tag in the `config.xml` file as it matches the color of your application's status bar on Chrome (found in the `<meta name="color" />` tag);
124132

125-
1. Add the following to the `head` of your HTML file(s):
133+
1. Add the following to the `head` of your HTML file(s):
126134

127135
```html
128136
<link rel="manifest" href="manifest.json" />
129137
```
130138

131139
You can read more about the Web App Manifest [here](https://developers.google.com/web/fundamentals/web-app-manifest/).
132140

133-
2. Add the following snippet to your application's JavaScript bundle or place it in a `script` tag just before the closing `</body>` tag in your HTML file(s):
141+
2. Add the following snippet to your application's JavaScript bundle or place it in a `script` tag just before the closing `</body>` tag in your HTML file(s):
134142

135143
```javascript
136144
if ('serviceWorker' in navigator) {
@@ -477,8 +485,9 @@ You can generate each of the components above separately using the Create PWA AP
477485
```javascript
478486
const { setAppCache } = require('create-pwa');
479487
const appName = 'Your application name';
488+
const distFolder = 'dist';
480489

481-
setAppCache(appName);
490+
setAppCache(appName, distFolder);
482491
```
483492

484493
**The generated `appcache` file contains references to the application icons and application launch screens. You must have these already generated otherwise you must edit your `appcache` file and remove them.**
@@ -488,8 +497,9 @@ setAppCache(appName);
488497
```javascript
489498
const { setIcons } = require('create-pwa');
490499
const appIcon = require('fs').resolve(__dirname, './your_icon.png');
500+
const distFolder = 'dist';
491501

492-
setIcons(appIcon);
502+
setIcons(appIcon, distFolder);
493503
```
494504

495505
**The generated icons are located in the `/icons` folder.**
@@ -499,8 +509,9 @@ setIcons(appIcon);
499509
```javascript
500510
const { setLaunchScreens } = require('create-pwa');
501511
const launchScreen = require('fs').resolve(__dirname, './your_launch_screen.png');
512+
const distFolder = 'dist';
502513

503-
setLaunchScreens(launchScreen);
514+
setLaunchScreens(launchScreen, distFolder);
504515
```
505516

506517
**The generated files are located in the `/launch-screens` folder.**
@@ -510,8 +521,9 @@ setLaunchScreens(launchScreen);
510521
```javascript
511522
const { setManifest } = require('create-pwa');
512523
const appName = 'Your application name';
524+
const distFolder = 'dist';
513525

514-
setManifest(appName);
526+
setManifest(appName, distFolder);
515527
```
516528

517529
**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.**
@@ -521,8 +533,9 @@ setManifest(appName);
521533
```javascript
522534
const { setFavicons } = require('create-pwa');
523535
const appIcon = require('fs').resolve(__dirname, './your_icon.png');
536+
const distFolder = 'dist';
524537

525-
setFavicons(appIcon);
538+
setFavicons(appIcon, distFolder);
526539
```
527540

528541
**The generated files are located in the `/favicons` folder.**
@@ -532,8 +545,9 @@ setFavicons(appIcon);
532545
```javascript
533546
const { setServiceWorker } = require('create-pwa');
534547
const appName = 'Your application name';
548+
const distFolder = 'dist';
535549

536-
setServiceWorker(appName);
550+
setServiceWorker(appName, distFolder);
537551
```
538552

539553
**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.**

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-pwa",
3-
"version": "2.6.1",
3+
"version": "2.7.0",
44
"description": "Easily create a progressive web app",
55
"keywords": [
66
"PWA",
@@ -27,7 +27,7 @@
2727
"url": "github:scriptex/create-pwa"
2828
},
2929
"scripts": {
30-
"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",
30+
"pwa": "rm -rf dist && node src/index.js --icon=./icon.png --launch=./launch.png --output=dist --icons=true --app-cache=true --manifest=true --favicons=true --service-worker=true --launch-screens=true",
3131
"test": "tape test.js"
3232
},
3333
"dependencies": {

src/index.js

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { existsSync, writeFileSync, mkdirSync } = require('fs');
99
const DEFAULTS = {
1010
icon: './icon.png',
1111
launch: './launch.png',
12-
output: './'
12+
output: ''
1313
};
1414

1515
const argv = require('yargs').options({
@@ -64,6 +64,21 @@ const serviceWorkerTemplate = require('./sw');
6464
*/
6565
const pwd = process.cwd();
6666

67+
/**
68+
* Create a directory if one doesn't exist
69+
* @param {string} dist Path to a folder inside the project's directory
70+
* @param {string} name Name of the new folder to be created
71+
*/
72+
const createDirIfNotExists = (dist, name) => {
73+
const dir = resolve(pwd, dist, name);
74+
75+
if (!existsSync(dir)) {
76+
mkdirSync(dir, { recursive: true });
77+
}
78+
79+
return dir;
80+
};
81+
6782
/**
6883
* Get application's name
6984
*/
@@ -79,107 +94,121 @@ const getAppName = () => {
7994

8095
/**
8196
* Create app's manifest.json file
82-
* @param {string} name
97+
* @param {string} name Name of the application
98+
* @param {string} dist Path to a folder inside the project's directory
8399
*/
84-
const setManifest = name => {
85-
writeFileSync(resolve(pwd, 'manifest.json'), manifestTemplate(name));
100+
const setManifest = (name, dist) => {
101+
const dir = createDirIfNotExists(dist, '');
102+
103+
writeFileSync(resolve(dir, 'manifest.json'), manifestTemplate(name));
86104
};
87105

88106
/**
89107
* Create app's service worker file
90-
* @param {string} name
108+
* @param {string} name Name of the application
109+
* @param {string} dist Path to a folder inside the project's directory
91110
*/
92-
const setServiceWorker = name => {
93-
writeFileSync(resolve(pwd, 'service-worker.js'), serviceWorkerTemplate(name));
111+
const setServiceWorker = (name, dist) => {
112+
const dir = createDirIfNotExists(dist, '');
113+
114+
writeFileSync(resolve(dir, 'service-worker.js'), serviceWorkerTemplate(name));
94115
};
95116

96117
/**
97118
* Create images with `sharp`
98-
* @param {string} file
99-
* @param {string} folder
119+
* @param {string} file Path to the image file
120+
* @param {string} dist Path to a folder inside the project's directory
121+
* @param {string} name Name of the new folder to be created
100122
* @param {Function} callback
101123
*/
102-
const generateImages = (file, folder, callback) => {
124+
const generateImages = (file, dist, name, callback) => {
103125
if (!file) {
104126
return;
105127
}
106128

107-
const dir = resolve(pwd, folder);
129+
const dir = createDirIfNotExists(dist, name);
108130
const image = resolve(pwd, file);
109131

110-
if (!existsSync(dir)) {
111-
mkdirSync(dir);
112-
}
113-
114132
callback(image, dir);
115133
};
116134

117135
/**
118136
* Create app's icons
119-
* @param {string} icon
137+
* @param {string} file Path to the image file
138+
* @param {string} dist Path to a folder inside the project's directory
120139
*/
121-
const setIcons = icon => generateImages(icon, 'icons', generateIcons);
140+
const setIcons = (file, dist) => generateImages(file, dist, 'icons', generateIcons);
122141

123142
/**
124143
* Create app's cache manifest
125-
* @param {string} name
144+
* @param {string} name Name of the application
145+
* @param {string} dist Path to a folder inside the project's directory
126146
*/
127-
const setAppCache = name => {
128-
writeFileSync(resolve(pwd, `${name}.appcache`), appCacheTemplate());
147+
const setAppCache = (name, dist) => {
148+
const dir = createDirIfNotExists(dist, '');
149+
150+
writeFileSync(resolve(dir, `${name}.appcache`), appCacheTemplate());
129151
};
130152

131153
/**
132154
* Create app's launch screens
133-
* @param {string} launchScreen
155+
* @param {string} file Path to the image file
156+
* @param {string} dist Path to a folder inside the project's directory
134157
*/
135-
const setLaunchScreens = launchScreen => generateImages(launchScreen, 'launch-screens', generateLaunchScreens);
158+
const setLaunchScreens = (file, dist) => {
159+
generateImages(file, dist, 'launch-screens', generateLaunchScreens);
160+
};
136161

137162
/**
138163
* Create app's config for Microsoft browsers
164+
* @param {string} dist Path to a folder inside the project's directory
139165
*/
140-
const setMsTileConfig = () => {
141-
writeFileSync(resolve(pwd, 'config.xml'), msTileConfigTemplate());
166+
const setMsTileConfig = dist => {
167+
const dir = createDirIfNotExists(dist, '');
168+
169+
writeFileSync(resolve(dir, 'config.xml'), msTileConfigTemplate());
142170
};
143171

144172
/**
145173
* Create app's favicons
146-
* @param {string} icon
174+
* @param {string} file Path to the image file
175+
* @param {string} dist Path to a folder inside the project's directory
147176
*/
148-
const setFavicons = icon => generateImages(icon, 'favicons', generateFavicons);
177+
const setFavicons = (file, dist) => generateImages(file, dist, 'favicons', generateFavicons);
149178

150179
/**
151180
* Create all PWA required files
152181
*/
153182
const create = async () => {
154183
const name = getAppName();
155184

156-
const { icon, launch, icons, manifest, favicons, appCache, serviceWorker, launchScreens } = await argv;
185+
const { icon, launch, output, icons, manifest, favicons, appCache, serviceWorker, launchScreens } = await argv;
157186
const iconToUse = icon || DEFAULTS.icon;
158187
const launchToUse = launch || DEFAULTS.launch;
159188

160189
if (icons) {
161-
setIcons(iconToUse);
190+
setIcons(iconToUse, output);
162191
}
163192

164193
if (manifest) {
165-
setManifest(name);
194+
setManifest(name, output);
166195
}
167196

168197
if (appCache) {
169-
setAppCache(name);
198+
setAppCache(name, output);
170199
}
171200

172201
if (favicons) {
173-
setFavicons(iconToUse);
174-
setMsTileConfig();
202+
setFavicons(iconToUse, output);
203+
setMsTileConfig(output);
175204
}
176205

177206
if (serviceWorker) {
178-
setServiceWorker(name);
207+
setServiceWorker(name, output);
179208
}
180209

181210
if (launchScreens) {
182-
setLaunchScreens(launchToUse);
211+
setLaunchScreens(launchToUse, output);
183212
}
184213
};
185214

0 commit comments

Comments
 (0)