NativeScript / nativescript-cli

Command-line interface for building NativeScript apps
https://www.npmjs.com/package/nativescript
Apache License 2.0
1.04k stars 195 forks source link

Setting the appResourcesPath or nsconfig through command line #4718

Open farfromrefug opened 5 years ago

farfromrefug commented 5 years ago

Is there a way to do this right now? I want to build a "wearos" variant of my app. But i need a different App_Resources folder and different app id. I am trying to achive this through package.json scripts. But to do that i need command line args to switch the folders

rosen-vladimirov commented 5 years ago

Hey @farfromrefug , Currently the id of the application is set in the package.json file, while the App_Resources can be set in nsconfig.json, as described in the docs. So for your case, you can use script to change both values and build the app after that. Does it sound reasonable for your case?

farfromrefug commented 5 years ago

@rosen-vladimirov someone advised that but I dont like that that solution. the thing Is that it modifies committed files. I get it is not in your timeline but it would be a great feature to have

rosen-vladimirov commented 5 years ago

So, just to clear this out, you would like to pass some -- flag to CLI's command, which will change the nsconfig.json which will be used for building the application. Also, the id should be persisted in the nsconfig.json instead of package.json.

Am I right?

farfromrefug commented 5 years ago

yes the id in the nsconfig would be great. and the cli arc that I would like is one to a specify the nsconfig file to use. just like tsc and the tsconfig file. thanks

Eonfuzz commented 4 years ago

Any updates on this? It's 🌶 and would our dev pipeline cleaner

pfumagalli commented 3 years ago

I do this with an environment variable.

The id and appResourcesPath are configurable via nativescript.config.ts and when this is executed, the code determines the correct values for those parameters.

I have two flavours of my app (all different resources, configurations, ...) and my tree looks like this:

+- resources (instead of App_Resources)
   +- flavour1
   |  +- Android
   |  +- iOS
   +- flavour2
      +- Android
      +- iOS

I use the APP_FLAVOUR environment variable to determine which flavour to build and my nativescript.config.ts looks like this:

import { NativeScriptConfig } from '@nativescript/core'

/* Get (and ensure we have the default) the "APP_FLAVOUR" environment variable */
const flavour = process.env.APP_FLAVOUR = process.env.APP_FLAVOUR || 'flavour1'
switch (flavour) {
  case 'flavour1': break
  case 'flavour2': break
  default:
    throw new Error(`Unknown / unsupported app flavour "${flavour}" (must be "flavour1" or "flavour2")`)
}

/* Export our NativeScript configuration */
export default {
  id: `foo.bar.${flavour}.myapp`,
  appResourcesPath: `resources/${flavour}`,
  // ... all the rest of the stuff
} as NativeScriptConfig

I also modify webpack.config.js to push my flavour as a constant in the app

new webpack.DefinePlugin({
  // ... all various definitions...
  'TNS_ENV': JSON.stringify(mode),
  'APP_FLAVOUR': JSON.stringify(process.env.APP_FLAVOUR),
})

And finally my types/env.d.ts file looks like this:

declare const TNS_ENV: string
declare const APP_FLAVOUR: 'flavour1' | 'flavour2'

So that anywhere in the code I can simply refer to the APP_FLAVOUR constant and handle some special cases...

When building/running it's as easy as doing something like (the flavour1 flavour is the default, as per nativescript.config.ts):

APP_FLAVOUR=flavour2 ns run ios