Skip to content

Commit

Permalink
added types
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 committed Sep 23, 2023
1 parent f899bb2 commit 9520eef
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-snakes-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'capkit': patch
---

Added types
17 changes: 9 additions & 8 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { promises as fs, existsSync } from 'fs';
import { asyncExec, getConfigExtension, getPM, isDirectory, executeJobs } from '../util/util.js';
import path from 'path';
import { fileURLToPath } from 'url';
import { Job, ProjectOptions } from '../types/types.js';
import { Job, Platform, Plugin, ProjectOptions } from '../types/types.js';

export async function init() {
intro(`Welcome to the ${kleur.underline('capkit')} CLI!`);
Expand Down Expand Up @@ -58,19 +58,20 @@ async function promptOptions() {
message: 'Do you want to add additional platforms?'
});

let selectedPlatforms: string[] | null = null;
const platforms = ['Android', 'iOS'];

let selectedPlatforms: Platform[] | null = null;
if (shouldPromptPlatforms) {
const platforms = ['Android', 'iOS'];
selectedPlatforms = (await multiselect({
message: 'What platforms do you want to add?',
options: platforms.map((platform) => {
return {
value: platform.toLowerCase(),
value: platform.toLowerCase() as Platform,
label: platform
};
}),
required: false
})) as string[];
})) as Platform[];
}

const plugins = [
Expand Down Expand Up @@ -104,18 +105,18 @@ async function promptOptions() {
message: 'Do you want to add additional plugins?'
});

let selectedPlugins: string[] | null = null;
let selectedPlugins: Plugin[] | null = null;
if (shouldPromptPlugins) {
selectedPlugins = (await multiselect({
message: 'What plugins do you want to add?',
options: plugins.map((plugin) => {
return {
value: plugin.toLowerCase().replace(/ /g, '-'),
value: plugin.toLowerCase().replace(/ /g, '-') as Plugin,
label: plugin
};
}),
required: false
})) as string[];
})) as Plugin[];
}

const pm = getPM();
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ program.command('initialize').alias('init').description('initialize capkit').act
program.parse(process.argv);

export { initializeProject } from './commands/init.js';
export { ProjectOptions } from './types/types.js';
export { ProjectOptions, Platform, Plugin, ConfigExtension, PackageManager } from './types/types.js';
40 changes: 36 additions & 4 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,40 @@ export type Job = {
export type ProjectOptions = {
name: string;
id: string;
selectedPlatforms: string[];
selectedPlugins: string[];
configExtension: string;
pm: string;
selectedPlatforms: Platform[];
selectedPlugins: Plugin[];
configExtension: ConfigExtension | null;
pm: PackageManager;
};

export type Platform = 'Android' | 'iOS';

export type Plugin =
| 'action-sheet'
| 'app'
| 'app-launcher'
| 'browser'
| 'camera'
| 'clipboard'
| 'device'
| 'dialog'
| 'filesystem'
| 'geolocation'
| 'google-maps'
| 'haptics'
| 'keyboard'
| 'local-notifications'
| 'motion'
| 'network'
| 'preferences'
| 'push-notifications'
| 'screen-reader'
| 'share'
| 'splash-screen'
| 'status-bar'
| 'text-zoom'
| 'toast';

export type ConfigExtension = 'json' | 'js' | 'ts';

export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun';
6 changes: 3 additions & 3 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { cancel } from '@clack/prompts';
import { spinner } from '@clack/prompts';
import { exec } from 'child_process';
import { existsSync, lstatSync } from 'fs';
import { Job } from '../types/types.js';
import { ConfigExtension, Job, PackageManager } from '../types/types.js';

export function asyncExec(command: string) {
return new Promise((resolve, reject) => {
Expand All @@ -16,7 +16,7 @@ export function getConfigExtension() {
const configExtensions = ['json', 'js', 'ts'];
for (const extension of configExtensions) {
if (existsSync(`capacitor.config.${extension}`)) {
return extension;
return extension as ConfigExtension;
}
}
}
Expand All @@ -29,7 +29,7 @@ export function getPM() {
const pmSpec = userAgent.split(' ')[0] || '';
const separatorPos = pmSpec.lastIndexOf('/');
const name = pmSpec?.substring(0, separatorPos);
return name === 'npminstall' ? 'npm' : name;
return name === 'npminstall' ? 'npm' : (name as PackageManager);
}

export function isDirectory(path: string) {
Expand Down

0 comments on commit 9520eef

Please sign in to comment.