vue-cli-plugin-cordova icon indicating copy to clipboard operation
vue-cli-plugin-cordova copied to clipboard

Use command line arguments for scripts.

Open cairin opened this issue 6 years ago • 27 comments

What?

I think it would be easier to specify npm run scripts with command line arguments rather, as it would allow greater diversity in scripts that are runnable.

Example:

As an example I will use a simple add method that take command line arguments in its script:

// add.js
// adds two integers received as command line arguments
function add(a, b) {
    return parseInt(a)+parseInt(b);
}
if(!process.argv[2] || !process.argv[3]) {
    console.log('Insufficient number of arguments! Give two numbers please!');
}
else {
    console.log('The sum of', process.argv[2], 'and', process.argv[3], 'is', add(process.argv[2], process.argv[3]));
}

The script becomes:

"js-add": "node add.js"

To run the script:

npm run js-add 2 3

Giving an output of:

The sum of 2 and 3 is 5

Relevance to vue-cli-plugin-cordova?

Using this method:

npm run cordova-serve-ios

can be replace with:

npm run cordova serve ios

This would be ideal in my situation, as I can't build my ios projects on the command line. So my typical build command would be cordova prepare ios and then I would run the app through xcode. But because your cli commands don't allow for cordova prepare ios I can't do this.

This way would also make adding commands like cordova prepare ios easier.

cairin avatar Oct 23 '18 14:10 cairin

Example source

cairin avatar Oct 23 '18 14:10 cairin

Hello, this sounds nice, but I think you need at least 2 commands then, to distinct between dev and prod environment, because of this

This is because the command's expected mode needs to be known before loading environment variables, which in turn needs to happen before loading user options / applying the plugins.`

Nevertheless, I like your syntax more, than mine, so feel free to implement it, and sent me a pull request.

m0dch3n avatar Oct 23 '18 15:10 m0dch3n

@cairinmichie have you given it a try ?

m0dch3n avatar Oct 25 '18 17:10 m0dch3n

I haven't had a chance yet, was planning on doing it this weekend.

cairin avatar Oct 25 '18 20:10 cairin

ok, no problem, I'll keep this issue open for 3 weeks, if nothing happens.

m0dch3n avatar Oct 26 '18 08:10 m0dch3n

@cairinmichie do you have any progress?

m0dch3n avatar Nov 12 '18 07:11 m0dch3n

No, sorry I have been too busy with other work.

cairin avatar Nov 12 '18 14:11 cairin

@cairinmichie did you made any progress or should I close the ticket ?

m0dch3n avatar Dec 04 '18 08:12 m0dch3n

It would be useful also to be able to specify command line arguments or a build.json file served to the cordova build command. I need to be able to do something like this: cordova build ios --release --buildFlag='-UseModernBuildSystem=0' --developmentTeam='MY_TEAM' --codeSignIdentity='iPhone Developer'

I could just run that command after running npm run cordova-build-ios (which will fail), but the core purpose of this plugin is to wrap that functionality with the webpack part.

jacobg avatar Dec 04 '18 14:12 jacobg

The build.json approach is cleaner:

  1. You save a build.json file:
{
    "ios": {
        "debug": {
            "codeSignIdentity": "iPhone Developer",
            "developmentTeam": "MY_TEAM",
            "packageType": "development",
            "automaticProvisioning": true,
            "buildFlag": [
                "-UseModernBuildSystem=0"
            ]
        },
        "release": {
            "codeSignIdentity": "iPhone Developer",
            "developmentTeam": "MY_TEAM",
            "packageType": "app-store",
            "automaticProvisioning": true,
            "buildFlag": [
                "-UseModernBuildSystem=0"
            ]
        }
    }
}
  1. Run command: cordova build ios --buildConfig=./build.json

In this approach, the plugin does not need to parse command line arguments. Rather it could automatically check if build.json exists in src-cordova, and if so, then add it to the cordova build command.

jacobg avatar Dec 04 '18 14:12 jacobg

@jacobg you came to the same problem then here I think:

https://github.com/m0dch3n/vue-cli-plugin-cordova/issues/20

My question there was, how would you distinct between cordova and vue arguments ? If all the arguments are passed to cordova, you no longer can use the arguments, like these:

https://github.com/vuejs/vue-cli/blob/7c91a187e46d7bb9a9b83bcdf97631794ac36b10/packages/%40vue/cli-service/lib/commands/serve.js#L18

An idea I had, was having a prompt asking for the commands, if you run cordova with i.e. new --interactive flag...

m0dch3n avatar Dec 04 '18 15:12 m0dch3n

@m0dch3n Thanks, I didn't notice issue #20

Does using build.json address your concern?

jacobg avatar Dec 04 '18 15:12 jacobg

never tried build.json, so in fact, you can define all the arguments you need, for every cordova command and keep then command arguments away ?

