How to use the cat function from shelljs

Find comprehensive JavaScript shelljs.cat code examples handpicked from public code repositorys.

The shelljs.cat method reads files and writes their content to standard output.

80
81
82
83
84
85
86
87
88
89
90
  'utf-8',
);


fs.writeFileSync(
  'packages/react-native/React/Base/RCTVersion.m',
  cat('scripts/versiontemplates/RCTVersion.m.template')
    .replace('${major}', `@(${major})`)
    .replace('${minor}', `@(${minor})`)
    .replace('${patch}', `@(${patch})`)
    .replace(
fork icon2
star icon0
watch icon4

+ 19 other calls in file

714
715
716
717
718
719
720
721
722
723
let errors = 0;

RULE_FILES.forEach(filename => {
    const basename = path.basename(filename, ".js");
    const docFilename = `docs/src/rules/${basename}.md`;
    const docText = cat(docFilename);
    const docTextWithoutFrontmatter = matter(String(docText)).content;
    const docMarkdown = marked.lexer(docTextWithoutFrontmatter, { gfm: true, silent: false });
    const ruleCode = cat(filename);
    const knownHeaders = ["Rule Details", "Options", "Environments", "Examples", "Known Limitations", "When Not To Use It", "Compatibility"];
fork icon0
star icon0
watch icon1

+ 59 other calls in file

How does shelljs.cat work?

shelljs.cat() is a function in the ShellJS library that reads and returns the contents of a file, or multiple files if provided as arguments, as a string or an array of strings. It supports file path expansion and globbing. It can also write the contents of the files to the console or a file if the -u or -o options are used.

653
654
655
656
657
658
659
660
661
662
    baseName = path.basename(filename),
    sourceBaseName = `${path.basename(filename, ".md")}.js`,
    sourcePath = path.join("lib/rules", sourceBaseName),
    ruleName = path.basename(filename, ".md"),
    filePath = path.join("docs", path.relative("tmp", filename));
let text = cat(filename),
    ruleType = "",
    title;

process.stdout.write(`> Updating files (Steps 4-9): ${i}/${length} - ${filePath + " ".repeat(30)}\r`);
fork icon0
star icon0
watch icon0

+ 38 other calls in file

0
1
2
3
4
5
6
7
8
9
10
11
const shell = require('shelljs');
const path = require('path');


shell.pushd(path.join(__dirname, '../frontend'));


if(shell.test('-f', '.env.production.local') && shell.cat('.env.production.local').toString() !== shell.cat('.env.testnet').toString()) {
    console.log('ERROR: .env.production.local already exists and is not equal to .env.testnet.');
    console.log('Delete .env.production.local and try again.');
    shell.exit(1);
}
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
const shell = require("shelljs");

// Display contents of a file
const fileContent = shell.cat("example.txt");
console.log(fileContent);

In this example, the shelljs.cat() method is used to read the contents of the file example.txt and store them in the fileContent variable. The console.log() method is then used to display the contents of the file in the console.

0
1
2
3
4
5
6
7
8
9
10
11
const shell = require('shelljs');
const path = require('path');


shell.pushd(path.join(__dirname, '../frontend'));


if(shell.test('-f', '.env.production.local') && shell.cat('.env.production.local').toString() !== shell.cat('.env.mainnet').toString()) {
    console.log('ERROR: .env.production.local already exists and is not equal to .env.mainnet.');
    console.log('Delete .env.production.local and try again.');
    shell.exit(1);
}
fork icon0
star icon0
watch icon0