0

In bash without using Ant, Grunt or similar, I want to concatenate some files.

I don't think is should be harder than a few lines of bash.

I don't want to use a build tool as they do in this SO Post.

I want to use bash similar to this SO Post.

It would be something like:

cat *.js > all.txt

However, I want to only cat .js files that have this form

object.SomeName.js

As a side question, do most people break out their .js into objects?

What about:

cat object.*.js >> all.txt

Also there are dependencies between the objects, so ordering matters.

What ordering does cat work in?

Community
  • 1
  • 1
cade galt
  • 3,843
  • 8
  • 32
  • 48
  • sounds like you just need to readup on wildcard file matching in bash... – dandavis Oct 04 '15 at 21:27
  • I posted what I think is the answer, but b.c. there are depeddencies, I need to know the order that cat works in or perhaps there is a batter bash tool? – cade galt Oct 04 '15 at 21:28
  • This answer: http://serverfault.com/a/122743 says bash wildcard expansion is alphabetical. – jjrv Oct 04 '15 at 21:30
  • you can add a prefix to depends to ensure that alpha order is the correct order. – dandavis Oct 04 '15 at 21:31
  • well I have dependencies, so I don't think I should do this. I would have to come up with a naming convention to assure the order. For example if obj2 needs obj0 and obj1, I would need to make sure they were loaded first. – cade galt Oct 04 '15 at 21:32
  • If you use ES6, JavaScript itself has a syntax to `import` dependencies so scripts can define a partial ordering of the files required for them to work. I'm using TypeScript partially for that feature, and other transpilers exist. I hate overcomplicated build tools also, but writing in "future JavaScript" has other big benefits and in x years the transpiler becomes unnecessary when everyone updates their engine. – jjrv Oct 04 '15 at 21:36

1 Answers1

2

First, create a file that describes your dependencies.

obj0 obj2
obj1 obj2

Now tsort(1) can give you an ordering.

$ tsort dep.txt
obj0
obj1
obj2

Now you can read each line in turn and output the files in the correct order.

{
  while read obj
  do
    cat "object.$obj.js"
  done < <(tsort dep.txt)
} > all.js
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Here is a nice explanation of tort - https://viget.com/extend/dependency-sorting-in-ruby-with-tsort - It uses a dependency hash. Can i write dep. txt in JSON ? Or is it just space separated like in your example. – cade galt Oct 24 '15 at 13:26
  • @cadegalt: As the man page explains, the input file is whitespace-delimited. – Ignacio Vazquez-Abrams Oct 24 '15 at 13:37