Skip to content

Instantly share code, notes, and snippets.

@jake-nz
Created July 12, 2021 06:48
Show Gist options
  • Save jake-nz/367983804ef5135cca5b50974998d051 to your computer and use it in GitHub Desktop.
Save jake-nz/367983804ef5135cca5b50974998d051 to your computer and use it in GitHub Desktop.
// Usage: node moveConfigs.js ../path/to/project
const editJsonFile = require("edit-json-file");
const fs = require("fs");
const path = require("path");
const folder = process.argv[2];
const getPath = (fileName) => path.join(__dirname, folder, fileName);
const packageFile = getPath("package.json");
console.log(packageFile);
const package = editJsonFile(packageFile);
const processKey = (key, processor) => {
const data = package.get(key);
if (data) {
const remove = processor(data);
if (remove !== false) {
package.unset(key);
}
}
};
const json = (data) => JSON.stringify(data, null, 2);
const writeFile = (fileName, data) => {
const fullPath = getPath(fileName);
fs.writeFile(fullPath, data, (err) => {
if (err) return console.error(err);
console.log("writing", fileName);
});
};
const writeJson = (fileName, data) => writeFile(fileName, json(data) + "\n");
const writeJs = (fileName, data) => {
const string = `module.exports = ${json(data)}\n`;
writeFile(fileName, string);
};
const moveToJson = (key, fileName) =>
processKey(key, (data) => {
writeJson(fileName, data);
});
const moveToJs = (key, fileName) =>
processKey(key, (data) => {
writeJs(fileName, data);
});
moveToFile = (key, fileName) =>
processKey(key, (data) => {
const string = data.join("\n") + "\n";
writeFile(fileName, string);
});
moveToJson("prettier", ".prettierrc");
moveToJs("jest", "jest.config.js");
moveToJson("husky", ".huskyrc");
moveToJson("lint-staged", ".lintstagedrc");
moveToFile("eslintIgnore", ".eslintignore");
moveToJson("commitlint", ".commitlintrc.json");
moveToJson("eslintConfig", ".eslintrc.json");
package.save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment