11

We want to use the same build system across multiple projects. I have a working brunch configuration file that I want to put in a git submodule, so that that submodule can be referenced in multiple projects and changes are easily propagated (less fragile than copy and paste and sets authoritative source of brunch-config.js).

Putting brunch-config.js in a git submodule though causes my folder structure to end up like this:

WebApp // git root
|---Brunch-Build-System // git submodule
|   |---brunch-config.js
|---node_modules
|---source // all the source code I want compiled

Brunch operates assuming brunch-config.js would be at the same or higher level than the source being compiled. In this setup, this is not the case. I've tried modifying my brunch-config.js to use a relative path to no avail. Here is my files block of the Brunch configuration as it currently stands, without any relative path attempt:

files: {
  javascripts: {
    joinTo: {
      'js/lib.js': /^(?!source\/)/
    },
    entryPoints: {
      'source/scripts/app.jsx': {
        'js/app.js': /^source\//
      },
    }
  },
  stylesheets: {joinTo: 'css/core.css'}
}

How could I modify this to use a relative path given the desired folder structure above? Is this even possible?

Scotty H
  • 6,432
  • 6
  • 41
  • 94

2 Answers2

2

A submodule needs to be associates with a root folder within the parent repo: it is the gitlink (a special entry in the main repo index) which records which SHA1 that submodule is checked out.

In your case, it would be best if you can use, in a post-checkout hook, a script ensuring there is a symbolic link between:

  • brunch-config.js at its correct place and
  • that Brunch-Build-System / brunch-config.js from the submodule

The lines to include in the hook would be something along the lines of

#!/bin/bash

if [ ! -e /correct/path/for/brunch-config.js ]; then
  ln -s /correct/path/for/brunch-config.js Brunch-Build-System/brunch-config.js
fi

Although the OP being on Windows:

I ended up just programmatically copying the file over and gitignoreing the copied file

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Could you include the line(s) I would put in the post-checkout hook to make the symlink? – Scotty H May 09 '17 at 13:40
  • @ScottH I have edited the answer with a idea of what the hook might look like – VonC May 09 '17 at 13:57
  • Thanks but when using this on Windows 10 it seems to copy the file rather than creating a symbolic link like `mklink` does. Any thoughts? Also I had to switch the order of the arguments for the `ln` command. – Scotty H May 09 '17 at 14:38
  • Then you can actually use the mklink, if you are running it as admin. If not, use mklink /J. – VonC May 09 '17 at 14:40
  • I'm having trouble using `mklink` as part of the bash script though, it says "Command not found". – Scotty H May 09 '17 at 14:47
  • @ScottH Try http://stackoverflow.com/a/10642325/6309 in order to call `cmd /C "mklink /J ..."` – VonC May 09 '17 at 14:55
  • As I cannot run this as an administrator, I will need the `/J` flag. I'm working on how to use that as that is a directory junction and not a symbolic link, suggestions welcome. Can you edit your answer to reflect the things we have discussed, including the Windows/Linux differences and that your `ln` commands seem to be out of order? – Scotty H May 09 '17 at 15:28
  • When I try `mklink /J` with a file it creates something but double clicking it results in popup error "The directory name is invalid". There seems to be a substantial difference between symbolic links and junctions, see [SO answer](http://stackoverflow.com/questions/9042542/what-is-the-difference-between-ntfs-junction-points-and-symbolic-links#answer-9042600). I need to be able to link to effectively have brunch-config.js at root, which seems accomplishable only with a symbolic link, which I need admin permissions for. Perhaps I should just copy the file on checkout and git ignore it. – Scotty H May 09 '17 at 15:45
  • @ScottH yes, this is expected: the goal is not to double click. The goal is for a program needing to access that file to be able to read it through this (directory) junction which could work for a file too. – VonC May 09 '17 at 15:51
  • Even though the junction seemed to produce a case such that when `cat`ed I could see the file, `brunch build` did nothing - no errors, no compilation, no nothing. I ended up just programmatically copying the file over and `gitignore`ing the copied file. – Scotty H May 09 '17 at 19:23
  • @ScottH OK. I have included your conclusion in the answer for more visibility. – VonC May 09 '17 at 19:24
0

In general, brunch supports relative paths. But in order to make it work, you have to first make sure that the other folders are watched. See this part in the docs. For your case, add this to your config.

paths = {
  watched: ['../source', '../node_modules'],
}

Then prepend the RegExs by ..\/.

joinTo: {
  'js/lib.js': /^..\/(?!source\/)/
},
entryPoints: {
  'source/scripts/app.jsx': {
    'js/app.js': /^..\/source\//
  },
}

Instead of a RegEx, you can also write a function that takes the path as an argument and has to return whether the path should be included or not. See this part in the docs.

Johannes Filter
  • 1,863
  • 3
  • 20
  • 25
  • I just got to try this and it prompted me to have a package.json with the brunch libraries in them. This required installing brunch npm packages in the submodule. Brunch building ran for a while, saying it was compiling but eventually I ended up with a "JavaScript heap out of memory" error. – Scotty H May 15 '17 at 15:32
  • Wait, you want the `package.json` to be outside of the submodule, right? – Johannes Filter May 15 '17 at 15:37
  • It seems that in order to make it work, the `package.json` and thus the `node_modules` have to be in the submodule folder since you cannot change the default `node_modules` location: http://stackoverflow.com/questions/26293049/specify-path-to-node-modules-in-package-json/26293141#26293141 – Johannes Filter May 15 '17 at 15:46