Skip to content

Commit

Permalink
feat(main): exposed private API via main.js
Browse files Browse the repository at this point in the history
Added generateImages function to make use of the lib functionality via node scripting

fix #5
  • Loading branch information
onderceylan committed Sep 1, 2019
1 parent b29e20f commit d3367ad
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 82 deletions.
84 changes: 3 additions & 81 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node

const meow = require('meow');
const puppets = require('./puppets');
const pwa = require('./helpers/pwa');
const preLogger = require('./helpers/logger');
const main = require('./main');

const logger = preLogger('cli');
const cli = meow(
`
$ pwa-asset-generator --help
Expand Down Expand Up @@ -112,87 +112,9 @@ $ pwa-asset-generator --help
},
);

const source = cli.input[0];
let output = cli.input[1];
let options = cli.flags;
const logger = preLogger('cli');

const normalizeOnlyFlagPairs = (flag1Key, flag2Key, opts) => {
const stripOnly = key => key.replace('Only', '');
if (opts[flag1Key] && opts[flag2Key]) {
logger.warn(
`Hmm, you want to _only_ generate both ${stripOnly(
flag1Key,
)} and ${stripOnly(
flag2Key,
)} set. Ignoring --x-only settings as this is default behavior`,
);
return {
...opts,
[flag1Key]: false,
[flag2Key]: false,
};
}
return opts;
};

if (!source) {
logger.error('Please specify a URL or file path as a source');
process.exit(1);
}

options = normalizeOnlyFlagPairs('splashOnly', 'iconOnly', options);
options = normalizeOnlyFlagPairs('landscapeOnly', 'portraitOnly', options);

if (!output) {
output = '.';
}

(async () => {
try {
const savedImages = await puppets.generateImages(source, output, options);
const manifestJsonContent = pwa.generateIconsContentForManifest(
savedImages,
options.manifest,
);
const htmlContent = pwa.generateHtmlForIndexPage(
savedImages,
options.index,
);

if (!options.splashOnly) {
if (options.manifest) {
await pwa.addIconsToManifest(manifestJsonContent, options.manifest);
logger.success(
`Icons are saved to Web App Manifest file ${options.manifest}`,
);
} else if (!options.splashOnly) {
logger.warn(
'Web App Manifest file is not specified, printing out the content to console instead',
);
logger.success(
'Below is the icons content for your manifest.json file. You can copy/paste it manually',
);
process.stdout.write(
`\n${JSON.stringify(manifestJsonContent, null, 2)}\n\n`,
);
}
}

if (options.index) {
await pwa.addMetaTagsToIndexPage(htmlContent, options.index);
logger.success(
`iOS meta tags are saved to index html file ${options.index}`,
);
} else {
logger.warn(
'Index html file is not specified, printing out the content to console instead',
);
logger.success(
'Below is the iOS meta tags content for your index.html file. You can copy/paste it manually',
);
process.stdout.write(`\n${htmlContent}\n`);
}
await main.generateImages(cli.input[0], cli.input[1], cli.flags, logger);
} catch (e) {
logger.error(e);
process.exit(1);
Expand Down
29 changes: 29 additions & 0 deletions helpers/flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const normalizeOnlyFlagPairs = (flag1Key, flag2Key, opts, logger) => {
const stripOnly = key => key.replace('Only', '');
if (opts[flag1Key] && opts[flag2Key]) {
logger.warn(
`Hmm, you want to _only_ generate both ${stripOnly(
flag1Key,
)} and ${stripOnly(
flag2Key,
)} set. Ignoring --x-only settings as this is default behavior`,
);
return {
[flag1Key]: false,
[flag2Key]: false,
};
}
return {};
};

const normalizeOutput = output => {
if (!output) {
return '.';
}
return output;
};

module.exports = {
normalizeOnlyFlagPairs,
normalizeOutput,
};
72 changes: 72 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const pwa = require('./helpers/pwa');
const puppets = require('./puppets');
const flags = require('./helpers/flags');
const preLogger = require('./helpers/logger');

const generateImages = async (source, _output, _options, loggerInjection) => {
const logger = loggerInjection || preLogger(generateImages.name);

if (!source) {
throw Error('Please specify a URL or file path as a source');
}

const options = {
..._options,
...flags.normalizeOnlyFlagPairs('splashOnly', 'iconOnly', _options, logger),
...flags.normalizeOnlyFlagPairs(
'landscapeOnly',
'portraitOnly',
_options,
logger,
),
};

const output = flags.normalizeOutput(_output);

const savedImages = await puppets.generateImages(source, output, options);
const manifestJsonContent = pwa.generateIconsContentForManifest(
savedImages,
options.manifest,
);
const htmlContent = pwa.generateHtmlForIndexPage(savedImages, options.index);

if (!options.splashOnly) {
if (options.manifest) {
await pwa.addIconsToManifest(manifestJsonContent, options.manifest);
logger.success(
`Icons are saved to Web App Manifest file ${options.manifest}`,
);
} else if (!options.splashOnly) {
logger.warn(
'Web App Manifest file is not specified, printing out the content to console instead',
);
logger.success(
'Below is the icons content for your manifest.json file. You can copy/paste it manually',
);
process.stdout.write(
`\n${JSON.stringify(manifestJsonContent, null, 2)}\n\n`,
);
}
}

if (options.index) {
await pwa.addMetaTagsToIndexPage(htmlContent, options.index);
logger.success(
`iOS meta tags are saved to index html file ${options.index}`,
);
} else {
logger.warn(
'Index html file is not specified, printing out the content to console instead',
);
logger.success(
'Below is the iOS meta tags content for your index.html file. You can copy/paste it manually',
);
process.stdout.write(`\n${htmlContent}\n`);
}

return savedImages;
};

module.exports = {
generateImages,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pwa-asset-generator",
"version": "1.1.7",
"description": "PWA asset generator based on Puppeteer. Automatically generates icons and splash screens guided by Web App Manifest specs and Apple Human Interface guidelines. Updates manifest.json and index.html files with the generated images.",
"main": "cli.js",
"main": "main.js",
"bin": {
"pwa-asset-generator": "cli.js"
},
Expand Down

0 comments on commit d3367ad

Please sign in to comment.