Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use command line arguments for scripts. #26

Open
cairin opened this issue Oct 23, 2018 · 27 comments
Open

Use command line arguments for scripts. #26

cairin opened this issue Oct 23, 2018 · 27 comments

Comments

@cairin
Copy link

cairin commented Oct 23, 2018

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
Copy link
Author

cairin commented Oct 23, 2018

Example source

@m0dch3n
Copy link
Owner

m0dch3n commented Oct 23, 2018

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
Copy link
Owner

m0dch3n commented Oct 25, 2018

@cairinmichie have you given it a try ?

@cairin
Copy link
Author

cairin commented Oct 25, 2018

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

@m0dch3n
Copy link
Owner

m0dch3n commented Oct 26, 2018

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

@m0dch3n
Copy link
Owner

m0dch3n commented Nov 12, 2018

@cairinmichie do you have any progress?

@cairin
Copy link
Author

cairin commented Nov 12, 2018

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

@m0dch3n
Copy link
Owner

m0dch3n commented Dec 4, 2018

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

@jacobg
Copy link

jacobg commented Dec 4, 2018

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
Copy link

jacobg commented Dec 4, 2018

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.

@m0dch3n
Copy link
Owner

m0dch3n commented Dec 4, 2018

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

#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...

@jacobg
Copy link

jacobg commented Dec 4, 2018

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

Does using build.json address your concern?

@m0dch3n
Copy link
Owner

m0dch3n commented Dec 4, 2018

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...

@jacobg
Copy link

jacobg commented Dec 4, 2018

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.

@cairin
Copy link
Author

cairin commented Dec 4, 2018

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

@m0dch3n
Copy link
Owner

m0dch3n commented Dec 4, 2018

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

@jacobg
Copy link

jacobg commented Dec 4, 2018

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.

@m0dch3n
Copy link
Owner

m0dch3n commented Dec 5, 2018

@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
Copy link
Owner

m0dch3n commented Dec 5, 2018

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
Copy link
Owner

m0dch3n commented Dec 5, 2018

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 ...

#31

@jacobg
Copy link

jacobg commented Dec 5, 2018

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

@JohnRSim
Copy link

Any progress with adding build.json support :)

@alexcroox
Copy link

+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'

@jacobg
Copy link

jacobg commented May 14, 2019

@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.

@alexcroox
Copy link

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: "-".

@jacobg
Copy link

jacobg commented May 14, 2019

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.

@alexcroox
Copy link

alexcroox commented May 14, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants