How to count the number of files in the project


#1

Is there a way to count the number files in the project, excluding all ignored names?

Thanks for any hint.


#2

I haven’t tested it, but something like this should work in a package:

const atom = require('atom');
const globby = require('globby');

// Options for globby
const options = {
    gitignore: true
};

// Array of project paths
const projectPaths = atom.project.getPaths();

(async () => {
    const paths = await globby(projectPaths, options);
    console.log(paths.length);
})();

Relies on globby, which supports .gitignore out of the box.


#3

Thanks for the suggestion. A great answer if I was to write my own package.

However, I was wondering if there is a built-in feature in Atom or a package that already takes care of counting.


#4

You can put that inside a command in your init script and run it with a keybinding if you want. No need to actually make a package.


#5

I see. Thanks for the hints. Gave it a try today but I’m struggling to get this running. I was not able to load the non-standard module globby.

Is the atom-import-js package the way to go to install a new module within atom? I installed globby via npm, but atom seems not to search for these resources.

Sorry, I’m a total newb, to both JavaScript and Atom.


#6

I didn’t notice that the sample uses a module Atom doesn’t ship with. A package would be the best way then, since Atom doesn’t have a node_modules/ file that it looks in. In the init script, you’d have to require() using the absolute path of the module.

No, that’s a package designed to make it easier to write clean code.

I installed globby via npm

Where did you install it? I installed it in my .atom/ directory and referenced it with globby = require('C:/Users/stick/.atom/node_modules/globby'); and was able to inspect the resulting variable to see that the module was there.

Sorry, I’m a total newb, to both JavaScript and Atom.

And Atom is a great environment to mess around in and try out new things. When learning a new programming language, the toughest part is finding well-defined projects where you have existing input and can easily see the effects of your work, and where the project itself is more useful than printing a sentence or rewriting a common algorithm. With Atom, you have whatever else you’re working on as input, you can immediately see the effects of your code, and most of the time you’re writing code that makes your experience with your editor more fun or productive.


#7

Many thanks for the detailed answer and the motivation. I’ll give it a try! :slight_smile: