Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Update prettifyJSON script #833

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const jestConfig = require('./packages/jest-config-terra');

module.exports = {
...jestConfig,
testMatch: jestConfig.testMatch.concat(['**/tests/(*.)(spec|test).js?(x)']),
watchPathIgnorePatterns: [
'./tests/jest/reports/results/terra-functional-testing.json',
],
Expand Down
42 changes: 42 additions & 0 deletions scripts/prettifyJSON/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This script can be used to format json files.
// To execute the script, run: node prettifyJSON.js <path to folder>

/* eslint-disable consistent-return */
/* eslint-disable array-callback-return */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-console */

const fs = require('fs');
const path = require('path');
const prettifyJSON = require('./prettifyJSON');

if (process.argv.length === 2) {
console.error('Expected at least one argument!');
process.exit(1);
}
const targetDir = process.argv[2] || 'Default';

// get all JSON files

const allPackageJSON = [];

const getAllPackageJSONRecursively = (directory) => {
const filesInDirectory = fs.readdirSync(directory);
for (const file of filesInDirectory) {
const absolute = path.join(directory, file);
if (fs.statSync(absolute).isDirectory()) {
getAllPackageJSONRecursively(absolute);
} else if (absolute.includes('package.json') && !absolute.includes('node_modules')) allPackageJSON.push(absolute);
}
};

getAllPackageJSONRecursively(targetDir);

allPackageJSON.map((JSONfile) => {
const oldJSON = JSON.parse(fs.readFileSync(JSONfile));
const newJSON = prettifyJSON(oldJSON);
fs.writeFileSync(JSONfile, JSON.stringify(newJSON, null, 2));
fs.appendFileSync(JSONfile, '\n');
});

process.exit(0);
128 changes: 57 additions & 71 deletions scripts/prettifyJSON.js → scripts/prettifyJSON/prettifyJSON.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
// This script can be used to format json files.
// To execute the script, run: node prettifyJSON.js <path to folder>

/* eslint-disable consistent-return */
/* eslint-disable array-callback-return */
/* eslint-disable no-restricted-syntax */
/* eslint-disable consistent-return */
/* eslint-disable no-console */
/* eslint-disable no-param-reassign */
/* eslint-disable no-restricted-syntax */

const fs = require('fs');
const path = require('path');

if (process.argv.length === 2) {
console.error('Expected at least one argument!');
process.exit(1);
}
const targetDir = process.argv[2] || 'Default';

// get all JSON files

const allPackageJSON = [];

const getAllPackageJSONRecursively = (directory) => {
const filesInDirectory = fs.readdirSync(directory);
for (const file of filesInDirectory) {
const absolute = path.join(directory, file);
if (fs.statSync(absolute).isDirectory()) {
getAllPackageJSONRecursively(absolute);
} else if (absolute.includes('package.json') && !absolute.includes('node_modules')) allPackageJSON.push(absolute);
}
};

getAllPackageJSONRecursively(targetDir);

// Update JSON files

for (const JSONfile of allPackageJSON) {
const oldJSON = JSON.parse(fs.readFileSync(JSONfile));
const newJSON = {};
module.exports = (oldJSON) => {
// const oldJSON = JSON.parse(fs.readFileSync(JSONfile));
let newJSON = {};

if (oldJSON.name) {
newJSON.name = oldJSON.name;
Expand All @@ -52,21 +23,11 @@ for (const JSONfile of allPackageJSON) {
delete oldJSON.description;
}

if (oldJSON.engines) {
newJSON.engines = oldJSON.engines;
delete oldJSON.engines;
}

if (oldJSON.author) {
newJSON.author = oldJSON.author;
delete oldJSON.author;
}

if (oldJSON.main) {
newJSON.main = oldJSON.main;
delete oldJSON.main;
}

if (oldJSON.repository) {
newJSON.repository = oldJSON.repository;
delete oldJSON.repository;
Expand All @@ -82,6 +43,18 @@ for (const JSONfile of allPackageJSON) {
delete oldJSON.homepage;
}

if (oldJSON.license) {
newJSON.license = oldJSON.license;
delete oldJSON.license;
}

if (oldJSON.keywords) {
newJSON.keywords = oldJSON.keywords;
// alphabetize the list
newJSON.keywords.sort(Intl.Collator().compare);
delete oldJSON.keywords;
}

if (oldJSON.private) {
newJSON.private = oldJSON.private;
delete oldJSON.private;
Expand All @@ -92,21 +65,21 @@ for (const JSONfile of allPackageJSON) {
delete oldJSON.publishConfig;
}

if (oldJSON.license) {
newJSON.license = oldJSON.license;
delete oldJSON.license;
if (oldJSON.workspaces) {
newJSON.workspaces = oldJSON.workspaces;
// alphabetize the list
newJSON.workspaces.sort(Intl.Collator().compare);
delete oldJSON.workspaces;
}

if (oldJSON.keywords) {
newJSON.keywords = oldJSON.keywords;
// alphabetize the list
newJSON.keywords.sort(Intl.Collator().compare);
delete oldJSON.keywords;
if (oldJSON.engines) {
newJSON.engines = oldJSON.engines;
delete oldJSON.engines;
}

if (oldJSON.browserslist) {
newJSON.browserslist = oldJSON.browserslist;
delete oldJSON.browserslist;
if (oldJSON.main) {
newJSON.main = oldJSON.main;
delete oldJSON.main;
}

if (oldJSON.files) {
Expand All @@ -121,6 +94,11 @@ for (const JSONfile of allPackageJSON) {
delete oldJSON.bin;
}

if (oldJSON.browserslist) {
newJSON.browserslist = oldJSON.browserslist;
delete oldJSON.browserslist;
}

if (oldJSON.eslintConfig) {
newJSON.eslintConfig = oldJSON.eslintConfig;
delete oldJSON.eslintConfig;
Expand All @@ -136,33 +114,41 @@ for (const JSONfile of allPackageJSON) {
delete oldJSON.stylelint;
}

const tempJSON = {};

if (oldJSON.dependencies) {
newJSON.dependencies = oldJSON.dependencies;
tempJSON.dependencies = oldJSON.dependencies;
delete oldJSON.dependencies;
}

if (oldJSON.devDependencies) {
newJSON.devDependencies = oldJSON.devDependencies;
delete oldJSON.devDependencies;
}

if (oldJSON.peerDependencies) {
newJSON.peerDependencies = oldJSON.peerDependencies;
tempJSON.peerDependencies = oldJSON.peerDependencies;
delete oldJSON.peerDependencies;
}

if (oldJSON.devDependencies) {
tempJSON.devDependencies = oldJSON.devDependencies;
delete oldJSON.devDependencies;
}

if (oldJSON.scripts) {
newJSON.scripts = oldJSON.scripts;
tempJSON.scripts = oldJSON.scripts;
delete oldJSON.scripts;
}

// add any remaining tags to here so they can be manually added to the appropriate location
if (Object.keys(oldJSON).length > 0) {
newJSON.otherTags = oldJSON;
// Remaining tags are added at the end
if (Object.keys(oldJSON).length) {
const orderedKeys = Object.keys(oldJSON).sort().reduce((obj, key) => {
obj[key] = oldJSON[key];
return obj;
}, {});

newJSON = { ...newJSON, ...orderedKeys };
}

fs.writeFileSync(JSONfile, JSON.stringify(newJSON, null, 2));
fs.appendFileSync(JSONfile, '\n');
}
// Add dependencies & scripts at the end
newJSON = { ...newJSON, ...tempJSON };

return newJSON;
};

process.exit(0);
Loading
Loading