Skip to content

Commit

Permalink
Improved types for exposed API
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugos68 committed Sep 25, 2023
1 parent 1835b5b commit bee2a49
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-actors-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'capkit': patch
---

Improved exposed API types and names
111 changes: 56 additions & 55 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ async function promptOptions() {

const packageJsonName = JSON.parse(String(await fs.readFile('package.json')))['name'];

const name = (await text({
const appName = (await text({
message: `What is the ${kleur.underline('name')} of your project?`,
placeholder: packageJsonName,
validate: (value) => {
if (value.length < 1) return 'Invalid name. Must be at least 1 character long.';
}
})) as string;

const id = (await text({
const appId = (await text({
message: `What is the ${kleur.underline('ID')} of your project?`,
placeholder: `com.company.${name}`,
placeholder: `com.company.${appName}`,
validate: (value) => {
if (!/^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+$/.test(value.toLowerCase())) {
return `Invalid App ID "${value}". Must be in Java package form with no dashes (ex: com.example.app)`;
Expand All @@ -65,13 +65,13 @@ async function promptOptions() {
message: 'Do you want to add additional platforms?'
});

const platforms = ['Android', 'iOS'];
const allPlatforms = ['Android', 'iOS'];

let selectedPlatforms: Platform[] | null = null;
let platforms: Platform[] | null = null;
if (shouldPromptPlatforms) {
selectedPlatforms = (await multiselect({
platforms = (await multiselect({
message: 'What platforms do you want to add?',
options: platforms.map((platform) => {
options: allPlatforms.map((platform) => {
return {
value: platform.toLowerCase() as Platform,
label: platform
Expand All @@ -81,7 +81,7 @@ async function promptOptions() {
})) as Platform[];
}

const plugins = [
const allPlugins = [
'Action Sheet',
'App',
'App Launcher',
Expand Down Expand Up @@ -112,11 +112,11 @@ async function promptOptions() {
message: 'Do you want to add additional plugins?'
});

let selectedPlugins: Plugin[] | null = null;
let plugins: Plugin[] | null = null;
if (shouldPromptPlugins) {
selectedPlugins = (await multiselect({
plugins = (await multiselect({
message: 'What plugins do you want to add?',
options: plugins.map((plugin) => {
options: allPlugins.map((plugin) => {
return {
value: plugin.toLowerCase().replace(/ /g, '-') as Plugin,
label: plugin
Expand All @@ -126,41 +126,37 @@ async function promptOptions() {
})) as Plugin[];
}

const pm = getPM();
const packageManager = getPM();

const options = {
name,
id,
selectedPlatforms,
selectedPlugins,
configExtension,
pm
appName,
appId,
platforms,
plugins,
packageManager
} as ProjectOptions;

return options;
}

export async function initializeProject({
name: appName,
id: appId,
selectedPlatforms,
configExtension,
selectedPlugins,
pm
appName,
appId,
platforms,
plugins,
packageManager
}: ProjectOptions) {
const jobs: Job[] = [];

/*
Configuration jobs
*/

if (configExtension) {
/* Configuration jobs */
const extension = getConfigExtension();
if (extension) {
jobs.push({
start: `Removing existing config: "${kleur.cyan(`capacitor.config.${configExtension}`)}"`,
start: `Removing existing config: "${kleur.cyan(`capacitor.config.${extension}`)}"`,
stop: `Successfully removed existing config: "${kleur.cyan(
`capacitor.config.${configExtension}`
`capacitor.config.${extension}`
)}"`,
task: async () => await fs.unlink(`capacitor.config.${configExtension}`)
task: async () => await fs.unlink(`capacitor.config.${extension}`)
});
}

Expand All @@ -186,50 +182,55 @@ export async function initializeProject({
});

if (existsSync(`${process.cwd()}/.gitignore`)) {
jobs.push({
start: `Configuring: "${kleur.cyan('.gitignore')}"`,
stop: `Successfully configured: "${kleur.cyan('.gitignore')}"`,
task: async () => {
const gitignores = ['# Capacitor', '/android', '/ios', 'capacitor.config.json.timestamp-*'];
const gitignore = await fs.readFile(`${process.cwd()}/.gitignore`, 'utf-8');
const newGitignore = gitignore + '\n' + gitignores.join('\n');
return fs.writeFile('.gitignore', newGitignore, 'utf-8');
}
});
const gitignore = await fs.readFile(`${process.cwd()}/.gitignore`, 'utf-8');
if (!gitignore.includes('# Capacitor')) {
jobs.push({
start: `Configuring: "${kleur.cyan('.gitignore')}"`,
stop: `Successfully configured: "${kleur.cyan('.gitignore')}"`,
task: async () => {
const gitignores = [
'# Capacitor',
'/android',
'/ios',
'capacitor.config.json.timestamp-*'
];
const gitignore = await fs.readFile(`${process.cwd()}/.gitignore`, 'utf-8');
const newGitignore = gitignore + '\n' + gitignores.join('\n');
return fs.writeFile('.gitignore', newGitignore, 'utf-8');
}
});
}
}

/*
Install jobs
*/

/* Install jobs */
jobs.push({
start: 'Installing Capacitor',
stop: 'Successfully installed Capacitor',
task: async () => await asyncExec(`${pm} install @capacitor/cli @capacitor/core`)
task: async () => await asyncExec(`${packageManager} install @capacitor/cli @capacitor/core`)
});

if (selectedPlatforms) {
if (platforms) {
jobs.push({
start: 'Adding additional platforms',
stop: 'Successfully added additional platforms',
task: async () => {
for (let i = 0; i < selectedPlatforms.length; i++) {
const platform = selectedPlatforms[i];
await asyncExec(`${pm} install @capacitor/${platform}`);
for (let i = 0; i < platforms.length; i++) {
const platform = platforms[i];
await asyncExec(`${packageManager} install @capacitor/${platform}`);
await asyncExec(`npx cap add ${platform}`);
}
}
});
}

if (selectedPlugins) {
if (plugins) {
jobs.push({
start: 'Adding additional plugins',
stop: 'Successfully added additional plugins',
task: async () => {
let installCommand = `${pm} install`;
for (let i = 0; i < selectedPlugins.length; i++) {
const platform = selectedPlugins[i];
let installCommand = `${packageManager} install`;
for (let i = 0; i < plugins.length; i++) {
const platform = plugins[i];
installCommand += ` @capacitor/${platform}`;
}
return await asyncExec(installCommand);
Expand Down
11 changes: 5 additions & 6 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ export type Job = {
};

export type ProjectOptions = {
name: string;
id: string;
selectedPlatforms: Platform[];
selectedPlugins: Plugin[];
configExtension: ConfigExtension | null;
pm: PackageManager;
appName: string;
appId: string;
platforms: Platform[];
plugins: Plugin[];
packageManager: PackageManager;
};

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

0 comments on commit bee2a49

Please sign in to comment.