aboutsummaryrefslogtreecommitdiff
path: root/tools/lint-sh.js
blob: 7b0beaadfe2fb90b215903f501259bb568321134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env node
'use strict';

const { execSync, spawn } = require('child_process');
const { promises: fs, readdirSync, statSync } = require('fs');
const { extname, join, relative, resolve } = require('path');

const FIX_MODE_ENABLED = process.argv.includes('--fix');
const USE_NPX = process.argv.includes('--from-npx');

const SHELLCHECK_EXE_NAME = 'shellcheck';
const SHELLCHECK_OPTIONS = ['--shell=sh', '--severity=info', '--enable=all'];
if (FIX_MODE_ENABLED) SHELLCHECK_OPTIONS.push('--format=diff');
else if (process.env.GITHUB_ACTIONS) SHELLCHECK_OPTIONS.push('--format=json');

const SPAWN_OPTIONS = {
  cwd: null,
  shell: false,
  stdio: ['pipe', 'pipe', 'inherit'],
};

function* findScriptFilesRecursively(dirPath) {
  const entries = readdirSync(dirPath, { withFileTypes: true });

  for (const entry of entries) {
    const path = join(dirPath, entry.name);

    if (
      entry.isDirectory() &&
      entry.name !== 'build' &&
      entry.name !== 'changelogs' &&
      entry.name !== 'deps' &&
      entry.name !== 'fixtures' &&
      entry.name !== 'gyp' &&
      entry.name !== 'inspector_protocol' &&
      entry.name !== 'node_modules' &&
      entry.name !== 'out' &&
      entry.name !== 'tmp'
    ) {
      yield* findScriptFilesRecursively(path);
    } else if (entry.isFile() && extname(entry.name) === '.sh') {
      yield path;
    }
  }
}

const expectedHashBang = Buffer.from('#!/bin/sh\n');
async function hasInvalidHashBang(fd) {
  const { length } = expectedHashBang;

  const actual = Buffer.allocUnsafe(length);
  await fd.read(actual, 0, length, 0);

  return Buffer.compare(actual, expectedHashBang);
}

async function checkFiles(...files) {
  const flags = FIX_MODE_ENABLED ? 'r+' : 'r';
  await Promise.all(
    files.map(async (file) => {
      const fd = await fs.open(file, flags);
      if (await hasInvalidHashBang(fd)) {
        if (FIX_MODE_ENABLED) {
          const file = await fd.readFile();

          const fileContent =
            file[0] === '#'.charCodeAt() ?
              file.subarray(file.indexOf('\n') + 1) :
              file;

          const toWrite = Buffer.concat([expectedHashBang, fileContent]);
          await fd.truncate(toWrite.length);
          await fd.write(toWrite, 0, toWrite.length, 0);
        } else {
          if (!process.exitCode) process.exitCode = 1;
          console.error(
            (process.env.GITHUB_ACTIONS ?
              `::error file=${file},line=1,col=1::` :
              'Fixable with --fix: ') +
              `Invalid hashbang for ${file} (expected /bin/sh).`
          );
        }
      }
      await fd.close();
    })
  );

  const stdout = await new Promise((resolve, reject) => {
    const SHELLCHECK_EXE =
      process.env.SHELLCHECK ||
      execSync('command -v ' + (USE_NPX ? 'npx' : SHELLCHECK_EXE_NAME))
        .toString()
        .trim();
    const NPX_OPTIONS = USE_NPX ? [SHELLCHECK_EXE_NAME] : [];

    const shellcheck = spawn(
      SHELLCHECK_EXE,
      [
        ...NPX_OPTIONS,
        ...SHELLCHECK_OPTIONS,
        ...(FIX_MODE_ENABLED ?
          files.map((filePath) => relative(SPAWN_OPTIONS.cwd, filePath)) :
          files),
      ],
      SPAWN_OPTIONS
    );
    shellcheck.once('error', reject);

    let json = '';
    let childProcess = shellcheck;
    if (FIX_MODE_ENABLED) {
      const GIT_EXE =
        process.env.GIT || execSync('command -v git').toString().trim();

      const gitApply = spawn(GIT_EXE, ['apply'], SPAWN_OPTIONS);
      shellcheck.stdout.pipe(gitApply.stdin);
      shellcheck.once('exit', (code) => {
        if (!process.exitCode && code) process.exitCode = code;
      });
      gitApply.stdout.pipe(process.stdout);

      gitApply.once('error', reject);
      childProcess = gitApply;
    } else if (process.env.GITHUB_ACTIONS) {
      shellcheck.stdout.on('data', (chunk) => {
        json += chunk;
      });
    } else {
      shellcheck.stdout.pipe(process.stdout);
    }
    childProcess.once('exit', (code) => {
      if (!process.exitCode && code) process.exitCode = code;
      resolve(json);
    });
  });

  if (!FIX_MODE_ENABLED && process.env.GITHUB_ACTIONS) {
    const data = JSON.parse(stdout);
    for (const { file, line, column, message } of data) {
      console.error(
        `::error file=${file},line=${line},col=${column}::` +
          `${file}:${line}:${column}: ${message}`
      );
    }
  }
}

const USAGE_STR =
  `Usage: ${process.argv[1]} <path> [--fix] [--from-npx]\n` +
  '\n' +
  'Environment variables:\n' +
  ' - SHELLCHECK: absolute path to `shellcheck`. If not provided, the\n' +
  '   script will use the result of `command -v shellcheck`, or\n' +
  '   `$(command -v npx) shellcheck` if the flag `--from-npx` is provided\n' +
  '   (may require an internet connection).\n' +
  ' - GIT: absolute path to `git`. If not provided, the \n' +
  '   script will use the result of `command -v git`.\n';

if (
  process.argv.length < 3 ||
  process.argv.includes('-h') ||
  process.argv.includes('--help')
) {
  console.log(USAGE_STR);
} else {
  console.log('Running Shell scripts checker...');
  const entryPoint = resolve(process.argv[2]);
  const stats = statSync(entryPoint, { throwIfNoEntry: false });

  function onError(e) {
    console.log(USAGE_STR);
    console.error(e);
    process.exitCode = 1;
  }
  if (stats?.isDirectory()) {
    SPAWN_OPTIONS.cwd = entryPoint;
    checkFiles(...findScriptFilesRecursively(entryPoint)).catch(onError);
  } else if (stats?.isFile()) {
    SPAWN_OPTIONS.cwd = process.cwd();
    checkFiles(entryPoint).catch(onError);
  } else {
    onError(new Error('You must provide a valid directory or file path. ' +
                      `Received '${process.argv[2]}'.`));
  }
}