If so, this would help a lot keeping all the special cordova features away from the plugin...

IMO it's important that the plugin is simple to understand and use, and if you are a more advanced user, you still can do more things with the plugin...

As the plugin is running 2 commands (vue serve, cordova run), it needs to be very clear, to which command, the arguments are passed...

That's why I had the idea of an interactive prompt, so you can normally pass the initial arguments to the vue command, and afterwards enter more arguments to the cordova command...

m0dch3n avatar Dec 04 '18 15:12 m0dch3n

I agree about keeping it simple and being clear about separating of vue and cordova. Sounds like build.json accomplishes that. The npm commands remain exactly the same, and build.json is not required to maintain existing functionality.

jacobg avatar Dec 04 '18 15:12 jacobg

I agree that the build.json seems to be the simplest way to give users more control.

cairin avatar Dec 04 '18 15:12 cairin

ok, I'll give build.json a try, and we can mention and explain this in the README for the "advanced" users

m0dch3n avatar Dec 04 '18 15:12 m0dch3n

Thanks @m0dch3n. What are your thoughts with respect to specifying a mode in the npm command? https://cli.vuejs.org/guide/mode-and-env.html#modes

For example, it's highly useful in order to specify which backend service environment the vue app should use (e.g., production, local development server, staging, etc). Then the specific project can define the .env.[mode] files to specify the environment variables that apply to that mode.

jacobg avatar Dec 04 '18 17:12 jacobg

@jacobg modes are already working, if you do

"cordova-serve-android": "vue-cli-service cordova-serve-android --mode staging"

the app takes the vars from .env.staging...

m0dch3n avatar Dec 05 '18 16:12 m0dch3n

so i.e. you could this

"cordova-staging-android": "vue-cli-service cordova-build-android --mode staging",

this would end up in :

vue-cli-service build --mode staging --dest src-cordova/www
cordova build android --release

m0dch3n avatar Dec 05 '18 16:12 m0dch3n

BTW, if we go with only one command for the plugin flexibility, we cannot solve this issue in the same time, because during the plugin runtime, it's already to late ...

https://github.com/m0dch3n/vue-cli-plugin-cordova/issues/31

m0dch3n avatar Dec 05 '18 17:12 m0dch3n

Ahh, that's great. I didn't realize that the vue cli will automatically use the mode even on custom commands. Thanks!

jacobg avatar Dec 05 '18 21:12 jacobg

Any progress with adding build.json support :)

JohnRSim avatar Dec 20 '18 17:12 JohnRSim

+1 for this. cordova-build-ios / cordova-serve-ios is broken with the latest version of xcode (10). We need to be able to pass --buildFlag='-UseModernBuildSystem=0'

alexcroox avatar May 13 '19 21:05 alexcroox

@alexcroox Are you running cordova-ios 4? Because that does require --buildFlag='-UseModernBuildSystem=0'. But cordova-ios 5 works with the new modern build system, so you won't need that flag anymore.

jacobg avatar May 14 '19 01:05 jacobg

Updated to 5 and it's working now thanks. How do I specify signing params and packageType for cordova-build-ios? I have these created in a build.json but they seem to be ignored and it just gets built for Emulator (instead of app-store) every time. Also I see Signing Identity: "-".

alexcroox avatar May 14 '19 10:05 alexcroox

For that you need build.json. For now, you would need to manually run cordova build ios --buildConfig=./build.json. You should only need to run it once in order to add those settings to your xcode project file.

jacobg avatar May 14 '19 10:05 jacobg

Thanks, for now I'm running it after this plugin runs, but it still builds twice for the emulator

Sorry for the complexity of the build script, I'm building 2 different apps from the same codebase so I have to remove and re-install the ios platform every time I change the name.

"scripts": {
    "app-init:ios": "cd src-cordova && cordova platform rm ios && cordova platform add ios && cd platforms/ios/cordova && npm install ios-sim",
    "app-build:ios": "VUE_APP_PUBLIC_PATH='' && npm run app-init:ios && vue-cli-service cordova-build-ios && cd src-cordova && cordova build ios --buildConfig=./build.json",
}
{
  "ios": {
    "debug": {
      "codeSignIdentity": "iPhone Developer",
      "developmentTeam": "xxxx",
      "packageType": "development",
      "automaticProvisioning": true,
      "buildFlag": ["-UseModernBuildSystem=0"]
    },
    "release": {
      "codeSignIdentity": "iPhone Developer",
      "developmentTeam": "xxxx",
      "packageType": "app-store",
      "automaticProvisioning": true,
      "buildFlag": ["-UseModernBuildSystem=0"]
    }
  }
}

I've also tried cordova build ios --release --buildConfig=./build.json

ss

alexcroox avatar May 14 '19 10:05 alexcroox