Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(build): add TS support #562

Merged
merged 2 commits into from
Feb 13, 2024
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 .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ jobs:
key: ${{ runner.os }}-yarn-14-${{ secrets.CACHE_VERSION }}-${{ hashFiles('yarn.lock') }}
- run: yarn install --frozen-lockfile
if: steps.yarn-cache.outputs.cache-hit != 'true'
- run: yarn build
- run: .github/release.sh
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
key: ${{ runner.os }}-yarn-14-${{ secrets.CACHE_VERSION }}-${{ hashFiles('yarn.lock') }}
- run: yarn install --frozen-lockfile
if: steps.yarn-cache.outputs.cache-hit != 'true'
- run: yarn build
- name: Run tests
run: yarn test --maxWorkers=2
error-tests:
Expand Down Expand Up @@ -63,5 +64,6 @@ jobs:
with:
path: test/packages/**
key: ${{ runner.os }}-yarn-14-${{ secrets.CACHE_VERSION }}-${{ hashFiles('getPackages.js', 'test/packages/*/package.json') }}
- run: yarn build
- name: Test packages
run: yarn test:packages
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ lerna-output.txt
.cache*
.idea
test/packages/*
.DS_Store
.DS_Store
dist
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"packages": [
"packages/eslint-plugin-pf-codemods",
"packages/pf-codemods",
"packages/class-name-updater"
"packages/class-name-updater",
"packages/codemods"
],
"version": "independent"
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
"test:console": "pf-codemods --no-cache test/console/frontend",
"test:integreatly": "pf-codemods --no-cache test/tutorial-web-app",
"test:packages": "yarn get:packages && node --unhandled-rejections=strict packages/pf-codemods/index.js --no-cache test/packages",
"test:classnames": "class-name-updater --no-cache test",
"get:packages": "node getPackages.js",
"generate": "plop"
"generate": "plop",
"build": "lerna run build",
"clean": "lerna run clean"
},
"repository": {
"type": "git",
Expand Down
47 changes: 0 additions & 47 deletions packages/class-name-updater/classNameUpdate.js

This file was deleted.

11 changes: 10 additions & 1 deletion packages/class-name-updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
"author": "Red Hat",
"license": "MIT",
"bin": {
"class-name-updater": "./index.js"
"class-name-updater": "./dist/cli.js"
},
"engines": {
"node": ">=8"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc --build --verbose ./tsconfig.json",
"clean": "rimraf ./dist",
"test": "jest"
},
"dependencies": {
"colors": "^1.4.0",
"commander": "^5.1.0",
"diff": "^5.1.0",
"glob": "^10.2.6"
},
"devDependencies": {
"typescript": "^5.3.3",
"@types/diff": "^5.0.9"
}
}
43 changes: 43 additions & 0 deletions packages/class-name-updater/src/classNameUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { sync } from "glob";
import { readFileSync, writeFileSync} from "fs";
import { join } from "path";
import { isDir } from "./utils";
import { printDiff } from "./printDiff";

export async function classNameUpdate(globTarget: string, makeChange: boolean, fileTypesRegex: RegExp, excludeFiles: string[] = []) {
const acceptedFileTypesRegex = fileTypesRegex || /\.(s?css|less|(t|j)sx?|md)$/;

const changeNeededRegex = /(\b|\$)pf-([cul]|global|theme|color)-/g;
const version = "v5";

const files = sync(globTarget, { ignore: "**/node_modules/**" });
const includedFiles = files.filter((filePath: string) => !excludeFiles.includes(filePath))

includedFiles.forEach(async (file: string) => {
const filePath = join(process.cwd(), file);
const isDirectory = await isDir(filePath);
const isUnexpectedFile = !acceptedFileTypesRegex.test(filePath);

if (isDirectory || isUnexpectedFile) {
return;
}

const fileContent = readFileSync(filePath, "utf8");
const needsChange = changeNeededRegex.test(fileContent);

if (!needsChange) {
return;
}

const newContent = fileContent.replace(
changeNeededRegex,
`$1pf-${version}-$2-`
);

printDiff(file, fileContent, newContent, changeNeededRegex);

if (makeChange) {
writeFileSync(filePath, newContent);
}
});
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#!/usr/bin/env node
const { join } = require("path");
const { Command } = require("commander");
import { join } from "path";
import { Command } from "commander";
const program = new Command();

const { isDir } = require("./utils");
const { classNameUpdate } = require("./classNameUpdate");
import { isDir } from "./utils";
import { classNameUpdate } from "./classNameUpdate";

program
.version(require("./package.json").version)
.version(
require(join(
process.cwd(),
"packages",
"class-name-updater",
"package.json"
)).version
)
.description("Update class name versioning")
.arguments("<path> [otherPaths...]")
.option(
Expand All @@ -21,7 +28,11 @@ program
.option("--fix", "Whether to run fixer")
.action(runClassNameUpdate);

async function runClassNameUpdate(path, otherPaths, options) {
async function runClassNameUpdate(
path: string,
otherPaths: string,
options: { extensions: string; fix: boolean; exclude: string[] | undefined }
) {
let allPaths = [path, ...otherPaths];

// if all paths are resolved to be directories assume that they want to run on all contents of those directories
Expand All @@ -33,7 +44,7 @@ async function runClassNameUpdate(path, otherPaths, options) {
allPaths = allPaths.map((path) => join(path, "**", "*"));
}

let fileTypes;
let fileTypes: RegExp;
if (options.extensions) {
fileTypes = new RegExp(`\.(${options.extensions.split(",").join("|")})$`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require("colors");
const Diff = require("diff");
import { Change, diffChars} from "diff";

function formatLineNumber(lastLineNumber, newLineNumber) {
function formatLineNumber(lastLineNumber: number, newLineNumber: number) {
if (newLineNumber === lastLineNumber) {
return "";
}
Expand All @@ -10,32 +10,33 @@ function formatLineNumber(lastLineNumber, newLineNumber) {
return `${newLine} ${newLineNumber + 1}: `;
}

function formatDiff(diff, part, i) {
function formatDiff(diff: Change[], part: Change, i: number) {
const proceedingPartSplit = diff[i - 1].value.split("\n");
const proceedingPart =
proceedingPartSplit[proceedingPartSplit.length - 1].trim();

const followingPartSplit = diff[i + 1].value.split("\n");
const followingPart = followingPartSplit[0].trim();

return proceedingPart["grey"] + part.value["green"] + followingPart["grey"];
// The color package is weird, which is why we have to cast to any
return proceedingPart["grey" as any] + part.value["green" as any] + followingPart["grey" as any];
}

function printDiff(fileName, oldContent, newContent, changeNeededRegex) {
export function printDiff(fileName: string, oldContent: string, newContent: string, changeNeededRegex: RegExp) {
if (oldContent.length > 400_000) {
process.stdout.write(`\n ${fileName}`);
process.stdout.write(
"\n this file may take a long time to diff or hang perpetually because of its size, you will likely want to exclude it"[
"red"
"red" as any
]
);
}

const fileSplitByLine = oldContent.split("\n");
const loggedFiles = [];
let lastPartLineNumber;
const loggedFiles: string[] = [];
let lastPartLineNumber: number;

const diff = Diff.diffChars(oldContent, newContent);
const diff = diffChars(oldContent, newContent);

diff.forEach((part, i) => {
if (!part.added) {
Expand Down Expand Up @@ -74,7 +75,3 @@ function printDiff(fileName, oldContent, newContent, changeNeededRegex) {
process.stdout.write(fullContextLine);
});
}

module.exports = {
printDiff,
};
8 changes: 8 additions & 0 deletions packages/class-name-updater/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { readdir } from "fs/promises";

export async function isDir(potentialDirPath: string) {
return readdir(potentialDirPath).then(
() => true,
() => false
);
}
9 changes: 9 additions & 0 deletions packages/class-name-updater/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "./dist"
},
"include": ["src/**/*"],
"exclude": ["dist"]
}
12 changes: 0 additions & 12 deletions packages/class-name-updater/utils.js

This file was deleted.

Loading
Loading