Skip to content

Commit

Permalink
fix: Properly import chalk.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-mitchell committed Aug 26, 2023
1 parent 4f62d01 commit 6ab766c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
10 changes: 10 additions & 0 deletions __tests__/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const spawn = require('cross-spawn');
const path = require('path');

test('runs without errors', () => {
const result = spawn.sync('node', [path.join(__dirname, '..', 'bin', 'index.js'), '-d', '.'], {
stdio: 'inherit',
});

expect(result.status).toBe(0);
});
113 changes: 66 additions & 47 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,86 @@ require('please-upgrade-node')(pkg);
const fs = require('fs');
const { createRequire } = require('module');
const path = require('path');
const chalk = require('chalk');
const spawn = require('cross-spawn');

const workingDirectoryRequire = createRequire(path.resolve(process.cwd(), 'index.js'));

const chalkImport = import('chalk');

async function logWarning(...args) {
const { default: chalk } = await chalkImport;
console.warn(chalk.yellow(...args));
}

async function logError(...args) {
const { default: chalk } = await chalkImport;
console.error(chalk.red(...args));
}

try {
workingDirectoryRequire('eslint');
} catch (x) {
console.error(chalk.red('eslint was not found.'));
console.error(
chalk.red('suppress-eslint-errors requires eslint to be installed in the working directory.')
);
process.exit(1);
Promise.all([
logError('eslint was not found.'),
logError('suppress-eslint-errors requires eslint to be installed in the working directory.'),
]).finally(() => process.exit(1));
}

const jscodeshiftPath = require.resolve('jscodeshift/bin/jscodeshift');
const transformPath = require.resolve('../transforms/suppress-eslint-errors');

const gitignoreArguments = [];
const gitignorePath = path.resolve(process.cwd(), '.gitignore');
if (fs.existsSync(gitignorePath)) {
if (
fs
.readFileSync(gitignorePath, { encoding: 'utf8' })
.split('\n')
.findIndex((line) => line.startsWith('!')) !== -1
) {
console.warn(
chalk.yellow(
'your .gitignore contains exclusions, which jscodeshift does not properly support.'
)
);
console.warn(chalk.yellow('skipping the ignore-config option.'));
} else {
gitignoreArguments.push(`--ignore-config=.gitignore`);
}
}
async function findGitignoreArguments() {
const gitignorePath = path.resolve(process.cwd(), '.gitignore');

const result = spawn.sync(
'node',
[jscodeshiftPath, '--no-babel', '-t', transformPath]
.concat(gitignoreArguments)
.concat(process.argv.slice(2)),
{
stdio: 'inherit',
if (!fs.existsSync(gitignorePath)) {
return [];
}
);

if (result.signal) {
if (result.signal === 'SIGKILL') {
console.error(
'The script failed because the process exited too early. ' +
'This probably means the system ran out of memory or someone called ' +
'`kill -9` on the process.'
);
} else if (result.signal === 'SIGTERM') {
console.error(
'The script failed because the process exited too early. ' +
'Someone might have called `kill` or `killall`, or the system could ' +
'be shutting down.'

const allLines = fs.readFileSync(gitignorePath, { encoding: 'utf8' }).split('\n');
if (allLines.findIndex((line) => line.startsWith('!')) !== -1) {
await logWarning(
'your .gitignore contains exclusions, which jscodeshift does not properly support.'
);
await logWarning('skipping the ignore-config option.');

return [];
}
process.exit(1);

return [`--ignore-config=.gitignore`];
}

(async function runJsCodeShift() {
const result = spawn.sync(
'node',
[
jscodeshiftPath,
'--no-babel',
'-t',
transformPath,
...(await findGitignoreArguments()),
...process.argv.slice(2),
],
{
stdio: 'inherit',
}
);

if (result.signal) {
if (result.signal === 'SIGKILL') {
console.error(
'The script failed because the process exited too early. ' +
'This probably means the system ran out of memory or someone called ' +
'`kill -9` on the process.'
);
} else if (result.signal === 'SIGTERM') {
console.error(
'The script failed because the process exited too early. ' +
'Someone might have called `kill` or `killall`, or the system could ' +
'be shutting down.'
);
}
process.exit(1);
}

process.exit(result.status);
})();

0 comments on commit 6ab766c

Please sign in to comment.