Skip to content

Commit

Permalink
refactor: xo aspects
Browse files Browse the repository at this point in the history
  • Loading branch information
mfranzke committed Jan 28, 2025
1 parent 34dfc8c commit 14ea1a4
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 26 deletions.
12 changes: 12 additions & 0 deletions .xo-config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ module.exports = {
// Playwright tests are async we shall use loops there
'no-await-in-loop': 0
}
},
{
files: ['./scripts/**'],
rules: {
// We could set the correct path to package.json for the following folder, as we're only using the scripts package as an abtraction, but list those dependencies in roots package.json file
'import/no-extraneous-dependencies': [
'error',
{ packageDir: './' }
],
// Node.js environment
'no-console': 'off'
}
}
],
rules: {
Expand Down
1 change: 1 addition & 0 deletions scripts/dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import childProcess from 'node:child_process';
// eslint-disable-next-line import/no-extraneous-dependencies
import { checkbox } from '@inquirer/prompts';

const answersFrameworkPairs = [
Expand Down
13 changes: 12 additions & 1 deletion scripts/github/build-gh-page.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import { extract } from 'tar';

// eslint-disable-next-line @typescript-eslint/naming-convention
const buildGHPage = async () => {
/* eslint-disable @typescript-eslint/naming-convention */
const NAME: string | undefined = process.env.NAME;
const OWNER_NAME: string | undefined = process.env.OWNER_NAME;
const REPO_NAME: string | undefined = process.env.REPO_NAME;
const OUT_DIR: string = process.env.OUT_DIR || 'out';
const OUT_DIR: string = process.env.OUT_DIR ?? 'out';
const RELEASE: boolean = process.env.RELEASE === 'true';
const PRE_RELEASE: boolean = process.env.PRE_RELEASE === 'true';
/* eslint-enable @typescript-eslint/naming-convention */

if (!NAME) {
console.error('Error: Missing NAME variable');
Expand All @@ -35,6 +39,7 @@ const buildGHPage = async () => {
console.log('📦 Unpack Tar');
await extract({
file: 'gh-pages.tar.gz',
// eslint-disable-next-line @typescript-eslint/naming-convention
C: 'public',
strip: 1
});
Expand All @@ -52,21 +57,25 @@ const buildGHPage = async () => {
console.log('Make dir ./public/version');
fs.mkdirSync(versionDir);
}

const nameDir = path.join(versionDir, NAME);
if (fs.existsSync(nameDir)) {
console.log(`Remove dir ./public/version/${NAME}`);
fs.rmdirSync(nameDir, { recursive: true });
}

if (RELEASE) {
const latestDir = path.join(versionDir, 'latest');
if (fs.existsSync(latestDir)) {
console.log('Remove dir ./public/version/latest');
fs.rmdirSync(latestDir, { recursive: true });
}

fs.mkdirSync(latestDir);
fs.cpSync(OUT_DIR, latestDir, { recursive: true });
console.log('Copied dir out to ./public/version/latest');
}

fs.cpSync(OUT_DIR, nameDir, { recursive: true });
console.log(`Moved dir out to ./public/version/${NAME}`);
} else {
Expand All @@ -75,11 +84,13 @@ const buildGHPage = async () => {
console.log('Make dir ./public/review');
fs.mkdirSync(reviewDir);
}

const nameDir = path.join(reviewDir, NAME);
if (fs.existsSync(nameDir)) {
console.log(`Remove dir ./public/review/${NAME}`);
fs.rmSync(nameDir, { recursive: true });
}

fs.cpSync(OUT_DIR, nameDir, { recursive: true });
console.log(`Moved dir out to ./public/review/${NAME}`);
}
Expand Down
13 changes: 6 additions & 7 deletions scripts/github/cleanup-gh-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const removeOldFromPath = (isTag, data) => {
data?.filter((branch) => branch.name).length > 0
) {
const dirsToDelete = FS.readdirSync(path)
// eslint-disable-next-line unicorn/prefer-array-some
.filter((file) => !data.find((branch) => branch.name === file))
// Let's not clean up specific folders
.filter((file) => !['main', 'latest'].includes(file));
Expand All @@ -21,20 +22,18 @@ const removeOldFromPath = (isTag, data) => {
`Start removing ${isTag ? 'tags' : 'branches'} from gh-pages`
);
console.log(TAG, dirsToDelete);
dirsToDelete.forEach((dir) => {
for (const dir of dirsToDelete) {
FS.rmSync(`${path}/${dir}`, {
recursive: true,
force: true
});
console.log(TAG, `deleted ${isTag ? 'tag' : 'branch'} ${dir}`);
});
}

return true;
} else {
console.log(
TAG,
`All ${isTag ? 'tags' : 'branches'} are up to date`
);
}

console.log(TAG, `All ${isTag ? 'tags' : 'branches'} are up to date`);
}

return false;
Expand Down
3 changes: 2 additions & 1 deletion scripts/github/get-playwright-version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import packageJson from './../../package.json' assert { type: 'json' };
#!/usr/bin/env node
import packageJson from '../../package.json' assert { type: 'json' };

export const getPlaywrightVersion = () => {
const version = packageJson.devDependencies['@playwright/test'];
Expand Down
17 changes: 9 additions & 8 deletions scripts/github/get-release.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/usr/bin/env node
const getRelease = () => {
/* eslint-disable @typescript-eslint/naming-convention */
const GITHUB_REF = process.env.GITHUB_REF;
const GITHUB_ACTOR = process.env.GITHUB_ACTOR;
const GITHUB_COMMITISH = process.env.GITHUB_COMMITISH;
const GITHUB_PRE_RELEASE = process.env.GITHUB_PRE_RELEASE === 'true';
/* eslint-enable @typescript-eslint/naming-convention */

if (GITHUB_REF && GITHUB_REF.startsWith('refs/tags/v')) {
if (GITHUB_ACTOR !== 'dependabot[bot]') {
if (GITHUB_COMMITISH === 'main' && !GITHUB_PRE_RELEASE) {
console.log('RELEASE');
} else {
console.log('PRE_RELEASE');
}
} else {
if (GITHUB_REF?.startsWith('refs/tags/v')) {
if (GITHUB_ACTOR === 'dependabot[bot]') {
console.error('Dependabot has no permission to publish!');
process.exit(1);
} else if (GITHUB_COMMITISH === 'main' && !GITHUB_PRE_RELEASE) {
console.log('RELEASE');
} else {
console.log('PRE_RELEASE');
}
} else {
console.error("Your tag has to start with 'v'");
Expand Down
11 changes: 9 additions & 2 deletions scripts/github/package-version.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env node
import findVersions from 'find-versions';

export const packageVersion = () => {
/* eslint-disable @typescript-eslint/naming-convention */
const TAG: string | undefined = process.env.TAG;
const RELEASE: boolean = process.env.RELEASE === 'true';
const PRE_RELEASE: boolean = process.env.PRE_RELEASE === 'true';
const GITHUB_SHA: string | undefined = process.env.GITHUB_SHA;
/* eslint-enable @typescript-eslint/naming-convention */

if (!TAG) {
console.error('TAG is not defined');
Expand All @@ -16,6 +19,7 @@ export const packageVersion = () => {
process.exit(1);
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const SEMVER_VERSION: string = findVersions(TAG).toString();

if (RELEASE) {
Expand All @@ -25,11 +29,14 @@ export const packageVersion = () => {
);
process.exit(1);
}

console.log(SEMVER_VERSION);
} else if (PRE_RELEASE) {
if (SEMVER_VERSION.includes('-')) {
const GITHUB_SHA_SHORT: string = GITHUB_SHA.substring(0, 7);
const VALID_SEMVER_VERSION: string = `${SEMVER_VERSION}-${GITHUB_SHA_SHORT}`;
// eslint-disable-next-line @typescript-eslint/naming-convention
const GITHUB_SHA_SHORT: string = GITHUB_SHA.slice(0, 7);
// eslint-disable-next-line @typescript-eslint/naming-convention
const VALID_SEMVER_VERSION = `${SEMVER_VERSION}-${GITHUB_SHA_SHORT}`;
console.log(VALID_SEMVER_VERSION);
} else {
console.error(
Expand Down
7 changes: 4 additions & 3 deletions scripts/github/publish-npm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execSync } from 'child_process';
#!/usr/bin/env node
import { execSync } from 'node:child_process';

const VALID_SEMVER_VERSION = process.env.VALID_SEMVER_VERSION;
const RELEASE = process.env.RELEASE === 'true';
Expand Down Expand Up @@ -82,10 +83,10 @@ for (const REGISTRY of registries) {
process.exit(1);
}

packages.forEach((PACKAGE) => {
for (const PACKAGE of packages) {
console.log(`⤴ Publish ${PACKAGE} with tag ${TAG} to ${REGISTRY}`);
execSync(
`npm publish --tag ${TAG} db-ui-${PACKAGE}-${VALID_SEMVER_VERSION}.tgz --provenance`
);
});
}
}
1 change: 0 additions & 1 deletion scripts/prettier-stage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-console */
import ChildProcess from 'node:child_process';

const runPrettierStage = () => {
Expand Down
6 changes: 4 additions & 2 deletions scripts/tests/github/get-release.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ describe('build-gh-page', () => {
let result: string;
try {
result = execSync(command).toString();
} catch (e) {
result = e.message;
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
result = error.message;
}

expect(result.toString().trim()).toEqual(
'Command failed: npx --no tsx github/get-release.ts\n' +
"Your tag has to start with 'v'"
Expand Down
2 changes: 1 addition & 1 deletion scripts/tests/github/package-version.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { execSync } from 'node:child_process';
import { describe, expect, test } from 'vitest';
import { execSync } from 'child_process';

const command = 'npx tsx github/package-version.ts';

Expand Down

0 comments on commit 14ea1a4

Please sign in to comment.