-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(main): exposed private API via main.js
Added generateImages function to make use of the lib functionality via node scripting fix #5
- Loading branch information
1 parent
b29e20f
commit d3367ad
Showing
4 changed files
with
105 additions
and
82 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
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,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, | ||
}; |
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,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, | ||
}; |
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