cordova-res
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -1,4 +0,4 @@ | ||
import * as et from 'elementtree'; | ||
import et from 'elementtree'; | ||
import { GeneratedImage, Platform } from './platform'; | ||
export declare function run(images: ReadonlyArray<GeneratedImage>, doc: et.ElementTree): void; | ||
export declare function run(configPath: string, images: ReadonlyArray<GeneratedImage>, doc: et.ElementTree): void; | ||
export declare function groupImages(images: ReadonlyArray<GeneratedImage>): Map<Platform, GeneratedImage[]>; | ||
@@ -5,0 +5,0 @@ export declare function read(path: string): Promise<et.ElementTree>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const utils_fs_1 = require("@ionic/utils-fs"); | ||
const Debug = require("debug"); | ||
const et = require("elementtree"); | ||
const debug = Debug('cordova-res:config'); | ||
function run(images, doc) { | ||
const debug_1 = tslib_1.__importDefault(require("debug")); | ||
const elementtree_1 = tslib_1.__importDefault(require("elementtree")); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const debug = debug_1.default('cordova-res:config'); | ||
function run(configPath, images, doc) { | ||
const root = doc.getroot(); | ||
@@ -15,15 +17,18 @@ const orientation = getPreference(doc, 'Orientation') || 'default'; | ||
debug('Creating node for %o', platform); | ||
platformElement = et.SubElement(root, 'platform', { name: platform }); | ||
platformElement = elementtree_1.default.SubElement(root, 'platform', { name: platform }); | ||
} | ||
const filteredImages = platformImages.filter(img => orientation === 'default' || typeof img.orientation === 'undefined' || img.orientation === orientation); | ||
for (const image of filteredImages) { | ||
// We use forward slashes, (not path.join) here to provide cross-platform | ||
// We force the use of forward slashes here to provide cross-platform | ||
// compatibility for paths. | ||
let imgElement = platformElement.find(`${image.nodeName}[@src='${image.dest}']`); | ||
const dest = path_1.default.relative(path_1.default.dirname(configPath), image.dest).replace(/\\/g, '/'); | ||
let imgElement = platformElement.find(`${image.nodeName}[@src='${dest}']`); | ||
if (!imgElement) { | ||
imgElement = platformElement.find(`${image.nodeName}[@src='${image.dest.split('/').join('\\')}']`); | ||
// We didn't find the element using forward slashes, so let's try to | ||
// find it with backslashes. | ||
imgElement = platformElement.find(`${image.nodeName}[@src='${dest.replace(/\//g, '\\')}']`); | ||
} | ||
if (!imgElement) { | ||
debug('Creating %O node for %o', image.nodeName, image.dest); | ||
imgElement = et.SubElement(platformElement, image.nodeName); | ||
debug('Creating %O node for %o', image.nodeName, dest); | ||
imgElement = elementtree_1.default.SubElement(platformElement, image.nodeName); | ||
} | ||
@@ -33,3 +38,3 @@ for (const attr of image.nodeAttributes) { | ||
if (attr === "src" /* SRC */) { | ||
v = image.dest; | ||
v = dest; | ||
} | ||
@@ -59,3 +64,3 @@ if (v) { | ||
const contents = await utils_fs_1.readFile(path, 'utf8'); | ||
const doc = et.parse(contents); | ||
const doc = elementtree_1.default.parse(contents); | ||
return doc; | ||
@@ -62,0 +67,0 @@ } |
@@ -16,2 +16,3 @@ "use strict"; | ||
--splash-source <path> ... Use specified file for splash source image | ||
--resources <path> ....... Use specified directory as resources directory | ||
@@ -18,0 +19,0 @@ -h, --help ............... Print help for the platform, then quit |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const utils_fs_1 = require("@ionic/utils-fs"); | ||
const Debug = require("debug"); | ||
const sharp = require("sharp"); | ||
const debug = Debug('cordova-res:image'); | ||
const debug_1 = tslib_1.__importDefault(require("debug")); | ||
const sharp_1 = tslib_1.__importDefault(require("sharp")); | ||
const debug = debug_1.default('cordova-res:image'); | ||
/** | ||
@@ -32,3 +33,3 @@ * Check an array of source files, returning the first viable image. | ||
async function transformImage(image, src) { | ||
return sharp(src) | ||
return sharp_1.default(src) | ||
.resize(image.width, image.height) | ||
@@ -35,0 +36,0 @@ .toBuffer(); |
@@ -0,3 +1,46 @@ | ||
/// <reference types="node" /> | ||
import { Platform, RunPlatformOptions } from './platform'; | ||
export declare function run(): Promise<void>; | ||
export declare function generateRunOptions(platform: Platform, args: ReadonlyArray<string>): RunPlatformOptions; | ||
declare function CordovaRes({ directory, resourcesDirectory, logstream, platforms, }?: CordovaRes.Options): Promise<void>; | ||
declare namespace CordovaRes { | ||
type PlatformOptions = { | ||
[P in Platform]?: Readonly<RunPlatformOptions>; | ||
}; | ||
const run: typeof CordovaRes; | ||
/** | ||
* Options for `cordova-res`. | ||
* | ||
* Each key may be excluded to use a provided default. | ||
*/ | ||
interface Options { | ||
/** | ||
* Operating directory. Usually the root of the project. | ||
* | ||
* `cordova-res` operates in the root of a standard Cordova project setup. | ||
* The specified directory should contain `config.xml` and a resources | ||
* folder, configured via `resourcesDirectory`. | ||
*/ | ||
readonly directory?: string; | ||
/** | ||
* Directory name or absolute path to resources directory. | ||
* | ||
* The resources directory contains the source images and generated images | ||
* of a Cordova project's resources. | ||
*/ | ||
readonly resourcesDirectory?: string; | ||
/** | ||
* Specify an alternative output mechanism. | ||
* | ||
* A NullStream may be used to silence output entirely. | ||
*/ | ||
readonly logstream?: NodeJS.WritableStream; | ||
/** | ||
* Resource generation configuration by platform. | ||
* | ||
* Each key/value represents the options for a supported platform. If | ||
* provided, resources are generated in an explicit, opt-in manner. | ||
*/ | ||
readonly platforms?: Readonly<PlatformOptions>; | ||
} | ||
function runCommandLine(args: ReadonlyArray<string>): Promise<void>; | ||
} | ||
export = CordovaRes; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Debug = require("debug"); | ||
const path = require("path"); | ||
const tslib_1 = require("tslib"); | ||
const debug_1 = tslib_1.__importDefault(require("debug")); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const cli_1 = require("./cli"); | ||
const config_1 = require("./config"); | ||
const platform_1 = require("./platform"); | ||
const resources_1 = require("./resources"); | ||
const cli_1 = require("./utils/cli"); | ||
const debug = Debug('cordova-res'); | ||
async function run() { | ||
const args = process.argv.slice(2); | ||
if (args.includes('--version')) { | ||
const pkg = await Promise.resolve().then(() => require(path.resolve(__dirname, '../package.json'))); | ||
process.stdout.write(pkg.version + '\n'); | ||
return; | ||
} | ||
if (args[0] === 'help' || args.includes('--help') || args.includes('-h')) { | ||
const help = await Promise.resolve().then(() => require('./help')); | ||
return help.run(); | ||
} | ||
const configPath = 'config.xml'; | ||
const platformArg = args[0] ? args[0].toString() : undefined; | ||
const typeOption = cli_1.getOptionValue(args, '--type'); | ||
try { | ||
const platforms = platform_1.validatePlatforms(platformArg && !platformArg.startsWith('-') ? [platformArg] : platform_1.PLATFORMS); | ||
const types = resources_1.validateResourceTypes(typeOption ? [typeOption] : resources_1.RESOURCE_TYPES); | ||
const config = await config_1.read(configPath); | ||
const images = []; | ||
for (const platform of platforms) { | ||
const platformImages = await platform_1.run(platform, types, generateRunOptions(platform, args)); | ||
process.stdout.write(`Generated ${platformImages.length} images for ${platform}\n`); | ||
const debug = debug_1.default('cordova-res'); | ||
async function CordovaRes({ directory = process.cwd(), resourcesDirectory = resources_1.DEFAULT_RESOURCES_DIRECTORY, logstream = process.stdout, platforms = { | ||
["android" /* ANDROID */]: cli_1.generateRunOptions("android" /* ANDROID */, resourcesDirectory, []), | ||
["ios" /* IOS */]: cli_1.generateRunOptions("ios" /* IOS */, resourcesDirectory, []), | ||
}, } = {}) { | ||
const configPath = path_1.default.resolve(directory, 'config.xml'); | ||
const resourcesPath = path_1.default.isAbsolute(resourcesDirectory) ? resourcesDirectory : path_1.default.resolve(directory, resourcesDirectory); | ||
debug('Paths: (config: %O) (resources: %O)', configPath, resourcesPath); | ||
const config = await config_1.read(configPath); | ||
const images = []; | ||
for (const platform of platform_1.PLATFORMS) { | ||
const platformOptions = platforms[platform]; | ||
if (platformOptions) { | ||
const platformImages = await platform_1.run(platform, resourcesPath, platformOptions); | ||
logstream.write(`Generated ${platformImages.length} images for ${platform}\n`); | ||
images.push(...platformImages); | ||
} | ||
config_1.run(images, config); | ||
await config_1.write(configPath, config); | ||
process.stdout.write(`Wrote to config.xml\n`); | ||
} | ||
catch (e) { | ||
debug('Caught fatal error: %O', e); | ||
process.exitCode = 1; | ||
process.stdout.write(e.stack ? e.stack : e.toString()); | ||
} | ||
config_1.run(configPath, images, config); | ||
await config_1.write(configPath, config); | ||
logstream.write(`Wrote to config.xml\n`); | ||
} | ||
exports.run = run; | ||
function generateRunOptions(platform, args) { | ||
const iconSourceOption = cli_1.getOptionValue(args, '--icon-source'); | ||
const splashSourceOption = cli_1.getOptionValue(args, '--splash-source'); | ||
const iconOptions = {}; | ||
const splashOptions = {}; | ||
if (iconSourceOption) { | ||
iconOptions.sources = [iconSourceOption]; | ||
(function (CordovaRes) { | ||
CordovaRes.run = CordovaRes; | ||
async function runCommandLine(args) { | ||
if (args.includes('--version')) { | ||
const pkg = await Promise.resolve().then(() => tslib_1.__importStar(require(path_1.default.resolve(__dirname, '../package.json')))); | ||
process.stdout.write(pkg.version + '\n'); | ||
return; | ||
} | ||
if (args[0] === 'help' || args.includes('--help') || args.includes('-h')) { | ||
const help = await Promise.resolve().then(() => tslib_1.__importStar(require('./help'))); | ||
return help.run(); | ||
} | ||
try { | ||
const options = cli_1.parseOptions(args); | ||
await CordovaRes.run(options); | ||
} | ||
catch (e) { | ||
debug('Caught fatal error: %O', e); | ||
process.exitCode = 1; | ||
process.stderr.write(e.stack ? e.stack : e.toString()); | ||
} | ||
} | ||
if (splashSourceOption) { | ||
splashOptions.sources = [splashSourceOption]; | ||
} | ||
return { | ||
["icon" /* ICON */]: Object.assign({ sources: [`resources/${platform}/icon.png`, 'resources/icon.png'] }, iconOptions), | ||
["splash" /* SPLASH */]: Object.assign({ sources: [`resources/${platform}/splash.png`, 'resources/splash.png'] }, splashOptions), | ||
}; | ||
} | ||
exports.generateRunOptions = generateRunOptions; | ||
CordovaRes.runCommandLine = runCommandLine; | ||
})(CordovaRes || (CordovaRes = {})); | ||
module.exports = CordovaRes; |
@@ -11,4 +11,4 @@ import { ResourceKey, ResourceType, ResourcesImageConfig } from './resources'; | ||
export interface RunPlatformOptions { | ||
[ResourceType.ICON]: RunPlatformResourceTypeOptions; | ||
[ResourceType.SPLASH]: RunPlatformResourceTypeOptions; | ||
[ResourceType.ICON]?: RunPlatformResourceTypeOptions; | ||
[ResourceType.SPLASH]?: RunPlatformResourceTypeOptions; | ||
} | ||
@@ -22,5 +22,5 @@ export interface GeneratedImage extends ResourcesImageConfig { | ||
} | ||
export declare function run(platform: Platform, types: ReadonlyArray<ResourceType>, options: Readonly<RunPlatformOptions>): Promise<GeneratedImage[]>; | ||
export declare function runType(platform: Platform, type: ResourceType, options: Readonly<RunPlatformOptions>): Promise<GeneratedImage[]>; | ||
export declare function run(platform: Platform, resourcesPath: string, options: Readonly<RunPlatformOptions>): Promise<GeneratedImage[]>; | ||
export declare function runType(platform: Platform, type: ResourceType, resourcesPath: string, options: Readonly<RunPlatformResourceTypeOptions>): Promise<GeneratedImage[]>; | ||
export declare function validatePlatforms(platforms: ReadonlyArray<string>): Platform[]; | ||
export declare function isSupportedPlatform(platform: any): platform is Platform; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const utils_fs_1 = require("@ionic/utils-fs"); | ||
const Debug = require("debug"); | ||
const pathlib = require("path"); | ||
const debug_1 = tslib_1.__importDefault(require("debug")); | ||
const path_1 = tslib_1.__importDefault(require("path")); | ||
const image_1 = require("./image"); | ||
const resources_1 = require("./resources"); | ||
const debug = Debug('cordova-res:platform'); | ||
const debug = debug_1.default('cordova-res:platform'); | ||
exports.PLATFORMS = ["android" /* ANDROID */, "ios" /* IOS */]; | ||
async function run(platform, types, options) { | ||
async function run(platform, resourcesPath, options) { | ||
debug('Running %s platform with options: %O', platform, options); | ||
const results = await Promise.all(types.map(async (type) => runType(platform, type, options))); | ||
return [].concat(...results); | ||
const results = []; | ||
return results.concat(...await Promise.all(resources_1.RESOURCE_TYPES.map(async (type) => { | ||
const typeOptions = options[type]; | ||
if (typeOptions) { | ||
return runType(platform, type, resourcesPath, typeOptions); | ||
} | ||
return []; | ||
}))); | ||
} | ||
exports.run = run; | ||
async function runType(platform, type, options) { | ||
async function runType(platform, type, resourcesPath, options) { | ||
debug('Building %s resources for %s platform', type, platform); | ||
const [src, srcbuf] = await image_1.resolveSourceImage(options[type].sources); | ||
const [src, srcbuf] = await image_1.resolveSourceImage(options.sources); | ||
debug('Using %O for %s source image for %s', src, type, platform); | ||
const config = resources_1.RESOURCES[platform][type]; | ||
const dir = pathlib.join('resources', platform, type); | ||
const dir = path_1.default.resolve(resourcesPath, platform, type); | ||
await utils_fs_1.ensureDir(dir); | ||
const images = await Promise.all(config.images.map(async (image) => { | ||
const dest = pathlib.join(dir, image.name); | ||
const dest = path_1.default.join(dir, image.name); | ||
await image_1.generateImage(image, srcbuf, dest); | ||
@@ -26,0 +33,0 @@ return Object.assign({ src, |
import { Platform } from './platform'; | ||
export declare const DEFAULT_RESOURCES_DIRECTORY = "resources"; | ||
export declare const enum ResourceType { | ||
@@ -3,0 +4,0 @@ ICON = "icon", |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DEFAULT_RESOURCES_DIRECTORY = 'resources'; | ||
exports.RESOURCE_TYPES = ["icon" /* ICON */, "splash" /* SPLASH */]; | ||
@@ -4,0 +5,0 @@ function validateResourceTypes(types) { |
{ | ||
"name": "cordova-res", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "This tool will crop and resize PNG source images into appropriate sizes for modern iOS and Android devices.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://ionicframework.com", |
@@ -17,2 +17,8 @@ # Resource Generator | ||
## Install | ||
```bash | ||
$ npm install -g cordova-res | ||
``` | ||
## Usage | ||
@@ -25,1 +31,33 @@ | ||
``` | ||
### Programmatic API | ||
`cordova-res` can be used programmatically. | ||
#### CommonJS Example | ||
```js | ||
const run = require('cordova-res'); | ||
await run(); | ||
``` | ||
#### TypeScript Example | ||
`run()` takes an options object described by the interface `Options`. If options are provided, resources are generated in an explicit, opt-in manner. In the following example, only Android icons and iOS splash screens are generated. | ||
```ts | ||
import { Options, run } from 'cordova-res'; | ||
const options: Options = { | ||
directory: '/path/to/project', | ||
resourcesDirectory: 'resources', | ||
logstream: process.stdout, // Any WritableStream | ||
platforms: { | ||
android: { icon: { sources: ['resources/icon.png'] } }, | ||
ios: { splash: { sources: ['resources/splash.png'] } }, | ||
}, | ||
}; | ||
await run(options); | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
32116
20
567
62