Forum Discussion

KSQian's avatar
KSQian
Contributor
7 years ago

Path of included file

So I have 2 projects and they share some common code.

 

Project1 is C:\Project1\one.pjs. It has a script file projOneLib.js

Project2 is C:\Project2\two.pjs. It has 2 script files. One is projTwoLib.js, but I also included the script file from Project1, projOneLib.js.

 

Now, while running Project2, I need the location of projOneLib.js' path. How would I do that? I need a way to say, grab me the original file location of projOneLib.js. 

 

This can't be too hard-coded either, so in projOneLib.js, if I can save its own file location to a in-memory variable, that'd be good.

 

Thanks!

 

 

 

  • Why can't you put your source code somewhere to the static path which doesn't include version number? It would simplify your work.

     

    Anyway, if that is not possible, you have at least two options:

    1. Take the version from any external source (like registry, file info, source control output, etc.). I am not aware about your specific situation, but usually there are different ways how you can get the same info from different places.
    2. Probably the simpler way: use FileFiles Method to search for the desired file.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Why do you need the path of the script file?  

    Usually, if I'm going to be doing stuff that requires "common" code and data, I create a "common" folder in my source repository.  I can then reference that for anything that needs common storage so I don't have to worry about paths... I just need to know that the common folder is something like " ..\..\..\Common\Storage\"

    • KSQian's avatar
      KSQian
      Contributor

      Good question...

       

      So longer story. We have a function that reads data from excel spreadsheet.

       

      Let's call it readExcel(). As you know, you need path of the sheet you want to read it from.

       

      Now, we have different applications (one for each project) and we export the classes out of it. 

       

      Some projects need 2 applications, so the code will look like:

       

      const App1 = require('App1')

      const App2 = require('App2')

       

      Each app should be able to read from their respective excel spreadsheets.

      App1.readExcel("Orders")

      App2.readExcel("Orders")

       

      But because they are 2 different apps, the "Orders" spreadsheets are in different locations. So in App2, I want it to just know to read the Orders spreadsheet from the App2 directory.

       

      I kinda don't want to hardcode the path either, because of version control. If I set the path anywhere as App2/v1.231/Orders.xls, it will need to be changed whenever we change the version, which happens a lot. 

       

      So I wanted to just say hey look for the Orders.xls in the directory where the file you are in currently...

       

      ... if that makes sense.

       

      This is the lazy solution I have for now. But welcome to take a diff approach entirely

        

      • karkadil's avatar
        karkadil
        Valued Contributor

        Hi,

         

        I haven't really understood your 2nd explanation, but try to help you with your 1st request, may be you'll be able to figure out everything else.

         

        So, if you have 2 projects, they are most probably are added to one project suite. With the help of property ProjectSuite.Path you can get path to the folder where your projects are located. Now you are able to write something like this:

         

        var libPath = ProjectSuite.Path + "\\Project1\\Script\\projOneLib.js";

         

        Does it help?

  • Hmmm

     

    So lets say 

    App1 - C:\Projects\App1\v1.3.2\scripts\Unit.js

    App2 - C:\Projects\App2\v.2.3.1\scripts\Unit2.js

     

    So in App2, I included a script from App1. Problem is in Unit.js, it doesn't know that it actually belongs to "App1." So Even I go back to the projects folder (that they share), I wouldn't know how to proceed into App1 folder. Unless in App1 Unit.js itself, I hard-code something like dir=App1... but that doesnt work when we include git versions unless we update the hard-code each time.

     

    The last way I could think of is provide a soft link in the directory to the latest version. And in App1 Unit.js I hard-code App1\latest and it can redirect to the latest v... but its so round-about.

     

    Thanks

    • karkadil's avatar
      karkadil
      Valued Contributor

      Why can't you put your source code somewhere to the static path which doesn't include version number? It would simplify your work.

       

      Anyway, if that is not possible, you have at least two options:

      1. Take the version from any external source (like registry, file info, source control output, etc.). I am not aware about your specific situation, but usually there are different ways how you can get the same info from different places.
      2. Probably the simpler way: use FileFiles Method to search for the desired file.
      • KSQian's avatar
        KSQian
        Contributor

        If you put it in the static path, once you check it back into git, you still need to change the version numbers.

         

        Because dependencies are different. If you have 8 Apps, and you update App1, you want the other Apps that depend on App1 to still use the previous version of App1. Some Apps will update faster than others, so the dependencies can get quite intricate. Currently thats being handled via cfg files. 

         

        I'll handle the file paths via cfg files for now too. I haven't looked at "FileFiles" but I'll take a look at that to see if its a good usecase.

         

        Thanks!