Skip to content

Commit

Permalink
enhance github-actions action
Browse files Browse the repository at this point in the history
enhance readme action
  • Loading branch information
TimurRin committed Sep 18, 2024
1 parent 3ced9fd commit 7e10412
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 80 deletions.
5 changes: 3 additions & 2 deletions anca.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"dataVersion": 0,
"version": {
"latest": "0.1.0-dev.2",
"latestNext": "0.1.0-dev.2+next.20240916_072645",
"timestamp": 1726471605
"latestNext": "0.1.0-dev.2+next.20240918_093908",
"timestamp": 1726652348
},
"files": [
{
Expand Down Expand Up @@ -50,6 +50,7 @@
"readme": {}
},
"namings": {
"bin": "anca",
"npmPackage": "anca",
"text": "Anca",
"textLong": "Anca Project Manager"
Expand Down
143 changes: 92 additions & 51 deletions src/actions/github-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ import {
NODEJS_22_VERSION,
} from "./utils/variables.js";

const RELEASE_NODEJS = `name: Release
const NAME_RELEASE = `name: Release`;
const NAME_TEST = `name: Test`;

on:
const ON_RELEASE = `on:
release:
types: [created]
types: [created]`;

jobs:
publish-npm:
const ON_PUSH = `on:
push:
branches: ["**"]`;

const JOBS = `jobs:`;

const JOBS_PUBLISH_NPM = ` publish-npm:
runs-on: ubuntu-latest
env:
ANCA_CI: true
Expand All @@ -35,38 +41,38 @@ jobs:
- run: npm publish
env:
NODE_AUTH_TOKEN: \${{secrets.npm_token}}
name: "Build distribution bundle and publish to registry"
`;
name: "Build distribution bundle and publish to registry"`;

const RELEASE_NODEJS_APP = `name: Release
on:
release:
types: [created]
jobs:
publish-npm:
const JOBS_BUILD_BUNDLE = ` build-bundle:
permissions: write-all
runs-on: ubuntu-latest
env:
ANCA_CI: true
name: "Publish package to npm registry"
name: Build bundle
steps:
- uses: actions/checkout@v4
name: "Checkout repo"
name: Checkout repo
- uses: actions/setup-node@v4
with:
node-version: ${NODEJS_22_VERSION}
registry-url: https://registry.npmjs.org/
name: "Install Node.js"
name: Install Node.js
- run: npm ci
name: "Install dependencies"
- run: npm test
name: "Run tests"
- run: npm publish
name: Install dependencies
- run: npm run build:bundle
name: Build bundle
- run: |
mv build/bundle/index.js build/bundle/\${{ github.event.repository.name }}-\${{ github.ref_name }}.js
shell: bash
name: Rename bundle
- uses: softprops/action-gh-release@v1
with:
files: |
build/bundle/\${{ github.event.repository.name }}-\${{ github.ref_name }}.js
env:
NODE_AUTH_TOKEN: \${{secrets.npm_token}}
name: "Build distribution bundle and publish to registry"
build-executables:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
name: Attach bundle to release`;

const JOBS_BUILD_EXECUTABLES = ` build-executables:
permissions: write-all
runs-on: \${{ matrix.os }}
strategy:
Expand Down Expand Up @@ -100,17 +106,9 @@ jobs:
build/sea/\${{ github.event.repository.name }}-\${{ github.ref_name }}-\${{ runner.arch }}\${{ matrix.os == 'windows-latest' && '.exe' || '' }}
env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
name: Attach executables to release
`;

const TEST_NODEJS = `name: Test
on:
push:
branches: ["**"]
name: Attach executables to release`;

jobs:
test-commit:
const JOBS_TEST_COMMIT = ` test-commit:
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -127,7 +125,21 @@ jobs:
- run: npm ci
name: "Install dependencies"
- run: npm test
name: "Run tests"
name: "Run tests"`;

const RELEASE_NODEJS_BEGIN = `${NAME_RELEASE}
${ON_RELEASE}
${JOBS}
`;

const TEST_NODEJS = `${NAME_TEST}
${ON_PUSH}
${JOBS}
${JOBS_TEST_COMMIT}
`;

const RELEASE_FILE_PATH = ".github/workflows/release.yml";
Expand All @@ -143,6 +155,29 @@ async function createGithubActionsFolders(fullPath: string) {
});
}

/**
*
* @param development
*/
function getReleaseContents(development: AncaDevelopment) {
let contents = RELEASE_NODEJS_BEGIN;

if (development.state?.config.public) {
contents += JOBS_PUBLISH_NPM;
contents += "\n";
}

contents += JOBS_BUILD_BUNDLE;
contents += "\n";

if (development.state?.config.type === "app") {
contents += JOBS_BUILD_EXECUTABLES;
contents += "\n";
}

return contents;
}

/**
*
* @param development
Expand All @@ -155,12 +190,7 @@ export async function checkGithubActionsRelease(development: AncaDevelopment) {
if (contents == null) {
return false;
}
return (
contents ===
(development.state.config.type === "app"
? RELEASE_NODEJS_APP
: RELEASE_NODEJS)
);
return contents === getReleaseContents(development);
}

/**
Expand All @@ -178,6 +208,17 @@ export async function checkGithubActionsTest(development: AncaDevelopment) {
return contents === TEST_NODEJS;
}

/**
*
* @param _development
* @param file
*/
function filterGithubActionsFile(_development: AncaDevelopment, file: string) {
if (file === "test.yml") return false;
if (file === "release.yml") return false;
return true;
}

/**
*
* @param development
Expand All @@ -189,10 +230,12 @@ export async function checkGithubActionsOtherFiles(
const files = fs.readdirSync(
path.join(development.fullPath, ".github", "workflows"),
);
return (
files.filter((file) => file !== "release.yml" && file !== "test.yml")
.length === 0
);

const test = files.filter((file) => {
return filterGithubActionsFile(development, file);
});

return test.length === 0;
}

/**
Expand All @@ -206,9 +249,7 @@ export async function fixGithubActionsRelease(development: AncaDevelopment) {
await createGithubActionsFolders(development.fullPath);
fs.writeFileSync(
path.join(development.fullPath, ".github/workflows/release.yml"),
development.state.config.type === "app"
? RELEASE_NODEJS_APP
: RELEASE_NODEJS,
getReleaseContents(development),
);
}

Expand Down Expand Up @@ -240,7 +281,7 @@ export async function fixGithubActionsOtherFiles(development: AncaDevelopment) {
path.join(development.fullPath, ".github", "workflows"),
);
files.forEach(async (file) => {
if (file !== "release.yml" && file !== "test.yml") {
if (filterGithubActionsFile(development, file)) {
fs.rmSync(path.join(development.fullPath, ".github", "workflows", file));
}
});
Expand Down
76 changes: 51 additions & 25 deletions src/actions/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ function getContents(development: AncaDevelopment) {
return "";
}

const appName = development.state.config.namings?.text ?? "New App";
const packageName =
development.state.config.namings?.npmPackage ?? "@anca/new-app";
const config = development.state.config;

if (config == null) {
return "";
}

const appName = config.namings?.text ?? "New App";
const binName = config.namings?.bin ?? "new-app";
const packageName = config.namings?.npmPackage ?? "@anca/new-app";

const lines = [];

Expand All @@ -30,47 +36,67 @@ function getContents(development: AncaDevelopment) {
lines.push(`${development.state.meta.description}`);
}

if (development.state.config.development?.readme?.description) {
lines.push(...development.state.config.development.readme.description);
if (config.development?.readme?.description) {
lines.push(...config.development.readme.description);
}

if (development.state.config.development?.readme?.features) {
if (config.development?.readme?.features) {
lines.push(`## Features`);
development.state.config.development.readme.features.forEach((feature) => {
config.development.readme.features.forEach((feature) => {
lines.push(`- ${feature}`);
});
}

lines.push(`## Installation`);

if (
development.state.config.stack === "nodejs" &&
config.stack === "nodejs" &&
development.state.jsonFiles["package.json"] != null
) {
const binName = development.state.meta?.name;
const isPublic = config.public;

if (isPublic) {
lines.push(`### npm`);
if (config.type === "app") {
lines.push(`\`\`\`bash\nnpm install -g ${packageName}\n\`\`\``);
} else {
lines.push(`\`\`\`bash\nnpm install ${packageName}\n\`\`\``);
}
}

const latestUrl =
config.cinnabarMeta?.repo?.value &&
`https://github.com/${config.cinnabarMeta?.repo?.value}/releases/latest`;

if (
development.state.config.type === "app" &&
development.state.config.downloadBinariesUrl
(config.type === "api" ||
config.type === "app" ||
config.type === "project" ||
config.type === "web") &&
latestUrl
) {
lines.push(`### Binary`);
lines.push(
`[Get the latest binaries](${development.state.config.downloadBinariesUrl}).`,
);
lines.push(`### Bundled script`);
lines.push(`[Get the latest bundled script](${latestUrl}).`);
lines.push(
`If you want to use the app with a command line, rename it to \`${binName}\` or \`${binName}.exe\` and add the location to \`PATH\`.`,
`This script has everything to run the project without any extra dependencies. You need Node.js binary to run it.`,
);
}

lines.push(`### npm`);
if (development.state.config.type === "app") {
lines.push(`\`\`\`bash\nnpm install -g ${packageName}\n\`\`\``);
} else {
lines.push(`\`\`\`bash\nnpm install ${packageName}\n\`\`\``);
if (config.type === "app") {
const binariesUrl = config.downloadBinariesUrl || latestUrl;

if (binariesUrl) {
lines.push(`### Binary`);

lines.push(`[Get the latest binaries](${binariesUrl}).`);
lines.push(
`If you want to use the app with a command line, rename it to \`${binName}\` or \`${binName}.exe\` and add the location to \`PATH\`.`,
);
}
}
}

if (development.state.config.development?.readme?.usage) {
if (config.development?.readme?.usage) {
lines.push(`## Usage`);

const generateUsageSection = (
Expand All @@ -95,14 +121,14 @@ function getContents(development: AncaDevelopment) {
});
};

generateUsageSection(development.state.config.development.readme.usage, 3);
generateUsageSection(config.development.readme.usage, 3);
}

lines.push(`## Contributing`);
lines.push(`Visit [\`CONTRIBUTING.md\`](CONTRIBUTING.md).`);

if (development.state.config.authors) {
const maintainers = development.state.config.authors.filter(
if (config.authors) {
const maintainers = config.authors.filter(
(author) =>
(author.type === "author" || author.type === "maintainer") &&
author.status !== "retired",
Expand Down
4 changes: 2 additions & 2 deletions src/cinnabar.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was generated by Cinnabar Meta. Do not edit.

export const CINNABAR_PROJECT_TIMESTAMP = 1726471605;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240916_072645";
export const CINNABAR_PROJECT_TIMESTAMP = 1726652348;
export const CINNABAR_PROJECT_VERSION = "0.1.0-dev.2+next.20240918_093908";
Loading

0 comments on commit 7e10412

Please sign in to comment.