Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Last active Jun 17, 2020
Embed
What would you like to do?

Migrate config to lint-staged >=10:

npx https://gist.github.com/remarkablemark/7732ca7f192550c8851419eec2aad7ea

Before running the script, make sure your working directory is clean:

git add .
git stash

Once the script finishes, pop your stash:

git stash pop

If you have trouble running lint-staged, try reinstalling node_modules:

rm -rf node_modules && npm i
#!/usr/bin/env node
const { resolve } = require('path');
const { cwd, exec, log, read, write } = require('./utilities');
const { name, version } = require('./package.json');
process.on('exit', code => {
log(`Exiting with code: ${code}`);
});
/**
* Display package info.
*/
log(`${name} v${version}`);
/**
* Migrates lint-staged config file.
*
* @param {String} file
*/
function migrateFile(file) {
switch (file) {
case '.lintstagedrc':
try {
const config = JSON.parse(read(file));
if (config) {
write(file, migrateConfig(config));
exec(`git add ${file}`);
}
} catch (err) {
// console.log(err);
}
break;
case '.lintstagedrc.json':
try {
const config = require(resolve(cwd, file));
if (config) {
write(file, migrateConfig(config));
exec(`git add ${file}`);
}
} catch (err) {
// console.log(err);
}
break;
case 'package.json':
try {
const packageJson = require(resolve(cwd, file));
const config = packageJson['lint-staged'];
if (config) {
packageJson['lint-staged'] = migrateConfig(config);
write(file, packageJson);
}
} catch (err) {
// console.log(err);
}
break;
default:
throw new Error(`lint-staged file \`${file}\` not supported by script`);
}
}
/**
* Migrates lint-staged config object.
*
* @param {Object} config
* @return {Object}
*/
function migrateConfig(config) {
Object.keys(config).forEach(key => {
const commands = config[key];
if (
Array.isArray(commands) &&
commands[commands.length - 1] === 'git add'
) {
commands.pop();
if (commands.length === 1) {
config[key] = commands[0];
} else {
config[key] = commands;
}
}
});
return config;
}
/**
* Migrate lint-staged config (if applicable).
*/
log('Migrating lint-staged config...');
['.lintstagedrc', '.lintstagedrc.json', 'package.json'].forEach(migrateFile);
/**
* Install `lint-staged@latest`.
*/
log('Installing `lint-staged@latest`...');
exec('npm install --save-dev lint-staged@latest');
exec('git add package.json');
/**
* Commit changes.
*/
log('Committing changes...');
exec(
'git commit -m "chore: migrate lint-staged config" -m "https://gist.github.com/remarkablemark/7732ca7f192550c8851419eec2aad7ea"'
);
log(`Finished ${name}`);
{
"name": "lint-staged-migration",
"version": "1.0.0",
"bin": "index.js"
}
const { execSync } = require('child_process');
const { resolve } = require('path');
const { readFileSync, writeFileSync } = require('fs');
const cwd = process.cwd();
/**
* Runs command.
*
* @param {String} command
* @return {String}
*/
const exec = command => execSync(command, { cwd, stdio: 'inherit' });
/**
* Logs to console.
*
* @param {...*} args
*/
const log = (...args) => console.log('INFO:', ...args);
/**
* Reads file.
*
* @param {String} file
* @return {*}
*/
const read = file => readFileSync(resolve(cwd, file)).toString();
/**
* Writes to file.
*
* @param {String} file
* @param {*} data
*/
const write = (file, data) =>
writeFileSync(
resolve(cwd, file),
(typeof data === 'string' ? data : JSON.stringify(data, null, 2)) + '\n'
);
module.exports = {
cwd: cwd,
exec: exec,
log: log,
read: read,
write: write
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment