Skip to content

Commit 35b791b

Browse files
committed
fix: vue based custom project should work with build and serve
1 parent 20a4cbf commit 35b791b

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/ionic-build.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { Project } from './project';
22
import { FrameworkType, MonoRepoType } from './monorepo';
33
import { ionicState } from './ionic-tree-provider';
44
import { InternalCommand } from './command-name';
5-
import { npx, preflightNPMCheck } from './node-commands';
5+
import { npmRun, npx, preflightNPMCheck } from './node-commands';
66
import { exists } from './analyzer';
77
import { CapacitorPlatform } from './capacitor-platform';
88
import { getConfigurationArgs } from './build-configuration';
99
import { workspace } from 'vscode';
1010
import { error } from 'console';
11+
import { existsSync, readFileSync } from 'fs';
12+
import { join } from 'path';
1113

1214
/**
1315
* Creates the ionic build command
@@ -59,7 +61,7 @@ function ionicCLIBuild(
5961
configurationArg?: string,
6062
platform?: CapacitorPlatform,
6163
): string {
62-
let cmd = `${npx(project.packageManager)} ${buildCmd(project.frameworkType)}`;
64+
let cmd = `${npx(project.packageManager)} ${buildCmd(project)}`;
6365
if (configurationArg) {
6466
cmd += ` ${configurationArg}`;
6567
} else if (prod) {
@@ -72,8 +74,8 @@ function ionicCLIBuild(
7274
return cmd;
7375
}
7476

75-
function buildCmd(framework: FrameworkType): string {
76-
switch (framework) {
77+
function buildCmd(project: Project): string {
78+
switch (project.frameworkType) {
7779
case 'angular':
7880
case 'angular-standalone':
7981
return 'ng build';
@@ -84,10 +86,28 @@ function buildCmd(framework: FrameworkType): string {
8486
return 'react-scripts build';
8587
case 'vue':
8688
return 'vue-cli-service build';
87-
default:
88-
error('build command is unknown');
89+
default: {
90+
const cmd = guessBuildCommand(project);
91+
if (!cmd) {
92+
error('build command is unknown');
93+
}
94+
return cmd;
95+
}
96+
}
97+
}
98+
99+
function guessBuildCommand(project: Project): string | undefined {
100+
const filename = join(project.projectFolder(), 'package.json');
101+
if (existsSync(filename)) {
102+
const packageFile = JSON.parse(readFileSync(filename, 'utf8'));
103+
if (packageFile.scripts['ionic:build']) {
104+
return npmRun('ionic:build');
105+
}
106+
if (packageFile.scripts?.build) {
107+
return npmRun('build');
108+
}
89109
}
90-
return 'build command unknown';
110+
return undefined;
91111
}
92112

93113
function nxBuild(prod: boolean, project: Project, configurationArg?: string): string {

src/ionic-serve.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { existsSync } from 'fs';
1+
import { existsSync, readFileSync } from 'fs';
22
import { networkInterfaces } from 'os';
33

44
import { getConfigurationArgs } from './build-configuration';
55
import { InternalCommand } from './command-name';
66
import { ionicState } from './ionic-tree-provider';
77
import { certPath } from './live-reload';
88
import { FrameworkType, MonoRepoType } from './monorepo';
9-
import { npx, preflightNPMCheck } from './node-commands';
9+
import { npmRun, npx, preflightNPMCheck } from './node-commands';
1010
import { Project } from './project';
1111
import { liveReloadSSL } from './live-reload';
1212
import { ExtensionSetting, getExtSetting, getSetting, setSetting, WorkspaceSetting } from './workspace-state';
1313
import { getWebConfiguration, WebConfigSetting } from './web-configuration';
1414
import { window, workspace } from 'vscode';
1515
import { write, writeError } from './logging';
1616
import { createServer } from 'http';
17+
import { join } from 'path';
1718

1819
/**
1920
* Create the ionic serve command
@@ -77,11 +78,11 @@ async function ionicCLIServe(project: Project, dontOpenBrowser: boolean): Promis
7778
serveFlags += ` --ssl-key='${certPath('key')}'`;
7879
}
7980

80-
return `${preop}${npx(project.packageManager)} ${serveCmd(project.frameworkType)}${serveFlags}`;
81+
return `${preop}${npx(project.packageManager)} ${serveCmd(project)}${serveFlags}`;
8182
}
8283

83-
function serveCmd(framework: FrameworkType): string {
84-
switch (framework) {
84+
function serveCmd(project: Project): string {
85+
switch (project.frameworkType) {
8586
case 'angular':
8687
case 'angular-standalone':
8788
return 'ng serve';
@@ -92,11 +93,29 @@ function serveCmd(framework: FrameworkType): string {
9293
return 'react-scripts start';
9394
case 'vue':
9495
return 'vue-cli-service serve';
95-
default:
96+
default: {
97+
const cmd = guessServeCommand(project);
98+
if (cmd) {
99+
return cmd;
100+
}
96101
writeError(`serve command is not know for this project type`);
102+
}
97103
}
98104
}
99105

106+
function guessServeCommand(project: Project): string | undefined {
107+
const filename = join(project.projectFolder(), 'package.json');
108+
if (existsSync(filename)) {
109+
const packageFile = JSON.parse(readFileSync(filename, 'utf8'));
110+
if (packageFile.scripts['ionic:serve']) {
111+
return npmRun('ionic:serve');
112+
}
113+
if (packageFile.scripts?.serve) {
114+
return npmRun('serve');
115+
}
116+
}
117+
return undefined;
118+
}
100119
async function findNextPort(port: number): Promise<number> {
101120
let availablePort = port;
102121
while (await isPortInUse(availablePort)) {

src/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ export async function run(
281281
'open your browser on ',
282282
'> Local:', // Nuxt
283283
'➜ Local:', // AnalogJs
284+
'- Local:', // Vue
284285
]);
285286
if (url) {
286287
findLocalUrl = false;
@@ -296,6 +297,7 @@ export async function run(
296297
'On Your Network:',
297298
'> Network:', // Nuxt
298299
'➜ Network:', // AnalogJs
300+
'- Network:', // Vue
299301
'open your browser on ', // NX
300302
]);
301303
if (url) {

0 commit comments

Comments
 (0)