Skip to content

Commit

Permalink
feat: add script to check and update issue on TS migration status (#1986
Browse files Browse the repository at this point in the history
)

* feat: add script to check and update issue on TS migration status

* fix: percentage

* refactor(scripts): render table with package name and location
  • Loading branch information
emmenko authored Aug 13, 2021
1 parent e46a45c commit 214f43d
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 15 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/ts-migration-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: TypeScript migration check

# The event triggers are configured as following:
# - on branch main, trigger the workflow on every push
on:
push:
branches:
- main

jobs:
build_lint_and_test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Read .nvmrc
run: echo ::set-output name=NVMRC::$(cat .nvmrc)
id: nvm

- name: Setup Node (uses version in .nvmrc)
uses: actions/setup-node@v2
with:
node-version: "${{ steps.nvm.outputs.NVMRC }}"

- name: Get yarn cache
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock', 'patches/*.patch') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Checking TypeScript migration
run: ./scripts/check-ts-migration.mjs
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@formatjs/cli": "4.2.30",
"@formatjs/intl-relativetimeformat": "9.2.1",
"@manypkg/cli": "0.18.0",
"@manypkg/get-packages": "1.1.1",
"@percy/cli": "1.0.0-beta.63",
"@percy/puppeteer": "2.0.0",
"@preconstruct/cli": "2.1.0",
Expand All @@ -111,6 +112,7 @@
"cross-env": "7.0.3",
"eslint": "7.32.0",
"eslint-formatter-pretty": "4.1.0",
"execa": "5.1.1",
"formik": "^2.2.9",
"glob": "7.1.7",
"global": "4.4.0",
Expand Down
141 changes: 141 additions & 0 deletions scripts/check-ts-migration.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import mri from 'mri';
import execa from 'execa';
import { getPackagesSync } from '@manypkg/get-packages';

const flags = mri(process.argv.slice(2), { alias: { help: ['h'] } });

if (flags.help) {
console.log(`
Usage: check-ts-migration [options]
Displays help information.
Options:
--dry-run (optional) Simulate a run, the generated content will be printed to stdout.
`);
process.exit(0);
}

const issueTitle = 'TypeScript migration overview';

const dryRun = flags['dry-run'] ? true : false;

const execute = async () => {
if (!dryRun) {
const result = await execa('gh', ['--version']);
if (result.failed) {
throw new Error(
'Missing required binary "gh". Make sure it is installed.'
);
}
}

const workspacePackages = getPackagesSync(process.cwd());
const publicPackages = workspacePackages.packages.filter(
(packageInfo) => !packageInfo.packageJson.private
);

const stats = publicPackages.reduce(
(stats, packageInfo) => {
const entryPointPath = path.join(packageInfo.dir, 'src/index.ts');
const isTS = fs.existsSync(entryPointPath);

return {
migrated: isTS ? stats.migrated + 1 : stats.migrated,
pending: isTS ? stats.pending : stats.pending + 1,
pendingPackages: [
...stats.pendingPackages,
isTS
? null
: {
name: packageInfo.packageJson.name,
dir: path.relative(process.cwd(), packageInfo.dir),
},
].filter(Boolean),
};
},
{
migrated: 0,
pending: 0,
pendingPackages: [],
}
);

const percentageCompleted = Math.floor(
(stats.migrated * 100) / publicPackages.length
);

const statsMd = `<!-- This is autogenerated issue. See ./scripts/check-ts-migration.mjs -->
## Progress
![${percentageCompleted}%](https://progress-bar.dev/${percentageCompleted})
**${stats.migrated} out of ${
publicPackages.length
} packages** have been migrated to TypeScript.
## Pending migration
The following **${
stats.pending
} packages** have not been migrated to TypeScript yet:
| Package | Location |
| --- | --- |
${stats.pendingPackages
.map(
(packageInfo) =>
`| \`${packageInfo.name}\` | [${packageInfo.dir}](https://github.com/commercetools/ui-kit/tree/main/${packageInfo.dir}) |`
)
.join('\n')}
`;

if (dryRun) {
console.log(statsMd);
process.exit(0);
}

// Push stats to repository
const resultGhIssueRaw = await execa('gh', [
'issue',
'list',
'--search',
`"${issueTitle}"`,
'--json',
'number',
]);
const resultGhIssue = JSON.parse(resultGhIssueRaw.stdout);

if (resultGhIssue.length === 0) {
// Create a new issue
await execa('gh', [
'issue',
'create',
'--title',
issueTitle,
'--body',
statsMd,
]);
} else {
const [issue] = resultGhIssue;
// Update existing issue
await execa('gh', [
'issue',
'edit',
issue.number,
'--title',
`${issueTitle} (\`${percentageCompleted}%\` completed)`,
'--body',
statsMd,
]);
}
};

execute().catch((error) => {
console.error(error.message || error);
process.exit(1);
});
30 changes: 15 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6107,6 +6107,21 @@ events@^3.2.0:
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==

[email protected], execa@^5.0.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
integrity sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==
dependencies:
cross-spawn "^7.0.3"
get-stream "^6.0.0"
human-signals "^2.1.0"
is-stream "^2.0.0"
merge-stream "^2.0.0"
npm-run-path "^4.0.1"
onetime "^5.1.2"
signal-exit "^3.0.3"
strip-final-newline "^2.0.0"

execa@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777"
Expand All @@ -6133,21 +6148,6 @@ execa@^1.0.0:
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^5.0.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
integrity sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==
dependencies:
cross-spawn "^7.0.3"
get-stream "^6.0.0"
human-signals "^2.1.0"
is-stream "^2.0.0"
merge-stream "^2.0.0"
npm-run-path "^4.0.1"
onetime "^5.1.2"
signal-exit "^3.0.3"
strip-final-newline "^2.0.0"

execall@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/execall/-/execall-2.0.0.tgz#16a06b5fe5099df7d00be5d9c06eecded1663b45"
Expand Down

1 comment on commit 214f43d

@vercel
Copy link

@vercel vercel bot commented on 214f43d Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.